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.

17.3 KB
Raw Blame
"""Tests for ``app.services.event_sanitiser`` — the submit-path hardening helper.

Pins the contract that protects ``POST /alerts/submit`` from three attack
shapes:

1. **Secret leakage** — a caller's connector accidentally (or maliciously)
   embeds an OAuth bearer / API key / password into an event payload, and
   that secret ends up persisted verbatim in the alert's ``raw_event``
   JSONB column. The sanitiser must redact sensitive keys at every depth
   of the event tree, including inside lists, and produce a stable
   sentinel that's grep-able in logs.

2. **Per-event DoS** — one rogue event in an otherwise benign batch is
   gigantic. The sanitiser must replace it inline with a small marker so
   the rest of the batch still surfaces to the analyst.

3. **Batch-level DoS** — the whole batch exceeds the configured total
   byte ceiling, or more events than ``MAX_EVENTS`` were submitted. The
   sanitiser must raise :class:`SubmitPayloadTooLarge` for the count cap
   (caller-facing 413) and silently mark trailing events with a
   ``batch_size_cap`` marker for the byte cap (analysts still see the
   head of the batch).

Also covers operator-facing safety: env-driven caps clamp to hard maxima
so a misconfigured ``AISOC_SUBMIT_MAX_TOTAL_BYTES=999999999999`` can't
disable the protection.

AiSOC — open-source AI Security Operations Center (MIT License)
Author: Beenu Arora <[email protected]>
"""

from __future__ import annotations

import json
from typing import Any

import pytest
from app.services.event_sanitiser import (
    REDACTED_SENTINEL,
    SubmitPayloadTooLarge,
    max_event_bytes,
    max_events,
    max_total_bytes,
    sanitise_event,
    sanitise_event_batch,
)

# ════════════════════════════════════════════════════════════════════════════
# Section 1: secret redaction — the most important guarantee
# ════════════════════════════════════════════════════════════════════════════


class TestSecretRedaction:
    """Sensitive keys must be replaced with the redaction sentinel."""

    @pytest.mark.parametrize(
        "key",
        [
            # Direct password / secret fields
            "password",
            "passwd",
            "secret",
            "client_secret",
            # Token variants
            "token",
            "access_token",
            "refresh_token",
            "id_token",
            "jwt",
            # API key variants
            "api_key",
            "apiKey",
            "api-key",
            "apikey",
            # Headers / cookies
            "authorization",
            "Authorization",
            "auth_header",
            "cookie",
            "set-cookie",
            "Set-Cookie",
            # Sessions & MFA
            "session_id",
            "sessionid",
            "otp",
            "mfa_code",
            # AWS-specific
            "aws_secret_access_key",
            "AWS_SECRET_ACCESS_KEY",
        ],
    )
    def test_known_sensitive_keys_are_redacted(self, key: str) -> None:
        """Any field whose name matches a sensitive pattern is masked."""
        event = {key: "should-not-leak-this"}
        sanitised, stats = sanitise_event(event)
        assert sanitised[key] == REDACTED_SENTINEL
        assert stats["redacted"] == 1
        assert stats["truncated"] == 0

    def test_redaction_is_case_insensitive(self) -> None:
        """Vendor connectors capitalise keys inconsistently."""
        event = {"AUTHORIZATION": "Bearer aaa", "Password": "pw"}
        sanitised, stats = sanitise_event(event)
        assert sanitised["AUTHORIZATION"] == REDACTED_SENTINEL
        assert sanitised["Password"] == REDACTED_SENTINEL
        assert stats["redacted"] == 2

    def test_benign_keys_pass_through(self) -> None:
        """Non-sensitive keys must keep their values intact — false
        negatives would silently break operator visibility into events."""
        event = {
            "actor": "[email protected]",
            "event_type": "user.login",
            "ip_address": "203.0.113.4",
            "displayMessage": "User signed in",
        }
        sanitised, stats = sanitise_event(event)
        assert sanitised == event
        assert stats["redacted"] == 0

    def test_redacts_nested_sensitive_keys(self) -> None:
        """Secrets buried two levels deep still get caught."""
        event = {
            "actor": "[email protected]",
            "session": {
                "id": "session-id-not-secret",
                "metadata": {
                    "refresh_token": "rt-leak-me",
                    "userAgent": "Mozilla/5.0",
                },
            },
        }
        sanitised, stats = sanitise_event(event)
        assert sanitised["session"]["metadata"]["refresh_token"] == REDACTED_SENTINEL
        assert sanitised["session"]["metadata"]["userAgent"] == "Mozilla/5.0"
        assert sanitised["session"]["id"] == "session-id-not-secret"
        assert stats["redacted"] == 1

    def test_redacts_inside_lists(self) -> None:
        """A list of credential dicts (e.g. AWS keys array) is walked."""
        event = {
            "credentials": [
                {"name": "primary", "secret": "leak-1"},
                {"name": "backup", "secret": "leak-2"},
                {"name": "rotated", "value": "fine"},
            ]
        }
        sanitised, stats = sanitise_event(event)
        assert sanitised["credentials"][0]["secret"] == REDACTED_SENTINEL
        assert sanitised["credentials"][1]["secret"] == REDACTED_SENTINEL
        assert sanitised["credentials"][2]["value"] == "fine"
        assert stats["redacted"] == 2

    def test_input_is_not_mutated(self) -> None:
        """The redactor must return a *new* dict — never mutate the caller's
        copy. Many connectors retain the original event for retry; mutating
        it would corrupt the retry payload."""
        original = {"password": "leak", "user": "alice"}
        snapshot = json.dumps(original, sort_keys=True)
        sanitise_event(original)
        assert json.dumps(original, sort_keys=True) == snapshot

    def test_non_string_keys_are_not_treated_as_sensitive(self) -> None:
        """Integer / weird keys shouldn't crash the regex matcher."""
        event = {42: "not a secret", "actor": "alice"}
        sanitised, stats = sanitise_event(event)
        assert sanitised[42] == "not a secret"
        assert stats["redacted"] == 0


