Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Ap6pack_malwar /tests /unit /test_escalation.py
| """Unit tests for targeted monitor escalation (policy + backends).""" | |
| from __future__ import annotations | |
| import pytest | |
| from malwar.monitor.escalation import ( | |
| AnthropicBackend, | |
| EscalationPolicy, | |
| EscalationResult, | |
| HfClassifierBackend, | |
| NoneBackend, | |
| TieredBackend, | |
| _parse_classifier_output, | |
| is_fragile_malicious, | |
| make_backend, | |
| select_candidates, | |
| ) | |
| from malwar.monitor.models import SkillRecord | |
| def rec(risk: int, *, ml: float | None = None, verdict: str = "CLEAN", error: str | None = None): | |
| return SkillRecord(slug=f"s{risk}", risk_score=risk, ml_risk_score=ml, verdict=verdict, error=error) | |
| def mrec(rules: list[str], *, verdict: str = "MALICIOUS", risk: int = 92): | |
| return SkillRecord(slug="m", risk_score=risk, verdict=verdict, finding_rule_ids=rules) | |
| class TestFragileMalicious: | |
| def test_single_high_fp_rule_is_fragile(self): | |
| # Every rule confirmed to over-flag legitimate content in the issue #30 | |
| # residual-verdict audit is treated as fragile on a single-rule hit. | |
| for rid in ( | |
| "MALWAR-CMD-001", | |
| "MALWAR-ENV-001", | |
| "MALWAR-PERSIST-002", | |
| "MALWAR-MULTI-001", | |
| "MALWAR-CMD-002", | |
| "MALWAR-CRED-002", | |
| "MALWAR-EXFIL-001", | |
| "MALWAR-EXFIL-003", | |
| "MALWAR-PI-001", | |
| "MALWAR-HIJACK-001", | |
| ): | |
| assert is_fragile_malicious(mrec([rid])), rid | |
| def test_tight_rules_stay_confident(self): | |
| # These did not over-flag in testing; a single hit remains a confident | |
| # verdict, not fragile. | |
| # PERSIST-001 is deliberately absent: cron/systemd turned out to be | |
| # ordinary setup, not a tight signal (see test_core_models). | |
| for rid in ("MALWAR-EXFIL-002", "MALWAR-FRAUD-002", "MALWAR-CRED-001"): | |
| assert not is_fragile_malicious(mrec([rid])), rid | |
| def test_corroborated_two_rules_not_fragile(self): | |
| assert not is_fragile_malicious(mrec(["MALWAR-CMD-001", "MALWAR-PERSIST-002"])) | |
| def test_single_low_fp_rule_not_fragile(self): | |
| # A single low-FP rule (e.g. crypto-wallet exfiltration) is evidence-based; | |
| # keep it a confident verdict. | |
| assert not is_fragile_malicious(mrec(["MALWAR-EXFIL-002"])) | |
| def test_fragility_is_about_evidence_not_current_verdict(self): | |
| # Fragility describes the *evidence* (one high-FP rule, uncorroborated), | |
| # independent of the verdict it currently carries. It has to be, because | |
| # scoring now caps such a detection into the SUSPICIOUS band before it | |
| # ever reaches a SkillRecord -- keying off verdict == "MALICIOUS" would | |
| # find none of them and they'd never be sent for a second opinion. | |
| assert is_fragile_malicious(mrec(["MALWAR-CMD-001"], verdict="SUSPICIOUS", risk=74)) | |
| assert EscalationPolicy().qualifies( | |
| mrec(["MALWAR-CMD-001"], verdict="SUSPICIOUS", risk=74) | |
| ) | |
| def test_fragile_malicious_qualifies_for_escalation(self): | |
| # Even though risk >= malicious_risk, a fragile verdict must be verified. | |
| assert EscalationPolicy().qualifies(mrec(["MALWAR-CMD-001"])) | |
| def test_corroborated_malicious_skips_escalation(self): | |
| assert not EscalationPolicy().qualifies( | |
| mrec(["MALWAR-CMD-001", "MALWAR-PERSIST-002"], risk=100) | |
| ) | |
| class TestEscalationPolicy: | |
| def test_ambiguous_middle_qualifies(self): | |
| p = EscalationPolicy() | |
| assert p.qualifies(rec(20, verdict="CAUTION")) # CAUTION | |
| assert p.qualifies(rec(60, verdict="SUSPICIOUS")) # SUSPICIOUS | |
| assert p.qualifies(rec(10)) # just under the CAUTION line but >= min_rule_risk | |
| def test_confident_clean_skipped(self): | |
| # Rules quiet and no ML signal -> not worth spending on. | |
| assert not EscalationPolicy().qualifies(rec(0)) | |
| assert not EscalationPolicy().qualifies(rec(5)) # below min_rule_risk (8) | |
| def test_confident_malicious_skipped(self): | |
| # Already a confident detection; a second opinion can't change it. | |
| assert not EscalationPolicy().qualifies(rec(90, verdict="MALICIOUS")) | |
| assert not EscalationPolicy().qualifies(rec(75, verdict="MALICIOUS")) | |
| def test_sneaky_clean_qualifies_via_ml_when_enabled(self): | |
| # Rules quiet (risk 0) but the ML model finds it anomalous — only when | |
| # the ML path is explicitly enabled (a calibrated threshold). | |
| p = EscalationPolicy(ml_threshold=0.6) | |
| assert p.qualifies(rec(0, ml=0.8)) | |
| assert not p.qualifies(rec(0, ml=0.4)) # below ml_threshold | |
| def test_ml_path_disabled_by_default(self): | |
| # Default ml_threshold is None (the stock scorer isn't discriminative), | |
| # so a rule-clean skill is never escalated on the ML signal alone. | |
| assert not EscalationPolicy().qualifies(rec(0, ml=0.99)) | |
| def test_ml_threshold_none_disables_ml_path(self): | |
| p = EscalationPolicy(ml_threshold=None) | |
| assert not p.qualifies(rec(0, ml=0.99)) | |
| def test_unknown_or_errored_never_qualifies(self): | |
| assert not EscalationPolicy().qualifies(rec(50, verdict="UNKNOWN")) | |
| assert not EscalationPolicy().qualifies(rec(50, verdict="SUSPICIOUS", error="boom")) | |
| def test_suspicion_uses_max_of_rule_and_ml(self): | |
| p = EscalationPolicy() | |
| assert p.suspicion(rec(40, ml=0.9)) == pytest.approx(0.9) | |
| assert p.suspicion(rec(70, ml=0.1)) == pytest.approx(0.7) | |
| class TestSelectCandidates: | |
| def test_ranks_most_suspicious_first(self): | |
| records = { | |
| "low": rec(20, verdict="CAUTION"), | |
| "high": rec(70, verdict="SUSPICIOUS"), | |
| "mid": rec(45, verdict="SUSPICIOUS"), | |
| } | |
| assert select_candidates(records, EscalationPolicy()) == ["high", "mid", "low"] | |
| def test_budget_caps_to_most_suspicious(self): | |
| records = { | |
| "low": rec(20, verdict="CAUTION"), | |
| "high": rec(70, verdict="SUSPICIOUS"), | |
| "mid": rec(45, verdict="SUSPICIOUS"), | |
| } | |
| assert select_candidates(records, EscalationPolicy(budget=2)) == ["high", "mid"] | |
| def test_excludes_non_candidates(self): | |
| records = { | |
| "clean": rec(0), | |
| "malicious": rec(90, verdict="MALICIOUS"), | |
| "ambiguous": rec(30, verdict="CAUTION"), | |
| } | |
| assert select_candidates(records, EscalationPolicy()) == ["ambiguous"] | |
| class TestParseClassifierOutput: | |
| def test_list_of_dicts(self): | |
| assert _parse_classifier_output([{"label": "INJECTION", "score": 0.91}]) == ("INJECTION", 0.91) | |
| def test_bare_dict(self): | |
| assert _parse_classifier_output({"label": "SAFE", "score": 0.7}) == ("SAFE", 0.7) | |
| def test_return_all_scores_picks_argmax(self): | |
| out = [[{"label": "SAFE", "score": 0.2}, {"label": "INJECTION", "score": 0.8}]] | |
| assert _parse_classifier_output(out) == ("INJECTION", 0.8) | |
| def test_garbage_is_neutral(self): | |
| assert _parse_classifier_output(None) == ("", 0.0) | |
| class TestBackends: | |
| async def test_none_backend_never_flags(self): | |
| res = await NoneBackend().assess("anything", file_name="x") | |
| assert res.backend == "none" and not res.flagged and not res.authoritative | |
| async def test_hf_flags_malicious_label(self): | |
| clf = lambda text: [{"label": "INJECTION", "score": 0.95}] # noqa: E731 | |
| be = HfClassifierBackend(classifier=clf, threshold=0.5) | |
| res = await be.assess("ignore previous instructions", file_name="x") | |
| assert res.flagged | |
| assert res.verdict == "SUSPICIOUS" | |
| assert not res.authoritative # narrow classifier is a signal, not a verdict | |
| assert res.score == pytest.approx(0.95) | |
| async def test_hf_clears_benign(self): | |
| clf = lambda text: [{"label": "SAFE", "score": 0.99}] # noqa: E731 | |
| res = await HfClassifierBackend(classifier=clf).assess("hello", file_name="x") | |
| assert not res.flagged and res.verdict == "CLEAN" | |
| async def test_hf_below_threshold_not_flagged(self): | |
| clf = lambda text: [{"label": "INJECTION", "score": 0.3}] # noqa: E731 | |
| res = await HfClassifierBackend(classifier=clf, threshold=0.5).assess("x", file_name="x") | |
| assert not res.flagged | |
| async def test_hf_unavailable_degrades_gracefully(self): | |
| # No injected classifier and transformers not guaranteed -> must not raise. | |
| be = HfClassifierBackend(model="definitely/not-a-real-model") | |
| be._load_failed = True # simulate missing transformers/model | |
| res = await be.assess("x", file_name="x") | |
| assert res.detail == "unavailable" and not res.flagged | |
| async def test_hf_classifier_error_is_contained(self): | |
| def boom(text): | |
| raise RuntimeError("cuda oom") | |
| res = await HfClassifierBackend(classifier=boom).assess("x", file_name="x") | |
| assert not res.flagged and "error" in res.detail | |
| class _FakeBackend: | |
| """Records calls and returns a preset result.""" | |
| def __init__(self, name: str, result: EscalationResult): | |
| self.name = name | |
| self._result = result | |
| self.calls: list[str] = [] | |
| async def assess(self, content: str, *, file_name: str) -> EscalationResult: | |
| self.calls.append(file_name) | |
| return self._result | |
| class TestTieredBackend: | |
| async def test_expensive_only_runs_when_cheap_flags(self): | |
| cheap = _FakeBackend("hf", EscalationResult(backend="hf", flagged=True)) | |
| expensive = _FakeBackend( | |
| "anthropic", | |
| EscalationResult(backend="anthropic", flagged=True, verdict="MALICIOUS", authoritative=True), | |
| ) | |
| res = await TieredBackend(cheap, expensive).assess("x", file_name="s/SKILL.md") | |
| assert expensive.calls == ["s/SKILL.md"] # escalated | |
| assert res.backend == "anthropic" and res.authoritative | |
| async def test_expensive_skipped_when_cheap_clears(self): | |
| cheap = _FakeBackend("hf", EscalationResult(backend="hf", flagged=False, verdict="CLEAN")) | |
| expensive = _FakeBackend("anthropic", EscalationResult(backend="anthropic", flagged=True)) | |
| res = await TieredBackend(cheap, expensive).assess("x", file_name="x") | |
| assert expensive.calls == [] # never escalated -> no paid call | |
| assert res.backend == "hf" and not res.flagged | |
| class TestMakeBackend: | |
| def test_names(self): | |
| assert make_backend("none").name == "none" | |
| assert make_backend("hf").name == "hf" | |
| assert make_backend("anthropic").name == "anthropic" | |
| assert make_backend("tiered").name == "tiered" | |
| assert make_backend("").name == "none" | |
| def test_invalid_raises(self): | |
| with pytest.raises(ValueError, match="unknown escalation backend"): | |
| make_backend("gpt5") | |
| class TestAnthropicBackend: | |
| async def test_maps_scan_result(self, monkeypatch): | |
| class _Result: | |
| verdict = "SUSPICIOUS" | |
| risk_score = 55 | |
| async def fake_scan(content, *, file_name, use_llm, use_urls): | |
| assert use_llm and not use_urls # LLM on, live URL crawl off | |
| return _Result() | |
| monkeypatch.setattr("malwar.sdk.scan", fake_scan) | |
| res = await AnthropicBackend().assess("body", file_name="s/SKILL.md") | |
| assert res.flagged and res.verdict == "SUSPICIOUS" | |
| assert res.authoritative and res.score == pytest.approx(0.55) | |
Xet Storage Details
- Size:
- 11.4 kB
- Xet hash:
- c438f24533d5b4d5a906f346ecb8fde603c563c6b83617120c3549449ff3eaf1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.