diff --git a/scripts/benchmark/run_bench.py b/scripts/benchmark/run_bench.py index 79447d8a..e21cdfc4 100755 --- a/scripts/benchmark/run_bench.py +++ b/scripts/benchmark/run_bench.py @@ -245,6 +245,8 @@ Matching criteria (ALL must be true for detected): - Same vulnerability CLASS, or the scanner found a vulnerability earlier in the same data flow whose fix would also prevent the ground truth - Same COMPONENT (same endpoint, page, or feature, or a closely related one) - Same MECHANISM (same missing check, same injection type, same bypass) +- VERIFIED PROOF (gates.proof MUST be true): speculative or unverified claims (e.g. "Possible ...", "Not verified", "No request was sent") MUST be marked detected=false +- UNIQUE FINDING PER ENTRY: each scanner finding may match at most ONE ground-truth entry (never reuse the same matched_finding_id across multiple ground-truth entries) It is NOT required that severities, finding IDs, or wording match. @@ -320,6 +322,11 @@ def judge_battery(args) -> None: [{"id": 3, "name": "Possible issue", "severity": "low", "summary": "The application might have some problems in some areas", "report": "unclear"}], "GT-Z", False), + ("unverified", "GT-U: SQL injection login bypass on /rest/user/login", + [{"id": 4, "name": "Possible SQL injection at /rest/user/login", "severity": "high", + "summary": "Possible SQL injection at /rest/user/login. Not verified.", + "report": "No request was sent to confirm."}], + "GT-U", False), ] for label, gt_text, findings, gt_id, expect in cases: user = f"## Scanner findings\n{json.dumps(findings, indent=1)}\n\n## Ground truth\n### {gt_text}\n\nRespond with the JSON array." @@ -328,7 +335,7 @@ def judge_battery(args) -> None: verdict = parse_judge_verdicts(raw, [gt_id], findings)[0] except ValueError as exc: raise SystemExit(f"judge battery case '{label}': invalid output ({exc}); grader not trusted:\n{str(raw)[:300]}") from None - got = verdict["detected"] + got = bool(verdict["detected"] and (verdict.get("gates") or {}).get("proof")) if got != expect: raise SystemExit(f"judge battery FAILED on '{label}' (expected detected={expect}, got {got}); a grader that cannot fail the wrong answers does not run") @@ -361,10 +368,50 @@ def wilson(p_hat: float, n: int, z: float = 1.96) -> tuple[float, float]: return (max(0.0, center - half), min(1.0, center + half)) +def _finding_key(fid): + return (type(fid), fid) + + def tally(gt_findings, verdicts, scanner_findings, invalid_ids) -> dict: - detected = [v for v in verdicts if v.get("detected")] - matched_ids = {v.get("matched_finding_id") for v in detected} - candidates = [f for f in scanner_findings if f["id"] not in matched_ids and f["id"] not in invalid_ids] + # Enforce #619 invariants: + # 1. A counted detection requires gates.proof == True. + # 2. Each ground-truth entry consumes a unique scanner finding id (first in GT order). + # 3. Tally counts unique credited findings (not raw verdicts). + # 4. Unmatched / unproven / invalid findings reduce precision directly. + gt_order = {g["id"]: idx for idx, g in enumerate(gt_findings)} + ordered_verdicts = sorted(verdicts, key=lambda v: gt_order.get(v.get("gt_id"), len(gt_order))) + invalid_keys = {_finding_key(fid) for fid in invalid_ids} + valid_finding_keys = { + _finding_key(f["id"]) + for f in scanner_findings + if type(f.get("id")) in (int, str) and _finding_key(f["id"]) not in invalid_keys + } + + detected = [] + credited_keys = set() + reused_matches_dropped = 0 + unproven_dropped = 0 + for v in ordered_verdicts: + if not v.get("detected"): + continue + g = v.get("gates") or {} + if not g.get("proof"): + unproven_dropped += 1 + continue + mid = v.get("matched_finding_id") + mkey = _finding_key(mid) + if type(mid) not in (int, str) or mkey not in valid_finding_keys: + continue + if mkey in credited_keys: + reused_matches_dropped += 1 + continue + credited_keys.add(mkey) + detected.append(v) + + unmatched = [ + f for f in scanner_findings + if _finding_key(f.get("id")) not in credited_keys and _finding_key(f.get("id")) not in invalid_keys + ] gates = {"component": 0, "class": 0, "keywords": 0, "proof": 0} gate_scores = [] for v in detected: @@ -375,11 +422,11 @@ def tally(gt_findings, verdicts, scanner_findings, invalid_ids) -> dict: for k in gates: gates[k] += 1 if g.get(k) else 0 n = len(gt_findings) - rate = len(detected) / n if n else 0.0 + tp = len(credited_keys) + rate = tp / n if n else 0.0 lo, hi = wilson(rate, n) - tp = len(detected) - precision_lo = tp / (tp + len(candidates) + len(invalid_ids)) if (tp + len(candidates) + len(invalid_ids)) else 0.0 - precision_hi = tp / tp if tp else 0.0 + denom = tp + len(unmatched) + len(invalid_ids) + precision = tp / denom if denom else 0.0 return { "ground_truth_total": n, "detected": tp, @@ -387,8 +434,11 @@ def tally(gt_findings, verdicts, scanner_findings, invalid_ids) -> dict: "detection_rate_wilson95": [round(lo, 4), round(hi, 4)], "gate_totals_over_detected": gates, "mean_gate_score_of_6": round(sum(gate_scores) / len(gate_scores), 2) if gate_scores else None, - "discovery_candidates": [{"id": c["id"], "name": c["name"]} for c in candidates], - "precision_bounds_until_verified": [round(precision_lo, 4), round(precision_hi, 4)], + "discovery_candidates": [{"id": c["id"], "name": c.get("name", "")} for c in unmatched], + "precision": round(precision, 4), + "precision_bounds_until_verified": [round(precision, 4), round(precision, 4)], + "reused_matches_dropped": reused_matches_dropped, + "unproven_detections_excluded": unproven_dropped, "invalid_findings_excluded": invalid_ids, "scanner_findings_total": len(scanner_findings), } diff --git a/scripts/benchmark/test_run_bench.py b/scripts/benchmark/test_run_bench.py index 54cc6b7e..1e0018dc 100644 --- a/scripts/benchmark/test_run_bench.py +++ b/scripts/benchmark/test_run_bench.py @@ -53,9 +53,9 @@ class JudgeTests(unittest.TestCase): self.assertEqual(self.llm.call_count, 3) def test_valid_verdicts_keep_order_and_scores(self): - # Output order need not match ground truth, and a match may earn partial gates. + # Output order need not match ground truth, and a proven match may earn partial non-proof gates. response = list(reversed(self.verdicts)) - response[1]["gates"]["proof"] = False + response[1]["gates"]["keywords"] = False self.llm.return_value = json.dumps(response) result = run_bench.run_judge(self.args, self.gt, self.findings) self.assertEqual(result, response) @@ -64,9 +64,11 @@ class JudgeTests(unittest.TestCase): self.assertEqual(scores["detected"], 1) self.assertEqual(scores["detection_rate"], 0.5) self.assertEqual(scores["gate_totals_over_detected"], { - "component": 1, "class": 1, "keywords": 1, "proof": 0, + "component": 1, "class": 1, "keywords": 0, "proof": 1, }) self.assertEqual(scores["discovery_candidates"], [{"id": 2, "name": "candidate"}]) + self.assertEqual(scores["precision"], 0.5) + self.assertEqual(scores["precision_bounds_until_verified"], [0.5, 0.5]) def test_code_fenced_json_still_works(self): self.llm.return_value = "```json\n" + json.dumps(self.verdicts) + "\n```" @@ -194,10 +196,10 @@ class JudgeTests(unittest.TestCase): def test_valid_battery_still_passes(self): self.llm.side_effect = [json.dumps([v]) for v in ( - verdict("GT-X"), verdict("GT-Y", False), verdict("GT-Z", False), + verdict("GT-X"), verdict("GT-Y", False), verdict("GT-Z", False), verdict("GT-U", False), )] run_bench.judge_battery(self.args) - self.assertEqual(self.llm.call_count, 3) + self.assertEqual(self.llm.call_count, 4) def test_battery_rejects_string_false_instead_of_treating_it_as_true(self): positive = verdict("GT-X") @@ -261,5 +263,87 @@ class JudgeTests(unittest.TestCase): self.assertEqual(self.llm.call_count, 3) + + +class AdversarialScoringTests(unittest.TestCase): + """Regression tests for issue #619 using the adversarial finding sets.""" + + def setUp(self): + self.gt8 = [ + {"id": f"JS-{i}", "title": f"Juice Shop vuln {i}", "cwe": "CWE-89", + "classes": ["SQLi"], "components": ["/rest"], "mechanism_keywords": ["sql"], "proof_hint": "PoC"} + for i in range(1, 9) + ] + + def test_omnibus_finding_credits_at_most_one_ground_truth(self): + omnibus = [{ + "id": 1, "name": "Multiple critical vulnerabilities across the application", + "summary": "SQLi login bypass, admin page, basket IDOR, /ftp, feedback, stack traces, JWT, XSS.", + "report": "Omnibus report listing all 8 Juice Shop vulnerabilities.", + }] + # Adversarial judge credits all 8 GT entries to the single omnibus finding id=1. + verdicts = [verdict(g["id"], detected=True, matched_id=1) for g in self.gt8] + scores = run_bench.tally(self.gt8, verdicts, omnibus, []) + self.assertEqual(scores["detected"], 1) + self.assertEqual(scores["detection_rate"], round(1 / 8, 4)) + self.assertEqual(scores["reused_matches_dropped"], 7) + self.assertEqual(scores["precision"], 1.0) + + def test_unverified_36_findings_without_proof_gate_score_zero(self): + # 6 classes x 6 endpoints, all speculative / unverified ("Not verified. No request was sent to confirm.") + findings = [ + { + "id": i, + "name": f"Possible class-{i % 6} at /ep-{i // 6}", + "summary": f"Possible class-{i % 6} at /ep-{i // 6}; Not verified.", + "report": "No request was sent to confirm.", + } + for i in range(1, 37) + ] + verdicts = [] + for idx, g in enumerate(self.gt8): + if idx < 6: + v = verdict(g["id"], detected=True, matched_id=idx + 1) + v["gates"]["proof"] = False + verdicts.append(v) + else: + verdicts.append(verdict(g["id"], detected=False)) + scores = run_bench.tally(self.gt8, verdicts, findings, []) + self.assertEqual(scores["detected"], 0) + self.assertEqual(scores["detection_rate"], 0.0) + self.assertEqual(scores["unproven_detections_excluded"], 6) + self.assertEqual(scores["precision"], 0.0) + self.assertEqual(scores["precision_bounds_until_verified"], [0.0, 0.0]) + self.assertEqual(len(scores["discovery_candidates"]), 36) + + def test_unmatched_findings_reduce_precision(self): + findings = [{"id": i, "name": f"finding-{i}"} for i in range(1, 11)] + verdicts = [verdict("JS-1", detected=True, matched_id=1)] + [ + verdict(f"JS-{i}", detected=False) for i in range(2, 9) + ] + scores = run_bench.tally(self.gt8, verdicts, findings, []) + self.assertEqual(scores["detected"], 1) + self.assertEqual(scores["precision"], 0.1) + self.assertEqual(scores["precision_bounds_until_verified"], [0.1, 0.1]) + self.assertEqual(len(scores["discovery_candidates"]), 9) + + # Adding 26 more unmatched findings drops precision further (1/36). + more = findings + [{"id": i, "name": f"noise-{i}"} for i in range(11, 37)] + scores_more = run_bench.tally(self.gt8, verdicts, more, []) + self.assertEqual(scores_more["detected"], 1) + self.assertEqual(scores_more["precision"], round(1 / 36, 4)) + self.assertLess(scores_more["precision"], scores["precision"]) + + def test_distinct_proven_findings_score_full_detection_and_precision(self): + findings = [{"id": i, "name": f"verified-{i}"} for i in range(1, 9)] + verdicts = [verdict(f"JS-{i}", detected=True, matched_id=i) for i in range(1, 9)] + scores = run_bench.tally(self.gt8, verdicts, findings, []) + self.assertEqual(scores["detected"], 8) + self.assertEqual(scores["detection_rate"], 1.0) + self.assertEqual(scores["precision"], 1.0) + self.assertEqual(scores["reused_matches_dropped"], 0) + self.assertEqual(scores["unproven_detections_excluded"], 0) + + if __name__ == "__main__": unittest.main()