@clear-capabilities/agentic-security-scanner 0.184.0 → 0.186.1

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 (228) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/bin/agentic-security.js +111 -1
  3. package/dist/1128.index.js +912 -0
  4. package/dist/1665.index.js +828 -0
  5. package/dist/2584.index.js +1 -1
  6. package/dist/4171.index.js +2 -2
  7. package/dist/5752.index.js +466 -0
  8. package/dist/7405.index.js +1 -1
  9. package/dist/8158.index.js +1713 -0
  10. package/dist/8798.index.js +1 -1
  11. package/dist/9382.index.js +2 -2
  12. package/dist/9579.index.js +1 -1
  13. package/dist/agentic-security.mjs +29 -29
  14. package/dist/agentic-security.mjs.sha256 +1 -1
  15. package/dist/{dad58d1113a12c23a142.json → ca62ee93079be4c24eee.json} +29 -12
  16. package/dist/frontend/src/shell.js +12 -2
  17. package/package.json +29 -12
  18. package/src/dataflow/CLAUDE.md +46 -1
  19. package/src/dataflow/access-paths.js +12 -2
  20. package/src/dataflow/backward.js +42 -7
  21. package/src/dataflow/cross-service-taint.js +83 -1
  22. package/src/dataflow/demand-query.js +1168 -0
  23. package/src/dataflow/dep-fingerprint.js +718 -0
  24. package/src/dataflow/engine.js +48 -6
  25. package/src/dataflow/exploit-prover.js +6 -2
  26. package/src/dataflow/ifds-precise.js +22 -0
  27. package/src/dataflow/incremental-replay.js +118 -0
  28. package/src/dataflow/incremental-session.js +257 -0
  29. package/src/dataflow/incremental.js +237 -35
  30. package/src/dataflow/index.js +25 -94
  31. package/src/dataflow/ir-cache.js +268 -0
  32. package/src/dataflow/privacy-taint.js +7 -0
  33. package/src/dataflow/summaries.js +72 -6
  34. package/src/dataflow/tabulation.js +80 -24
  35. package/src/engine.js +113 -8
  36. package/src/graph/CLAUDE.md +31 -0
  37. package/src/graph/adapters/artifact.js +81 -0
  38. package/src/graph/adapters/crossrepo.js +75 -0
  39. package/src/graph/adapters/dataflow.js +76 -0
  40. package/src/graph/adapters/provenance.js +92 -0
  41. package/src/graph/adapters/semantic.js +89 -0
  42. package/src/graph/crosswalk-builders.js +152 -0
  43. package/src/graph/crosswalk.js +146 -0
  44. package/src/graph/index.js +15 -0
  45. package/src/graph/observation.js +57 -0
  46. package/src/graph/refs.js +105 -0
  47. package/src/graph/registry.js +129 -0
  48. package/src/graph/relations.js +81 -0
  49. package/src/graph/scc.js +241 -0
  50. package/src/graph/view.js +254 -0
  51. package/src/ir/CLAUDE.md +4 -0
  52. package/src/ir/callgraph.js +36 -8
  53. package/src/ir/index.js +18 -4
  54. package/src/ir/parser-js.js +10 -0
  55. package/src/language/CLAUDE.md +13 -0
  56. package/src/language/nix-option-types.js +170 -0
  57. package/src/language/nix-script-taint.js +46 -3
  58. package/src/language/nix-secrets.js +20 -2
  59. package/src/lineage/CLAUDE.md +72 -8
  60. package/src/lineage/DESIGN_GRAPH_BUILDER.md +1 -1
  61. package/src/lineage/DESIGN_PATH_PROVENANCE.md +6 -0
  62. package/src/lineage/boundary-contract.js +273 -0
  63. package/src/lineage/completion.js +205 -0
  64. package/src/lineage/continuation.js +140 -0
  65. package/src/lineage/control-credit.js +313 -0
  66. package/src/lineage/correlation.js +199 -0
  67. package/src/lineage/coverage.js +8 -1
  68. package/src/lineage/cross-repo-link.js +22 -0
  69. package/src/lineage/cut-planner.js +548 -0
  70. package/src/lineage/driver.js +219 -66
  71. package/src/lineage/engine.js +166 -21
  72. package/src/lineage/export-json.js +5 -0
  73. package/src/lineage/graph-builder.js +84 -7
  74. package/src/lineage/haskell-view.js +59 -0
  75. package/src/lineage/index.js +3 -0
  76. package/src/lineage/nix-view.js +35 -0
  77. package/src/lineage/owned-build.js +77 -0
  78. package/src/lineage/owned-worker-entry.js +22 -0
  79. package/src/lineage/owned-worker.js +95 -0
  80. package/src/lineage/path-evidence.js +123 -0
  81. package/src/lineage/path-predicates.js +529 -0
  82. package/src/lineage/path-query.js +189 -20
  83. package/src/lineage/path-store.js +191 -17
  84. package/src/lineage/scc-solver.js +186 -0
  85. package/src/lineage/smt-orchestrator.js +174 -0
  86. package/src/lineage/smt-z3-backend.js +294 -0
  87. package/src/lineage/source-seeding.js +36 -2
  88. package/src/lineage/summaries.js +66 -4
  89. package/src/lineage/value-versions.js +130 -0
  90. package/src/lineage/witness-selection.js +95 -0
  91. package/src/pipeline/scan-health.js +18 -0
  92. package/src/posture/CLAUDE.md +17 -0
  93. package/src/posture/business-logic.js +16 -7
  94. package/src/posture/dag-commands.js +442 -0
  95. package/src/posture/evidence-bundle.js +1 -1
  96. package/src/posture/finding-defaults.js +19 -0
  97. package/src/posture/git-history.js +91 -1
  98. package/src/posture/witness-bundle.js +573 -0
  99. package/src/posture/witness-runtime.js +276 -0
  100. package/src/report/index.js +2 -0
  101. package/src/sast/CLAUDE.md +2 -0
  102. package/src/sast/_call-args.js +70 -0
  103. package/src/sast/init-scope.js +87 -0
  104. package/src/sast/listen-bind-address.js +49 -0
  105. package/src/sast/redirect-target.js +106 -0
  106. package/src/sast/reflected-input.js +76 -0
  107. package/src/sast/send-file-root.js +27 -0
  108. package/src/sast/sensitive-data-logging.js +38 -1
  109. package/dist/119.index.js +0 -197
  110. package/dist/128.index.js +0 -129
  111. package/dist/1293.index.js +0 -5689
  112. package/dist/1336.index.js +0 -16182
  113. package/dist/1351.index.js +0 -133
  114. package/dist/1387.index.js +0 -271
  115. package/dist/1437.index.js +0 -23
  116. package/dist/1445.index.js +0 -110
  117. package/dist/1505.index.js +0 -151
  118. package/dist/1557.index.js +0 -293
  119. package/dist/1621.index.js +0 -311
  120. package/dist/1751.index.js +0 -160
  121. package/dist/1765.index.js +0 -727
  122. package/dist/1861.index.js +0 -690
  123. package/dist/1906.index.js +0 -433
  124. package/dist/208.index.js +0 -1631
  125. package/dist/2223.index.js +0 -159
  126. package/dist/2238.index.js +0 -295
  127. package/dist/2530.index.js +0 -152
  128. package/dist/2565.index.js +0 -116
  129. package/dist/2574.index.js +0 -127
  130. package/dist/2620.index.js +0 -345
  131. package/dist/2773.index.js +0 -98
  132. package/dist/2775.index.js +0 -32
  133. package/dist/2966.index.js +0 -171
  134. package/dist/2972.index.js +0 -87
  135. package/dist/2991.index.js +0 -131
  136. package/dist/3063.index.js +0 -833
  137. package/dist/3075.index.js +0 -227
  138. package/dist/3121.index.js +0 -297
  139. package/dist/3314.index.js +0 -263
  140. package/dist/3480.index.js +0 -1822
  141. package/dist/3481.index.js +0 -24
  142. package/dist/3506.index.js +0 -268
  143. package/dist/3533.index.js +0 -591
  144. package/dist/3653.index.js +0 -450
  145. package/dist/3740.index.js +0 -722
  146. package/dist/3822.index.js +0 -131
  147. package/dist/3868.index.js +0 -292
  148. package/dist/3918.index.js +0 -192
  149. package/dist/4090.index.js +0 -288
  150. package/dist/4113.index.js +0 -1286
  151. package/dist/4121.index.js +0 -1190
  152. package/dist/4142.index.js +0 -690
  153. package/dist/4168.index.js +0 -509
  154. package/dist/4281.index.js +0 -993
  155. package/dist/4336.index.js +0 -436
  156. package/dist/4387.index.js +0 -532
  157. package/dist/4413.index.js +0 -302
  158. package/dist/4476.index.js +0 -4510
  159. package/dist/4491.index.js +0 -133
  160. package/dist/4538.index.js +0 -185
  161. package/dist/4684.index.js +0 -319
  162. package/dist/4740.index.js +0 -165
  163. package/dist/4780.index.js +0 -209
  164. package/dist/4802.index.js +0 -225
  165. package/dist/4812.index.js +0 -379
  166. package/dist/4835.index.js +0 -126
  167. package/dist/494.index.js +0 -226
  168. package/dist/4991.index.js +0 -549
  169. package/dist/5199.index.js +0 -305
  170. package/dist/5216.index.js +0 -109
  171. package/dist/5439.index.js +0 -301
  172. package/dist/5443.index.js +0 -97
  173. package/dist/5496.index.js +0 -377
  174. package/dist/5738.index.js +0 -240
  175. package/dist/5801.index.js +0 -588
  176. package/dist/5817.index.js +0 -714
  177. package/dist/5858.index.js +0 -223
  178. package/dist/5933.index.js +0 -143
  179. package/dist/601.index.js +0 -23
  180. package/dist/6173.index.js +0 -273
  181. package/dist/6291.index.js +0 -1871
  182. package/dist/6312.index.js +0 -666
  183. package/dist/634.index.js +0 -16182
  184. package/dist/644.index.js +0 -87
  185. package/dist/6508.index.js +0 -283
  186. package/dist/6560.index.js +0 -645
  187. package/dist/6576.index.js +0 -422
  188. package/dist/6579.index.js +0 -271
  189. package/dist/6655.index.js +0 -3221
  190. package/dist/6745.index.js +0 -305
  191. package/dist/6818.index.js +0 -21642
  192. package/dist/6932.index.js +0 -1190
  193. package/dist/6972.index.js +0 -167
  194. package/dist/6995.index.js +0 -163
  195. package/dist/7249.index.js +0 -191
  196. package/dist/7372.index.js +0 -1197
  197. package/dist/7546.index.js +0 -541
  198. package/dist/76.index.js +0 -448
  199. package/dist/7879.index.js +0 -163
  200. package/dist/8025.index.js +0 -130
  201. package/dist/8054.index.js +0 -248
  202. package/dist/808.index.js +0 -1330
  203. package/dist/8097.index.js +0 -201
  204. package/dist/8126.index.js +0 -191
  205. package/dist/8178.index.js +0 -78
  206. package/dist/8382.index.js +0 -93
  207. package/dist/8408.index.js +0 -261
  208. package/dist/8540.index.js +0 -174
  209. package/dist/8635.index.js +0 -802
  210. package/dist/8688.index.js +0 -410
  211. package/dist/8699.index.js +0 -779
  212. package/dist/8891.index.js +0 -457
  213. package/dist/8916.index.js +0 -885
  214. package/dist/9146.index.js +0 -170
  215. package/dist/9168.index.js +0 -841
  216. package/dist/9292.index.js +0 -369
  217. package/dist/9422.index.js +0 -866
  218. package/dist/9440.index.js +0 -453
  219. package/dist/9479.index.js +0 -152
  220. package/dist/9481.index.js +0 -220
  221. package/dist/9539.index.js +0 -100
  222. package/dist/9547.index.js +0 -103
  223. package/dist/9692.index.js +0 -90
  224. package/dist/9800.index.js +0 -462
  225. package/dist/9899.index.js +0 -138
  226. package/dist/9981.index.js +0 -206
  227. package/dist/999.index.js +0 -117
  228. package/dist/9999.index.js +0 -1698
