feat: plan 16 P2 batch 2 (SEC-12 drift, SEC-29 audit fail-closed, SEC-30 replay, SEC-15 low)

- SEC-12: drift-detect adds semantic invariants — negation-flip detection (a dropped
  "not" now FAILS despite high char-similarity) + env must-keep patterns.
- SEC-29 (X-05): governance-check audit write fails CLOSED — an unwritable audit log
  denies the action and empties the output (no unaudited output).
- SEC-30 (X-06): approval-verify records a one-time-use nonce (sha of token/sig) and
  rejects replays (enforced mode / when a nonce ledger is set); dev unchanged.
- SEC-15 (low): typosquat distance<=2 with the levenshtein length-sentinel bug fixed
  (no false positives); tool-exec fails closed with no timeout backend in enforced
  mode; validate-tool-input now validates nested objects/arrays recursively.

Verify: new SEC suites all green via gate, run-casan4 0-FAIL, adversarial 44/44,
track-c 29/0, h5-approval 12/0, no regressions.

Plan-16 P2 remaining: infra-gated only (SEC-14/22/23/24/25/26).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
thanhnv
2026-07-06 22:55:24 +09:00
co-authored by Claude Opus 4.8
parent 8c06a55aed
commit d695a598ee
14 changed files with 365 additions and 30 deletions
@@ -69,8 +69,12 @@ def parse_deps(text: str, name: str):
def levenshtein(a: str, b: str) -> int:
if abs(len(a) - len(b)) > 1:
return 2
# SEC-15: return a LARGE sentinel (not 2) when lengths are far apart — the old
# sentinel 2 collided with the widened distance<=2 typosquat threshold and
# produced false positives (e.g. fastapi vs numpy). For len-diff<=2 the DP below
# computes the true edit distance.
if abs(len(a) - len(b)) > 2:
return 99
prev = list(range(len(b) + 1))
for i, ca in enumerate(a, 1):
row = [i]
@@ -106,7 +110,11 @@ def main() -> int:
findings.append({"package": name, "reason": "denylisted_or_known_malicious"})
continue
if name not in known:
near = next((k for k in known if levenshtein(name.lower(), k.lower()) == 1), None)
# SEC-15: distance==1 missed 2-char typosquats (e.g. reqests/reqeusts of
# "requests"). Allow distance<=2 for names long enough that a 2-edit match
# is meaningful (short names stay at 1 to avoid false positives).
_maxd = 2 if len(name) >= 5 else 1
near = next((k for k in known if 1 <= levenshtein(name.lower(), k.lower()) <= _maxd), None)
if near:
findings.append({"package": name, "reason": "typosquat_of:" + near})
continue