@clear-capabilities/agentic-security-scanner 0.166.0 → 0.168.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/bin/agentic-security.js +40 -4
  3. package/dist/2199.index.js +348 -0
  4. package/dist/3736.index.js +2 -1
  5. package/dist/3740.index.js +722 -0
  6. package/dist/4113.index.js +1 -1
  7. package/dist/4142.index.js +929 -0
  8. package/dist/5738.index.js +240 -0
  9. package/dist/6932.index.js +1190 -0
  10. package/dist/7838.index.js +1 -1
  11. package/dist/808.index.js +1330 -0
  12. package/dist/818.index.js +93 -0
  13. package/dist/9800.index.js +245 -0
  14. package/dist/9999.index.js +2 -2
  15. package/dist/agentic-security.mjs +2 -2
  16. package/dist/agentic-security.mjs.sha256 +1 -1
  17. package/package.json +2 -2
  18. package/src/posture/artifact-registry.js +6 -0
  19. package/src/posture/fleet/cli.js +103 -0
  20. package/src/posture/fleet/fleet-graph.js +142 -0
  21. package/src/posture/fleet/fleet-manifest.js +77 -0
  22. package/src/posture/proof-fabric/cli.js +115 -0
  23. package/src/posture/proof-fabric/fix-proof.js +73 -0
  24. package/src/posture/proof-fabric/oracles.js +101 -0
  25. package/src/posture/proof-fabric/orchestrator.js +129 -0
  26. package/src/posture/proof-fabric/reproducer.js +108 -0
  27. package/src/posture/proof-fabric/runner.js +243 -0
  28. package/src/posture/proof-fabric/states.js +53 -0
  29. package/src/posture/proof-fabric/store.js +83 -0
  30. package/src/posture/rule-compiler/cli.js +141 -0
  31. package/src/posture/rule-compiler/corpus.js +61 -0
  32. package/src/posture/rule-compiler/eligibility.js +25 -0
  33. package/src/posture/rule-compiler/ir.js +87 -0
  34. package/src/posture/rule-compiler/lifecycle.js +57 -0
  35. package/src/posture/rule-compiler/mutation.js +60 -0
  36. package/src/posture/rule-compiler/promotion.js +47 -0
  37. package/src/posture/rule-compiler/store.js +96 -0
  38. package/src/sandbox/backend-userspace.js +7 -1
  39. package/src/sandbox/trust-boundary.js +2 -1