@@ -0,0 +1,268 @@
1
+ // Cross-scan cache of parsed JavaScript / TypeScript IR (DAG-05, the stage the taint cache does not cover).
2
+ //
3
+ // The dependency-complete taint cache (incremental-session.js) removes most of the cost of the interprocedural
4
+ // engine on a re-scan, and what is left is dominated by parsing every file to IR again. This module keeps one
5
+ // record per source file so an unchanged file is read back instead of parsed. It changes the cost of building
6
+ // the IR and never its content:
7
+ //
8
+ // * A record is keyed by the SHA-256 of the file's path and text, the analyzer/parser digest (a change to any
9
+ // source under ir/, dataflow/, graph/ or language/, or to the bundle, invalidates every record) and the
10
+ // Babel versions the parse ran under. A different file text, parser or toolchain is a miss.
11
+ // * The IR numbers its CFG nodes from a process-global counter (`n123`). A restored file is given fresh ids
12
+ // from the same counter, in the same relative order, so an id is as unique across the process as one the
13
+ // parser made. Ids appear only in the fields remapped below; a file that holds an id anywhere else is
14
+ // never written.
15
+ // * A record is written only after the IR has been checked to survive JSON exactly: anything that would not
16
+ // (an undefined member, a non-finite number, a BigInt, a Map, an object with a foreign prototype, an id
17
+ // held in a field the remap does not know) is not cached, and that file is parsed every scan. Regular
18
+ // expressions, which JSON would turn into `{}`, are recorded by path and rebuilt on load.
19
+ // * Each record carries the SHA-256 of its payload; a truncated, edited or foreign file is a miss, never an
20
+ // error. Writes go to a temp file and are renamed into place.
21
+ //
22
+ // Scope: JavaScript and TypeScript only (the parser that owns the id counter). Every other language is parsed
23
+ // as before. Trust boundary: the same as the rest of the incremental state under .agentic-security/incremental
24
+ // (digests detect corruption; they are not a signature, so a hostile working tree that plants a consistent
25
+ // record could substitute an IR; a planted record must also match the file's exact text and the analyzer digest).
26
+
27
+ import * as crypto from 'node:crypto';
28
+ import * as fs from 'node:fs';
29
+ import * as path from 'node:path';
30
+ import { createRequire } from 'node:module';
31
+
32
+ import { reserveNodeIds } from '../ir/parser-js.js';
33
+ import { analyzerDigest } from './dep-fingerprint.js';
34
+ import { statePath, stateWritesEnabled } from '../posture/state-dir.js';
35
+
36
+ const IR_CACHE_FORMAT = 'ir-cache/1';
37
+ const ID_RE = /^n\d+$/;
38
+ const ID_NUM = (s) => Number(s.slice(1));
39
+
40
+ let _toolchain = null;
41
+ function toolchainDigest() {
42
+ if (_toolchain) return _toolchain;
43
+ const parts = [IR_CACHE_FORMAT, analyzerDigest()];
44
+ try {
45
+ const req = createRequire(import.meta.url);
46
+ for (const pkg of ['@babel/core', '@babel/preset-react', '@babel/preset-typescript', '@babel/parser']) {
47
+ try { parts.push(`${pkg}@${req(`${pkg}/package.json`).version}`); } catch { parts.push(`${pkg}@unresolved`); }
48
+ }
49
+ } catch { parts.push('toolchain@unresolved'); }
50
+ _toolchain = crypto.createHash('sha256').update(parts.join('\n')).digest('hex');
51
+ return _toolchain;
52
+ }
53
+
54
+ const sha = (s) => crypto.createHash('sha256').update(s).digest('hex');
55
+
56
+ // ── id handling ─────────────────────────────────────────────────────────────
57
+
58
+ /** Every node id of a file's IR, or null when the IR holds something this cache cannot place. */
59
+ function fileIds(ir) {
60
+ if (!ir || !Array.isArray(ir.functions)) return null;
61
+ const ids = [];
62
+ for (const fn of ir.functions) {
63
+ if (!fn || !fn.cfg || !fn.cfg.nodes || typeof fn.cfg.nodes !== 'object') return null;
64
+ for (const k of Object.keys(fn.cfg.nodes)) { if (!ID_RE.test(k)) return null; ids.push(k); }
65
+ }
66
+ return ids;
67
+ }
68
+
69
+ /**
70
+ * Give an IR read back from a record new ids. Throws when a reference names an id the file does not define:
71
+ * the caller treats that as a miss.
72
+ */
73
+ function remapIds(ir) {
74
+ const ids = fileIds(ir);
75
+ if (!ids) throw new Error('unplaceable ids');
76
+ ids.sort((a, b) => ID_NUM(a) - ID_NUM(b));
77
+ const base = reserveNodeIds(ids.length);
78
+ const map = new Map();
79
+ ids.forEach((id, i) => map.set(id, `n${base + i + 1}`));
80
+ const m = (id) => { const v = map.get(id); if (v === undefined) throw new Error(`dangling id ${id}`); return v; };
81
+ for (const fn of ir.functions) {
82
+ const cfg = fn.cfg;
83
+ cfg.entry = m(cfg.entry);
84
+ cfg.exit = m(cfg.exit);
85
+ const entries = Object.entries(cfg.nodes);
86
+ for (const [k, node] of entries) {
87
+ if (node.id !== k) throw new Error('node id differs from its key');
88
+ node.id = m(node.id);
89
+ if (!Array.isArray(node.succ) || !Array.isArray(node.pred)) throw new Error('node without edges');
90
+ node.succ = node.succ.map(m);
91
+ node.pred = node.pred.map(m);
92
+ }
93
+ cfg.nodes = Object.fromEntries(entries.map(([k, node]) => [m(k), node]));
94
+ fn.returns = (fn.returns || []).map(m);
95
+ for (const c of (fn.calls || [])) c.site = m(c.site);
96
+ for (const list of Object.values(fn.writes || {})) for (const w of list) w.node = m(w.node);
97
+ for (const list of Object.values(fn.reads || {})) { for (let i = 0; i < list.length; i++) list[i] = m(list[i]); }
98
+ }
99
+ return ir;
100
+ }
101
+
102
+ /**
103
+ * The IR as text with every node id replaced by its rank among the file's ids, and regular expressions kept
104
+ * distinct from `{}`. Two IRs that differ only in the ids they were numbered with give the same text. Used by
105
+ * the tests that prove a restored IR equals a fresh parse.
106
+ */
107
+ function canonical(ir) {
108
+ const ids = fileIds(ir);
109
+ if (!ids) return null;
110
+ ids.sort((a, b) => ID_NUM(a) - ID_NUM(b));
111
+ const rank = new Map(ids.map((id, i) => [id, `#${i}`]));
112
+ const walk = (v) => {
113
+ if (typeof v === 'string') return rank.has(v) ? rank.get(v) : v;
114
+ if (v instanceof RegExp) return { $re: [v.source, v.flags] };
115
+ if (Array.isArray(v)) return v.map(walk);
116
+ if (v && typeof v === 'object') {
117
+ const o = {};
118
+ for (const [k, x] of Object.entries(v)) o[rank.has(k) ? rank.get(k) : k] = walk(x);
119
+ return o;
120
+ }
121
+ return v;
122
+ };
123
+ return JSON.stringify(walk(ir));
124
+ }
125
+
126
+ /**
127
+ * One walk over a freshly parsed IR: is it exactly what JSON gives back (plus the regular expressions that are
128
+ * recorded by path), and does every node id sit in a field the remap knows? Returns { regexes } or null.
129
+ * `ids` is the set of the file's node ids.
130
+ */
131
+ function analyze(ir, ids) {
132
+ const regexes = [];
133
+ let idLike = 0;
134
+ const trail = [];
135
+ const walk = (v) => {
136
+ switch (typeof v) {
137
+ case 'string': if (ids.has(v)) idLike++; return true;
138
+ case 'boolean': return true;
139
+ case 'number': return Number.isFinite(v);
140
+ case 'object': break;
141
+ default: return false; // undefined, function, symbol, bigint
142
+ }
143
+ if (v === null) return true;
144
+ if (v instanceof RegExp) {
145
+ if (v.lastIndex !== 0) return false;
146
+ regexes.push([trail.slice(), v.source, v.flags]);
147
+ return true;
148
+ }
149
+ if (Array.isArray(v)) {
150
+ if (Object.getPrototypeOf(v) !== Array.prototype) return false;
151
+ for (let i = 0; i < v.length; i++) {
152
+ if (!(i in v)) return false; // a hole
153
+ trail.push(i);
154
+ const ok = walk(v[i]);
155
+ trail.pop();
156
+ if (!ok) return false;
157
+ }
158
+ return true;
159
+ }
160
+ if (Object.getPrototypeOf(v) !== Object.prototype) return false;
161
+ for (const k of Object.keys(v)) {
162
+ if (ids.has(k)) idLike++;
163
+ trail.push(k);
164
+ const ok = walk(v[k]);
165
+ trail.pop();
166
+ if (!ok) return false;
167
+ }
168
+ return Object.getOwnPropertySymbols(v).length === 0;
169
+ };
170
+ if (!walk(ir)) return null;
171
+ // The ids the remap handles: entry, exit, each node's key and id and edges, returns, call sites, writes, reads.
172
+ let known = 0;
173
+ for (const fn of ir.functions) {
174
+ known += 2 + (fn.returns || []).length + (fn.calls || []).length;
175
+ for (const node of Object.values(fn.cfg.nodes)) {
176
+ if (!Array.isArray(node.succ) || !Array.isArray(node.pred)) return null;
177
+ known += 2 + node.succ.length + node.pred.length;
178
+ }
179
+ for (const list of Object.values(fn.writes || {})) known += list.length;
180
+ for (const list of Object.values(fn.reads || {})) known += list.length;
181
+ }
182
+ return idLike === known ? { regexes } : null;
183
+ }
184
+
185
+ /** Put the recorded regular expressions back where JSON left `{}` (before ids are remapped: the paths use the old ids). */
186
+ function restoreRegexes(ir, regexes) {
187
+ for (const [trail, source, flags] of regexes) {
188
+ let holder = ir;
189
+ for (let i = 0; i < trail.length - 1; i++) holder = holder[trail[i]];
190
+ holder[trail[trail.length - 1]] = new RegExp(source, flags);
191
+ }
192
+ }
193
+
194
+ // ── the cache ───────────────────────────────────────────────────────────────
195
+
196
+ /**
197
+ * Open the cache under `<state>/incremental/ir`. `read` / `write` select what it may do (a scan with state
198
+ * writes off still reads). Returns an object with `load`, `store` and `stats`.
199
+ */
200
+ export function openIrCache(scanRoot, { read = true, write = stateWritesEnabled() } = {}) {
201
+ const dir = statePath(scanRoot, 'incremental', 'ir');
202
+ const stats = { hits: 0, misses: 0, stored: 0, skipped: 0, rejected: 0, loadMs: 0, storeMs: 0, bytesRead: 0, bytesWritten: 0 };
203
+ let tool = null;
204
+ const keyOf = (file, code) => sha(`${tool || (tool = toolchainDigest())}\0${file}\0${code}`);
205
+ const slotOf = (file) => path.join(dir, `${sha(file).slice(0, 40)}.ir.json`);
206
+
207
+ function readRecord(file, key) {
208
+ let text;
209
+ try { text = fs.readFileSync(slotOf(file), 'utf8'); } catch { return null; }
210
+ const nl = text.indexOf('\n');
211
+ if (nl < 0) return { bad: true };
212
+ let head;
213
+ try { head = JSON.parse(text.slice(0, nl)); } catch { return { bad: true }; }
214
+ if (!head || head.format !== IR_CACHE_FORMAT || head.key !== key) return null; // another file text or toolchain: a plain miss
215
+ const payload = text.slice(nl + 1);
216
+ stats.bytesRead += payload.length;
217
+ if (payload.length !== head.bytes || sha(payload) !== head.sha256 || !Array.isArray(head.regexes)) return { bad: true };
218
+ return { payload, regexes: head.regexes };
219
+ }
220
+
221
+ function load(file, code) {
222
+ if (!read) return undefined;
223
+ const t0 = performance.now();
224
+ try {
225
+ const key = keyOf(file, code);
226
+ const rec = readRecord(file, key);
227
+ if (!rec) { stats.misses++; return undefined; }
228
+ if (rec.bad) { stats.rejected++; return undefined; }
229
+ const parsed = JSON.parse(rec.payload);
230
+ restoreRegexes(parsed, rec.regexes);
231
+ const ir = remapIds(parsed);
232
+ if (!ir || ir.file !== file || !Array.isArray(ir.functions)) { stats.rejected++; return undefined; }
233
+ stats.hits++;
234
+ return ir;
235
+ } catch {
236
+ stats.rejected++;
237
+ return undefined;
238
+ } finally { stats.loadMs += performance.now() - t0; }
239
+ }
240
+
241
+ function store(file, code, ir) {
242
+ if (!write) return false;
243
+ const t0 = performance.now();
244
+ try {
245
+ const ids = fileIds(ir);
246
+ const found = ids ? analyze(ir, new Set(ids)) : null;
247
+ if (!found) { stats.skipped++; return false; }
248
+ const key = keyOf(file, code);
249
+ const payload = JSON.stringify(ir);
250
+ const head = JSON.stringify({ format: IR_CACHE_FORMAT, key, sha256: sha(payload), bytes: payload.length, file, regexes: found.regexes });
251
+ fs.mkdirSync(dir, { recursive: true });
252
+ const slot = slotOf(file);
253
+ const tmp = `${slot}.tmp-${process.pid}-${crypto.randomBytes(4).toString('hex')}`;
254
+ fs.writeFileSync(tmp, `${head}\n${payload}`, { mode: 0o600 });
255
+ fs.renameSync(tmp, slot);
256
+ stats.stored++;
257
+ stats.bytesWritten += payload.length;
258
+ return true;
259
+ } catch {
260
+ stats.skipped++;
261
+ return false;
262
+ } finally { stats.storeMs += performance.now() - t0; }
263
+ }
264
+
265
+ return { load, store, stats: () => ({ ...stats }), dir };
266
+ }
267
+
268
+ export const _internals = { remapIds, canonical, fileIds, analyze, restoreRegexes, toolchainDigest };
@@ -37,6 +37,7 @@ import {
37
37
  loadPrivacyTaxonomy, classifyFieldAgainst, severityForClasses,
38
38
  } from './privacy-taxonomy.js';
