@clear-capabilities/agentic-security-scanner 0.165.1 → 0.167.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 (36) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/bin/agentic-security.js +39 -4
  3. package/dist/2199.index.js +348 -0
  4. package/dist/3740.index.js +722 -0
  5. package/dist/4113.index.js +1 -1
  6. package/dist/4142.index.js +929 -0
  7. package/dist/5738.index.js +240 -0
  8. package/dist/7838.index.js +1 -1
  9. package/dist/808.index.js +1330 -0
  10. package/dist/818.index.js +93 -0
  11. package/dist/9800.index.js +245 -0
  12. package/dist/9999.index.js +2 -2
  13. package/dist/agentic-security.mjs +2 -2
  14. package/dist/agentic-security.mjs.sha256 +1 -1
  15. package/package.json +4 -4
  16. package/src/capabilities/CLAUDE.md +8 -0
  17. package/src/posture/artifact-registry.js +3 -0
  18. package/src/posture/proof-fabric/cli.js +115 -0
  19. package/src/posture/proof-fabric/fix-proof.js +73 -0
  20. package/src/posture/proof-fabric/oracles.js +101 -0
  21. package/src/posture/proof-fabric/orchestrator.js +129 -0
  22. package/src/posture/proof-fabric/reproducer.js +108 -0
  23. package/src/posture/proof-fabric/runner.js +243 -0
  24. package/src/posture/proof-fabric/states.js +53 -0
  25. package/src/posture/proof-fabric/store.js +83 -0
  26. package/src/posture/rule-compiler/cli.js +141 -0
  27. package/src/posture/rule-compiler/corpus.js +61 -0
  28. package/src/posture/rule-compiler/eligibility.js +25 -0
  29. package/src/posture/rule-compiler/ir.js +87 -0
  30. package/src/posture/rule-compiler/lifecycle.js +57 -0
  31. package/src/posture/rule-compiler/mutation.js +60 -0
  32. package/src/posture/rule-compiler/promotion.js +47 -0
  33. package/src/posture/rule-compiler/store.js +96 -0
  34. package/src/sandbox/backend-userspace.js +7 -1
  35. package/src/sandbox/supervise.js +26 -16
  36. package/src/sandbox/trust-boundary.js +2 -1