# ════════════════════════════════════════════════════════════════════════════
# Section 2: per-event size cap
# ════════════════════════════════════════════════════════════════════════════


class TestPerEventSizeCap:
    """Oversized single events are replaced with a marker, not dropped."""

    def test_oversized_event_becomes_truncation_marker(self) -> None:
        event = {"payload": "x" * 2000}
        sanitised, stats = sanitise_event(event, max_bytes=256)
        # Marker shape — preserves enough debug context for an analyst.
        assert sanitised["_truncated"] is True
        assert sanitised["_reason"] == "event_too_large"
        assert "_size" in sanitised
        assert sanitised["_size"] > 256
        # Top-level keys preserved so an analyst can correlate.
        assert "_keys" in sanitised
        assert "payload" in sanitised["_keys"]
        assert stats["truncated"] == 1

    def test_undersized_event_is_passed_through(self) -> None:
        event = {"payload": "small"}
        sanitised, stats = sanitise_event(event, max_bytes=256)
        assert sanitised == event
        assert stats["truncated"] == 0

    def test_oversized_marker_does_not_leak_payload_bytes(self) -> None:
        """The marker must NOT echo any of the original event's values —
        only top-level keys. A noisy event with a credit-card number in
        the value mustn't sneak past via the marker."""
        event = {"card": "4111-1111-1111-1111", "user": "[email protected]"}
        sanitised, _ = sanitise_event(event, max_bytes=4)
        flat_json = json.dumps(sanitised)
        assert "4111-1111-1111-1111" not in flat_json
        assert "[email protected]" not in flat_json


# ════════════════════════════════════════════════════════════════════════════
# Section 3: batch-level caps
# ════════════════════════════════════════════════════════════════════════════


class TestBatchCaps:
    """Three cap levels: event count, total bytes, and the hard reject."""

    def test_happy_path_short_batch_passes_through(self) -> None:
        events = [{"actor": f"user-{i}"} for i in range(5)]
        sanitised, stats = sanitise_event_batch(events)
        assert len(sanitised) == 5
        assert stats["redacted"] == 0
        assert stats["truncated"] == 0
        # Order is preserved.
        assert sanitised[0]["actor"] == "user-0"
        assert sanitised[4]["actor"] == "user-4"

    def test_over_max_events_raises_413(self) -> None:
        """Sending more events than ``MAX_EVENTS`` is the only condition
        that raises — the endpoint translates this to HTTP 413."""
        events = [{"actor": f"u{i}"} for i in range(11)]
        with pytest.raises(SubmitPayloadTooLarge):
            sanitise_event_batch(events, max_events_override=10)

    def test_non_list_input_raises(self) -> None:
        with pytest.raises(SubmitPayloadTooLarge):
            sanitise_event_batch("not a list")  # type: ignore[arg-type]

    def test_per_event_cap_applies_within_batch(self) -> None:
        """Oversized events become markers; the rest of the batch survives."""
        events = [
            {"actor": "alice"},
            {"payload": "x" * 2000},  # oversized
            {"actor": "bob"},
        ]
        sanitised, stats = sanitise_event_batch(
            events,
            max_event_bytes_override=256,
        )
        assert sanitised[0]["actor"] == "alice"
        assert sanitised[1]["_truncated"] is True
        assert sanitised[2]["actor"] == "bob"
        assert stats["truncated"] == 1

    def test_batch_total_cap_replaces_trailing_events(self) -> None:
        """When the running total exceeds ``MAX_TOTAL_BYTES``, trailing
        events get cheap markers — the head of the batch is preserved."""
        events = [{"payload": "x" * 100} for _ in range(20)]
        sanitised, stats = sanitise_event_batch(
            events,
            max_event_bytes_override=10 * 1024,
            max_total_bytes_override=500,  # only ~3-4 events fit
        )
        # Some prefix passed, the rest got truncated with the batch marker.
        truncated_count = sum(1 for e in sanitised if e.get("_truncated"))
        passing_count = sum(1 for e in sanitised if not e.get("_truncated"))
        assert passing_count >= 1
        assert truncated_count >= 1
        assert truncated_count + passing_count == 20
        assert stats["truncated"] == truncated_count
        # Markers carry the event index for forensics.
        for ev in sanitised:
            if ev.get("_truncated"):
                assert "_index" in ev
                assert ev["_reason"] == "batch_size_cap"

    def test_redaction_stats_aggregate_across_batch(self) -> None:
        """``stats['redacted']`` is the *total* sensitive keys masked
        across the whole batch, not just the last event."""
        events = [
            {"actor": "a", "password": "p1"},
            {"actor": "b", "api_key": "k2"},
            {"actor": "c", "token": "t3", "Authorization": "Bearer xyz"},
        ]
        sanitised, stats = sanitise_event_batch(events)
        assert stats["redacted"] == 4
        # All sensitive values masked across the batch.
        assert sanitised[0]["password"] == REDACTED_SENTINEL
        assert sanitised[1]["api_key"] == REDACTED_SENTINEL
        assert sanitised[2]["token"] == REDACTED_SENTINEL
        assert sanitised[2]["Authorization"] == REDACTED_SENTINEL


