Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to report via email #10

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/reporter/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from fastapi import FastAPI
from typing import Annotated
from fastapi import Depends, FastAPI
from msgraph import GraphServiceClient
from reporter.schemas import ReportPayload
from reporter.constants import Mail
import sentry_sdk

from reporter.constants import GIT_SHA, Sentry
from reporter.http_client import HTTPClientDependency
from reporter.models import Observation, ServerMetadata
from reporter.observations import send_observation

from reporter.dependencies import build_graph_client
from reporter.mailer import build_report_email_content, send_mail

sentry_sdk.init(
dsn=Sentry.dsn,
environment=Sentry.environment,
Expand All @@ -29,3 +36,23 @@ async def metadata() -> ServerMetadata:
@app.post("/report/{project_name}")
async def report_endpoint(project_name: str, observation: Observation, http_client: HTTPClientDependency):
await send_observation(project_name=project_name, observation=observation, http_client=http_client)


@app.post("/report/email")
async def report_email_endpoint(
payload: ReportPayload, graph_client: Annotated[GraphServiceClient, Depends(build_graph_client)]
):
content = build_report_email_content(
name=payload.name,
version=payload.version,
inspector_url=payload.inspector_url,
rules_matched=payload.rules_matched,
additional_information=payload.additional_information,
)
await send_mail(
graph_client,
to_addresses=[payload.recipient or Mail.recipient],
bcc_addresses=[],
subject=f"Automated PyPI Malware Report: {payload.name}@{payload.version}",
content=content,
)
26 changes: 25 additions & 1 deletion src/reporter/mailer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sending emails."""

from logging import getLogger
from typing import Optional

from msgraph import GraphServiceClient
from msgraph.generated.models.attachment import Attachment
Expand All @@ -12,12 +13,35 @@
from msgraph.generated.users.item.send_mail.send_mail_post_request_body import (
SendMailPostRequestBody,
)

from textwrap import dedent
from reporter.constants import Mail
from reporter.utils.pypi import file_path_from_inspector_url

logger = getLogger(__name__)


def build_report_email_content(
*,
name: str,
version: str,
inspector_url: str,
rules_matched: list[str],
additional_information: Optional[str],
) -> str:
content = f"""
PyPI Malicious Package Report
-
Package Name: {name}
Version: {version}
File path: {file_path_from_inspector_url(inspector_url)}
Inspector URL: {inspector_url}
Additional Information: {additional_information or "No user description provided"}
Yara rules matched: {", ".join(rules_matched) or "No rules matched"}
"""

return dedent(content)


async def send_mail(
graph_client: GraphServiceClient,
to_addresses: list[str],
Expand Down
11 changes: 11 additions & 0 deletions src/reporter/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import BaseModel
from typing import Optional


class ReportPayload(BaseModel):
name: str
version: str
rules_matched: list[str]
inspector_url: str
additional_information: Optional[str] = None
recipient: Optional[str] = None
14 changes: 14 additions & 0 deletions src/reporter/utils/pypi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Utilities related to PyPI"""

from urllib.parse import urlparse


def file_path_from_inspector_url(inspector_url: str) -> str:
"""Parse the file path out of a PyPI inspector URL"""

parsed_url = urlparse(inspector_url)
path = parsed_url.path.strip("/")
segments = path.split("/")

# The 8th element of path is when the file path starts
return "/".join(segments[8:])
Robin5605 marked this conversation as resolved.
Show resolved Hide resolved