circle-ir 4.19.0 → 4.21.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 (262) hide show
  1. package/dist/analysis/advisories.d.ts +21 -3
  2. package/dist/analysis/advisories.d.ts.map +1 -1
  3. package/dist/analysis/advisories.js +26 -8
  4. package/dist/analysis/advisories.js.map +1 -1
  5. package/dist/analysis/arg-executing-programs.d.ts +16 -0
  6. package/dist/analysis/arg-executing-programs.d.ts.map +1 -0
  7. package/dist/analysis/arg-executing-programs.js +24 -0
  8. package/dist/analysis/arg-executing-programs.js.map +1 -0
  9. package/dist/analysis/config-loader.d.ts.map +1 -1
  10. package/dist/analysis/config-loader.js +27 -3
  11. package/dist/analysis/config-loader.js.map +1 -1
  12. package/dist/analysis/constant-caller-gate.d.ts +3 -1
  13. package/dist/analysis/constant-caller-gate.d.ts.map +1 -1
  14. package/dist/analysis/constant-caller-gate.js +108 -1
  15. package/dist/analysis/constant-caller-gate.js.map +1 -1
  16. package/dist/analysis/constant-propagation/index.d.ts.map +1 -1
  17. package/dist/analysis/constant-propagation/index.js +1 -1
  18. package/dist/analysis/constant-propagation/index.js.map +1 -1
  19. package/dist/analysis/constant-propagation/patterns.d.ts.map +1 -1
  20. package/dist/analysis/constant-propagation/patterns.js +8 -0
  21. package/dist/analysis/constant-propagation/patterns.js.map +1 -1
  22. package/dist/analysis/constant-propagation/propagator.d.ts +47 -2
  23. package/dist/analysis/constant-propagation/propagator.d.ts.map +1 -1
  24. package/dist/analysis/constant-propagation/propagator.js +422 -57
  25. package/dist/analysis/constant-propagation/propagator.js.map +1 -1
  26. package/dist/analysis/constant-propagation/types.d.ts +2 -0
  27. package/dist/analysis/constant-propagation/types.d.ts.map +1 -1
  28. package/dist/analysis/dependency-versions.d.ts +33 -0
  29. package/dist/analysis/dependency-versions.d.ts.map +1 -1
  30. package/dist/analysis/dependency-versions.js +70 -0
  31. package/dist/analysis/dependency-versions.js.map +1 -1
  32. package/dist/analysis/findings.d.ts.map +1 -1
  33. package/dist/analysis/findings.js +7 -5
  34. package/dist/analysis/findings.js.map +1 -1
  35. package/dist/analysis/interprocedural.d.ts +16 -0
  36. package/dist/analysis/interprocedural.d.ts.map +1 -1
  37. package/dist/analysis/interprocedural.js +545 -197
  38. package/dist/analysis/interprocedural.js.map +1 -1
  39. package/dist/analysis/passes/constant-propagation-pass.d.ts.map +1 -1
  40. package/dist/analysis/passes/constant-propagation-pass.js +1 -0
  41. package/dist/analysis/passes/constant-propagation-pass.js.map +1 -1
  42. package/dist/analysis/passes/cross-file-pass.d.ts.map +1 -1
  43. package/dist/analysis/passes/cross-file-pass.js +61 -0
  44. package/dist/analysis/passes/cross-file-pass.js.map +1 -1
  45. package/dist/analysis/passes/csharp-sanitizer-credit.d.ts.map +1 -1
  46. package/dist/analysis/passes/csharp-sanitizer-credit.js +60 -3
  47. package/dist/analysis/passes/csharp-sanitizer-credit.js.map +1 -1
  48. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts +58 -0
  49. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts.map +1 -0
  50. package/dist/analysis/passes/csrf-samesite-none-session-pass.js +159 -0
  51. package/dist/analysis/passes/csrf-samesite-none-session-pass.js.map +1 -0
  52. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts +15 -0
  53. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts.map +1 -1
  54. package/dist/analysis/passes/deserialization-safety-gate-pass.js +60 -1
  55. package/dist/analysis/passes/deserialization-safety-gate-pass.js.map +1 -1
  56. package/dist/analysis/passes/interprocedural-pass.d.ts +23 -1
  57. package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
  58. package/dist/analysis/passes/interprocedural-pass.js +220 -7
  59. package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
  60. package/dist/analysis/passes/language-sources-pass.d.ts +0 -18
  61. package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
  62. package/dist/analysis/passes/language-sources-pass.js +549 -22
  63. package/dist/analysis/passes/language-sources-pass.js.map +1 -1
  64. package/dist/analysis/passes/param-source-flow-gate-pass.d.ts.map +1 -1
  65. package/dist/analysis/passes/param-source-flow-gate-pass.js +71 -1
  66. package/dist/analysis/passes/param-source-flow-gate-pass.js.map +1 -1
  67. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js +1 -1
  68. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js.map +1 -1
  69. package/dist/analysis/passes/resource-leak-pass.d.ts.map +1 -1
  70. package/dist/analysis/passes/resource-leak-pass.js +15 -3
  71. package/dist/analysis/passes/resource-leak-pass.js.map +1 -1
  72. package/dist/analysis/passes/scan-secrets-pass.js +2 -2
  73. package/dist/analysis/passes/scan-secrets-pass.js.map +1 -1
  74. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts +66 -0
  75. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts.map +1 -0
  76. package/dist/analysis/passes/sensitive-data-in-log-pass.js +462 -0
  77. package/dist/analysis/passes/sensitive-data-in-log-pass.js.map +1 -0
  78. package/dist/analysis/passes/sink-filter-pass.d.ts +7 -1
  79. package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
  80. package/dist/analysis/passes/sink-filter-pass.js +564 -22
  81. package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
  82. package/dist/analysis/passes/taint-propagation-pass.d.ts +7 -3
  83. package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
  84. package/dist/analysis/passes/taint-propagation-pass.js +464 -27
  85. package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
  86. package/dist/analysis/passes/xml-entity-expansion-pass.d.ts.map +1 -1
  87. package/dist/analysis/passes/xml-entity-expansion-pass.js +50 -6
  88. package/dist/analysis/passes/xml-entity-expansion-pass.js.map +1 -1
  89. package/dist/analysis/pep440-version.d.ts +40 -0
  90. package/dist/analysis/pep440-version.d.ts.map +1 -0
  91. package/dist/analysis/pep440-version.js +149 -0
  92. package/dist/analysis/pep440-version.js.map +1 -0
  93. package/dist/analysis/project-callee-escape-gate.js +1 -1
  94. package/dist/analysis/project-callee-escape-gate.js.map +1 -1
  95. package/dist/analysis/rules.d.ts.map +1 -1
  96. package/dist/analysis/rules.js +15 -0
  97. package/dist/analysis/rules.js.map +1 -1
  98. package/dist/analysis/sbom.d.ts +92 -4
  99. package/dist/analysis/sbom.d.ts.map +1 -1
  100. package/dist/analysis/sbom.js +390 -33
  101. package/dist/analysis/sbom.js.map +1 -1
  102. package/dist/analysis/taint-matcher.d.ts.map +1 -1
  103. package/dist/analysis/taint-matcher.js +124 -19
  104. package/dist/analysis/taint-matcher.js.map +1 -1
  105. package/dist/analysis/taint-propagation.d.ts +1 -0
  106. package/dist/analysis/taint-propagation.d.ts.map +1 -1
  107. package/dist/analysis/taint-propagation.js +32 -0
  108. package/dist/analysis/taint-propagation.js.map +1 -1
  109. package/dist/analysis/test-to-production-taint-gate.d.ts +13 -0
  110. package/dist/analysis/test-to-production-taint-gate.d.ts.map +1 -0
  111. package/dist/analysis/test-to-production-taint-gate.js +30 -0
  112. package/dist/analysis/test-to-production-taint-gate.js.map +1 -0
  113. package/dist/analyzer.d.ts +21 -3
  114. package/dist/analyzer.d.ts.map +1 -1
  115. package/dist/analyzer.js +83 -3
  116. package/dist/analyzer.js.map +1 -1
  117. package/dist/browser/circle-ir.js +8118 -2526
  118. package/dist/core/circle-ir-core.cjs +3098 -2280
  119. package/dist/core/circle-ir-core.js +3036 -2214
  120. package/dist/core/extractors/calls.d.ts +10 -1
  121. package/dist/core/extractors/calls.d.ts.map +1 -1
  122. package/dist/core/extractors/calls.js +148 -6
  123. package/dist/core/extractors/calls.js.map +1 -1
  124. package/dist/core/extractors/cfg.d.ts.map +1 -1
  125. package/dist/core/extractors/cfg.js +12 -0
  126. package/dist/core/extractors/cfg.js.map +1 -1
  127. package/dist/core/extractors/dfg.d.ts.map +1 -1
  128. package/dist/core/extractors/dfg.js +151 -18
  129. package/dist/core/extractors/dfg.js.map +1 -1
  130. package/dist/core/extractors/ts-declaration-detail.d.ts +8 -0
  131. package/dist/core/extractors/ts-declaration-detail.d.ts.map +1 -1
  132. package/dist/core/extractors/ts-declaration-detail.js +22 -0
  133. package/dist/core/extractors/ts-declaration-detail.js.map +1 -1
  134. package/dist/core/extractors/types.d.ts +5 -0
  135. package/dist/core/extractors/types.d.ts.map +1 -1
  136. package/dist/core/extractors/types.js +149 -4
  137. package/dist/core/extractors/types.js.map +1 -1
  138. package/dist/core/parser.d.ts.map +1 -1
  139. package/dist/core/parser.js +3 -1
  140. package/dist/core/parser.js.map +1 -1
  141. package/dist/core/ts-grammar-workarounds.d.ts +18 -0
  142. package/dist/core/ts-grammar-workarounds.d.ts.map +1 -0
  143. package/dist/core/ts-grammar-workarounds.js +40 -0
  144. package/dist/core/ts-grammar-workarounds.js.map +1 -0
  145. package/dist/facts/arrow.d.ts +106 -0
  146. package/dist/facts/arrow.d.ts.map +1 -0
  147. package/dist/facts/arrow.js +196 -0
  148. package/dist/facts/arrow.js.map +1 -0
  149. package/dist/facts/facts2.d.ts +68 -0
  150. package/dist/facts/facts2.d.ts.map +1 -0
  151. package/dist/facts/facts2.js +379 -0
  152. package/dist/facts/facts2.js.map +1 -0
  153. package/dist/facts/index.d.ts +22 -0
  154. package/dist/facts/index.d.ts.map +1 -0
  155. package/dist/facts/index.js +15 -0
  156. package/dist/facts/index.js.map +1 -0
  157. package/dist/facts/java-facts.d.ts +56 -0
  158. package/dist/facts/java-facts.d.ts.map +1 -0
  159. package/dist/facts/java-facts.js +250 -0
  160. package/dist/facts/java-facts.js.map +1 -0
  161. package/dist/facts/order.d.ts +39 -0
  162. package/dist/facts/order.d.ts.map +1 -0
  163. package/dist/facts/order.js +120 -0
  164. package/dist/facts/order.js.map +1 -0
  165. package/dist/facts/schema.d.ts +60 -0
  166. package/dist/facts/schema.d.ts.map +1 -0
  167. package/dist/facts/schema.js +126 -0
  168. package/dist/facts/schema.js.map +1 -0
  169. package/dist/facts/schema2.d.ts +47 -0
  170. package/dist/facts/schema2.d.ts.map +1 -0
  171. package/dist/facts/schema2.js +194 -0
  172. package/dist/facts/schema2.js.map +1 -0
  173. package/dist/facts/t6.d.ts +84 -0
  174. package/dist/facts/t6.d.ts.map +1 -0
  175. package/dist/facts/t6.js +226 -0
  176. package/dist/facts/t6.js.map +1 -0
  177. package/dist/facts/table.d.ts +80 -0
  178. package/dist/facts/table.d.ts.map +1 -0
  179. package/dist/facts/table.js +548 -0
  180. package/dist/facts/table.js.map +1 -0
  181. package/dist/graph-facts/dead-code.d.ts +2 -0
  182. package/dist/graph-facts/dead-code.d.ts.map +1 -1
  183. package/dist/graph-facts/dead-code.js +4 -3
  184. package/dist/graph-facts/dead-code.js.map +1 -1
  185. package/dist/graph-facts/index.d.ts +1 -1
  186. package/dist/graph-facts/index.d.ts.map +1 -1
  187. package/dist/graph-facts/index.js +1 -1
  188. package/dist/graph-facts/index.js.map +1 -1
  189. package/dist/graph-facts/reach.d.ts +53 -8
  190. package/dist/graph-facts/reach.d.ts.map +1 -1
  191. package/dist/graph-facts/reach.js +172 -16
  192. package/dist/graph-facts/reach.js.map +1 -1
  193. package/dist/graph-facts/removal-reach.d.ts +2 -0
  194. package/dist/graph-facts/removal-reach.d.ts.map +1 -1
  195. package/dist/graph-facts/removal-reach.js +1 -1
  196. package/dist/graph-facts/removal-reach.js.map +1 -1
  197. package/dist/index.d.ts +15 -7
  198. package/dist/index.d.ts.map +1 -1
  199. package/dist/index.js +19 -4
  200. package/dist/index.js.map +1 -1
  201. package/dist/navigation/index.d.ts +1 -0
  202. package/dist/navigation/index.d.ts.map +1 -1
  203. package/dist/navigation/index.js +1 -0
  204. package/dist/navigation/index.js.map +1 -1
  205. package/dist/navigation/java-graph.d.ts +47 -2
  206. package/dist/navigation/java-graph.d.ts.map +1 -1
  207. package/dist/navigation/java-graph.js +395 -18
  208. package/dist/navigation/java-graph.js.map +1 -1
  209. package/dist/navigation/java-registrations.d.ts +93 -0
  210. package/dist/navigation/java-registrations.d.ts.map +1 -0
  211. package/dist/navigation/java-registrations.js +254 -0
  212. package/dist/navigation/java-registrations.js.map +1 -0
  213. package/dist/navigation/navigation-index.d.ts +74 -3
  214. package/dist/navigation/navigation-index.d.ts.map +1 -1
  215. package/dist/navigation/navigation-index.js +206 -11
  216. package/dist/navigation/navigation-index.js.map +1 -1
  217. package/dist/navigation/ts-call-edges.d.ts +15 -0
  218. package/dist/navigation/ts-call-edges.d.ts.map +1 -1
  219. package/dist/navigation/ts-call-edges.js.map +1 -1
  220. package/dist/navigation/ts-entry-tables.d.ts +1 -1
  221. package/dist/navigation/ts-entry-tables.d.ts.map +1 -1
  222. package/dist/navigation/ts-entry-tables.js.map +1 -1
  223. package/dist/navigation/xml-scan.d.ts +40 -0
  224. package/dist/navigation/xml-scan.d.ts.map +1 -0
  225. package/dist/navigation/xml-scan.js +269 -0
  226. package/dist/navigation/xml-scan.js.map +1 -0
  227. package/dist/resolution/cross-file.d.ts +44 -0
  228. package/dist/resolution/cross-file.d.ts.map +1 -1
  229. package/dist/resolution/cross-file.js +259 -13
  230. package/dist/resolution/cross-file.js.map +1 -1
  231. package/dist/stable-ids/files.d.ts +22 -0
  232. package/dist/stable-ids/files.d.ts.map +1 -0
  233. package/dist/stable-ids/files.js +28 -0
  234. package/dist/stable-ids/files.js.map +1 -0
  235. package/dist/stable-ids/hash.d.ts +16 -4
  236. package/dist/stable-ids/hash.d.ts.map +1 -1
  237. package/dist/stable-ids/hash.js +266 -80
  238. package/dist/stable-ids/hash.js.map +1 -1
  239. package/dist/stable-ids/index.d.ts +6 -5
  240. package/dist/stable-ids/index.d.ts.map +1 -1
  241. package/dist/stable-ids/index.js +19 -6
  242. package/dist/stable-ids/index.js.map +1 -1
  243. package/dist/stable-ids/nodes.d.ts +61 -7
  244. package/dist/stable-ids/nodes.d.ts.map +1 -1
  245. package/dist/stable-ids/nodes.js +134 -21
  246. package/dist/stable-ids/nodes.js.map +1 -1
  247. package/dist/stable-ids/ts-parse-errors.d.ts +5 -1
  248. package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
  249. package/dist/stable-ids/ts-parse-errors.js +55 -3
  250. package/dist/stable-ids/ts-parse-errors.js.map +1 -1
  251. package/dist/stable-ids/typescript.d.ts +5 -0
  252. package/dist/stable-ids/typescript.d.ts.map +1 -1
  253. package/dist/stable-ids/typescript.js +41 -8
  254. package/dist/stable-ids/typescript.js.map +1 -1
  255. package/dist/types/index.d.ts +21 -3
  256. package/dist/types/index.d.ts.map +1 -1
  257. package/dist/types/index.js +1 -0
  258. package/dist/types/index.js.map +1 -1
  259. package/docs/SPEC.md +9 -0
  260. package/package.json +9 -5
  261. package/schemas/facts@1.json +134 -0
  262. package/schemas/facts@2.json +216 -0