@@ -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
+ }
@@ -0,0 +1,96 @@
1
+ // Persistence for the rule compiler: one directory per rule under `.agentic-security/rule-compiler/<id>/`
2
+ // (`ir.json`, `ledger.json`, `evaluation.json`), the promoted custom-rule document at
3
+ // `.agentic-security/rules/<id>.yml` (so the EXISTING `loadCustomRules`/`rule test` machinery picks it up with no
4
+ // change), and a regression fixture pair at `.agentic-security/rule-compiler/fixtures/<id>/{vulnerable,clean}/`
5
+ // using the SAME filename convention `runRuleTests` already reads (`vulnerable`/`clean` in the path).
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import { statePath, ensureStateDir, safeWriteState } from '../state-dir.js';
9
+ import { newLedger, verifyLedger } from './lifecycle.js';
10
+ import { compileToCustomRule } from './ir.js';
11
+
12
+ const DIR = 'rule-compiler';
13
+ const EXT = { javascript: 'js', typescript: 'ts', python: 'py', go: 'go', ruby: 'rb', java: 'java', rust: 'rs', csharp: 'cs', php: 'php', any: 'txt' };
14
+
15
+ function ruleDir(scanRoot, id) { return statePath(scanRoot, DIR, id); }
16
+
17
+ export function loadLedger(scanRoot) {
18
+ const f = statePath(scanRoot, DIR, 'ledger.json');
19
+ if (!fs.existsSync(f)) return newLedger();
20
+ try {
21
+ const ledger = JSON.parse(fs.readFileSync(f, 'utf8'));
22
+ const v = verifyLedger(ledger);
23
+ return v.ok ? ledger : { ...newLedger(), tampered: true, tamperReason: v.reason };
24
+ } catch { return newLedger(); }
25
+ }
26
+
27
+ export function saveLedger(scanRoot, ledger) {
28
+ if (!ensureStateDir(scanRoot)) return { ok: false, reason: 'no safe state directory for this project' };
29
+ const f = statePath(scanRoot, DIR, 'ledger.json');
30
+ return { ok: safeWriteState(f, JSON.stringify(ledger, null, 2)) };
31
+ }
32
+
33
+ export function saveIR(scanRoot, ir) {
34
+ if (!ensureStateDir(scanRoot)) return { ok: false, reason: 'no safe state directory for this project' };
35
+ const f = path.join(ruleDir(scanRoot, ir.id), 'ir.json');
36
+ return { ok: safeWriteState(f, JSON.stringify(ir, null, 2)), file: f };
37
+ }
38
+
39
+ export function loadIR(scanRoot, id) {
40
+ const f = path.join(ruleDir(scanRoot, id), 'ir.json');
41
+ try { return JSON.parse(fs.readFileSync(f, 'utf8')); } catch { return null; }
42
+ }
43
+
44
+ export function saveEvaluation(scanRoot, id, evaluation) {
45
+ if (!ensureStateDir(scanRoot)) return { ok: false, reason: 'no safe state directory for this project' };
46
+ const f = path.join(ruleDir(scanRoot, id), 'evaluation.json');
47
+ return { ok: safeWriteState(f, JSON.stringify(evaluation, null, 2)) };
48
+ }
49
+
50
+ /** Write the regression fixtures a promoted rule needs: the SAME vulnerable/clean convention `runRuleTests`
51
+ * already reads, scored against the exact corpus the rule was promoted on. */
52
+ export function writeFixtures(scanRoot, ir, corpus) {
53
+ const ext = EXT[ir.languages[0]] || 'txt';
54
+ const base = path.join(ruleDir(scanRoot, ir.id), 'fixtures');
55
+ if (!ensureStateDir(scanRoot)) return { ok: false, reason: 'no safe state directory for this project' };
56
+ const written = [];
57
+ for (const p of corpus.positives) {
58
+ const f = path.join(base, 'vulnerable', `${p.label.replace(/[^a-z0-9_-]/gi, '_')}.${ext}`);
59
+ if (safeWriteState(f, p.code)) written.push(f);
60
+ }
61
+ for (const n of corpus.negatives) {
62
+ const f = path.join(base, 'clean', `${n.label.replace(/[^a-z0-9_-]/gi, '_')}.${ext}`);
63
+ if (safeWriteState(f, n.code)) written.push(f);
64
+ }
65
+ return { ok: true, written, glob: path.join(base, '**', '*') };
66
+ }
67
+
68
+ /** Promote: write the compiled custom-rule document to the live rules directory. Does not touch the ledger —
69
+ * the caller records the PROMOTED transition itself, so a failed write here can never produce an inconsistent
70
+ * "promoted but not live" or "live but not recorded" state silently. */
71
+ export function publishCustomRule(scanRoot, ir) {
72
+ const { ok, doc, errs } = compileToCustomRule(ir);
73
+ if (!ok) return { ok: false, reason: `invalid rule IR: ${errs.join('; ')}` };
74
+ if (!ensureStateDir(scanRoot)) return { ok: false, reason: 'no safe state directory for this project' };
75
+ const f = statePath(scanRoot, 'rules', `${ir.id}.yml`);
76
+ const yamlText = [
77
+ `# Compiled by the rule compiler from finding ${ir.provenance.originatingFindingId} (${ir.provenance.evidenceState}).`,
78
+ `- id: ${doc.id}`,
79
+ ` title: ${JSON.stringify(doc.title)}`,
80
+ ` severity: ${doc.severity}`,
81
+ ` cwe: ${doc.cwe}`,
82
+ ` languages: [${doc.languages.join(', ')}]`,
83
+ ` match:`,
84
+ ...(doc.match.pattern ? [` pattern: ${JSON.stringify(doc.match.pattern)}`] : []),
85
+ ...(doc.match.allOf ? [` allOf:`, ...doc.match.allOf.map((p) => ` - ${JSON.stringify(p)}`), ` window: ${doc.match.window}`] : []),
86
+ ...(doc.match.notMatch ? [` notMatch: ${JSON.stringify(doc.match.notMatch)}`] : []),
87
+ ` message: ${JSON.stringify(doc.message)}`,
88
+ '',
89
+ ].join('\n');
90
+ return { ok: safeWriteState(f, yamlText), file: f };
91
+ }
92
+
93
+ export function unpublishCustomRule(scanRoot, id) {
94
+ const f = statePath(scanRoot, 'rules', `${id}.yml`);
95
+ try { if (fs.existsSync(f)) fs.unlinkSync(f); return { ok: true }; } catch (e) { return { ok: false, reason: e.message }; }
96
+ }
@@ -50,11 +50,17 @@ function _profile({ allowNetwork, proxyPort = null, denyRead = [], confineReads
50
50
  readRoots.forEach((_, i) => lines.push(`(allow file-read* (subpath (param "RD${i}")))`));
51
51
  writeRoots.forEach((_, i) => lines.push(`(allow file-read* (subpath (param "WR${i}")))`));
52
52
  metaLiterals.forEach((_, i) => lines.push(`(allow file-read-metadata (literal (param "META${i}")))`));
53
- lines.push('(allow file-write* (literal "/dev/null"))');
54
53
  }
55
54
  // Read denial (CORE-003). SBPL is last-match-wins, so these deny rules MUST
56
55
  // come after every read allow.
57
56
  denyRead.forEach((_, i) => lines.push(`(deny file-read* (subpath (param "DENY${i}")))`));
57
+ // /dev/null is a kernel sink: writing to it (`stdio: 'ignore'`, `> /dev/null`, a piped-to-null redirect — all
58
+ // common and harmless) cannot leak data or escape confinement. Allowed unconditionally, in BOTH the default and
59
+ // capability-mode profiles (it used to be capability-mode only, which left the far more common default profile
60
+ // denying it: `posix_spawn` opens /dev/null to set up an ignored fd BEFORE exec, so the spawn itself failed with
61
+ // EPERM — any target that redirects a child's output to /dev/null never ran at all, silently, under the default
62
+ // profile only).
63
+ lines.push('(allow file-write* (literal "/dev/null"))');
58
64
  lines.push('(allow file-write* (subpath (param "ROOT")))');
59
65
  writeRoots.forEach((_, i) => lines.push(`(allow file-write* (subpath (param "WR${i}")))`));
60
66
  if (allowNetwork) lines.push('(allow network*)');
@@ -22,7 +22,7 @@
22
22
  // sweep interval bounds the window; it does not close it. Only a PID-namespace
23
23
  // or cgroup boundary closes it, and macOS has neither. The namespace backend
24
24
  // has the boundary (see `runConfinedSupervised`).
25
- import { spawn, spawnSync } from 'node:child_process';
25
+ import { spawn, execFile } from 'node:child_process';
26
26
  import { detectBackend } from './capabilities.js';
27
27
  import { runDisabled } from './backend-disabled.js';
28
28
  import { buildUserspaceInvocation } from './backend-userspace.js';
@@ -40,12 +40,21 @@ function groupAlive(pgid) {
40
40
  function signalPid(pid, sig) { try { process.kill(pid, sig); } catch { /* already gone */ } }
41
41
  function signalGroup(pgid, sig) { try { process.kill(-pgid, sig); } catch { /* already gone */ } }
42
42
 
43
- /** Descendants of `rootPid` plus members of its group, from one `ps` snapshot. */
43
+ /**
44
+ * Descendants of `rootPid` plus members of its group, from one `ps` snapshot. Asynchronous on purpose: a synchronous `ps` blocks the
45
+ * controller's event loop for as long as the machine takes to run it (tens of milliseconds idle, far more under load), and this runs on
46
+ * every sweep.
47
+ */
44
48
  function snapshotTree(rootPid) {
45
- const r = spawnSync('ps', ['-A', '-o', 'pid=,ppid=,pgid='], { encoding: 'utf8', timeout: 2000 });
46
- if (r.error || r.status !== 0) return [];
49
+ return new Promise((resolve) => {
50
+ execFile('ps', ['-A', '-o', 'pid=,ppid=,pgid='], { encoding: 'utf8', timeout: 2000, maxBuffer: 16 * 1024 * 1024 }, (err, stdout) => {
51
+ resolve(err ? [] : treeFromPs(stdout, rootPid));
52
+ });
53
+ });
54
+ }
55
+ function treeFromPs(stdout, rootPid) {
47
56
  const rows = [];
48
- for (const line of String(r.stdout).split('\n')) {
57
+ for (const line of String(stdout).split('\n')) {
49
58
  const m = line.trim().split(/\s+/).map(Number);
50
59
  if (m.length === 3 && m.every(Number.isFinite)) rows.push({ pid: m[0], ppid: m[1], pgid: m[2] });
51
60
  }
@@ -86,27 +95,28 @@ export function superviseSpawn(bin, args, {
86
95
  let finishing = null;
87
96
  let timer = null;
88
97
 
89
- const sweep = () => { if (pgid) for (const p of snapshotTree(pgid)) tracked.add(p); };
90
- // sweepMs <= 0 turns the periodic sweep off. Each sweep is a SYNCHRONOUS `ps`, which blocks the controller's event loop for as long as
91
- // it runs; on the namespace backend the kernel ends every member with the supervisor (--kill-child), so the periodic sweep adds nothing
92
- // there and only costs responsiveness. The cleanup below still takes its own snapshots.
93
- const sweeper = sweepMs > 0 ? setInterval(sweep, sweepMs) : null;
98
+ const sweep = async () => { if (pgid) for (const p of await snapshotTree(pgid)) tracked.add(p); };
99
+ // sweepMs <= 0 turns the periodic sweep off: on the namespace backend the kernel ends every member with the supervisor
100
+ // (--kill-child), so the periodic sweep adds nothing there. The cleanup below still takes its own snapshots. A sweep never overlaps the
101
+ // previous one, so a slow `ps` under load cannot pile up work.
102
+ let sweeping = false;
103
+ const sweeper = sweepMs > 0 ? setInterval(() => { if (sweeping) return; sweeping = true; sweep().finally(() => { sweeping = false; }); }, sweepMs) : null;
94
104
 
95
- const survivors = () => {
96
- sweep();
105
+ const survivors = async () => {
106
+ await sweep();
97
107
  return { live: [...tracked].filter(alive), group: groupAlive(pgid) };
98
108
  };
99
109
 
100
110
  async function terminate() {
101
111
  const t = _emptyTermination();
102
- const before = survivors();
112
+ const before = await survivors();
103
113
  if (!before.group && before.live.length === 0) return t;
104
114
  t.signalled = 'SIGTERM';
105
115
  signalGroup(pgid, 'SIGTERM');
106
116
  for (const p of tracked) signalPid(p, 'SIGTERM');
107
117
  const deadline = Date.now() + graceMs;
108
118
  while (Date.now() < deadline) {
109
- const s = survivors();
119
+ const s = await survivors();
110
120
  if (!s.group && s.live.length === 0) return t;
111
121
  await sleep(25);
112
122
  }
@@ -115,13 +125,13 @@ export function superviseSpawn(bin, args, {
115
125
  t.signalled = 'SIGKILL';
116
126
  const hard = Date.now() + 3000;
117
127
  while (Date.now() < hard) {
118
- const s = survivors();
128
+ const s = await survivors();
119
129
  if (!s.group && s.live.length === 0) break;
120
130
  signalGroup(pgid, 'SIGKILL');
121
131
  for (const p of s.live) { signalPid(p, 'SIGKILL'); if (!t.killedPids.includes(p)) t.killedPids.push(p); }
122
132
  await sleep(25);
123
133
  }
124
- const s = survivors();
134
+ const s = await survivors();
125
135
  t.survivors = s.live.length ? s.live : (s.group ? [-pgid] : []);
126
136
  return t;
127
137
  }
@@ -58,7 +58,7 @@ export async function runInBoundary(argv, o = {}) {
58
58
  const {
59
59
  root, labelDirs = [], evidenceDirs = [], denyReadPaths = [], env = {},
60
60
  allowNetwork = false, timeoutMs = 10000, graceMs = 1000, signal, force, limits,
61
- maxOutputBytes, controlProbes,
61
+ maxOutputBytes, controlProbes, networkProxyPort = null,
62
62
  } = o;
63
63
  const required = [...(o.require || DEFAULT_REQUIRED_CONTROLS)];
64
64
  if (!allowNetwork && !required.includes('network')) required.push('network');
@@ -94,6 +94,7 @@ export async function runInBoundary(argv, o = {}) {
94
94
  const r = await runConfinedSupervised(argv, {
95
95
  root, timeoutMs, graceMs, signal, force, allowNetwork, limits, maxOutputBytes,
96
96
  env, denyReadPaths: protectedPaths,
97
+ ...(networkProxyPort != null ? { networkProxyPort } : {}),
97
98
  });
98
99
  const executed = r.status !== 'disabled' && !(r.status === 'error' && !r.supervised);
99
100
  const run = { ...r, executed };