Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

6.3 KB
Raw Blame
"""The body of an investigation summary carries the operator's brand too.

``apps/docs/docs/operations/white-label.md`` lists "Investigation summary
(PDF)" as carrying the product name, the palette, the footer and the support
contact, and the renderer does exactly that: ``_render_pdf`` draws a branded
header line and a branded footer around whatever markdown it is handed.

What it is handed was not branded. ``_build_summary_markdown`` ended every
summary with a hard-coded line naming the platform and a single address:

    *Generated by AiSOC — [email protected]*

So a white-labelled export drew "ACME SHIELD" across the top and "Generated
by AiSOC" four inches below it. That is the co-branding failure this
repository already names in the digest row of the claim matrix — a report
showing both names is not white-labelled, it is co-branded by accident — and
it travels one step further here, because the address beside it is a fixed
mailbox rather than the support contact the organisation configured. An
analyst at a managed customer reading "something looks wrong with this
report" writes to whoever that address belongs to.

Why no test caught it
---------------------
Nothing in ``services/api/tests`` referenced ``_build_summary_markdown``, the
close route's artefact content or the summary PDF at all. The function was
covered by its absence from every suite, which is why a literal could sit in
a customer-facing document across releases.

These cases assert on the markdown rather than on the PDF bytes, for the
reason the digest suite gives for asserting on HTML: the PDF *is* this
markdown, drawn by reportlab, so a line that is absent here cannot appear
there. That the close route actually passes branding in — the wiring, as
opposed to the renderer — is proved against the real app and real Postgres
in ``tests/isolation/test_white_label_live.py``.
"""

from __future__ import annotations

import uuid
from datetime import UTC, datetime

from app.api.v1.endpoints.investigations import _build_summary_markdown
from app.models.investigation import InvestigationEvent, InvestigationRun
from app.services.branding.resolver import DEFAULT_BRANDING, Branding

PRODUCT = "Acme Shield"
SUPPORT_URL = "https://support.acme.example"

#: The address that was hard-coded into every summary. Asserted by name: a
#: future edit that reaches for a literal mailbox again should fail here
#: rather than ship.
PLATFORM_CONTACT = "[email protected]"

BRANDED = Branding(
    product_name=PRODUCT,
    primary_color="#123456",
    accent_color="#654321",
    support_email=None,
    support_url=SUPPORT_URL,
    sender_name="Acme SOC",
    footer_text="Acme Shield — operated by Acme Managed Security.",
    logo_url=None,
    logo_data_uri=None,
    org_id=uuid.UUID("0a0a0a0a-0000-0000-0000-00000000000a"),
    is_white_labelled=True,
)


def _run() -> InvestigationRun:
    """A run with the attributes the summary reads, and nothing more.

    The real ORM class rather than a stand-in. A ``SimpleNamespace`` would
    answer for whatever attribute the formatter happened to ask for, so it
    could not notice a column being renamed out from under it.
    """
    return InvestigationRun(
        id=uuid.UUID("11111111-0000-0000-0000-000000000011"),
        case_id="CASE-4821",
        tenant_id=uuid.UUID("eeeeeeee-0000-0000-0000-0000000000e1"),
        status="completed",
        model_used="llama3.2:3b",
        started_at=datetime(2026, 5, 4, 9, 0, tzinfo=UTC),
        iterations=3,
        total_tokens=1234,
    )


def _events() -> list[InvestigationEvent]:
    return [
        InvestigationEvent(
            run_id=uuid.UUID("11111111-0000-0000-0000-000000000011"),
            seq=1,
            kind="llm_response",
            agent="aisoc-recon",
            summary="Resolved the host and its recent sign-ins.",
            ts=datetime(2026, 5, 4, 9, 1, tzinfo=UTC),
        )
    ]


class TestTheSummaryBody:
    def test_the_footer_names_the_operators_product(self) -> None:
        body = _build_summary_markdown(_run(), _events(), None, BRANDED)
        assert PRODUCT in body

    def test_the_footer_does_not_also_name_the_platform(self) -> None:
        """A document showing both names is co-branded, not white-labelled."""
        body = _build_summary_markdown(_run(), _events(), None, BRANDED)
        assert DEFAULT_BRANDING.product_name not in body

    def test_the_support_contact_is_the_one_the_organisation_configured(self) -> None:
        body = _build_summary_markdown(_run(), _events(), None, BRANDED)
        assert SUPPORT_URL in body

    def test_no_customer_report_carries_a_fixed_mailbox(self) -> None:
        """The defect, stated as an assertion.

        Checked on the branded *and* the unbranded render: the address was
        wrong in a white-labelled export because it is not the operator's,
        and wrong in an unbranded one because a deployment that has
        configured no support contact does not have one to print.
        """
        assert PLATFORM_CONTACT not in _build_summary_markdown(_run(), _events(), None, BRANDED)
        assert PLATFORM_CONTACT not in _build_summary_markdown(_run(), _events(), None, DEFAULT_BRANDING)

    def test_an_unbranded_deployment_still_names_the_platform(self) -> None:
        """The positive control.

        Without this, deleting the footer outright would satisfy every case
        above. An unbranded deployment should still say what produced the
        document.
        """
        body = _build_summary_markdown(_run(), _events(), None, DEFAULT_BRANDING)
        assert DEFAULT_BRANDING.product_name in body

    def test_branding_is_optional_so_every_existing_caller_keeps_working(self) -> None:
        body = _build_summary_markdown(_run(), _events(), None)
        assert DEFAULT_BRANDING.product_name in body
        assert PLATFORM_CONTACT not in body

    def test_the_content_the_summary_is_about_is_untouched(self) -> None:
        """Branding decorates the report; it must not rewrite it.

        ``close_investigation`` hashes this markdown into the artefact, so a
        branding change that altered the body would change evidence.
        """
        body = _build_summary_markdown(_run(), _events(), "Escalated to the on-call.", BRANDED)
        assert "CASE-4821" in body
        assert "Resolved the host and its recent sign-ins." in body
        assert "Escalated to the on-call." in body