39
39
  import { loadPrivacySinkPolicy, isSinkPermitted, permittingRules } from './privacy-sink-policy.js';
40
+ import { isListenCallbackParam } from '../sast/listen-bind-address.js';
40
41
  import { GOVERNANCE_FIELDS, governanceRecordFor } from './privacy-governance.js';
41
42
 
42
43
  const _BUILTIN_COMPILED = compileTaxonomy(DEFAULT_TAXONOMY);
@@ -119,8 +120,14 @@ export function annotatePrivacyTaint(perFileIR, opts = {}) {
119
120
  // (evidence locations must cover both ends of a flow, not just the
120
121
  // sink) without re-deriving it from piiFields by proximity guessing.
121
122
  const declLineByName = new Map();
123
+ const lineStart = [];
124
+ { let off = 0; for (const l of lines) { lineStart.push(off); off += l.length + 1; } }
122
125
  for (const d of ir.decls || []) {
123
126
  const classes = classifyFieldAgainst(d.name, compiled);
127
+ // The name alone does not make a binding personal data: the `address` a `listen` callback
128
+ // receives is the bound socket address (see sast/listen-bind-address.js).
129
+ if (classes.length && Number.isInteger(d.line) && lineStart[d.line - 1] !== undefined
130
+ && isListenCallbackParam(ir._content, lineStart[d.line - 1] + Math.max(0, (lines[d.line - 1] || '').indexOf(d.name)), d.name)) continue;
124
131
  if (classes.length) {
125
132
  taintedVars.set(d.name, classes);
126
133
  declLineByName.set(d.name, d.line);
@@ -52,12 +52,22 @@ function _hashState(taintedParams) {
52
52
  return crypto.createHash('sha256').update(sorted).digest('hex').slice(0, 12);
53
53
  }
54
54
 
55
+ // DAG-05 R05.3: the canonical entry list a key was derived from, so a persisted
56
+ // context can carry its COMPLETE key (qid, entry state, receiver) and be checked
57
+ // on reload rather than trusted by hash alone.
58
+ function entryListOf(taintedParams) {
59
+ if (!taintedParams || taintedParams.size === 0) return [];
60
+ return [...canonicalizeAccessSet(taintedParams)].sort();
61
+ }
62
+
55
63
  export class SummaryCache {
56
64
  constructor() {
57
65
  this._cache = new Map(); // qid::hash → summary
58
66
  this._stack = new Set(); // qids currently being analyzed (recursion guard)
59
67
  this._iter = 0;
60
- this._maxIter = 5000;
68
+ // Computation budget for one scan. AGENTIC_SECURITY_SUMMARY_BUDGET raises (or lowers) it; the default is unchanged.
69
+ const envBudget = Number(process.env.AGENTIC_SECURITY_SUMMARY_BUDGET);
70
+ this._maxIter = Number.isFinite(envBudget) && envBudget > 0 ? Math.floor(envBudget) : 5000;
61
71
  // FR-SEM-2 (roadmap #2): the engine computes a distinct summary per
62
72
  // entry-taint-state (value-context sensitivity), computed lazily at call
63
73
  // sites. To stop that from blowing up when a hot helper is reached under
@@ -70,6 +80,13 @@ export class SummaryCache {
70
80
  const envCap = Number(process.env.AGENTIC_SECURITY_KCFA_MAX_CONTEXTS);
71
81
  this._maxContextsPerFn = Number.isFinite(envCap) && envCap >= 0 ? envCap : 16;
72
82
  this._contextCapHits = 0;
83
+ // DAG-07 R07.3: append-only record of every place a result is coarser
84
+ // than a fresh analysis would give: a tainted entry answered with the
85
+ // empty-entry summary (context cap), a recursion refinement that was still
86
+ // changing when its rounds ran out, and the subset of cap fallbacks whose
87
+ // empty-entry summary was restored from a previous scan rather than
88
+ // computed in this one.
89
+ this._notes = [];
73
90
  // R2 (PRD §5): bounded k=1 call-string sensitivity. OPT-IN (default OFF).
74
91
  // When on, the cache key is extended with the immediate caller's qid, so two
75
92
  // call paths reaching a helper with the SAME tainted-arg shape get DISTINCT
@@ -78,6 +95,9 @@ export class SummaryCache {
78
95
  // before, so no behavior change for the shipped engine.
79
96
  this._callString = process.env.AGENTIC_SECURITY_KCFA_CALLSTRING === '1';
80
97
  this._callerCtx = null;
98
+ // DAG-05 R05.3: key -> { qid, entry:[...], stateHash, recv } for every key this
99
+ // cache has produced, so cross-scan persistence can store typed contexts.
100
+ this._meta = new Map();
81
101
  }
82
102
 
83
103
  // Set the immediate-caller qid for call-string keying; returns the previous
@@ -111,8 +131,38 @@ export class SummaryCache {
111
131
  // caller dimension there only ever narrows an already-isolated lookup,
112
132
  // never disconnects a shared one.
113
133
  if (this._callString && this._callerCtx && stateHash !== 'empty') base = `${base}@${this._callerCtx}`;
114
- if (!receiverType) return base;
115
- return `${base}::${hashReceiverType(receiverType)}`;
134
+ const finalKey = receiverType ? `${base}::${hashReceiverType(receiverType)}` : base;
135
+ if (!this._meta.has(finalKey)) {
136
+ this._meta.set(finalKey, {
137
+ qid, entry: entryListOf(taintedParams), stateHash,
138
+ recv: receiverType ? hashReceiverType(receiverType) : null,
139
+ });
140
+ }
141
+ return finalKey;
142
+ }
143
+
144
+ /**
145
+ * DAG-05 R05.3: restore a persisted context under its REAL key. Never writes
146
+ * the empty-entry slot on behalf of a tainted context: the key is rebuilt from
147
+ * the persisted entry list and refused unless it reproduces the persisted hash.
148
+ * Returns false (nothing stored) on any mismatch.
149
+ */
150
+ restoreContext(qid, entryList, recv, stateHash, summary) {
151
+ if (!qid || !Array.isArray(entryList) || !summary) return false;
152
+ const entry = new Set(entryList);
153
+ const h = _hashState(entry);
154
+ if (h !== stateHash) return false;
155
+ if (this._callString && h !== 'empty') return false;
156
+ const base = `${qid}::${h}`;
157
+ const key = recv ? `${base}::${recv}` : base;
158
+ this._cache.set(key, summary);
159
+ this._meta.set(key, { qid, entry: entryListOf(entry), stateHash: h, recv: recv || null });
160
+ if (h !== 'empty') {
161
+ let ctxs = this._contextsByQid.get(qid);
162
+ if (!ctxs) { ctxs = new Set(); this._contextsByQid.set(qid, ctxs); }
163
+ ctxs.add(h);
164
+ }
165
+ return true;
116
166
  }
117
167
 
118
168
  get(qid, taintedParams, receiverType) {
@@ -156,7 +206,11 @@ export class SummaryCache {
156
206
  if (!ctxs.has(stateHash) && ctxs.size >= this._maxContextsPerFn) {
157
207
  this._contextCapHits++;
158
208
  const base = this._cache.get(this._key(qid, new Set()));
159
- if (base && !base._recursive) return base;
209
+ if (base && !base._recursive) {
210
+ this._notes.push({ reason: 'context-cap-fallback', qid });
211
+ if (base._incrementalSeed) this._notes.push({ reason: 'incremental-empty-entry-reuse', qid });
212
+ return base;
213
+ }
160
214
  // No base summary yet → fall through and compute this one (we need a
161
215
  // result), but don't grow the context set.
162
216
  } else {
@@ -178,13 +232,15 @@ export class SummaryCache {
178
232
  this._cache.set(k, summary);
179
233
  if (this._hitRecursion) {
180
234
  const FP_MAX = 3;
235
+ let converged = false;
181
236
  for (let fp = 0; fp < FP_MAX; fp++) {
182
237
  if (++this._iter > this._maxIter) break;
183
238
  const prev = summary;
184
239
  summary = analyze(qid, taintedParams);
185
- if (_summaryEq(prev, summary)) break;
240
+ if (_summaryEq(prev, summary)) { converged = true; break; }
186
241
  this._cache.set(k, summary);
187
242
  }
243
+ if (!converged) this._notes.push({ reason: 'recursion-nonconverged', qid });
188
244
  }
189
245
  if (summary._recursive) delete summary._recursive;
190
246
  return summary;
@@ -224,7 +280,17 @@ export class SummaryCache {
224
280
  }
225
281
 
226
282
  size() { return this._cache.size; }
227
- clear() { this._cache.clear(); this._iter = 0; this._contextsByQid.clear(); this._contextCapHits = 0; }
283
+ clear() { this._cache.clear(); this._iter = 0; this._contextsByQid.clear(); this._contextCapHits = 0; this._notes = []; }
284
+
285
+ /** DAG-07 R07.3: [{reason, count, example}] over everything recorded in `_notes`, sorted by reason. */
286
+ completionNotes() {
287
+ const byReason = new Map();
288
+ for (const n of this._notes) {
289
+ const cur = byReason.get(n.reason);
290
+ if (cur) cur.count += 1; else byReason.set(n.reason, { reason: n.reason, count: 1, example: String(n.qid ?? '').slice(0, 160) });
291
+ }
292
+ return [...byReason.values()].sort((a, b) => (a.reason < b.reason ? -1 : 1));
293
+ }
228
294
  }
229
295
 
230
296
  // Stage 3 correctness audit (detection depth): this compared mutatedParams
@@ -67,53 +67,107 @@ export function enumerateSinks(perFileIR, callGraph) {
67
67
  }
68
68
 
69
69
  /**
70
- * RHS-lite demand-driven slice. For each sink, walk backward through the
71
- * intra-procedural CFG via def-use, then ascend via the call graph for
72
- * each unresolved parameter binding. Returns the set of fn qids that are
73
- * REACHABLE from the sink (and therefore worth deep-analyzing).
74
- *
75
- * sinks: enumerateSinks() output
76
- * callGraph: { functions, callers, callees }
77
- * maxCallerDepth: bounds reverse-call-graph walk
70
+ * Canonical caller index: callee qid -> [caller qid]. Built from each
71
+ * function's recorded `calls` (statement-level call nodes) and resolved with
72
+ * resolveKnownCallee, never the guessing resolve(). The demand selector
73
+ * (demand-query.js) extends it with the embedded call sites that `fn.calls`
74
+ * does not record (assignment right-hand sides, return values, nested
75
+ * arguments, callback registrations).
78
76
  */
79
- export function reachabilitySliceFromSinks(sinks, callGraph, maxCallerDepth = MAX_CALLER_DEPTH) {
80
- const reachableFns = new Set();
81
- if (!Array.isArray(sinks) || !callGraph || !callGraph.functions) return reachableFns;
82
-
83
- // Build a reverse callgraph: who calls fn-qid?
77
+ export function buildCallerQidIndex(callGraph) {
84
78
  const callersOf = new Map();
79
+ if (!callGraph || !callGraph.functions) return callersOf;
85
80
  for (const fn of callGraph.functions.values()) {
86
81
  for (const callee of (fn.calls || [])) {
87
- // callee.callee is a SOURCE-LEVEL name ("b.fill"); the worklist below
88
- // looks this map up by qid, so keying by the name means the lookup can
89
- // never match. Resolve to a qid first.
90
- const name = callee && typeof callee === 'object' ? callee.callee : callee;
91
- if (!name) continue;
82
+ // callee.callee is a SOURCE-LEVEL name ("b.fill"); lookups are by qid,
83
+ // so keying by the name could never match. Resolve to a qid first.
92
84
  // resolveKnownCallee (not resolve): callee.callee may be a flattened
93
85
  // member-call string ("loader.read"); the plain resolve() would guess
94
86
  // via its bare-tail fallback and invent an edge to an unrelated
95
87
  // same-named function. See callgraph.js.
88
+ const name = callee && typeof callee === 'object' ? callee.callee : callee;
89
+ if (!name) continue;
96
90
  const qid = callGraph.resolveKnownCallee ? callGraph.resolveKnownCallee(name, fn.file) : null;
97
91
  if (!qid) continue;
98
92
  if (!callersOf.has(qid)) callersOf.set(qid, []);
99
93
  callersOf.get(qid).push(fn.qid);
100
94
  }
101
95
  }
96
+ return callersOf;
97
+ }
98
+
99
+ /**
100
+ * RHS-lite demand-driven slice. For each sink, walk backward through the
101
+ * intra-procedural CFG via def-use, then ascend via the call graph for
102
+ * each unresolved parameter binding. Returns the set of fn qids that are
103
+ * REACHABLE from the sink (and therefore worth deep-analyzing).
104
+ *
105
+ * sinks: enumerateSinks() output
106
+ * callGraph: { functions, callers, callees }
107
+ * maxCallerDepth: bounds reverse-call-graph walk
108
+ */
109
+ export function reachabilitySliceFromSinks(sinks, callGraph, maxCallerDepth = MAX_CALLER_DEPTH) {
110
+ return reachabilitySliceWithCompletion(sinks, callGraph, maxCallerDepth).reachable;
111
+ }
112
+
113
+ /**
114
+ * Same walk as reachabilitySliceFromSinks, plus a completion record
115
+ * (DAG-07 R07.1). A function the walk never reached is skipped by
116
+ * `shouldAnalyzeUnderRhs`, so a walk that stopped at its worklist cap or its
117
+ * caller-depth ceiling has silently narrowed what is analyzed; the record
118
+ * says so, with the number of callers left unexplored.
119
+ *
120
+ * Returns { reachable: Set<qid>, completion: { status, reasons,
121
+ * unexploredFrontierCount, visited, limits } }.
122
+ */
123
+ export function reachabilitySliceWithCompletion(sinks, callGraph, maxCallerDepth = MAX_CALLER_DEPTH) {
124
+ const reachableFns = new Set();
125
+ const completion = {
126
+ status: 'complete', reasons: [], unexploredFrontierCount: 0, visited: 0,
127
+ limits: { maxWorklist: MAX_WORKLIST, maxCallerDepth },
128
+ };
129
+ if (!Array.isArray(sinks) || !callGraph || !callGraph.functions) {
130
+ completion.status = 'unavailable';
131
+ completion.reasons = ['no-call-graph'];
132
+ return { reachable: reachableFns, completion };
133
+ }
134
+
135
+ // Build a reverse callgraph: who calls fn-qid?
136
+ // DAG-02: factored into the shared, exported buildCallerQidIndex so the
137
+ // demand selector (demand-query.js) reuses the same canonical incoming-edge
138
+ // index. Behaviour is unchanged.
139
+ const callersOf = buildCallerQidIndex(callGraph);
102
140
 
103
141
  const work = [];
104
142
  for (const s of sinks) work.push({ qid: s.fnQid, depth: 0 });
105
143
  let visitedCount = 0;
144
+ let depthCut = 0;
145
+ let capped = false;
106
146
  while (work.length) {
107
- if (++visitedCount > MAX_WORKLIST) break;
147
+ if (++visitedCount > MAX_WORKLIST) { capped = true; break; }
108
148
  const { qid, depth } = work.shift();
109
149
  if (reachableFns.has(qid)) continue;
110
150
  reachableFns.add(qid);
111
- if (depth >= maxCallerDepth) continue;
151
+ if (depth >= maxCallerDepth) {
152
+ // Callers of this function exist but are not explored.
153
+ for (const callerQid of (callersOf.get(qid) || [])) if (!reachableFns.has(callerQid)) depthCut += 1;
154
+ continue;
155
+ }
112
156
  for (const callerQid of (callersOf.get(qid) || [])) {
113
157
  if (!reachableFns.has(callerQid)) work.push({ qid: callerQid, depth: depth + 1 });
114
158
  }
115
159
  }
116
- return reachableFns;
160
+ completion.visited = visitedCount;
161
+ if (capped) {
162
+ completion.reasons.push('worklist-limit');
163
+ completion.unexploredFrontierCount += work.length;
164
+ }
165
+ if (depthCut > 0) {
166
+ completion.reasons.push('caller-depth-limit');
167
+ completion.unexploredFrontierCount += depthCut;
168
+ }
169
+ if (completion.reasons.length) completion.status = 'partial';
170
+ return { reachable: reachableFns, completion };
117
171
  }
118
172
 
119
173
  /**
@@ -128,10 +182,12 @@ export function rhsReachableFunctions(perFileIR, callGraph) {
128
182
  const sinks = enumerateSinks(perFileIR, callGraph);
129
183
  if (!sinks.length) {
130
184
  // No sinks → no demand. Return null to signal "analyze all" to caller.
131
- return { reachable: null, sinks: [] };
185
+ // `completion` is an additive property (DAG-07 R07.1); existing callers
186
+ // destructure only { reachable, sinks }.
187
+ return { reachable: null, sinks: [], completion: { status: 'complete', reasons: [], unexploredFrontierCount: 0, visited: 0, limits: {}, note: 'no-sinks-analyze-all' } };
132
188
  }
133
- const reachable = reachabilitySliceFromSinks(sinks, callGraph);
134
- return { reachable, sinks };
189
+ const { reachable, completion } = reachabilitySliceWithCompletion(sinks, callGraph);
190
+ return { reachable, sinks, completion };
135
191
  }
136
192
 
137
193
  /**