# ════════════════════════════════════════════════════════════════════════════
# Section 4: env-driven caps & operator safety
# ════════════════════════════════════════════════════════════════════════════


class TestEnvCaps:
    """Caps read from env vars must clamp to hard maxima and reject junk."""

    def test_default_caps_are_reasonable(self) -> None:
        # No env override → defaults documented in the module docstring.
        assert max_events() == 1000
        assert max_event_bytes() == 256 * 1024
        assert max_total_bytes() == 4 * 1024 * 1024

    def test_env_override_lowers_cap(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AISOC_SUBMIT_MAX_EVENTS", "50")
        assert max_events() == 50

    def test_env_zero_falls_back_to_default(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """A non-positive value is silently ignored — operators sometimes
        type 0 to "disable" caps; we explicitly refuse that footgun."""
        monkeypatch.setenv("AISOC_SUBMIT_MAX_EVENTS", "0")
        assert max_events() == 1000

    def test_env_negative_falls_back_to_default(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AISOC_SUBMIT_MAX_EVENTS", "-5")
        assert max_events() == 1000

    def test_env_garbage_falls_back_to_default(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AISOC_SUBMIT_MAX_EVENTS", "not-a-number")
        assert max_events() == 1000

    def test_env_huge_value_clamps_to_hard_max(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """An operator typing 10**12 mustn't disable the cap. The hard
        maximum (100k events) is the real upper bound."""
        monkeypatch.setenv("AISOC_SUBMIT_MAX_EVENTS", "999999999")
        assert max_events() == 100_000

    def test_total_bytes_clamps_to_hard_max(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AISOC_SUBMIT_MAX_TOTAL_BYTES", "10737418240")  # 10 GiB
        assert max_total_bytes() == 64 * 1024 * 1024  # hard ceiling


# ════════════════════════════════════════════════════════════════════════════
# Section 5: recursion guards (depth & node count)
# ════════════════════════════════════════════════════════════════════════════


class TestRecursionGuards:
    """Deeply nested attacker payloads must not blow the Python stack."""

    def test_deeply_nested_payload_does_not_recurse_forever(self) -> None:
        # Build a 200-deep nested dict — far above the 32-level guard.
        event: dict[str, Any] = {}
        cursor: dict[str, Any] = event
        for _ in range(200):
            cursor["next"] = {}
            cursor = cursor["next"]
        cursor["password"] = "deep-leak"

        # Must not raise RecursionError; should produce a truncation
        # marker somewhere in the chain.
        sanitised, _ = sanitise_event(event)
        # Walk what we got back looking for the depth marker.
        cursor = sanitised
        found_marker = False
        for _ in range(40):
            if not isinstance(cursor, dict):
                break
            if cursor.get("_truncated") == "max_depth":
                found_marker = True
                break
            cursor = cursor.get("next")  # type: ignore[assignment]
        assert found_marker, "expected max_depth marker somewhere in nested chain"

    def test_node_count_cap_stops_runaway_payload(self) -> None:
        """A flat dict with 100k keys hits the node ceiling before the
        depth guard does — the redactor must short-circuit cleanly."""
        # Build a wide payload that exceeds the 50k node guard.
        event = {f"k{i}": i for i in range(60_000)}
        sanitised, _ = sanitise_event(event, max_bytes=64 * 1024 * 1024)
        # The truncation marker is buried inside the redacted dict because
        # the redactor doesn't *return* the marker for a top-level wide
        # dict — only for a value it gave up on. So instead: confirm the
        # output is much smaller than the input (the redactor stopped
        # producing entries past the node cap).
        # NOTE: we permit either truncation behaviour — what matters is
        # we didn't crash and we produced something reasonable.
        assert isinstance(sanitised, dict) or sanitised.get("_truncated") == "max_nodes"