@@ -164,6 +164,61 @@ const JAVA_LOGGER_METHODS = new Set([
164
164
  function isJavaLoggerCall(call) {
165
165
  return JAVA_LOGGER_METHODS.has(call.method_name);
166
166
  }
167
+ /**
168
+ * #468 S2: drop duplicate `log_injection` on gated loggers for SLF4J/Log4j-style
169
+ * calls, but keep `java.util.logging.Logger` flows for flow-sanitize recall (#947).
170
+ */
171
+ export function isJavaLoggerLogInjectionDuplicate(call) {
172
+ if (!isJavaLoggerCall(call))
173
+ return false;
174
+ const fqn = call.receiver_type_fqn ?? '';
175
+ const recv = call.receiver ?? '';
176
+ const target = call.resolution?.target ?? '';
177
+ if (fqn.startsWith('java.util.logging.') || target.startsWith('java.util.logging.')) {
178
+ return false;
179
+ }
180
+ if (recv.includes('java.util.logging.') || /\bjava\.util\.logging\.Logger\b/.test(recv)) {
181
+ return false;
182
+ }
183
+ if (/^Logger\.getLogger\b/.test(recv) || /^java\.util\.logging\.Logger\.getLogger\b/.test(recv)) {
184
+ return false;
185
+ }
186
+ return true;
187
+ }
188
+ /**
189
+ * #468 S2: suppress `log_injection` only when it duplicates a gated SLF4J/Log4j
190
+ * escape — not when a bare tainted argument still reaches the logger (issue-216).
191
+ */
192
+ export function shouldSuppressGatedLoggerLogInjection(flow, callsAtSink) {
193
+ if (flow.sink_type !== 'log_injection')
194
+ return false;
195
+ const duplicateCalls = callsAtSink.filter(c => isJavaLoggerLogInjectionDuplicate(c));
196
+ if (duplicateCalls.length === 0)
197
+ return false;
198
+ // Log4j message lookup substitution — `${…}` in the format string (cognium-dev#1247).
199
+ for (const call of duplicateCalls) {
200
+ for (const arg of call.arguments) {
201
+ const expr = (arg.expression ?? '').trim();
202
+ if (/\$\{/.test(expr) && /\+/.test(expr))
203
+ return false;
204
+ }
205
+ }
206
+ const sinkVar = flow.path.length > 0 ? flow.path[flow.path.length - 1]?.variable : undefined;
207
+ if (sinkVar && /^[A-Za-z_$][\w$]*$/.test(sinkVar)) {
208
+ const escaped = sinkVar.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
209
+ const re = new RegExp(`(?:^|[^A-Za-z0-9_$])${escaped}(?:[^A-Za-z0-9_$]|$)`);
210
+ for (const call of duplicateCalls) {
211
+ for (const arg of call.arguments) {
212
+ const expr = (arg.expression ?? '').trim();
213
+ if (expr === sinkVar)
214
+ return false;
215
+ if (re.test(expr) && expr.length <= sinkVar.length + 2)
216
+ return false;
217
+ }
218
+ }
219
+ }
220
+ return true;
221
+ }
167
222
  // Java (#468, slice 3): `java.util.Objects` null checks. They validate or return
168
223
  // the reference and do not send the value outside the program.
169
224
  const JAVA_OBJECTS_CLASS = new Map([['Objects', 'java.util']]);
@@ -353,14 +408,17 @@ export function analyzeInterprocedural(graphOrTypes, callsOrSources, dfgOrSinks,
353
408
  const types = graph.ir.types;
354
409
  const calls = graph.ir.calls;
355
410
  const declaredTypeNames = new Set(types.map(t => t.name));
356
- // Build method nodes from type information
357
- const methodNodes = buildMethodNodes(types);
358
411
  // Build call graph edges with receiver type resolution
359
412
  const lang = graph.ir.meta.language;
413
+ // Java: overloads get one node each, and calls bind by arity and argument
414
+ // types (see `resolveJavaCallTargets`).
415
+ const isJava = lang === 'java';
416
+ // Build method nodes from type information
417
+ const methodNodes = buildMethodNodes(types, isJava);
360
418
  const collectionReceiver = (lang === 'typescript' || lang === 'javascript') && typeof options.sourceCode === 'string'
361
419
  ? makeBuiltinCollectionReceiverTest(options.sourceCode)
362
420
  : null;
363
- const callEdges = buildCallEdges(calls, methodNodes, types, collectionReceiver);
421
+ const callEdges = buildCallEdges(calls, methodNodes, types, collectionReceiver, isJava);
364
422
  // Identify initially tainted parameters (from sources)
365
423
  const taintedMethods = new Set();
366
424
  const taintedReturns = new Map();
@@ -368,8 +426,7 @@ export function analyzeInterprocedural(graphOrTypes, callsOrSources, dfgOrSinks,
368
426
  for (const source of sources) {
369
427
  const methodInfo = findMethodAtLine(types, source.line);
370
428
  if (methodInfo) {
371
- const fqn = buildMethodFQN(methodInfo.packageName, methodInfo.className, methodInfo.methodName);
372
- taintedMethods.add(fqn);
429
+ taintedMethods.add(nodeKeyOf(methodNodes, methodInfo));
373
430
  }
374
431
  }
375
432
  // Build taint map from DFG via CodeGraph (eliminates O(N) scan per source)
@@ -489,201 +546,219 @@ export function analyzeInterprocedural(graphOrTypes, callsOrSources, dfgOrSinks,
489
546
  }
490
547
  }
491
548
  }
492
- for (const call of calls) {
493
- // Check if any arguments are tainted
494
- const taintedArgPositions = [];
495
- const taintedArgVars = [];
496
- for (const arg of call.arguments) {
497
- if (arg.variable) {
498
- // Check 1: DFG-based taint tracking (indexed lookup, no O(N) scan)
499
- const use = graph.usesAtLine(call.location.line).find(u => u.variable === arg.variable) ?? null;
500
- const isTaintedByDFG = use && use.def_id !== null && taintedDefIds.has(use.def_id);
501
- // Check 2: Constant propagation taint tracking (for collections with tainted elements)
502
- const isTaintedByCP = taintedVarsFromCP.has(arg.variable);
503
- if (isTaintedByDFG || isTaintedByCP) {
504
- taintedArgPositions.push(arg.position);
505
- taintedArgVars.push(arg.variable);
506
- continue;
549
+ // Java: the calls are visited until no parameter gains taint, and only the
550
+ // last visit reports sinks, so a callee declared above its caller (or a
551
+ // helper moved within the file) is analysed as if declared below it. Other
552
+ // languages keep the single visit in source order.
553
+ const maxCallPasses = isJava ? 10 : 1;
554
+ let emit = !isJava;
555
+ for (let pass = 0;; pass++) {
556
+ let grew = false;
557
+ for (const call of calls) {
558
+ // Check if any arguments are tainted
559
+ const taintedArgPositions = [];
560
+ const taintedArgVars = [];
561
+ for (const arg of call.arguments) {
562
+ if (arg.variable) {
563
+ // Check 1: DFG-based taint tracking (indexed lookup, no O(N) scan)
564
+ const use = graph.usesAtLine(call.location.line).find(u => u.variable === arg.variable) ?? null;
565
+ const isTaintedByDFG = use && use.def_id !== null && taintedDefIds.has(use.def_id);
566
+ // Check 2: Constant propagation taint tracking (for collections with tainted elements)
567
+ const isTaintedByCP = taintedVarsFromCP.has(arg.variable);
568
+ if (isTaintedByDFG || isTaintedByCP) {
569
+ taintedArgPositions.push(arg.position);
570
+ taintedArgVars.push(arg.variable);
571
+ continue;
572
+ }
507
573
  }
508
- }
509
- // cognium-dev#626: a Java argv container built in place —
510
- // `launchCommand(Arrays.asList("/bin/sh", "-c", "make " + step))` — carries
511
- // every tainted variable named inside it. For a call expression `variable`
512
- // is the callee name, so the check above never sees `step`.
513
- if (graph.ir.meta.language === 'java' && ARGV_CONTAINER_ARG_RE.test(arg.expression ?? '')) {
514
- const inCode = (arg.expression ?? '').replace(/"(?:[^"\\]|\\.)*"/g, ' ');
515
- const carried = graph.usesAtLine(call.location.line).find(u => u.def_id !== null && taintedDefIds.has(u.def_id) &&
516
- new RegExp(`(?<![\\p{L}\\p{N}_.])${u.variable.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\p{L}\\p{N}_])`, 'u').test(inCode));
517
- if (carried) {
518
- taintedArgPositions.push(arg.position);
519
- taintedArgVars.push(carried.variable);
574
+ // cognium-dev#626: a Java argv container built in place —
575
+ // `launchCommand(Arrays.asList("/bin/sh", "-c", "make " + step))` — carries
576
+ // every tainted variable named inside it. For a call expression `variable`
577
+ // is the callee name, so the check above never sees `step`.
578
+ if (graph.ir.meta.language === 'java' && ARGV_CONTAINER_ARG_RE.test(arg.expression ?? '')) {
579
+ const inCode = (arg.expression ?? '').replace(/"(?:[^"\\]|\\.)*"/g, ' ');
580
+ const carried = graph.usesAtLine(call.location.line).find(u => u.def_id !== null && taintedDefIds.has(u.def_id) &&
581
+ new RegExp(`(?<![\\p{L}\\p{N}_.])${u.variable.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\p{L}\\p{N}_])`, 'u').test(inCode));
582
+ if (carried) {
583
+ taintedArgPositions.push(arg.position);
584
+ taintedArgVars.push(carried.variable);
585
+ }
520
586
  }
521
587
  }
522
- }
523
- // Resolve callee by receiver type when available (nested / inner classes).
524
- // A method of a built-in Map/Set receiver (`this.cache.set(k, v)`) is
525
- // never a method of a class in this file, whatever its name.
526
- const builtinReceiver = collectionReceiver !== null && collectionReceiver(call.receiver);
527
- const resolvedCalleeFqn = builtinReceiver ? null : resolveMethodCall(call, methodNodes, types);
528
- const targetMethod = builtinReceiver
529
- ? undefined
530
- : resolvedCalleeFqn
531
- ? methodNodes.byFqn.get(resolvedCalleeFqn)
532
- : getMethodNode(methodNodes, call.method_name);
533
- if (!targetMethod) {
534
- // External method call - check if tainted data is escaping
535
- // Skip collection methods (data manipulation), sanitizer methods (data cleaning),
536
- // and safe utility methods (validation, normalization)
537
- if (taintedArgPositions.length > 0 &&
538
- !collectionMethods.has(call.method_name) &&
539
- !sanitizerMethods.has(call.method_name) &&
540
- !safeUtilityMethods.has(call.method_name) &&
541
- !ormQueryBuilderMethods.has(call.method_name)) {
542
- // Bash specialization: every external utility (ping, whois, curl, nc,
543
- // …) is an unknown call, but an unquoted tainted positional yields
544
- // word-splitting/arg-injection that is concretely CWE-78 command
545
- // injection — not a generic CWE-668 escape. Re-classify, except for a
546
- // small allowlist of side-effect-free builtins.
547
- const isBash = graph.ir.meta.language === 'bash';
548
- // Go: a value consumed by a builtin, wrapped into an error, formatted
549
- // into a string, logged, or run through a pure stdlib helper has not
550
- // left the program. Measured on 203 Go repositories these shapes were
551
- // 78% of ALL Go taint flows (9469 escapes vs 2742 classical), with
552
- // `len` alone at 709 — so a handler file with a single request read
553
- // produced a page of CWE-668 before any real sink was reached.
554
- if (graph.ir.meta.language === 'go' && isGoNonEscapeCall(call)) {
555
- continue;
556
- }
557
- // Go: a bool test of the value against a constant-pattern regexp (#349).
558
- if (graph.ir.meta.language === 'go' && isGoConstRegexMatchCall(call, graph, calls)) {
559
- continue;
560
- }
561
- // C# — same gate, same reason (#474, sibling of #455/#468).
562
- if (graph.ir.meta.language === 'csharp' && isCSharpNonEscapeCall(call)) {
563
- continue;
564
- }
565
- // Java — JDK exception construction, static formatters (#468, minimal slices)
566
- // and Character predicates (#807).
567
- if (graph.ir.meta.language === 'java' && isJavaNonEscapeCall(call, declaredTypeNames, graph.ir.imports ?? [], calls)) {
568
- continue;
569
- }
570
- const bashSafeBuiltins = new Set([
571
- 'echo', 'printf', 'test', '[', '[[', 'true', 'false', ':',
572
- 'declare', 'local', 'export', 'readonly', 'typeset',
573
- ]);
574
- if (isBash && bashSafeBuiltins.has(call.method_name)) {
588
+ // Resolve callee by receiver type when available (nested / inner classes).
589
+ // A method of a built-in Map/Set receiver (`this.cache.set(k, v)`) is
590
+ // never a method of a class in this file, whatever its name.
591
+ const builtinReceiver = collectionReceiver !== null && collectionReceiver(call.receiver);
592
+ const targets = builtinReceiver ? [] : resolveCallTargets(call, methodNodes, types, isJava);
593
+ if (targets.length === 0) {
594
+ if (!emit)
575
595
  continue;
576
- }
577
- // Bash: a function defined in this script is not an external utility.
578
- // Its arguments reach its own positional parameters through the DFG
579
- // (call-argument chains), so a real sink inside its body still fires.
580
- if (isBash && bashScriptFunctions.has(call.method_name)) {
581
- continue;
582
- }
583
- // Bash argv-terminator gate (#216 FP #4): when `--` appears as an
584
- // argument before every tainted position AND every tainted positional
585
- // is double-quoted, both flag-injection and word-splitting are
586
- // impossible. Common pattern: `grep -- "$pattern" file`. The two
587
- // conditions are conjunctive — quoting alone permits `-e payload`
588
- // flag injection, `--` alone permits word-splitting on unquoted
589
- // expansion.
590
- if (isBash && taintedArgPositions.length > 0) {
591
- const terminatorPos = call.arguments
592
- .filter(a => a.expression === '--')
593
- .map(a => a.position)
594
- .reduce((min, p) => (min === null || p < min ? p : min), null);
595
- const earliestTainted = Math.min(...taintedArgPositions);
596
- if (terminatorPos !== null && terminatorPos < earliestTainted) {
597
- const allTaintedQuoted = taintedArgPositions.every(p => {
598
- const a = call.arguments[p];
599
- if (!a)
600
- return false;
601
- const expr = a.expression.trim();
602
- return expr.startsWith('"') && expr.endsWith('"');
603
- });
604
- if (allTaintedQuoted) {
605
- continue;
606
- }
596
+ // External method call - check if tainted data is escaping
597
+ // Skip collection methods (data manipulation), sanitizer methods (data cleaning),
598
+ // and safe utility methods (validation, normalization)
599
+ if (taintedArgPositions.length > 0 &&
600
+ !collectionMethods.has(call.method_name) &&
601
+ !sanitizerMethods.has(call.method_name) &&
602
+ !safeUtilityMethods.has(call.method_name) &&
603
+ !ormQueryBuilderMethods.has(call.method_name)) {
604
+ // Bash specialization: every external utility (ping, whois, curl, nc,
605
+ // …) is an unknown call, but an unquoted tainted positional yields
606
+ // word-splitting/arg-injection that is concretely CWE-78 command
607
+ // injection — not a generic CWE-668 escape. Re-classify, except for a
608
+ // small allowlist of side-effect-free builtins.
609
+ const isBash = graph.ir.meta.language === 'bash';
610
+ // Go: a value consumed by a builtin, wrapped into an error, formatted
611
+ // into a string, logged, or run through a pure stdlib helper has not
612
+ // left the program. Measured on 203 Go repositories these shapes were
613
+ // 78% of ALL Go taint flows (9469 escapes vs 2742 classical), with
614
+ // `len` alone at 709 — so a handler file with a single request read
615
+ // produced a page of CWE-668 before any real sink was reached.
616
+ if (graph.ir.meta.language === 'go' && isGoNonEscapeCall(call)) {
617
+ continue;
607
618
  }
608
- }
609
- // Bash SQL CLI re-classification (#216 FP #5): SQL client utilities
610
- // (sqlite3, mysql, psql, mariadb) take SQL strings as positional
611
- // args, not shell commands. When taint reaches the fallback (e.g.
612
- // via variable indirection that bypasses the per-utility builtin
613
- // sink in `bash.ts`), classify as CWE-89 sql_injection instead of
614
- // CWE-78 command_injection. The flow IS a vulnerability; only the
615
- // type label is corrected.
616
- const bashSqlCliTools = new Set(['sqlite3', 'mysql', 'psql', 'mariadb']);
617
- const isBashSqlCli = isBash && bashSqlCliTools.has(call.method_name);
618
- const sink = isBashSqlCli
619
- ? {
620
- type: 'sql_injection',
621
- cwe: 'CWE-89',
622
- location: `Tainted data (${taintedArgVars.join(', ')}) interpolated into SQL query passed to ${call.method_name}`,
623
- line: call.location.line,
624
- confidence: 0.7,
625
- method: call.method_name,
626
- argPositions: taintedArgPositions,
619
+ // Go: a bool test of the value against a constant-pattern regexp (#349).
620
+ if (graph.ir.meta.language === 'go' && isGoConstRegexMatchCall(call, graph, calls)) {
621
+ continue;
622
+ }
623
+ // C# — same gate, same reason (#474, sibling of #455/#468).
624
+ if (graph.ir.meta.language === 'csharp' && isCSharpNonEscapeCall(call)) {
625
+ continue;
626
+ }
627
+ // Java — JDK exception construction, static formatters (#468, minimal slices)
628
+ // and Character predicates (#807).
629
+ if (graph.ir.meta.language === 'java' && isJavaNonEscapeCall(call, declaredTypeNames, graph.ir.imports ?? [], calls)) {
630
+ continue;
631
+ }
632
+ const bashSafeBuiltins = new Set([
633
+ 'echo', 'printf', 'test', '[', '[[', 'true', 'false', ':',
634
+ 'declare', 'local', 'export', 'readonly', 'typeset',
635
+ ]);
636
+ if (isBash && bashSafeBuiltins.has(call.method_name)) {
637
+ continue;
638
+ }
639
+ // Bash: a function defined in this script is not an external utility.
640
+ // Its arguments reach its own positional parameters through the DFG
641
+ // (call-argument chains), so a real sink inside its body still fires.
642
+ if (isBash && bashScriptFunctions.has(call.method_name)) {
643
+ continue;
644
+ }
645
+ // Bash argv-terminator gate (#216 FP #4): when `--` appears as an
646
+ // argument before every tainted position AND every tainted positional
647
+ // is double-quoted, both flag-injection and word-splitting are
648
+ // impossible. Common pattern: `grep -- "$pattern" file`. The two
649
+ // conditions are conjunctive — quoting alone permits `-e payload`
650
+ // flag injection, `--` alone permits word-splitting on unquoted
651
+ // expansion.
652
+ if (isBash && taintedArgPositions.length > 0) {
653
+ const terminatorPos = call.arguments
654
+ .filter(a => a.expression === '--')
655
+ .map(a => a.position)
656
+ .reduce((min, p) => (min === null || p < min ? p : min), null);
657
+ const earliestTainted = Math.min(...taintedArgPositions);
658
+ if (terminatorPos !== null && terminatorPos < earliestTainted) {
659
+ const allTaintedQuoted = taintedArgPositions.every(p => {
660
+ const a = call.arguments[p];
661
+ if (!a)
662
+ return false;
663
+ const expr = a.expression.trim();
664
+ return expr.startsWith('"') && expr.endsWith('"');
665
+ });
666
+ if (allTaintedQuoted) {
667
+ continue;
668
+ }
669
+ }
627
670
  }
628
- : isBash
671
+ // Bash SQL CLI re-classification (#216 FP #5): SQL client utilities
672
+ // (sqlite3, mysql, psql, mariadb) take SQL strings as positional
673
+ // args, not shell commands. When taint reaches the fallback (e.g.
674
+ // via variable indirection that bypasses the per-utility builtin
675
+ // sink in `bash.ts`), classify as CWE-89 sql_injection instead of
676
+ // CWE-78 command_injection. The flow IS a vulnerability; only the
677
+ // type label is corrected.
678
+ const bashSqlCliTools = new Set(['sqlite3', 'mysql', 'psql', 'mariadb']);
679
+ const isBashSqlCli = isBash && bashSqlCliTools.has(call.method_name);
680
+ const sink = isBashSqlCli
629
681
  ? {
630
- type: 'command_injection',
631
- cwe: 'CWE-78',
632
- location: `Tainted data (${taintedArgVars.join(', ')}) passed unquoted to shell utility ${call.method_name}`,
682
+ type: 'sql_injection',
683
+ cwe: 'CWE-89',
684
+ location: `Tainted data (${taintedArgVars.join(', ')}) interpolated into SQL query passed to ${call.method_name}`,
633
685
  line: call.location.line,
634
- confidence: 0.6,
686
+ confidence: 0.7,
635
687
  method: call.method_name,
636
688
  argPositions: taintedArgPositions,
637
689
  }
638
- : {
639
- // Create an "external_taint_escape" sink for this call
640
- // This represents tainted data being passed to code we can't analyze
641
- type: 'external_taint_escape',
642
- cwe: 'CWE-668', // Exposure of Resource to Wrong Sphere
643
- location: `Tainted data (${taintedArgVars.join(', ')}) passed to external method ${call.receiver ? call.receiver + '.' : ''}${call.method_name}()`,
644
- line: call.location.line,
645
- confidence: 0.7, // Lower confidence since we can't verify the external method is dangerous
646
- method: call.method_name,
647
- argPositions: taintedArgPositions,
648
- };
649
- // Only add if not already present
650
- if (!propagatedSinks.some(s => s.line === sink.line && s.type === sink.type)) {
651
- propagatedSinks.push(sink);
690
+ : isBash
691
+ ? {
692
+ type: 'command_injection',
693
+ cwe: 'CWE-78',
694
+ location: `Tainted data (${taintedArgVars.join(', ')}) passed unquoted to shell utility ${call.method_name}`,
695
+ line: call.location.line,
696
+ confidence: 0.6,
697
+ method: call.method_name,
698
+ argPositions: taintedArgPositions,
699
+ }
700
+ : {
701
+ // Create an "external_taint_escape" sink for this call
702
+ // This represents tainted data being passed to code we can't analyze
703
+ type: 'external_taint_escape',
704
+ cwe: 'CWE-668', // Exposure of Resource to Wrong Sphere
705
+ location: `Tainted data (${taintedArgVars.join(', ')}) passed to external method ${call.receiver ? call.receiver + '.' : ''}${call.method_name}()`,
706
+ line: call.location.line,
707
+ confidence: 0.7, // Lower confidence since we can't verify the external method is dangerous
708
+ method: call.method_name,
709
+ argPositions: taintedArgPositions,
710
+ };
711
+ // Only add if not already present
712
+ if (!propagatedSinks.some(s => s.line === sink.line && s.type === sink.type)) {
713
+ propagatedSinks.push(sink);
714
+ }
652
715
  }
716
+ continue;
653
717
  }
654
- continue;
655
- }
656
- if (taintedArgPositions.length > 0) {
657
- // Mark corresponding parameters as tainted
658
- for (const pos of taintedArgPositions) {
659
- if (pos < targetMethod.parameters.length) {
660
- targetMethod.parameters[pos].isTainted = true;
661
- targetMethod.parameters[pos].sourceLine = call.location.line;
662
- const paramName = targetMethod.parameters[pos].name;
663
- for (const def of graph.ir.dfg.defs) {
664
- if (def.kind === 'param' &&
665
- def.variable === paramName &&
666
- def.line >= targetMethod.startLine &&
667
- def.line <= targetMethod.endLine) {
668
- taintedDefIds.add(def.id);
718
+ if (taintedArgPositions.length > 0) {
719
+ for (const targetMethod of targets) {
720
+ // Mark corresponding parameters as tainted
721
+ for (const pos of taintedArgPositions) {
722
+ if (pos < targetMethod.parameters.length) {
723
+ targetMethod.parameters[pos].sourceLine = call.location.line;
724
+ // Its param defs are already seeded once the parameter is tainted.
725
+ if (targetMethod.parameters[pos].isTainted)
726
+ continue;
727
+ targetMethod.parameters[pos].isTainted = true;
728
+ grew = true;
729
+ const paramName = targetMethod.parameters[pos].name;
730
+ for (const def of graph.ir.dfg.defs) {
731
+ if (def.kind === 'param' &&
732
+ def.variable === paramName &&
733
+ def.line >= targetMethod.startLine &&
734
+ def.line <= targetMethod.endLine) {
735
+ taintedDefIds.add(def.id);
736
+ }
737
+ }
738
+ }
739
+ }
740
+ taintedMethods.add(targetMethod.fqn);
741
+ if (!emit)
742
+ continue;
743
+ // Check if target method has sinks
744
+ const methodSinks = sinks.filter(s => s.line >= targetMethod.startLine && s.line <= targetMethod.endLine);
745
+ // These sinks are now reachable via inter-procedural flow
746
+ for (const sink of methodSinks) {
747
+ // Check if not already in the list
748
+ if (!propagatedSinks.some(s => s.line === sink.line)) {
749
+ propagatedSinks.push({
750
+ ...sink,
751
+ confidence: sink.confidence * 0.85, // Slightly lower confidence for inter-proc
752
+ });
669
753
  }
670
754
  }
671
- }
672
- }
673
- taintedMethods.add(targetMethod.fqn);
674
- // Check if target method has sinks
675
- const methodSinks = sinks.filter(s => s.line >= targetMethod.startLine && s.line <= targetMethod.endLine);
676
- // These sinks are now reachable via inter-procedural flow
677
- for (const sink of methodSinks) {
678
- // Check if not already in the list
679
- if (!propagatedSinks.some(s => s.line === sink.line)) {
680
- propagatedSinks.push({
681
- ...sink,
682
- confidence: sink.confidence * 0.85, // Slightly lower confidence for inter-proc
683
- });
684
755
  }
685
756
  }
686
757
  }
758
+ if (emit)
759
+ break;
760
+ if (!grew || pass + 1 >= maxCallPasses)
761
+ emit = true;
687
762
  }
688
763
  // Propagate taint through return values
689
764
  propagateReturnTaint(types, graph, taintedDefIds, taintedReturns, taintedMethods, methodNodes);
@@ -711,12 +786,30 @@ function buildMethodFQN(packageName, className, methodName) {
711
786
  * Build method nodes from type information.
712
787
  * Uses fully qualified names (FQN) as keys for precise method resolution.
713
788
  */
714
- function buildMethodNodes(types) {
789
+ function buildMethodNodes(types, javaOverloads = false) {
715
790
  const byFqn = new Map();
716
791
  const byName = new Map();
792
+ const byInfo = new Map();
717
793
  for (const type of types) {
794
+ // Java: a name declared more than once in the type is keyed by its erased
795
+ // parameter types, `pkg.C.m(String)`, so no overload replaces another.
796
+ // A name declared once keeps the plain `pkg.C.m` key.
797
+ const nameCount = new Map();
798
+ if (javaOverloads) {
799
+ for (const m of type.methods)
800
+ nameCount.set(m.name, (nameCount.get(m.name) ?? 0) + 1);
801
+ }
718
802
  for (const method of type.methods) {
719
- const fqn = buildMethodFQN(type.package, type.name, method.name);
803
+ let fqn = buildMethodFQN(type.package, type.name, method.name);
804
+ if (javaOverloads) {
805
+ if ((nameCount.get(method.name) ?? 0) > 1) {
806
+ fqn += `(${method.parameters.map(p => p.type === null ? '?' : javaErasure(p.type).text).join(',')})`;
807
+ }
808
+ // Two declarations with one key (same-named nested types, or a
809
+ // parse that lost the types): keep both, the later one by line.
810
+ if (byFqn.has(fqn))
811
+ fqn += `@${method.start_line}`;
812
+ }
720
813
  const node = {
721
814
  name: method.name,
722
815
  fqn,
@@ -737,13 +830,265 @@ function buildMethodNodes(types) {
737
830
  };
738
831
  // Store with FQN as primary key
739
832
  byFqn.set(fqn, node);
833
+ byInfo.set(method, node);
740
834
  // Store with simple name for fallback (first occurrence wins)
741
835
  if (!byName.has(method.name)) {
742
836
  byName.set(method.name, node);
743
837
  }
744
838
  }
745
839
  }
746
- return { byFqn, byName };
840
+ // Elsewhere a later declaration of one FQN replaces the earlier one.
841
+ if (!javaOverloads) {
842
+ for (const [info, node] of byInfo) {
843
+ if (byFqn.get(node.fqn) !== node)
844
+ byInfo.delete(info);
845
+ }
846
+ }
847
+ return { byFqn, byName, byInfo };
848
+ }
849
+ /** The node key of the method a `findMethodAtLine` context names. */
850
+ function nodeKeyOf(maps, ctx) {
851
+ return maps.byInfo.get(ctx.method)?.fqn
852
+ ?? buildMethodFQN(ctx.packageName, ctx.className, ctx.methodName);
853
+ }
854
+ /**
855
+ * The same-file methods a call may bind to. Java: see
856
+ * `resolveJavaCallTargets`. Elsewhere: the one node `resolveMethodCall`
857
+ * names, if any.
858
+ */
859
+ function resolveCallTargets(call, methodNodes, types, isJava) {
860
+ if (isJava)
861
+ return resolveJavaCallTargets(call, methodNodes, types);
862
+ const fqn = resolveMethodCall(call, methodNodes, types);
863
+ const node = fqn ? methodNodes.byFqn.get(fqn) : getMethodNode(methodNodes, call.method_name);
864
+ return node ? [node] : [];
865
+ }
866
+ /**
867
+ * Java call resolution (C5 follow-up F4). The owner type is chosen as
868
+ * before (the receiver's declared type; else, with a receiver, the first type
869
+ * declaring the name; else the innermost type around the call that declares
870
+ * it, then the first type declaring it). Among that type's methods of the
871
+ * name:
872
+ * 1. those whose arity fits the call (a trailing varargs parameter takes
873
+ * any number of arguments, including none);
874
+ * 2. of those, the ones every known argument type can be passed to
875
+ * (`javaArgDefinitelyIncompatible`);
876
+ * 3. if exactly one candidate takes every argument at its exact erased
877
+ * type, that one.
878
+ * Whatever is still ambiguous is kept whole, so taint reaches every
879
+ * overload the call could be, never just the one declared last. A step that
880
+ * would leave nothing falls back to the step before it (our types are a
881
+ * syntactic approximation, and dropping every candidate would hide flows).
882
+ * The result does not depend on the order of the declarations.
883
+ */
884
+ function resolveJavaCallTargets(call, methodNodes, types) {
885
+ const name = call.method_name;
886
+ const declares = (t) => t.methods.some(m => m.name === name);
887
+ let owners = [];
888
+ if (call.receiver_type) {
889
+ const rt = call.receiver_type;
890
+ owners = types.filter(t => declares(t) && (t.name === rt || (t.package !== null && `${t.package}.${t.name}` === rt)));
891
+ }
892
+ if (owners.length === 0 && call.receiver) {
893
+ const first = types.find(declares);
894
+ if (first)
895
+ owners = [first];
896
+ }
897
+ if (owners.length === 0) {
898
+ const line = call.location.line;
899
+ const around = types
900
+ .filter(t => declares(t) && line >= t.start_line && line <= t.end_line)
901
+ .sort((a, b) => (a.end_line - a.start_line) - (b.end_line - b.start_line));
902
+ const first = around[0] ?? types.find(declares);
903
+ if (first)
904
+ owners = [first];
905
+ }
906
+ const candidates = owners.flatMap(t => t.methods.filter(m => m.name === name));
907
+ const chosen = selectJavaOverloads(candidates, call, types);
908
+ const out = [];
909
+ for (const m of chosen) {
910
+ const node = methodNodes.byInfo.get(m);
911
+ if (node && !out.includes(node))
912
+ out.push(node);
913
+ }
914
+ return out;
915
+ }
916
+ function selectJavaOverloads(candidates, call, types) {
917
+ if (candidates.length <= 1)
918
+ return candidates;
919
+ const n = call.arguments.length;
920
+ const byArity = candidates.filter(m => javaArityFits(m, n));
921
+ if (byArity.length === 0)
922
+ return candidates;
923
+ if (byArity.length === 1)
924
+ return byArity;
925
+ const argTypes = call.arguments.map(a => a.type ?? null);
926
+ const applicable = byArity.filter(m => argTypes.every((t, i) => !javaArgDefinitelyIncompatibleAt(t, m, i, n, types)));
927
+ if (applicable.length === 0)
928
+ return byArity;
929
+ if (applicable.length === 1)
930
+ return applicable;
931
+ const exact = applicable.filter(m => m.parameters.length === n && argTypes.every((t, i) => {
932
+ const p = m.parameters[i]?.type;
933
+ return t !== null && p !== null && p !== undefined && javaErasure(t).text === javaErasure(p).text;
934
+ }));
935
+ return exact.length === 1 ? exact : applicable;
936
+ }
937
+ /** A trailing parameter is varargs when spelled `T...`, or untyped (the default extractor reports a varargs parameter with no type). */
938
+ function javaIsVarargs(m) {
939
+ const last = m.parameters[m.parameters.length - 1];
940
+ return last !== undefined && (last.type === null || last.type.trimEnd().endsWith('...'));
941
+ }
942
+ function javaArityFits(m, n) {
943
+ const k = m.parameters.length;
944
+ return k === n || (javaIsVarargs(m) && n >= k - 1);
945
+ }
946
+ function javaArgDefinitelyIncompatibleAt(argType, m, i, n, types) {
947
+ if (argType === null)
948
+ return false;
949
+ const k = m.parameters.length;
950
+ const varargs = javaIsVarargs(m);
951
+ if (varargs && i >= k - 1) {
952
+ const raw = m.parameters[k - 1].type;
953
+ if (raw === null)
954
+ return false;
955
+ const arrayType = raw.trimEnd().replace(/\.\.\.$/, '[]');
956
+ const elemType = raw.trimEnd().replace(/\.\.\.$/, '');
957
+ // One argument in the varargs slot may be the array itself.
958
+ if (n === k && !javaArgDefinitelyIncompatible(argType, arrayType, types))
959
+ return false;
960
+ return javaArgDefinitelyIncompatible(argType, elemType, types);
961
+ }
962
+ const p = m.parameters[i]?.type ?? null;
963
+ return p === null ? false : javaArgDefinitelyIncompatible(argType, p, types);
964
+ }
965
+ const JAVA_PRIMITIVES = new Set(['byte', 'short', 'char', 'int', 'long', 'float', 'double', 'boolean']);
966
+ const JAVA_BOX = {
967
+ byte: 'Byte', short: 'Short', char: 'Character', int: 'Integer',
968
+ long: 'Long', float: 'Float', double: 'Double', boolean: 'Boolean',
969
+ };
970
+ const JAVA_UNBOX = Object.fromEntries(Object.entries(JAVA_BOX).map(([p, b]) => [b, p]));
971
+ const JAVA_WIDENS = {
972
+ byte: ['short', 'int', 'long', 'float', 'double'],
973
+ short: ['int', 'long', 'float', 'double'],
974
+ char: ['int', 'long', 'float', 'double'],
975
+ int: ['long', 'float', 'double'],
976
+ long: ['float', 'double'],
977
+ float: ['double'],
978
+ double: [],
979
+ boolean: [],
980
+ };
981
+ const NUMBER_SUPERS = ['Number', 'Object', 'Comparable', 'Serializable', 'Constable', 'ConstantDesc'];
982
+ /**
983
+ * Final JDK types whose every supertype is known: an argument of one of
984
+ * these static types can be passed only to a parameter typed as one of its
985
+ * supertypes, and no other class can be passed to a parameter of one of them.
986
+ */
987
+ const JAVA_FINAL_SUPERS = {
988
+ String: ['String', 'Object', 'CharSequence', 'Comparable', 'Serializable', 'Constable', 'ConstantDesc'],
989
+ Integer: ['Integer', ...NUMBER_SUPERS],
990
+ Long: ['Long', ...NUMBER_SUPERS],
991
+ Short: ['Short', 'Number', 'Object', 'Comparable', 'Serializable', 'Constable'],
992
+ Byte: ['Byte', 'Number', 'Object', 'Comparable', 'Serializable', 'Constable'],
993
+ Float: ['Float', ...NUMBER_SUPERS],
994
+ Double: ['Double', ...NUMBER_SUPERS],
995
+ Boolean: ['Boolean', 'Object', 'Comparable', 'Serializable', 'Constable'],
996
+ Character: ['Character', 'Object', 'Comparable', 'Serializable', 'Constable'],
997
+ Class: ['Class', 'Object', 'Serializable', 'Type', 'GenericDeclaration', 'AnnotatedElement', 'TypeDescriptor', 'OfField'],
998
+ };
999
+ /** A Java type spelled in source, erased: no annotations, type arguments or package; `...` read as `[]`. */
1000
+ function javaErasure(raw) {
1001
+ let t = raw.replace(/@[\w.]+(?:\([^)]*\))?\s*/g, '').replace(/\bfinal\s+/g, '').trim();
1002
+ let prev = '';
1003
+ while (prev !== t) {
1004
+ prev = t;
1005
+ t = t.replace(/<[^<>]*>/g, '');
1006
+ }
1007
+ t = t.replace(/\.\.\.$/, '[]').replace(/\s+/g, '');
1008
+ const dims = (t.match(/\[\]/g) ?? []).length;
1009
+ const qualified = t.replace(/\[\]/g, '');
1010
+ const base = qualified.slice(qualified.lastIndexOf('.') + 1);
1011
+ return { base, dims, text: base + '[]'.repeat(dims) };
1012
+ }
1013
+ /**
1014
+ * `true` only when an argument of static type `argType` certainly cannot be
1015
+ * passed to a parameter of type `paramType` (no subtyping, widening, boxing
1016
+ * or unboxing makes it fit). Anything this cannot decide (an unknown class,
1017
+ * a type variable, a supertype declared in another file) is compatible.
1018
+ */
1019
+ function javaArgDefinitelyIncompatible(argType, paramType, types) {
1020
+ const a = javaErasure(argType);
1021
+ const p = javaErasure(paramType);
1022
+ if (!a.base || !p.base)
1023
+ return false;
1024
+ const declared = new Map();
1025
+ for (const t of types)
1026
+ if (!declared.has(t.name))
1027
+ declared.set(t.name, t);
1028
+ // A type variable (or anything else we cannot read) decides nothing.
1029
+ const opaque = (b) => !JAVA_PRIMITIVES.has(b) && !(b in JAVA_FINAL_SUPERS) && !declared.has(b) && /^[A-Z][A-Z0-9]?$/.test(b);
1030
+ if (opaque(a.base) || opaque(p.base))
1031
+ return false;
1032
+ if (a.base === 'null' && a.dims === 0)
1033
+ return p.dims === 0 && JAVA_PRIMITIVES.has(p.base);
1034
+ if (a.dims !== p.dims) {
1035
+ // An array goes to Object / Cloneable / Serializable, or to a shallower
1036
+ // array of one of those.
1037
+ if (a.dims > p.dims && ['Object', 'Cloneable', 'Serializable'].includes(p.base))
1038
+ return false;
1039
+ return true;
1040
+ }
1041
+ if (a.dims > 0) {
1042
+ if (JAVA_PRIMITIVES.has(a.base) || JAVA_PRIMITIVES.has(p.base))
1043
+ return a.base !== p.base;
1044
+ return javaArgDefinitelyIncompatible(a.base, p.base, types);
1045
+ }
1046
+ if (a.base === p.base)
1047
+ return false;
1048
+ const aPrim = JAVA_PRIMITIVES.has(a.base);
1049
+ const pPrim = JAVA_PRIMITIVES.has(p.base);
1050
+ if (aPrim && pPrim)
1051
+ return !JAVA_WIDENS[a.base].includes(p.base);
1052
+ if (aPrim)
1053
+ return !JAVA_FINAL_SUPERS[JAVA_BOX[a.base]].includes(p.base);
1054
+ if (pPrim) {
1055
+ const unboxed = JAVA_UNBOX[a.base];
1056
+ if (unboxed)
1057
+ return unboxed !== p.base && !JAVA_WIDENS[unboxed].includes(p.base);
1058
+ return true;
1059
+ }
1060
+ if (a.base in JAVA_FINAL_SUPERS)
1061
+ return !JAVA_FINAL_SUPERS[a.base].includes(p.base);
1062
+ if (p.base === 'Object')
1063
+ return false;
1064
+ if (p.base in JAVA_FINAL_SUPERS)
1065
+ return true;
1066
+ // A class or interface declared in this file: incompatible only when its
1067
+ // whole supertype closure is declared here too and does not hold the
1068
+ // parameter type.
1069
+ const start = declared.get(a.base);
1070
+ if (!start)
1071
+ return false;
1072
+ const seen = new Set();
1073
+ const queue = [start];
1074
+ while (queue.length > 0) {
1075
+ const t = queue.pop();
1076
+ if (seen.has(t.name))
1077
+ continue;
1078
+ seen.add(t.name);
1079
+ for (const sup of [t.extends, ...t.implements]) {
1080
+ if (!sup)
1081
+ continue;
1082
+ const sb = javaErasure(sup).base;
1083
+ if (sb === p.base)
1084
+ return false;
1085
+ const st = declared.get(sb);
1086
+ if (!st)
1087
+ return false; // a supertype from elsewhere could be anything
1088
+ queue.push(st);
1089
+ }
1090
+ }
1091
+ return !seen.has(p.base);
747
1092
  }
748
1093
  /**
749
1094
  * Get a method node by FQN or simple name.
@@ -837,25 +1182,28 @@ function makeBuiltinCollectionReceiverTest(sourceCode) {
837
1182
  * Build call edges from call information.
838
1183
  * Uses receiver type information for precise method resolution.
839
1184
  */
840
- function buildCallEdges(calls, methodNodes, types, collectionReceiver = null) {
1185
+ function buildCallEdges(calls, methodNodes, types, collectionReceiver = null, isJava = false) {
841
1186
  const edges = [];
842
1187
  for (const call of calls) {
843
1188
  if (collectionReceiver !== null && collectionReceiver(call.receiver))
844
1189
  continue;
845
- // Resolve the call target using receiver type
846
- const resolvedFqn = resolveMethodCall(call, methodNodes, types);
847
- if (!resolvedFqn)
848
- continue;
849
1190
  // Find the caller method
850
1191
  const callerMethod = call.in_method;
851
1192
  if (!callerMethod)
852
1193
  continue;
853
- edges.push({
854
- callerMethod,
855
- calleeMethod: resolvedFqn,
856
- callLine: call.location.line,
857
- taintedArgs: [],
858
- });
1194
+ // Resolve the call target(s) using receiver type (Java: every overload
1195
+ // the call may bind to gets an edge).
1196
+ const calleeKeys = isJava
1197
+ ? resolveJavaCallTargets(call, methodNodes, types).map(n => n.fqn)
1198
+ : [resolveMethodCall(call, methodNodes, types)].filter((k) => k !== null);
1199
+ for (const calleeMethod of calleeKeys) {
1200
+ edges.push({
1201
+ callerMethod,
1202
+ calleeMethod,
1203
+ callLine: call.location.line,
1204
+ taintedArgs: [],
1205
+ });
1206
+ }
859
1207
  }
860
1208
  return edges;
861
1209
  }
@@ -890,7 +1238,7 @@ function propagateReturnTaint(types, graph, taintedDefIds, taintedReturns, taint
890
1238
  const methodCtx = findMethodAtLine(types, returnDef.line);
891
1239
  if (!methodCtx)
892
1240
  continue;
893
- const fqn = buildMethodFQN(methodCtx.packageName, methodCtx.className, methodCtx.methodName);
1241
+ const fqn = nodeKeyOf(methodNodes, methodCtx);
894
1242
  // Find uses on the same line (the returned value) — indexed lookup
895
1243
  const usesOnLine = graph.usesAtLine(returnDef.line);
896
1244
  for (const use of usesOnLine) {