feat(ruleset): Phase 2 (task 3) — rules as governed data, severity from policy

Externalize detection rules to versioned TOML and remove rule-local severity, so
rules detect and the central CWE policy decides severity — fixing "rules too
influential / not centrally managed". Zero-dependency; detection is byte-identical
(benchmark recall 44/47 = 93.6%, FP 0% unchanged) and the 90/10 gate holds.

- RulesetStore (ruleset/store.py + ruleset/<lang>/rules.toml): PatternRule as data
  (rule_id, title, languages, cwe, tags, pattern, status, min_evidence_level,
  precision_estimate, version) with NO severity; load_ruleset (sorted, with
  optional rule_policy.json ledger overlay) and a stdlib TOML write_ruleset. The
  32 rules were generated from the prior PATTERN_RULES with a verified exact
  round-trip (pattern/cwe/languages/tags/title).
- findings.py: drop the hardcoded PatternRule class + PATTERN_RULES tuple; load the
  default ruleset from TOML; quick_scan_findings(root, targets, rankings, ruleset,
  policy) resolves each finding's severity from policy via resolve_severity
  (SEVERITY_LABEL maps 0-5 -> string) and tags findings with their rule status.
- Three-status volume control: StaticFinding gains status (default "enabled");
  disabled rules never fire; shadow findings are partitioned out of the report and
  score in the CLI (written to shadow_findings.json) — no shadow rules by default.
- Emission floors (RulesetConfig.min_emit_priority / min_emit_precision, default
  0.0 = emit all) replace "every match is a finding".
- CLI loads the ruleset as a stage and uses it for the fail-loud policy check,
  detection, hunter pool, and the score CWE map. Wheel ships the ruleset TOML.

85 tests pass; ruff + mypy clean. Spec tasks 3.1-3.4 checked off.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
Marius Ciepluch
2026-06-22 15:27:16 +02:00
co-authored by Claude Opus 4.8
parent fb7594a3ed
commit 444d8c9490
15 changed files with 645 additions and 347 deletions
+71
View File
@@ -0,0 +1,71 @@
[[rule]]
rule_id = "c-exec-family"
title = "C exec command execution needs review"
languages = ["c", "cpp"]
cwe = "CWE-78"
tags = ["syscall_entry"]
status = "enabled"
min_evidence_level = "static_corroboration"
precision_estimate = 0.0
version = "1"
pattern = '''\b(execl|execlp|execle|execv|execvp|execvpe)\s*\('''
[[rule]]
rule_id = "c-shell-exec"
title = "C shell execution needs review"
languages = ["c", "cpp"]
cwe = "CWE-78"
tags = ["syscall_entry"]
status = "enabled"
min_evidence_level = "static_corroboration"
precision_estimate = 0.0
version = "1"
pattern = '''\b(system|popen)\s*\('''
[[rule]]
rule_id = "c-unsafe-copy"
title = "Unsafe C string copy needs review"
languages = ["c", "cpp"]
cwe = "CWE-121"
tags = ["memory_unsafe"]
status = "enabled"
min_evidence_level = "static_corroboration"
precision_estimate = 0.0
version = "1"
pattern = '''\b(gets|strcpy|strcat|sprintf)\s*\('''
[[rule]]
rule_id = "c-unsafe-format"
title = "Unsafe C format string needs review"
languages = ["c", "cpp"]
cwe = "CWE-134"
tags = ["memory_unsafe"]
status = "enabled"
min_evidence_level = "static_corroboration"
precision_estimate = 0.0
version = "1"
pattern = '''\b(printf|vprintf|vsprintf|vsnprintf|vfprintf|syslog)\s*\(\s*[A-Za-z_]'''
[[rule]]
rule_id = "c-unsafe-memory-copy"
title = "Unsafe C memory copy needs review"
languages = ["c", "cpp"]
cwe = "CWE-121"
tags = ["memory_unsafe"]
status = "enabled"
min_evidence_level = "static_corroboration"
precision_estimate = 0.0
version = "1"
pattern = '''\b(memcpy|memmove|bcopy)\s*\('''
[[rule]]
rule_id = "c-unsafe-scanf"
title = "Unbounded C scanf needs review"
languages = ["c", "cpp"]
cwe = "CWE-121"
tags = ["memory_unsafe"]
status = "enabled"
min_evidence_level = "static_corroboration"
precision_estimate = 0.0
version = "1"
pattern = '''\b(scanf|fscanf|sscanf)\s*\([^\n]*%s'''