@@ -0,0 +1,83 @@
1
+ // Proof artifact store: `.agentic-security/proofs/<findingKey>/<recordDigest>.json`.
2
+ //
3
+ // Reuses the existing state machinery rather than adding another: `safeWriteState` (refuses unsafe dirs and symlinks),
4
+ // `maybeEncryptForWrite` / `maybeDecryptForRead` (the `proofs` artifact is registered `confidential`), retention
5
+ // (evidence class) and legal holds (a hold on `proofs` blocks purge). Nothing persisted contains a plaintext credential:
6
+ // records are passed through a recursive secret filter first, and the runner never returns its per-run nonce or secrets.
7
+ import fs from 'node:fs';
8
+ import path from 'node:path';
9
+ import { statePath, ensureStateDir, safeWriteState } from '../state-dir.js';
10
+ import { maybeEncryptForWrite, maybeDecryptForRead } from '../encryption-provider.js';
11
+ import { redactSecrets } from '../../llm-validator/redact.js';
12
+ import { canonicalize, sha256 } from './reproducer.js';
13
+
14
+ const PROOF_RECORD_SCHEMA = 'agentic-security/proof-record';
15
+ const PROOFS_DIR = 'proofs';
16
+ const MAX_RECORD_BYTES = 512 * 1024;
17
+
18
+ export const findingKey = (id) => `${String(id).replace(/[^A-Za-z0-9._-]/g, '_').slice(0, 80)}-${sha256(String(id)).slice(0, 8)}`;
19
+
20
+ // The shared redactor catches key=value shapes; bare tokens need their own pass, so persisted evidence cannot carry one.
21
+ const BARE_SECRET_RES = [
22
+ /\b(?:ghp|gho|ghu|ghs|ghr|github_pat)_[A-Za-z0-9_]{20,}\b/g, /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g, /\bsk-[A-Za-z0-9_-]{20,}\b/g,
23
+ /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g, /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,
24
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g, /\bSECRET-[0-9a-f]{24}-[a-z0-9_-]+\b/g,
25
+ ];
26
+ function scrubString(text) {
27
+ let t = String(text);
28
+ try { t = redactSecrets(t).text; } catch { return '[unredactable]'; }
29
+ for (const re of BARE_SECRET_RES) t = t.replace(re, '[REDACTED-SECRET]');
30
+ return t;
31
+ }
32
+
33
+ /** Deep-copy `v` with every string passed through the secret redactor. */
34
+ export function scrubDeep(v) {
35
+ if (typeof v === 'string') return scrubString(v);
36
+ if (Array.isArray(v)) return v.map(scrubDeep);
37
+ if (v && typeof v === 'object') return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, scrubDeep(x)]));
38
+ return v;
39
+ }
40
+
41
+ function recordDigest(record) {
42
+ const { recordDigest: _omit, ...rest } = record;
43
+ return sha256(canonicalize(rest));
44
+ }
45
+
46
+ export function saveProof(scanRoot, record) {
47
+ const safe = scrubDeep(record);
48
+ safe.schema = PROOF_RECORD_SCHEMA;
49
+ safe.recordDigest = recordDigest(safe);
50
+ const text = JSON.stringify(safe, null, 2);
51
+ if (Buffer.byteLength(text) > MAX_RECORD_BYTES) return { ok: false, reason: 'proof record exceeds the size limit' };
52
+ if (!ensureStateDir(scanRoot)) return { ok: false, reason: 'no safe state directory for this project' };
53
+ const enc = maybeEncryptForWrite(scanRoot, PROOFS_DIR, text);
54
+ if (!enc.ok) return { ok: false, reason: enc.reason };
55
+ const file = statePath(scanRoot, PROOFS_DIR, findingKey(record.findingId), `${safe.recordDigest}.json`);
56
+ if (!safeWriteState(file, enc.content)) return { ok: false, reason: 'state write refused' };
57
+ return { ok: true, file, digest: safe.recordDigest, encrypted: enc.encrypted };
58
+ }
59
+
60
+ function readRecord(file) {
61
+ const text = maybeDecryptForRead(fs.readFileSync(file, 'utf8'));
62
+ const rec = JSON.parse(text);
63
+ // A record whose content no longer matches its own digest was modified after it was written.
64
+ if (rec.recordDigest !== recordDigest(rec)) return { tampered: true, record: rec };
65
+ return { tampered: false, record: rec };
66
+ }
67
+
68
+ /** Newest-first records for a finding. Tampered records are returned flagged, never silently dropped or trusted. */
69
+ export function listProofs(scanRoot, findingId) {
70
+ const dir = statePath(scanRoot, PROOFS_DIR, findingKey(findingId));
71
+ let names = [];
72
+ try { names = fs.readdirSync(dir).filter((n) => n.endsWith('.json')); } catch { return []; }
73
+ const out = [];
74
+ for (const n of names) {
75
+ const f = path.join(dir, n);
76
+ try { const { tampered, record } = readRecord(f); out.push({ file: f, tampered, record, mtime: fs.statSync(f).mtimeMs }); } catch { out.push({ file: f, tampered: true, record: null, mtime: 0 }); }
77
+ }
78
+ return out.sort((a, b) => b.mtime - a.mtime);
79
+ }
80
+
81
+ export function latestProof(scanRoot, findingId) {
82
+ return listProofs(scanRoot, findingId).find((p) => p.record) ?? null;
83
+ }
@@ -0,0 +1,141 @@
1
+ // CLI surface: `agentic-security rules synthesize|evaluate|promote|status|rollback`.
2
+ //
3
+ // Distinct from the pre-existing `labs synthesize-rule` (posture/rule-synthesis.js), which proposes SUPPRESSION
4
+ // rules from repeated false-positive triage verdicts — a different input, a different output and a different
5
+ // command group on purpose, so the two are never confused at the CLI.
6
+ //
7
+ // Non-interactive by construction: no command reads stdin, every corpus evaluation is bounded (no network, no
8
+ // model call — this is the deterministic half; a model may PROPOSE the candidate pattern this CLI is handed, but
9
+ // it never decides promotion).
10
+ import fs from 'node:fs';
11
+ import { statePath } from '../state-dir.js';
12
+ import { draftRuleIR, validateRuleIR, ruleIRDigest } from './ir.js';
13
+ import { checkEligibility } from './eligibility.js';
14
+ import { buildCorpus } from './corpus.js';
15
+ import { gatePromotion, checkLeakage, PROMOTION_DEFAULTS } from './promotion.js';
16
+ import { recordTransition, stateOf, historyFor } from './lifecycle.js';
17
+ import { loadLedger, saveLedger, saveIR, loadIR, saveEvaluation, writeFixtures, publishCustomRule, unpublishCustomRule } from './store.js';
18
+
19
+ function loadFinding(root, id) {
20
+ const p = statePath(root, 'last-scan.json');
21
+ if (!fs.existsSync(p)) return null;
22
+ const last = JSON.parse(fs.readFileSync(p, 'utf8'));
23
+ return (last.findings || []).find((f) => f.id === id || f.stableId === id) ?? null;
24
+ }
25
+
26
+ function readJsonArg(pathOrInline) {
27
+ if (!pathOrInline) return null;
28
+ try { return JSON.parse(fs.existsSync(pathOrInline) ? fs.readFileSync(pathOrInline, 'utf8') : pathOrInline); } catch { return null; }
29
+ }
30
+
31
+ export async function cmdRules(args, root) {
32
+ const sub = args._[1];
33
+ if (sub === 'synthesize') return cmdSynthesize(args, root);
34
+ if (sub === 'evaluate') return cmdEvaluate(args, root);
35
+ if (sub === 'promote') return cmdPromote(args, root);
36
+ if (sub === 'status') return cmdStatus(args, root);
37
+ if (sub === 'rollback') return cmdRollback(args, root);
38
+ console.error('usage: rules synthesize <finding-id> --spec <file.json> | evaluate <rule-id> | promote <rule-id> | status [rule-id] | rollback <rule-id>');
39
+ return 4;
40
+ }
41
+
42
+ // `--spec` carries exactly what a model or a human proposed: id/title/severity/cwe/family/languages/match/
43
+ // remediation plus the corpus inputs (originalCode/patchedCode/nearbyCleanCode/protectedVariantsCode). This
44
+ // command never invents a pattern; it only drafts the IR, checks eligibility and records DRAFT->CANDIDATE.
45
+ async function cmdSynthesize(args, root) {
46
+ const findingId = args._[2];
47
+ const spec = readJsonArg(args.flags.spec);
48
+ if (!findingId || !spec) { console.error('usage: rules synthesize <finding-id> --spec <file.json>'); return 4; }
49
+ const finding = loadFinding(root, findingId);
50
+ if (!finding) { console.error(`Finding ${findingId} not found in last scan.`); return 4; }
51
+ const elig = checkEligibility(finding, { proofState: args.flags['proof-state'] || null });
52
+ if (!elig.eligible) { console.error(`Not eligible for rule synthesis: ${elig.reasons.join('; ')}`); return 4; }
53
+
54
+ const ir = draftRuleIR({ ...spec, originatingFindingId: finding.stableId ?? finding.id, originatingCommit: spec.originatingCommit ?? null, evidenceState: elig.evidenceState });
55
+ const errs = validateRuleIR(ir);
56
+ if (errs.length) { console.error(`Invalid rule: ${errs.join('; ')}`); return 4; }
57
+
58
+ const ledger = loadLedger(root);
59
+ const draft = recordTransition(ledger, { ruleId: ir.id, from: null, to: 'DRAFT', reason: `drafted from ${findingId}`, actor: args.flags.actor || 'agent' });
60
+ if (!draft.ok) { console.error(draft.reason); return 4; }
61
+ const cand = recordTransition(ledger, { ruleId: ir.id, from: 'DRAFT', to: 'CANDIDATE', reason: 'eligibility passed', evidence: { eligibility: elig }, actor: args.flags.actor || 'agent' });
62
+ saveLedger(root, ledger);
63
+ saveIR(root, { ...ir, state: 'CANDIDATE' });
64
+ console.log(`Rule ${ir.id}: DRAFT -> CANDIDATE (digest ${ruleIRDigest(ir).slice(0, 12)})`);
65
+ if (!cand.ok) console.error(cand.reason);
66
+ return 0;
67
+ }
68
+
69
+ async function cmdEvaluate(args, root) {
70
+ const id = args._[2];
71
+ if (!id) { console.error('usage: rules evaluate <rule-id> --corpus <file.json>'); return 4; }
72
+ const ir = loadIR(root, id);
73
+ if (!ir) { console.error(`No rule IR found for ${id}. Run rules synthesize first.`); return 4; }
74
+ const corpusSpec = readJsonArg(args.flags.corpus);
75
+ if (!corpusSpec) { console.error('No corpus supplied. Pass --corpus <file.json> with originalCode (and optionally patchedCode/nearbyCleanCode/protectedVariantsCode).'); return 4; }
76
+ const corpus = buildCorpus(ir, corpusSpec);
77
+ const leakage = checkLeakage(JSON.stringify(corpusSpec), { benchmarkNames: args.flags['benchmark-names'] ? String(args.flags['benchmark-names']).split(',') : [] });
78
+ const gate = gatePromotion({ ir, corpus, leakageCheck: leakage, thresholds: args.flags.thresholds ? readJsonArg(args.flags.thresholds) : PROMOTION_DEFAULTS });
79
+
80
+ const ledger = loadLedger(root);
81
+ const cur = stateOf(ledger, id);
82
+ if (gate.pass && cur === 'CANDIDATE') recordTransition(ledger, { ruleId: id, from: 'CANDIDATE', to: 'VALIDATED', reason: 'promotion gate passed', evidence: { score: gate.evaluation.score }, actor: args.flags.actor || 'agent' });
83
+ saveLedger(root, ledger);
84
+ saveEvaluation(root, id, gate);
85
+ if (args.flags.json) console.log(JSON.stringify(gate, null, 2));
86
+ else {
87
+ console.log(`Rule ${id}: ${gate.pass ? 'VALIDATED' : 'gate FAILED'}`);
88
+ console.log(` precision ${fmtPct(gate.evaluation.score.precision)} recall ${fmtPct(gate.evaluation.score.recall)} protected-failures ${gate.evaluation.score.protectedFailures}`);
89
+ for (const r of gate.reasons) console.log(` - ${r}`);
90
+ }
91
+ if (gate.pass) { const f = writeFixtures(root, ir, corpus); if (f.ok) console.log(` regression fixtures written: ${f.written.length} file(s) under .agentic-security/rule-compiler/${id}/fixtures/`); }
92
+ return gate.pass ? 0 : 1;
93
+ }
94
+ const fmtPct = (n) => (n == null ? 'unmeasured' : `${(n * 100).toFixed(1)}%`);
95
+
96
+ async function cmdPromote(args, root) {
97
+ const id = args._[2];
98
+ if (!id) { console.error('usage: rules promote <rule-id>'); return 4; }
99
+ const ledger = loadLedger(root);
100
+ if (ledger.tampered) { console.error(`Refusing: rule lifecycle ledger failed its integrity check (${ledger.tamperReason}).`); return 4; }
101
+ if (stateOf(ledger, id) !== 'VALIDATED') { console.error(`Rule ${id} is not VALIDATED (run rules evaluate first); refusing to promote.`); return 4; }
102
+ const ir = loadIR(root, id);
103
+ if (!ir) { console.error(`No rule IR found for ${id}.`); return 4; }
104
+ const pub = publishCustomRule(root, ir);
105
+ if (!pub.ok) { console.error(`Promotion refused: ${pub.reason}`); return 4; }
106
+ const t = recordTransition(ledger, { ruleId: id, from: 'VALIDATED', to: 'PROMOTED', reason: args.flags.reason || 'promoted', actor: args.flags.actor || 'operator' });
107
+ saveLedger(root, ledger);
108
+ console.log(`Rule ${id}: VALIDATED -> PROMOTED. Published to ${pub.file}.`);
109
+ if (!t.ok) console.error(t.reason);
110
+ return 0;
111
+ }
112
+
113
+ async function cmdRollback(args, root) {
114
+ const id = args._[2];
115
+ if (!id) { console.error('usage: rules rollback <rule-id> [--reason text]'); return 4; }
116
+ const ledger = loadLedger(root);
117
+ const cur = stateOf(ledger, id);
118
+ if (cur !== 'PROMOTED') { console.error(`Rule ${id} is not PROMOTED (current: ${cur ?? 'unknown'}); nothing to roll back.`); return 4; }
119
+ const un = unpublishCustomRule(root, id);
120
+ const t = recordTransition(ledger, { ruleId: id, from: 'PROMOTED', to: 'ROLLED_BACK', reason: args.flags.reason || 'operator rollback', actor: args.flags.actor || 'operator' });
121
+ saveLedger(root, ledger);
122
+ console.log(`Rule ${id}: PROMOTED -> ROLLED_BACK.${un.ok ? '' : ` (warning: could not remove published rule file: ${un.reason})`}`);
123
+ return t.ok ? 0 : 1;
124
+ }
125
+
126
+ async function cmdStatus(args, root) {
127
+ const ledger = loadLedger(root);
128
+ const id = args._[2];
129
+ if (id) {
130
+ const h = historyFor(ledger, id);
131
+ if (!h.length) { console.error(`No history for rule ${id}.`); return 4; }
132
+ if (args.flags.json) console.log(JSON.stringify(h, null, 2));
133
+ else { console.log(`Rule ${id}: ${stateOf(ledger, id)}`); for (const e of h) console.log(` ${e.at} ${e.from ?? '(new)'} -> ${e.to} (${e.reason || ''})`); }
134
+ return 0;
135
+ }
136
+ const ids = [...new Set(ledger.entries.map((e) => e.ruleId))];
137
+ if (args.flags.json) { console.log(JSON.stringify(ids.map((rid) => ({ id: rid, state: stateOf(ledger, rid) })), null, 2)); return 0; }
138
+ if (!ids.length) { console.log('No rules in the lifecycle ledger yet.'); return 0; }
139
+ for (const rid of ids) console.log(` ${rid.padEnd(40)} ${stateOf(ledger, rid)}`);
140
+ return 0;
141
+ }
@@ -0,0 +1,61 @@
1
+ // Automatic corpus creation and scoring for ONE candidate rule.
2
+ //
3
+ // Positive corpus: the original vulnerable snippet plus its generated sibling variants (mutation.js).
4
+ // Negative corpus: an explicit patched/clean variant the caller supplies, nearby clean patterns, protected
5
+ // (sanitizer/guard) variants, and the NEGATIVE mutations of those (same mutators, applied to the safe side) — a
6
+ // candidate rule must decline on ALL of them, not just the one hand-picked negative.
7
+ //
8
+ // Scoring reuses the exact runtime matcher (`runRule` in `../custom-rules.js`), never a second implementation —
9
+ // the score a rule gets here is the score it will actually produce once promoted.
10
+ import { generateVariants } from './mutation.js';
11
+ import { compileToCustomRule } from './ir.js';
12
+ import { runRule } from '../custom-rules.js';
13
+
14
+ function toRuntimeRule(ir) {
15
+ const { ok, doc, errs } = compileToCustomRule(ir);
16
+ if (!ok) throw new Error(`cannot build corpus for an invalid rule IR: ${errs.join('; ')}`);
17
+ const regexes = doc.match.allOf ? doc.match.allOf.map((p) => new RegExp(p, 'g')) : [new RegExp(doc.match.pattern, 'g')];
18
+ return {
19
+ id: doc.id, title: doc.title, severity: doc.severity, cwe: doc.cwe, languages: doc.languages, message: doc.message,
20
+ regexes, requireAll: !!doc.match.allOf, windowLines: doc.match.window ?? 5,
21
+ notMatch: doc.match.notMatch ? new RegExp(doc.match.notMatch) : null,
22
+ };
23
+ }
24
+
25
+ const EXT = { javascript: 'x.js', typescript: 'x.ts', python: 'x.py', go: 'x.go', ruby: 'x.rb', java: 'x.java', rust: 'x.rs', csharp: 'x.cs', php: 'x.php', any: 'x.txt' };
26
+
27
+ export function buildCorpus(ir, { originalCode, patchedCode = null, nearbyCleanCode = [], protectedVariantsCode = [] } = {}) {
28
+ const positives = [{ label: 'original', code: originalCode }];
29
+ for (const v of generateVariants(originalCode)) positives.push({ label: `sibling:${v.mutator}`, code: v.code });
30
+
31
+ const negatives = [];
32
+ if (patchedCode) {
33
+ negatives.push({ label: 'patched', code: patchedCode });
34
+ for (const v of generateVariants(patchedCode)) negatives.push({ label: `patched-sibling:${v.mutator}`, code: v.code });
35
+ }
36
+ nearbyCleanCode.forEach((code, i) => negatives.push({ label: `nearby-clean-${i}`, code }));
37
+ protectedVariantsCode.forEach((code, i) => negatives.push({ label: `protected-${i}`, code }));
38
+
39
+ return { positives, negatives };
40
+ }
41
+
42
+ export function scoreRule(ir, corpus) {
43
+ const rule = toRuntimeRule(ir);
44
+ const lang = ir.languages[0] || 'javascript';
45
+ const file = EXT[lang] || 'x.txt';
46
+ const run = (code) => runRule(rule, file, code).length > 0;
47
+
48
+ const posResults = corpus.positives.map((p) => ({ ...p, fired: run(p.code) }));
49
+ const negResults = corpus.negatives.map((n) => ({ ...n, fired: run(n.code) }));
50
+
51
+ const tp = posResults.filter((p) => p.fired).length;
52
+ const fn = posResults.length - tp;
53
+ const fp = negResults.filter((n) => n.fired).length;
54
+ const tn = negResults.length - fp;
55
+ const precision = tp + fp === 0 ? null : tp / (tp + fp);
56
+ const recall = corpus.positives.length === 0 ? null : tp / corpus.positives.length;
57
+ // "Protected negative failures" per PRD 3: a rule firing on a sanitizer/guard-carrying variant specifically.
58
+ const protectedFailures = negResults.filter((n) => n.label.startsWith('protected-') && n.fired).length;
59
+
60
+ return { precision, recall, tp, fp, tn, fn, protectedFailures, positives: posResults, negatives: negResults };
61
+ }
@@ -0,0 +1,25 @@
1
+ // X-eligibility (PRD 3 "Eligibility"): a finding may enter synthesis only with a stable id, a vulnerable span, a
2
+ // CWE/family, code-flow evidence, and deterministic corroboration OR exploit proof. Reuses the existing
3
+ // vocabularies rather than inventing a third: proof-fabric's evidence-state ladder when present, else the older
4
+ // proof-tier (`taint-proven`/`execution-proven`), else the finding's own `parser` (an IR-TAINT/MULTI-SINK parser
5
+ // origin is code-flow evidence even with no execution).
6
+ import { stateRank } from '../proof-fabric/states.js';
7
+
8
+ const ANALYSED_PARSERS = new Set(['IR-TAINT', 'MULTI-SINK']);
9
+
10
+ export function checkEligibility(finding, { proofState = null } = {}) {
11
+ const reasons = [];
12
+ if (!finding?.stableId && !finding?.id) reasons.push('no stable id');
13
+ if (finding?.file == null || finding?.line == null) reasons.push('no vulnerable span (file:line)');
14
+ if (!finding?.cwe && !finding?.family) reasons.push('no CWE or family');
15
+ const hasFlowEvidence = ANALYSED_PARSERS.has(finding?.parser) || !!finding?.snippet;
16
+ if (!hasFlowEvidence) reasons.push('no code-flow evidence (parser not IR-TAINT/MULTI-SINK, no snippet)');
17
+
18
+ let evidenceState = 'none';
19
+ if (proofState && stateRank(proofState) >= stateRank('EXPLOIT_CONFIRMED')) evidenceState = 'execution-proven';
20
+ else if (finding?.proofTier === 'execution-proven') evidenceState = 'execution-proven';
21
+ else if (finding?.proofTier === 'taint-proven' || ANALYSED_PARSERS.has(finding?.parser)) evidenceState = 'taint-proven';
22
+ if (evidenceState === 'none') reasons.push('no deterministic corroboration or exploit proof');
23
+
24
+ return { eligible: reasons.length === 0, reasons, evidenceState };
25
+ }
@@ -0,0 +1,87 @@
1
+ // Rule intermediate representation: compiles DOWN into the existing pattern-rule engine
2
+ // (`../custom-rules.js`), never a parallel matcher. "Compile" here means "emit a
3
+ // custom-rules.js-compatible document" — detection at runtime is 100% the already-proven
4
+ // regex/allOf/notMatch/window engine; this module only adds PROVENANCE metadata around it.
5
+ //
6
+ // A rule's match semantics are deliberately the same closed vocabulary custom-rules.js
7
+ // already supports (pattern | allOf+window | notMatch) — no new matching primitive is
8
+ // introduced, so a promoted rule is exactly as auditable as a hand-written one.
9
+ import { sha256 } from '../proof-fabric/reproducer.js';
10
+
11
+ const RULE_IR_SCHEMA = 'agentic-security/rule-ir';
12
+ const RULE_IR_VERSION = 1;
13
+ export const RULE_STATES = Object.freeze(['DRAFT', 'CANDIDATE', 'VALIDATED', 'PROMOTED', 'DEPRECATED', 'ROLLED_BACK']);
14
+ const VALID_LANGS = new Set(['javascript', 'typescript', 'python', 'go', 'ruby', 'java', 'rust', 'csharp', 'php', 'any']);
15
+
16
+ const isStr = (v) => typeof v === 'string' && v.length > 0;
17
+
18
+ /** Problems with a draft rule IR; an empty list means it is well-formed (not yet validated/promoted). */
19
+ export function validateRuleIR(ir) {
20
+ const errs = [];
21
+ if (!ir || typeof ir !== 'object') return ['rule IR must be an object'];
22
+ if (ir.schema !== RULE_IR_SCHEMA) errs.push('unknown schema');
23
+ if (ir.version !== RULE_IR_VERSION) errs.push('unsupported rule IR version');
24
+ if (!isStr(ir.id) || !/^[a-z][a-z0-9-]{2,79}$/.test(ir.id)) errs.push('id must be a lowercase kebab-case string');
25
+ if (!isStr(ir.title)) errs.push('title required');
26
+ if (!['critical', 'high', 'medium', 'low'].includes(ir.severity)) errs.push('severity must be critical|high|medium|low');
27
+ if (!isStr(ir.cwe) || !/^CWE-\d+$/.test(ir.cwe)) errs.push('cwe must look like CWE-###');
28
+ if (!isStr(ir.family)) errs.push('family required');
29
+ if (!Array.isArray(ir.languages) || !ir.languages.length || !ir.languages.every((l) => VALID_LANGS.has(l))) errs.push('languages must be a non-empty array from the supported set');
30
+ const m = ir.match;
31
+ if (!m || typeof m !== 'object') errs.push('match required');
32
+ else {
33
+ const hasPattern = isStr(m.pattern);
34
+ const hasAllOf = Array.isArray(m.allOf) && m.allOf.length > 0 && m.allOf.every(isStr);
35
+ if (!hasPattern && !hasAllOf) errs.push('match needs a pattern or a non-empty allOf');
36
+ if (m.notMatch != null && !isStr(m.notMatch)) errs.push('notMatch must be a string');
37
+ if (m.window != null && !(Number.isInteger(m.window) && m.window > 0 && m.window <= 200)) errs.push('window must be an integer in 1..200');
38
+ for (const re of [m.pattern, ...(m.allOf || []), m.notMatch].filter(isStr)) {
39
+ try { new RegExp(re); } catch { errs.push(`invalid regex: ${re}`); }
40
+ }
41
+ }
42
+ // Declarative-only hint fields (sink/source/propagator/sanitizer/dominatingGuard/frameworkConstraint): free text,
43
+ // never executable, carried for provenance/review only and NOT consulted by the matcher.
44
+ for (const k of ['sink', 'source', 'propagator', 'sanitizer', 'dominatingGuard', 'frameworkConstraint']) {
45
+ if (ir[k] != null && !isStr(ir[k])) errs.push(`${k} must be a string when present`);
46
+ }
47
+ if (ir.exclusions != null && !(Array.isArray(ir.exclusions) && ir.exclusions.every(isStr))) errs.push('exclusions must be an array of strings');
48
+ if (!ir.provenance || typeof ir.provenance !== 'object') errs.push('provenance required');
49
+ else {
50
+ if (!isStr(ir.provenance.originatingFindingId)) errs.push('provenance.originatingFindingId required');
51
+ if (ir.provenance.originatingCommit != null && !isStr(ir.provenance.originatingCommit)) errs.push('provenance.originatingCommit must be a string');
52
+ if (!['execution-proven', 'taint-proven', 'deterministic-corroboration'].includes(ir.provenance.evidenceState)) errs.push('provenance.evidenceState must be execution-proven|taint-proven|deterministic-corroboration');
53
+ }
54
+ return errs;
55
+ }
56
+
57
+ export function ruleIRDigest(ir) {
58
+ const { state: _s, ...rest } = ir;
59
+ return sha256(JSON.stringify(rest, Object.keys(rest).sort()));
60
+ }
61
+
62
+ /** Build a DRAFT rule IR. Caller supplies the candidate pattern — this module never invents one. */
63
+ export function draftRuleIR({ id, title, severity, cwe, family, languages, match, remediation, originatingFindingId, originatingCommit, evidenceState, model = null, promptConfigVersion = null, ...hints }) {
64
+ const ir = {
65
+ schema: RULE_IR_SCHEMA, version: RULE_IR_VERSION, id, title, severity, cwe, family, languages, match,
66
+ remediation: remediation ?? null, state: 'DRAFT',
67
+ ...hints,
68
+ provenance: { originatingFindingId, originatingCommit: originatingCommit ?? null, evidenceState, model, promptConfigVersion },
69
+ };
70
+ return ir;
71
+ }
72
+
73
+ /** Compile a rule IR into a custom-rules.js-compatible document. Caller writes the YAML. */
74
+ export function compileToCustomRule(ir) {
75
+ const errs = validateRuleIR(ir);
76
+ if (errs.length) return { ok: false, errs };
77
+ const doc = {
78
+ id: `rule-compiler/${ir.id}`,
79
+ title: ir.title,
80
+ severity: ir.severity,
81
+ cwe: ir.cwe,
82
+ languages: ir.languages,
83
+ match: { ...(ir.match.pattern ? { pattern: ir.match.pattern } : {}), ...(ir.match.allOf ? { allOf: ir.match.allOf, window: ir.match.window ?? 5 } : {}), ...(ir.match.notMatch ? { notMatch: ir.match.notMatch } : {}) },
84
+ message: ir.remediation || `${ir.title} (${ir.cwe}, compiled from ${ir.provenance.originatingFindingId})`,
85
+ };
86
+ return { ok: true, doc };
87
+ }
@@ -0,0 +1,57 @@
1
+ // Rule lifecycle ledger: DRAFT -> CANDIDATE -> VALIDATED -> PROMOTED -> DEPRECATED/ROLLED_BACK.
2
+ //
3
+ // Append-only, hash-chained (same idiom as remediation-ledger.js / invariants/lifecycle.js): each entry links the
4
+ // previous entry's hash, so a dropped or edited middle record breaks the chain and `verifyLedger` catches it. A
5
+ // rule cannot promote itself: `transition` only records a transition a caller already decided; `advance`-style
6
+ // guarding on VALID edges lives here so no caller can jump DRAFT straight to PROMOTED.
7
+ import { createHash } from 'node:crypto';
8
+ import { RULE_STATES } from './ir.js';
9
+
10
+ export const LEDGER_SCHEMA = 'agentic-security/rule-lifecycle-ledger';
11
+ const EDGES = Object.freeze({
12
+ DRAFT: ['CANDIDATE'], CANDIDATE: ['VALIDATED', 'DRAFT'], VALIDATED: ['PROMOTED', 'DRAFT'],
13
+ PROMOTED: ['DEPRECATED', 'ROLLED_BACK'], DEPRECATED: [], ROLLED_BACK: ['CANDIDATE'],
14
+ });
15
+
16
+ const hashOf = (entry) => createHash('sha256').update(JSON.stringify(entry)).digest('hex');
17
+
18
+ export function newLedger() { return { schema: LEDGER_SCHEMA, entries: [] }; }
19
+
20
+ function currentState(ledger, ruleId) {
21
+ for (let i = ledger.entries.length - 1; i >= 0; i--) if (ledger.entries[i].ruleId === ruleId) return ledger.entries[i].to;
22
+ return null;
23
+ }
24
+
25
+ /**
26
+ * Record a transition. Refuses an edge not in EDGES, and refuses DRAFT->DRAFT re-entry for a rule that has never
27
+ * existed (the first entry for a rule id must be `from: null, to: 'DRAFT'`).
28
+ */
29
+ export function recordTransition(ledger, { ruleId, from, to, reason, evidence = null, actor }) {
30
+ if (!RULE_STATES.includes(to)) return { ok: false, reason: `unknown target state: ${to}` };
31
+ const cur = currentState(ledger, ruleId);
32
+ if (cur === null) {
33
+ if (from !== null || to !== 'DRAFT') return { ok: false, reason: 'first transition for a rule must be null -> DRAFT' };
34
+ } else {
35
+ if (from !== cur) return { ok: false, reason: `stale transition: current state is ${cur}, not ${from}` };
36
+ if (!EDGES[cur].includes(to)) return { ok: false, reason: `cannot go from ${cur} to ${to}` };
37
+ }
38
+ const prevHash = ledger.entries.length ? ledger.entries[ledger.entries.length - 1].entryHash : null;
39
+ const entry = { ruleId, from: cur, to, reason: reason ?? null, evidence, actor: actor ?? 'unknown', at: new Date().toISOString(), prevHash };
40
+ entry.entryHash = hashOf({ ...entry, entryHash: undefined });
41
+ ledger.entries.push(entry);
42
+ return { ok: true, entry };
43
+ }
44
+
45
+ export function verifyLedger(ledger) {
46
+ let prevHash = null;
47
+ for (const e of ledger.entries) {
48
+ if (e.prevHash !== prevHash) return { ok: false, reason: 'chain broken: prevHash mismatch' };
49
+ const recomputed = hashOf({ ...e, entryHash: undefined });
50
+ if (recomputed !== e.entryHash) return { ok: false, reason: `entry hash mismatch for ${e.ruleId} at ${e.at}` };
51
+ prevHash = e.entryHash;
52
+ }
53
+ return { ok: true };
54
+ }
55
+
56
+ export function historyFor(ledger, ruleId) { return ledger.entries.filter((e) => e.ruleId === ruleId); }
57
+ export function stateOf(ledger, ruleId) { return currentState(ledger, ruleId); }
@@ -0,0 +1,60 @@
1
+ // Bounded structural mutation for ONE candidate rule's own corpus — not bench/mutation's engine-wide verdict-flip
2
+ // harness (a different scope: the whole deterministic+taint engine against curated adversarial cases). This module
3
+ // exists only to answer PRD 3's "unseen sibling" question for a single rule: does the candidate still fire on a
4
+ // structurally different variant of the same vulnerable shape, and does it still decline on a structurally
5
+ // different variant of a mitigated shape.
6
+ //
7
+ // Mutations are SYNTACTIC, deterministic, seed-free and language-agnostic: variable rename, wrap-in-function,
8
+ // reorder-adjacent-statements, insert-indirection (a pass-through local variable). No semantics are changed on
9
+ // purpose — a mutation that silently changed behavior would make a "same vulnerability, different shape" claim
10
+ // false.
11
+ const RESERVED = new Set(['if', 'else', 'for', 'while', 'return', 'function', 'const', 'let', 'var', 'import', 'from', 'def', 'class', 'req', 'res']);
12
+
13
+ function identifiers(code) {
14
+ const seen = new Set();
15
+ for (const m of code.matchAll(/\b([A-Za-z_][A-Za-z0-9_]*)\b/g)) if (!RESERVED.has(m[1]) && !/^[A-Z_]+$/.test(m[1])) seen.add(m[1]);
16
+ return [...seen];
17
+ }
18
+
19
+ function renameOne(code, from, to) {
20
+ if (!from || code.includes(to)) return null;
21
+ return code.replace(new RegExp(`\\b${from}\\b`, 'g'), to);
22
+ }
23
+
24
+ export function variableRename(code, { suffix = '_v2' } = {}) {
25
+ const ids = identifiers(code);
26
+ const target = ids.find((id) => id.length > 1);
27
+ if (!target) return null;
28
+ return renameOne(code, target, `${target}${suffix}`);
29
+ }
30
+
31
+ export function wrapInFunction(code, { name = 'wrapped_v2' } = {}) {
32
+ const indented = code.split('\n').map((l) => (l ? ` ${l}` : l)).join('\n');
33
+ return `function ${name}() {\n${indented}\n}\n${name}();\n`;
34
+ }
35
+
36
+ /** Insert a pass-through local for the first simple right-hand-side identifier assigned with `=` (indirection,
37
+ * no behavior change): `x = y;` becomes `const indirectN = y; x = indirectN;`. */
38
+ function insertIndirection(code) {
39
+ const m = /^(\s*)([A-Za-z_][A-Za-z0-9_.]*)\s*=\s*([A-Za-z_][A-Za-z0-9_.]*)\s*;/m.exec(code);
40
+ if (!m) return null;
41
+ const [whole, indent, lhs, rhs] = m;
42
+ const varName = `indirect_${Math.abs(_hash(rhs)) % 1000}`;
43
+ if (code.includes(varName)) return null;
44
+ return code.replace(whole, `${indent}const ${varName} = ${rhs};\n${indent}${lhs} = ${varName};`);
45
+ }
46
+
47
+ function _hash(s) { let h = 0; for (const c of s) h = (h * 31 + c.charCodeAt(0)) | 0; return h; }
48
+
49
+ const MUTATORS = Object.freeze({ 'variable-rename': variableRename, 'wrap-in-function': wrapInFunction, 'insert-indirection': insertIndirection });
50
+
51
+ /** Every mutator applied to `code`; mutators that don't apply (return null) are omitted, never silently skipped without saying so. */
52
+ export function generateVariants(code) {
53
+ const out = [];
54
+ for (const [name, fn] of Object.entries(MUTATORS)) {
55
+ let variant = null;
56
+ try { variant = fn(code); } catch { variant = null; }
57
+ if (variant && variant !== code) out.push({ mutator: name, code: variant });
58
+ }
59
+ return out;
60
+ }
@@ -0,0 +1,47 @@
1
+ // Promotion gate (PRD 3 "Promotion Gate"): precision/recall thresholds, zero protected-negative failures, zero
2
+ // benchmark leakage. A candidate rule does NOT automatically become a production rule — `evaluateRule` only
3
+ // scores; `gatePromotion` only judges; the lifecycle ledger (`lifecycle.js`) is the one place a PROMOTED state is
4
+ // actually recorded, and nothing in this module writes to it.
5
+ import { scoreRule } from './corpus.js';
6
+
7
+ export const PROMOTION_DEFAULTS = Object.freeze({ minPrecision: 0.95, minRecall: 0.80, maxProtectedFailures: 0 });
8
+
9
+ // Deliberately independent of posture/evaluation/custody.js: that module's term list and guard are reserved for
10
+ // the evaluation custodian's sealed-label boundary (QA-002.AC03), and a test enforces that nothing outside
11
+ // posture/evaluation/ may import it. The anti-cheating need here is smaller and different in kind — a rule
12
+ // synthesis prompt/corpus must not embed a benchmark name or an advisory id — so it gets its own small, independent
13
+ // checker rather than reaching across that isolation boundary.
14
+ const ADVISORY_ID = /\b(?:CVE|GHSA|CWE)-[A-Za-z0-9-]+\b/i;
15
+
16
+ /** Anti-cheating: the synthesis agent must never see benchmark answer keys, hidden labels, CWE-bearing hidden
17
+ * filenames or vulnerability-comment metadata. */
18
+ export function checkLeakage(promptText, { benchmarkNames = [] } = {}) {
19
+ const hay = String(promptText ?? '');
20
+ const offending = [];
21
+ for (const name of benchmarkNames) if (name && hay.toLowerCase().includes(String(name).toLowerCase())) offending.push(name);
22
+ const m = ADVISORY_ID.exec(hay);
23
+ if (m) offending.push(m[0]);
24
+ return { ok: offending.length === 0, offendingTerms: offending };
25
+ }
26
+
27
+ export function evaluateRule(ir, corpus, thresholds = PROMOTION_DEFAULTS) {
28
+ const score = scoreRule(ir, corpus);
29
+ const t = { ...PROMOTION_DEFAULTS, ...thresholds };
30
+ const reasons = [];
31
+ if (score.precision === null) reasons.push('precision unmeasured (no positive or negative corpus fired)');
32
+ else if (score.precision < t.minPrecision) reasons.push(`precision ${(score.precision * 100).toFixed(1)}% below ${(t.minPrecision * 100).toFixed(0)}%`);
33
+ if (score.recall === null) reasons.push('recall unmeasured (empty positive corpus)');
34
+ else if (score.recall < t.minRecall) reasons.push(`recall ${(score.recall * 100).toFixed(1)}% below ${(t.minRecall * 100).toFixed(0)}%`);
35
+ if (score.protectedFailures > t.maxProtectedFailures) reasons.push(`${score.protectedFailures} protected-negative failure(s) (max ${t.maxProtectedFailures})`);
36
+ return { pass: reasons.length === 0, reasons, score, thresholds: t };
37
+ }
38
+
39
+ /** The one promotion decision. Never auto-applied: a caller still has to act on `pass:true` by recording a
40
+ * lifecycle transition. Localization ("acceptable") is a human/caller judgment this function cannot make from
41
+ * line/column alone; it is surfaced for review, never silently assumed. */
42
+ export function gatePromotion({ ir, corpus, leakageCheck, thresholds } = {}) {
43
+ const evalResult = evaluateRule(ir, corpus, thresholds);
44
+ const reasons = [...evalResult.reasons];
45
+ if (!leakageCheck || leakageCheck.ok !== true) reasons.push(leakageCheck ? `leakage check failed: ${leakageCheck.offendingTerms.join(', ')}` : 'leakage check was not run');
46
+ return { pass: reasons.length === 0, reasons, evaluation: evalResult };
47
+ }