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.

fix(phishing): linear-time display-name scan on attacker-sized headers (GHSA-9m2j-23jf-gg3p) (#1541)

Beenu Aroraa0a6ac3d8661
diff --git a/services/api/app/services/phishing/headers.py b/services/api/app/services/phishing/headers.py
index 8561c186b..be15337ff 100644
--- a/services/api/app/services/phishing/headers.py
+++ b/services/api/app/services/phishing/headers.py
@@ -104,7 +104,26 @@ _AUTH_CLAUSE = re.compile(r"\b(spf|dkim|dmarc)\s*=\s*([a-z]+)", re.IGNORECASE)
_RECEIVED_SPF = re.compile(r"^\s*([a-z]+)", re.IGNORECASE)
#: An address embedded in a display name. Deliberately narrow: it must look
#: like an address, because "Billing (accounts)" is not impersonation.
-_ADDRESS_IN_NAME = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")
+#:
+#: The lookbehind pins every match attempt to the *start* of a local-part
+#: run. Without it the unanchored scan retried from each interior position
+#: of a long run and re-consumed the tail every time — quadratic, measured
+#: at 33 s for a 128 KB display name, which an authenticated reporter
+#: controls and which this module processes synchronously on the event
+#: loop (GHSA-9m2j-23jf-gg3p). Interior starts now fail in O(1), the same
+#: property the leading ``\b`` already gives the other scans in this
+#: package. No match changes: an interior start could only ever find an
+#: address the run-start attempt also finds, because the suffix after the
+#: ``@`` decides both.
+_ADDRESS_IN_NAME = re.compile(r"(?<![\w.+-])[\w.+-]+@[\w-]+\.[\w.-]+")
+
+#: A display name is analysed on its first kilobyte only. RFC 5322 caps a
+#: header line at 998 octets, so a display name beyond this is already
+#: pathological; the cap bounds the scan independently of the pattern's
+#: complexity and keeps the impersonation finding's ``evidence`` — which
+#: embeds the display name verbatim, and is stored and rendered — out of
+#: the megabyte range on a crafted report.
+_MAX_DISPLAY_LEN = 1024
@dataclass(frozen=True)
@@ -275,7 +294,7 @@ def analyse_headers(headers: dict[str, str], *, reported_sender: str = "") -> He
)
)
- display = _display_name(from_header)
+ display = _display_name(from_header)[:_MAX_DISPLAY_LEN]
embedded = _ADDRESS_IN_NAME.findall(display)
impersonated = [a.lower() for a in embedded if a.lower() != from_address]
if impersonated:
diff --git a/services/api/tests/test_phishing_message_analysis.py b/services/api/tests/test_phishing_message_analysis.py
index 4e60f97f4..f3a98a08a 100644
--- a/services/api/tests/test_phishing_message_analysis.py
+++ b/services/api/tests/test_phishing_message_analysis.py
@@ -19,6 +19,8 @@ can be broken:
from __future__ import annotations
+import time
+
import pytest
from app.services.phishing.analysis import ArtefactResult, MessageAnalysis, _citations
from app.services.phishing.headers import AuthResult, analyse_headers
@@ -359,3 +361,41 @@ class TestTheIntakeAdapterFeedsTheAnalysis:
message = from_graph_message(payload)
analysis = analyse_headers(message.headers, reported_sender=message.sender)
assert analysis.failed_methods == ("dmarc", "spf")
+
+
+class TestAdversarialDisplayName:
+ """GHSA-9m2j-23jf-gg3p: an attacker-sized ``From`` display name must not
+ stall the process.
+
+ The reported shape: ``_ADDRESS_IN_NAME`` was an unanchored scan, so a long
+ display name containing no ``@`` was re-consumed from every interior
+ position — quadratic, measured here at 8.4 s for 64 KB and reported at
+ 71 s for 128 KB through ``POST /phishing/reports``, which runs this
+ synchronously on the event loop and therefore stalls every other request
+ on the process while it spins. The display name is attacker-sized because
+ nothing bounds a reported message's headers.
+
+ The 1-second budget cannot flap in either direction: the pre-fix cost at
+ 128 KB is ~30 s on a fast machine (slower on a runner, so further *over*
+ budget), and the post-fix cost is linear ``parseaddr`` plus a capped scan,
+ ~20 ms on a slow runner — 30× or more of margin both ways.
+ """
+
+ def test_analysis_is_bounded_on_a_long_non_address_display_name(self) -> None:
+ name = "a" * (128 * 1024)
+ started = time.perf_counter()
+ analysis = analyse_headers({"from": f'"{name}" <{PHISH}>'})
+ elapsed = time.perf_counter() - started
+ assert elapsed < 1.0, f"header analysis took {elapsed:.1f}s on a 128 KB display name"
+ # The pathological name embeds no address, so no impersonation finding.
+ assert "display_name_impersonation" not in {f.kind for f in analysis.findings}
+
+ def test_impersonation_still_fires_and_evidence_stays_bounded(self) -> None:
+ """Detection is not traded away, and a crafted name cannot push
+ megabytes into the finding's stored ``evidence``."""
+ embedded = "[email protected]"
+ name = f"{embedded} " + "b" * (128 * 1024)
+ analysis = analyse_headers({"from": f'"{name}" <{PHISH}>'})
+ finding = next(f for f in analysis.findings if f.kind == "display_name_impersonation")
+ assert finding.evidence["claimed"] == embedded
+ assert len(finding.evidence["display_name"]) <= 1024