100 lines
4.6 KiB
Python
100 lines
4.6 KiB
Python
"""Guards for the performance shortcuts: they must never change results or
|
|
bypass the normalized-event contract."""
|
|
|
|
import asyncio
|
|
import gzip
|
|
from datetime import UTC, datetime
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from app import correlate as correlate_module
|
|
from app import main
|
|
from app.models import NormalizedEvent
|
|
from app.normalize import _render_scalar, normalize_record
|
|
from app.parser import ParseResult
|
|
|
|
RECORDS = [
|
|
{"Event": {"System": {"Provider": {"#attributes": {"Name": "Service Control Manager"}}, "EventID": {"#text": 7031, "#attributes": {"Qualifiers": 49152}}, "Level": 2, "Channel": "System", "Computer": "LAB-PC", "EventRecordID": 9, "TimeCreated": {"#attributes": {"SystemTime": "2026-11-20T08:00:00.123456Z"}}}, "EventData": {"param1": "Sync", "param2": 1, "flag": True, "none": None, "ratio": 1.5, "huge": 10**30}}},
|
|
{"Event": {"System": {"Provider": "P" * 400, "EventID": "not-a-number", "Level": "weird", "Channel": "C" * 400, "Computer": "H" * 400}, "UserData": {"Nested": {"List": [{"a": "x" * 5000}, [1, [2, [3]]]]}}}},
|
|
{"Event": {"System": {"Provider": {"Name": "Legacy"}, "EventID": 1, "TimeCreated": {"#attributes": {"SystemTime": "garbage"}}}}},
|
|
{"Event": {"System": {"EventID": 4294967296, "Keywords": "0x8010000000000000"}, "EventData": {f"k{index}": index for index in range(400)}}},
|
|
{"System": {"Provider": {"#attributes": {"Name": "Microsoft-Windows-Eventlog"}}, "EventID": 1102, "Channel": "Security"}},
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("payload", RECORDS)
|
|
def test_constructed_events_satisfy_the_validated_contract(payload):
|
|
event = normalize_record(payload, record_timestamp="2026-11-20T08:00:00Z", record_ordinal=3)
|
|
revalidated = NormalizedEvent.model_validate(event.model_dump())
|
|
assert revalidated == event
|
|
|
|
|
|
@pytest.mark.parametrize("value", [None, True, False, 0, -7, 10**30, 1.5, float("inf"), [1, "a"], {"a": 1}])
|
|
def test_fast_scalar_rendering_matches_json_dumps(value):
|
|
import json
|
|
|
|
assert _render_scalar(value) == json.dumps(value, ensure_ascii=False)
|
|
|
|
|
|
def test_strings_are_kept_verbatim():
|
|
assert _render_scalar('a "quoted" value') == 'a "quoted" value'
|
|
|
|
|
|
def test_rule_predicates_run_once_per_signature(monkeypatch):
|
|
calls = 0
|
|
original = correlate_module._failed_logon
|
|
|
|
def counting(event):
|
|
nonlocal calls
|
|
calls += 1
|
|
return original(event)
|
|
|
|
rules = tuple(
|
|
correlate_module.Rule(**{**rule.__dict__, "predicate": counting}) if rule.predicate is original else rule
|
|
for rule in correlate_module.RULES
|
|
)
|
|
monkeypatch.setattr(correlate_module, "RULES", rules)
|
|
events = [
|
|
NormalizedEvent(id=str(index), timestamp=datetime(2026, 11, 20, tzinfo=UTC), provider="Microsoft-Windows-Security-Auditing", channel="Security", computer="LAB-PC", event_id=4625, fields={"EventData.TargetUserName": "alice"})
|
|
for index in range(1_000)
|
|
]
|
|
correlate_module.correlate(events)
|
|
# two rules use the predicate (failed-logon and password-spray), one signature each
|
|
assert calls == 2
|
|
|
|
|
|
def _request(headers: dict[str, str]) -> httpx.Response:
|
|
async def send() -> httpx.Response:
|
|
transport = httpx.ASGITransport(app=main.app)
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
|
|
return await client.post("/api/analyze", headers=headers, files={"file": ("a.evtx", b"x", "application/octet-stream")})
|
|
|
|
return asyncio.run(send())
|
|
|
|
|
|
def _fake_parse(_data) -> ParseResult:
|
|
event = NormalizedEvent(id="1", timestamp=datetime(2026, 11, 20, tzinfo=UTC), provider="Test", event_id=1, level="error", channel="Application", computer="LAB-PC", summary="x")
|
|
return ParseResult(events=[event], issues=[], truncated=False, duration_ms=1)
|
|
|
|
|
|
def test_analysis_response_is_gzipped_when_accepted(monkeypatch):
|
|
monkeypatch.setattr(main, "parse_evtx_bytes", _fake_parse)
|
|
response = _request({"Accept-Encoding": "gzip"})
|
|
assert response.status_code == 200
|
|
assert response.headers["content-encoding"] == "gzip"
|
|
assert response.headers["vary"] == "Accept-Encoding"
|
|
assert response.json()["events"][0]["id"] == "1"
|
|
|
|
|
|
def test_analysis_response_is_plain_without_gzip_support(monkeypatch):
|
|
monkeypatch.setattr(main, "parse_evtx_bytes", _fake_parse)
|
|
response = _request({"Accept-Encoding": "identity"})
|
|
assert "content-encoding" not in response.headers
|
|
assert response.json()["events"][0]["id"] == "1"
|
|
|
|
|
|
def test_gzip_output_is_deterministic():
|
|
payload = b'{"events": []}' * 100
|
|
assert gzip.compress(payload, compresslevel=main.GZIP_LEVEL, mtime=0) == gzip.compress(payload, compresslevel=main.GZIP_LEVEL, mtime=0)
|