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 4 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
23 changes: 21 additions & 2 deletions src/reporter/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
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
from reporter.http_client import HTTPClientDependency
from reporter.models import Observation
from reporter.observations import send_observation

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

app = FastAPI()


Expand All @@ -21,3 +27,16 @@ async def report_endpoint(
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)
Xithrius marked this conversation as resolved.
Show resolved Hide resolved
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,
)
25 changes: 24 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,11 +13,33 @@
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,
Expand Down
10 changes: 10 additions & 0 deletions src/reporter/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
13 changes: 13 additions & 0 deletions src/reporter/utils/pypi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""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