circle-ir 4.18.1 → 4.20.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 (185) 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 +32 -23
  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 +5 -0
  21. package/dist/analysis/constant-propagation/patterns.js.map +1 -1
  22. package/dist/analysis/constant-propagation/propagator.d.ts +1 -1
  23. package/dist/analysis/constant-propagation/propagator.d.ts.map +1 -1
  24. package/dist/analysis/constant-propagation/propagator.js +15 -1
  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/interprocedural.d.ts +6 -0
  33. package/dist/analysis/interprocedural.d.ts.map +1 -1
  34. package/dist/analysis/interprocedural.js +53 -6
  35. package/dist/analysis/interprocedural.js.map +1 -1
  36. package/dist/analysis/js-helper-return.d.ts +12 -0
  37. package/dist/analysis/js-helper-return.d.ts.map +1 -0
  38. package/dist/analysis/js-helper-return.js +64 -0
  39. package/dist/analysis/js-helper-return.js.map +1 -0
  40. package/dist/analysis/passes/constant-propagation-pass.d.ts.map +1 -1
  41. package/dist/analysis/passes/constant-propagation-pass.js +1 -0
  42. package/dist/analysis/passes/constant-propagation-pass.js.map +1 -1
  43. package/dist/analysis/passes/cross-file-pass.d.ts.map +1 -1
  44. package/dist/analysis/passes/cross-file-pass.js +61 -0
  45. package/dist/analysis/passes/cross-file-pass.js.map +1 -1
  46. package/dist/analysis/passes/csharp-sanitizer-credit.d.ts.map +1 -1
  47. package/dist/analysis/passes/csharp-sanitizer-credit.js +60 -3
  48. package/dist/analysis/passes/csharp-sanitizer-credit.js.map +1 -1
  49. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts +15 -0
  50. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts.map +1 -1
  51. package/dist/analysis/passes/deserialization-safety-gate-pass.js +60 -1
  52. package/dist/analysis/passes/deserialization-safety-gate-pass.js.map +1 -1
  53. package/dist/analysis/passes/interprocedural-pass.d.ts +23 -1
  54. package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
  55. package/dist/analysis/passes/interprocedural-pass.js +148 -6
  56. package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
  57. package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
  58. package/dist/analysis/passes/language-sources-pass.js +150 -7
  59. package/dist/analysis/passes/language-sources-pass.js.map +1 -1
  60. package/dist/analysis/passes/resource-leak-pass.d.ts.map +1 -1
  61. package/dist/analysis/passes/resource-leak-pass.js +20 -4
  62. package/dist/analysis/passes/resource-leak-pass.js.map +1 -1
  63. package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
  64. package/dist/analysis/passes/sink-filter-pass.js +229 -8
  65. package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
  66. package/dist/analysis/passes/taint-propagation-pass.d.ts +7 -3
  67. package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
  68. package/dist/analysis/passes/taint-propagation-pass.js +176 -15
  69. package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
  70. package/dist/analysis/passes/xml-entity-expansion-pass.d.ts.map +1 -1
  71. package/dist/analysis/passes/xml-entity-expansion-pass.js +50 -6
  72. package/dist/analysis/passes/xml-entity-expansion-pass.js.map +1 -1
  73. package/dist/analysis/pep440-version.d.ts +40 -0
  74. package/dist/analysis/pep440-version.d.ts.map +1 -0
  75. package/dist/analysis/pep440-version.js +149 -0
  76. package/dist/analysis/pep440-version.js.map +1 -0
  77. package/dist/analysis/rules.d.ts.map +1 -1
  78. package/dist/analysis/rules.js +6 -0
  79. package/dist/analysis/rules.js.map +1 -1
  80. package/dist/analysis/sbom.d.ts +43 -4
  81. package/dist/analysis/sbom.d.ts.map +1 -1
  82. package/dist/analysis/sbom.js +265 -33
  83. package/dist/analysis/sbom.js.map +1 -1
  84. package/dist/analysis/taint-matcher.d.ts.map +1 -1
  85. package/dist/analysis/taint-matcher.js +107 -5
  86. package/dist/analysis/taint-matcher.js.map +1 -1
  87. package/dist/analyzer.d.ts +6 -0
  88. package/dist/analyzer.d.ts.map +1 -1
  89. package/dist/analyzer.js +27 -1
  90. package/dist/analyzer.js.map +1 -1
  91. package/dist/browser/circle-ir.js +3379 -101
  92. package/dist/core/circle-ir-core.cjs +218 -35
  93. package/dist/core/circle-ir-core.js +218 -35
  94. package/dist/core/extractors/dfg.js +46 -7
  95. package/dist/core/extractors/dfg.js.map +1 -1
  96. package/dist/core/extractors/ts-declaration-detail.d.ts +8 -0
  97. package/dist/core/extractors/ts-declaration-detail.d.ts.map +1 -1
  98. package/dist/core/extractors/ts-declaration-detail.js +22 -0
  99. package/dist/core/extractors/ts-declaration-detail.js.map +1 -1
  100. package/dist/facts/arrow.d.ts +106 -0
  101. package/dist/facts/arrow.d.ts.map +1 -0
  102. package/dist/facts/arrow.js +196 -0
  103. package/dist/facts/arrow.js.map +1 -0
  104. package/dist/facts/facts2.d.ts +59 -0
  105. package/dist/facts/facts2.d.ts.map +1 -0
  106. package/dist/facts/facts2.js +362 -0
  107. package/dist/facts/facts2.js.map +1 -0
  108. package/dist/facts/index.d.ts +20 -0
  109. package/dist/facts/index.d.ts.map +1 -0
  110. package/dist/facts/index.js +14 -0
  111. package/dist/facts/index.js.map +1 -0
  112. package/dist/facts/java-facts.d.ts +56 -0
  113. package/dist/facts/java-facts.d.ts.map +1 -0
  114. package/dist/facts/java-facts.js +250 -0
  115. package/dist/facts/java-facts.js.map +1 -0
  116. package/dist/facts/order.d.ts +39 -0
  117. package/dist/facts/order.d.ts.map +1 -0
  118. package/dist/facts/order.js +120 -0
  119. package/dist/facts/order.js.map +1 -0
  120. package/dist/facts/schema.d.ts +60 -0
  121. package/dist/facts/schema.d.ts.map +1 -0
  122. package/dist/facts/schema.js +126 -0
  123. package/dist/facts/schema.js.map +1 -0
  124. package/dist/facts/schema2.d.ts +47 -0
  125. package/dist/facts/schema2.d.ts.map +1 -0
  126. package/dist/facts/schema2.js +194 -0
  127. package/dist/facts/schema2.js.map +1 -0
  128. package/dist/facts/table.d.ts +80 -0
  129. package/dist/facts/table.d.ts.map +1 -0
  130. package/dist/facts/table.js +548 -0
  131. package/dist/facts/table.js.map +1 -0
  132. package/dist/graph-facts/dead-code.d.ts +2 -0
  133. package/dist/graph-facts/dead-code.d.ts.map +1 -1
  134. package/dist/graph-facts/dead-code.js +1 -1
  135. package/dist/graph-facts/dead-code.js.map +1 -1
  136. package/dist/graph-facts/reach.d.ts +24 -6
  137. package/dist/graph-facts/reach.d.ts.map +1 -1
  138. package/dist/graph-facts/reach.js +52 -12
  139. package/dist/graph-facts/reach.js.map +1 -1
  140. package/dist/graph-facts/removal-reach.d.ts +2 -0
  141. package/dist/graph-facts/removal-reach.d.ts.map +1 -1
  142. package/dist/graph-facts/removal-reach.js +1 -1
  143. package/dist/graph-facts/removal-reach.js.map +1 -1
  144. package/dist/index.d.ts +10 -4
  145. package/dist/index.d.ts.map +1 -1
  146. package/dist/index.js +13 -2
  147. package/dist/index.js.map +1 -1
  148. package/dist/navigation/java-graph.d.ts +23 -0
  149. package/dist/navigation/java-graph.d.ts.map +1 -1
  150. package/dist/navigation/java-graph.js +222 -8
  151. package/dist/navigation/java-graph.js.map +1 -1
  152. package/dist/navigation/navigation-index.d.ts +9 -0
  153. package/dist/navigation/navigation-index.d.ts.map +1 -1
  154. package/dist/navigation/navigation-index.js +25 -0
  155. package/dist/navigation/navigation-index.js.map +1 -1
  156. package/dist/navigation/ts-call-edges.d.ts +9 -0
  157. package/dist/navigation/ts-call-edges.d.ts.map +1 -1
  158. package/dist/navigation/ts-call-edges.js.map +1 -1
  159. package/dist/resolution/cross-file.d.ts +46 -0
  160. package/dist/resolution/cross-file.d.ts.map +1 -1
  161. package/dist/resolution/cross-file.js +281 -13
  162. package/dist/resolution/cross-file.js.map +1 -1
  163. package/dist/stable-ids/files.d.ts +22 -0
  164. package/dist/stable-ids/files.d.ts.map +1 -0
  165. package/dist/stable-ids/files.js +28 -0
  166. package/dist/stable-ids/files.js.map +1 -0
  167. package/dist/stable-ids/hash.d.ts +16 -4
  168. package/dist/stable-ids/hash.d.ts.map +1 -1
  169. package/dist/stable-ids/hash.js +266 -80
  170. package/dist/stable-ids/hash.js.map +1 -1
  171. package/dist/stable-ids/index.d.ts +3 -2
  172. package/dist/stable-ids/index.d.ts.map +1 -1
  173. package/dist/stable-ids/index.js +3 -2
  174. package/dist/stable-ids/index.js.map +1 -1
  175. package/dist/stable-ids/nodes.d.ts +45 -1
  176. package/dist/stable-ids/nodes.d.ts.map +1 -1
  177. package/dist/stable-ids/nodes.js +118 -15
  178. package/dist/stable-ids/nodes.js.map +1 -1
  179. package/dist/stable-ids/ts-parse-errors.d.ts +5 -1
  180. package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
  181. package/dist/stable-ids/ts-parse-errors.js +35 -2
  182. package/dist/stable-ids/ts-parse-errors.js.map +1 -1
  183. package/package.json +8 -4
  184. package/schemas/facts@1.json +134 -0
  185. package/schemas/facts@2.json +216 -0
@@ -107,6 +107,7 @@ export class CrossFileResolver {
107
107
  typeHierarchy;
108
108
  // Cache: file -> IR
109
109
  fileIRs = new Map();
110
+ sourceLines;
110
111
  // Cache: method FQN -> taint info
111
112
  methodTaintInfo = new Map();
112
113
  // Cache: `${typeFqn}.${fieldName}` -> field taint info
@@ -122,6 +123,14 @@ export class CrossFileResolver {
122
123
  /**
123
124
  * Add a file's IR for analysis
124
125
  */
126
+ /**
127
+ * Raw source lines per file. Optional: the JS/TS parameter-to-sink check reads a
128
+ * def's right-hand side from them (the DFG carries none) to stop at literal-only
129
+ * values; without them the check still runs, only less precisely.
130
+ */
131
+ setSourceLines(lines) {
132
+ this.sourceLines = lines;
133
+ }
125
134
  addFile(filePath, ir) {
126
135
  this.fileIRs.set(filePath, ir);
127
136
  this.symbolTable.addFromIR(ir, filePath);
@@ -227,9 +236,25 @@ export class CrossFileResolver {
227
236
  const lang = this.fileIRs.get(fromFile)?.meta.language;
228
237
  if (receiverType ? isJdkType(receiverType, lang) : isJdkClassReceiver(receiver, lang))
229
238
  return undefined;
239
+ // JS/TS: `signer.getAddress()` on a receiver of unknown type is a method of
240
+ // that object, not a module-level function that happens to share the name
241
+ // (juice-shop: the ethers Signer resolved to routes/address.ts getAddress and
242
+ // made req.body a source in the frontend, #992). Only a receiver bound by an
243
+ // import (`import * as addr from './address'`) may reach a module function.
244
+ if (!receiverType && (lang === 'javascript' || lang === 'typescript')) {
245
+ const byName = this.resolveByMethodName(call, fromFile);
246
+ if (byName && byName.targetClass === '<module>' && !this.isImportBinding(fromFile, receiver))
247
+ return undefined;
248
+ return byName;
249
+ }
230
250
  // Fallback: search by method name
231
251
  return this.resolveByMethodName(call, fromFile);
232
252
  }
253
+ /** Is `name` bound by an import in `file` (alias, default or namespace import)? */
254
+ isImportBinding(file, name) {
255
+ const ir = this.fileIRs.get(file);
256
+ return (ir?.imports ?? []).some(i => (i.alias ?? i.imported_name) === name);
257
+ }
233
258
  /**
234
259
  * Resolve static or local method call
235
260
  */
@@ -598,13 +623,21 @@ export class CrossFileResolver {
598
623
  if (name)
599
624
  paramNameToIndex.set(name, i);
600
625
  }
626
+ const isJs = ir.meta.language === 'javascript' || ir.meta.language === 'typescript';
601
627
  for (const sink of ir.taint.sinks) {
602
628
  if (sink.line < method.start_line || sink.line > method.end_line)
603
629
  continue;
604
630
  // Find the call(s) at the sink line — match by line number.
605
- const callsAtSink = ir.calls.filter(c => c.location.line === sink.line);
631
+ // JS/TS (#1120): only the sink call itself, at its dangerous positions —
632
+ // in `fs.writeFileSync(file, JSON.stringify(data))` the parameter `data`
633
+ // is written, not used as a path.
634
+ const callsAtSink = ir.calls.filter(c => c.location.line === sink.line
635
+ && (!isJs || !sink.method || this.callMatchesSink(sink, c)));
606
636
  for (const call of callsAtSink) {
607
637
  for (const arg of call.arguments) {
638
+ if (isJs && sink.argPositions && sink.argPositions.length > 0
639
+ && !sink.argPositions.includes(arg.position))
640
+ continue;
608
641
  // Argument that's a direct variable reference.
609
642
  const candidates = [];
610
643
  if (arg.variable)
@@ -734,6 +767,12 @@ export class CrossFileResolver {
734
767
  for (const call of ir.calls) {
735
768
  if (call.location.line < source.line)
736
769
  continue;
770
+ // A source with no variable (`parse(fs.readFileSync(p))`) can reach
771
+ // only the call it is nested in, on its own line — not every later
772
+ // cross-file call in the file (dogfood 2026-10-31: one principles.md
773
+ // read in security-scan/scanner.ts reached readFileSync in 12 files).
774
+ if (!reachable && call.location.line !== source.line)
775
+ continue;
737
776
  const resolved = this.resolveCall(call, filePath);
738
777
  if (!resolved || resolved.targetFile === filePath)
739
778
  continue;
@@ -820,6 +859,15 @@ export class CrossFileResolver {
820
859
  if (sinksInMethod.length === 0)
821
860
  continue;
822
861
  for (const sink of sinksInMethod) {
862
+ // JS/TS: the carried parameter must reach the sink's own arguments, not just
863
+ // any line of the callee (dogfood 2026-11-01: a route value bound as a `?`
864
+ // parameter was credited to `db.prepare(<constant SQL>)` in the same method).
865
+ // Every parameter is tried, not only `term.positions`: the caller-side carried
866
+ // positions can miss the real carrier (juice-shop `downloadToFile(imageUrl, dest)`
867
+ // reported [1] only), and this check must not lose a flow on that.
868
+ if ((targetIR.meta.language === 'javascript' || targetIR.meta.language === 'typescript')
869
+ && !this.jsParamReachesSinkArgs(targetIR, targetMethod, targetMethod.parameters.map((_, i) => i), sink, term.file))
870
+ continue;
823
871
  const key = `${filePath}:${source.line}→${term.file}:${sink.line}`;
824
872
  if (seen.has(key))
825
873
  continue;
@@ -842,6 +890,68 @@ export class CrossFileResolver {
842
890
  }
843
891
  return flows;
844
892
  }
893
+ /**
894
+ * JS/TS: does a value in one of the parameters at `positions` of `method` reach the
895
+ * arguments of `sink`? Names derived from those parameters by def-use chains are matched
896
+ * against the sink call's dangerous arguments (`argPositions`, else all). Conservative:
897
+ * true when the sink call or the parameter defs cannot be found, so it only drops a
898
+ * flow whose sink call is present and mentions none of those names.
899
+ */
900
+ jsParamReachesSinkArgs(ir, method, positions, sink, file) {
901
+ const params = positions.map(i => method.parameters[i]?.name).filter((n) => !!n);
902
+ if (params.length === 0)
903
+ return true;
904
+ const calls = (this.getFileIndex(ir).callsByLine.get(sink.line) ?? [])
905
+ .filter(c => c.method_name === sink.method
906
+ && (sink.callColumn === undefined || c.location.column === undefined || c.location.column === sink.callColumn));
907
+ if (calls.length === 0)
908
+ return true;
909
+ const seeds = ir.dfg.defs
910
+ .filter(d => params.includes(d.variable) && d.line >= method.start_line && d.line <= method.end_line)
911
+ .map(d => d.id);
912
+ const names = new Set(params);
913
+ if (seeds.length > 0) {
914
+ // Def-use walk from the parameters that does not pass through a def whose value is
915
+ // literal-only (`const where = options.id ? 'WHERE id = ?' : ''`): the parameter
916
+ // decides which literal, but the value carries none of it.
917
+ const lines = this.sourceLines?.get(file);
918
+ const defById = new Map(ir.dfg.defs.map(d => [d.id, d]));
919
+ const visited = new Set(seeds);
920
+ const frontier = [...seeds];
921
+ while (frontier.length > 0) {
922
+ const cur = frontier.shift();
923
+ for (const ch of ir.dfg.chains ?? []) {
924
+ if (ch.from_def !== cur || visited.has(ch.to_def))
925
+ continue;
926
+ visited.add(ch.to_def);
927
+ const d = defById.get(ch.to_def);
928
+ if (!d || (lines && literalOnlyAssignment(lines[d.line - 1] ?? '', d.variable)))
929
+ continue;
930
+ if (d.variable)
931
+ names.add(d.variable);
932
+ frontier.push(ch.to_def);
933
+ }
934
+ }
935
+ }
936
+ const res = [...names].map(n => new RegExp(`(?<![\\w$.])${escapeRegExp(n)}(?![\\w$])`));
937
+ return calls.some(c => c.arguments
938
+ .filter(a => !sink.argPositions || sink.argPositions.length === 0 || sink.argPositions.includes(a.position))
939
+ .some(a => (a.variable && names.has(a.variable)) || res.some(re => re.test(a.expression ?? ''))));
940
+ }
941
+ /** Sink call on this line (method + optional column), same filter as `jsParamReachesSinkArgs`. */
942
+ callMatchesSink(sink, call) {
943
+ return call.method_name === sink.method
944
+ && (sink.callColumn === undefined
945
+ || call.location?.column === undefined
946
+ || call.location.column === sink.callColumn);
947
+ }
948
+ /** Arguments the sink marks dangerous (`argPositions`, else all). */
949
+ dangerousSinkArguments(sink, call) {
950
+ const args = call.arguments ?? [];
951
+ if (!sink.argPositions || sink.argPositions.length === 0)
952
+ return args;
953
+ return args.filter(a => sink.argPositions.includes(a.position));
954
+ }
845
955
  /**
846
956
  * C# (cognium-dev#637): the methods a value ends in after a chain of pure
847
957
  * forwarders. A callee that holds a sink is its own terminal. One that holds no
@@ -944,8 +1054,10 @@ export class CrossFileResolver {
944
1054
  continue;
945
1055
  // 2a. If callee returns a source and is NOT a sanitizer, mark every
946
1056
  // local def on this caller line as tainted (the assignment LHS).
947
- if (callee.returnsSource && !callee.sanitizes && callee.sourceType) {
948
- const calleeNode = methodIndex.get(resolved.targetMethod);
1057
+ const calleeNode0 = methodIndex.get(resolved.targetMethod);
1058
+ if (callee.returnsSource && !callee.sanitizes && callee.sourceType &&
1059
+ !(calleeNode0 && this.sourcesOnlySteerBranches(callee.file, calleeNode0.ir, calleeNode0.method))) {
1060
+ const calleeNode = calleeNode0;
949
1061
  const calleeSourceLine = calleeNode
950
1062
  ? this.findRealSourceLineInMethod(calleeNode.ir, calleeNode.method)
951
1063
  : undefined;
@@ -978,7 +1090,7 @@ export class CrossFileResolver {
978
1090
  if (!callee.taintedParams.includes(argIdx))
979
1091
  continue;
980
1092
  const arg = call.arguments[argIdx];
981
- const matched = this.matchTaintedArg(arg, tainted);
1093
+ const matched = this.matchTaintedArg(arg, tainted, callerIR.meta.language === 'javascript' || callerIR.meta.language === 'typescript');
982
1094
  if (!matched)
983
1095
  continue;
984
1096
  // #266: skip when the matched var's value was reassigned away
@@ -1036,11 +1148,13 @@ export class CrossFileResolver {
1036
1148
  // caller's file rather than in a cross-file callee.
1037
1149
  if (tainted.size > 0) {
1038
1150
  const sinksInCaller = callerIdx.sinksByMethod.get(method) ?? [];
1151
+ const isJs = callerIR.meta.language === 'javascript' || callerIR.meta.language === 'typescript';
1039
1152
  for (const sink of sinksInCaller) {
1040
- const callsAtSink = callerIdx.callsByLine.get(sink.line) ?? [];
1153
+ const callsAtSink = (callerIdx.callsByLine.get(sink.line) ?? [])
1154
+ .filter(c => this.callMatchesSink(sink, c));
1041
1155
  for (const sinkCall of callsAtSink) {
1042
- for (const arg of sinkCall.arguments ?? []) {
1043
- const matched = this.matchTaintedArg(arg, tainted);
1156
+ for (const arg of this.dangerousSinkArguments(sink, sinkCall)) {
1157
+ const matched = this.matchTaintedArg(arg, tainted, isJs);
1044
1158
  if (!matched)
1045
1159
  continue;
1046
1160
  const key = `${matched.origin.file}:${matched.origin.line}→${callerFile}:${sink.line}`;
@@ -1238,9 +1352,10 @@ export class CrossFileResolver {
1238
1352
  // 3a. Caller-body sinks consuming a tainted local.
1239
1353
  const sinksInCaller = callerIdx.sinksByMethod.get(method) ?? [];
1240
1354
  for (const sink of sinksInCaller) {
1241
- const callsAtSink = callerIdx.callsByLine.get(sink.line) ?? [];
1355
+ const callsAtSink = (callerIdx.callsByLine.get(sink.line) ?? [])
1356
+ .filter(c => this.callMatchesSink(sink, c));
1242
1357
  for (const sinkCall of callsAtSink) {
1243
- for (const arg of sinkCall.arguments ?? []) {
1358
+ for (const arg of this.dangerousSinkArguments(sink, sinkCall)) {
1244
1359
  const matched = this.matchTaintedArg(arg, tainted);
1245
1360
  if (!matched)
1246
1361
  continue;
@@ -1520,18 +1635,29 @@ export class CrossFileResolver {
1520
1635
  }
1521
1636
  }
1522
1637
  }
1523
- matchTaintedArg(arg, tainted) {
1638
+ matchTaintedArg(arg, tainted,
1639
+ /** JS/TS: a name after a single `.` is a property (`args.output`), not the tainted local `output` (a spread `...output` still is). */
1640
+ jsMember = false) {
1524
1641
  if (tainted.size === 0)
1525
1642
  return null;
1643
+ // JS/TS (#1137): `name.length` is a number, so an argument that uses the
1644
+ // tainted name only as `name.length` carries no taint across the call.
1645
+ const lengthOnly = (tv) => arg.expression?.replace(new RegExp(`(?<![\\w$.])${escapeRegExp(tv)}\\s*\\??\\.\\s*length(?![\\w$(\\[])`, 'g'), '0');
1526
1646
  // Direct variable reference
1527
1647
  if (arg.variable && tainted.has(arg.variable)) {
1528
- return { var: arg.variable, origin: tainted.get(arg.variable) };
1648
+ const rest = jsMember ? lengthOnly(arg.variable) : undefined;
1649
+ if (rest === undefined || rest === arg.expression
1650
+ || new RegExp(`(?<![\\w$])${escapeRegExp(arg.variable)}(?![\\w$])`).test(rest)) {
1651
+ return { var: arg.variable, origin: tainted.get(arg.variable) };
1652
+ }
1529
1653
  }
1530
1654
  // Whole-word scan inside the expression (handles `prefix + url`, `url.trim()`, etc.)
1531
1655
  if (arg.expression) {
1532
1656
  for (const [tv, origin] of tainted) {
1533
- const re = new RegExp(`\\b${escapeRegExp(tv)}\\b`);
1534
- if (re.test(arg.expression))
1657
+ const re = jsMember
1658
+ ? new RegExp(`(?<![\\w$])(?<!(?:^|[^.])\\.)${escapeRegExp(tv)}(?![\\w$])`)
1659
+ : new RegExp(`\\b${escapeRegExp(tv)}\\b`);
1660
+ if (re.test(jsMember ? lengthOnly(tv) : arg.expression))
1535
1661
  return { var: tv, origin };
1536
1662
  }
1537
1663
  }
@@ -1685,6 +1811,102 @@ export class CrossFileResolver {
1685
1811
  return false;
1686
1812
  return !taintDefIds.has(use.def_id);
1687
1813
  }
1814
+ /**
1815
+ * True when every real source inside `method` lands in a local that is only
1816
+ * ever read in a branch condition (`if (...)`, `while (...)`) after it is
1817
+ * assigned, so no source value can reach what the method returns (#914).
1818
+ *
1819
+ * OWASP `Utils.getOSCommandString(append)` reads
1820
+ * `String osName = System.getProperty("os.name");` only to choose between
1821
+ * `"cmd.exe /c " + append` and `append`. Without this, `returnsSource`
1822
+ * marks the method's return as `env_input` and every caller's
1823
+ * `exec(... getOSCommandString("ls ") ...)` became a command_injection path.
1824
+ *
1825
+ * Java / JS / TS when the source text is available; otherwise false
1826
+ * (the method keeps `returnsSource`).
1827
+ */
1828
+ sourcesOnlySteerBranches(file, ir, method) {
1829
+ const lang = ir.meta.language;
1830
+ if (lang !== 'java' && lang !== 'javascript' && lang !== 'typescript')
1831
+ return false;
1832
+ const lines = this.sourceLines?.get(file);
1833
+ if (!lines)
1834
+ return false;
1835
+ const real = ir.taint.sources.filter(s => s.type !== 'interprocedural_param' && s.line >= method.start_line && s.line <= method.end_line);
1836
+ if (real.length === 0)
1837
+ return false;
1838
+ for (const src of real) {
1839
+ if (lang === 'java') {
1840
+ if (!this.javaSourceOnlySteersBranches(lines, method, src))
1841
+ return false;
1842
+ }
1843
+ else if (!this.jsTsSourceOnlySteersBranches(lines, method, src)) {
1844
+ return false;
1845
+ }
1846
+ }
1847
+ return true;
1848
+ }
1849
+ /** #914 — local assigned from a source, then only read in branch conditions. */
1850
+ javaSourceOnlySteersBranches(lines, method, src) {
1851
+ const v = src.variable;
1852
+ if (!v || !/^[A-Za-z_$][\w$]*$/.test(v))
1853
+ return false;
1854
+ const decl = new RegExp(`^\\s*(?:final\\s+)?[\\w<>\\[\\].]+\\s+${v}\\s*=[^;]*;\\s*$`);
1855
+ if (!decl.test(stripJavaLiterals(lines[src.line - 1] ?? '')))
1856
+ return false;
1857
+ const mention = new RegExp(`(?<![\\w$.])${v.replace(/\$/g, '\\$')}(?![\\w$])`);
1858
+ for (let l = src.line + 1; l <= method.end_line; l++) {
1859
+ const text = withoutLeadingConditions(stripJavaLiterals(lines[l - 1] ?? ''));
1860
+ if (mention.test(text))
1861
+ return false;
1862
+ }
1863
+ return true;
1864
+ }
1865
+ /**
1866
+ * #1136 — JS/TS counterpart of #914: a read used only to steer a branch
1867
+ * (including inline `if (guard(read(...))) push(path)`) does not taint the
1868
+ * helper's return for cross-file chaining.
1869
+ */
1870
+ jsTsSourceOnlySteersBranches(lines, method, src) {
1871
+ const v = src.variable;
1872
+ if (v && /^[A-Za-z_$][\w$]*$/.test(v)) {
1873
+ const decl = new RegExp(`^\\s*(?:const|let|var)\\s+${v.replace(/\$/g, '\\$')}\\s*=`);
1874
+ if (decl.test(stripJavaLiterals(lines[src.line - 1] ?? ''))) {
1875
+ const mention = new RegExp(`(?<![\\w$.])${v.replace(/\$/g, '\\$')}(?![\\w$])`);
1876
+ for (let l = src.line + 1; l <= method.end_line; l++) {
1877
+ const text = withoutLeadingConditions(stripJavaLiterals(lines[l - 1] ?? ''));
1878
+ if (mention.test(text))
1879
+ return false;
1880
+ }
1881
+ return true;
1882
+ }
1883
+ }
1884
+ const anchor = this.jsTsBranchOnlyAnchor(lines, src);
1885
+ if (!anchor)
1886
+ return false;
1887
+ for (let l = method.start_line; l <= method.end_line; l++) {
1888
+ const text = stripJavaLiterals(lines[l - 1] ?? '');
1889
+ if (!text.includes(anchor))
1890
+ continue;
1891
+ const outsideCond = withoutLeadingConditions(text);
1892
+ if (outsideCond.includes(anchor))
1893
+ return false;
1894
+ }
1895
+ return true;
1896
+ }
1897
+ /** Stable substring for an inline read (e.g. `readFileSync`) on the source line. */
1898
+ jsTsBranchOnlyAnchor(lines, src) {
1899
+ const line = stripJavaLiterals(lines[src.line - 1] ?? '');
1900
+ const readCall = line.match(/\b(readFileSync|readFile|readSync)\s*\(/);
1901
+ if (readCall)
1902
+ return readCall[1];
1903
+ if (src.location) {
1904
+ const m = src.location.match(/\b(readFileSync|readFile|readSync)\b/);
1905
+ if (m)
1906
+ return m[1];
1907
+ }
1908
+ return undefined;
1909
+ }
1688
1910
  findRealSourceLineInMethod(ir, method) {
1689
1911
  for (const src of ir.taint.sources) {
1690
1912
  if (src.type === 'interprocedural_param')
@@ -1754,6 +1976,41 @@ export class CrossFileResolver {
1754
1976
  return this.fieldTaintInfo.get(`${typeFqn}.${fieldName}`);
1755
1977
  }
1756
1978
  }
1979
+ /** A Java line with comments and string / char literals blanked. */
1980
+ function stripJavaLiterals(text) {
1981
+ return text
1982
+ .replace(/"(?:[^"\\]|\\.)*"/g, '""')
1983
+ .replace(/'(?:[^'\\]|\\.)'/g, "''")
1984
+ .replace(/\/\/.*$/, '');
1985
+ }
1986
+ /**
1987
+ * The line with the parenthesised condition of every `if` / `else if` /
1988
+ * `while` it opens removed, so what remains is what the line does with
1989
+ * data. An unbalanced condition (it wraps to the next line) is left in
1990
+ * place, which only makes the caller more conservative.
1991
+ */
1992
+ function withoutLeadingConditions(text) {
1993
+ let out = text;
1994
+ for (;;) {
1995
+ const m = /\b(?:if|while)\s*\(/.exec(out);
1996
+ if (!m)
1997
+ return out;
1998
+ const open = m.index + m[0].length - 1;
1999
+ let depth = 0;
2000
+ let close = -1;
2001
+ for (let i = open; i < out.length; i++) {
2002
+ if (out[i] === '(')
2003
+ depth++;
2004
+ else if (out[i] === ')' && --depth === 0) {
2005
+ close = i;
2006
+ break;
2007
+ }
2008
+ }
2009
+ if (close < 0)
2010
+ return out;
2011
+ out = out.slice(0, m.index) + out.slice(close + 1);
2012
+ }
2013
+ }
1757
2014
  /**
1758
2015
  * Build a cross-file resolver from multiple IR results
1759
2016
  */
@@ -1783,4 +2040,15 @@ function isJdkClassReceiver(receiver, language) {
1783
2040
  return true;
1784
2041
  return JAVA_LANG_CLASSES.has(receiver);
1785
2042
  }
2043
+ // A JS literal: quoted string, a template literal without `${…}`, number, boolean, null, undefined.
2044
+ const JS_LITERAL = '(?:\'(?:[^\'\\\\]|\\\\.)*\'|"(?:[^"\\\\]|\\\\.)*"|`[^`$\\\\]*`|-?\\d+(?:\\.\\d+)?|true|false|null|undefined)';
2045
+ const LITERAL_RHS = new RegExp(`^(?:${JS_LITERAL}|[^?;]+\\?\\s*${JS_LITERAL}\\s*:\\s*${JS_LITERAL})$`);
2046
+ /**
2047
+ * True when the line assigns `variable` a literal-only value: a literal, or a
2048
+ * conditional whose two branches are literals (`x = cond ? 'a' : ''`).
2049
+ */
2050
+ function literalOnlyAssignment(line, variable) {
2051
+ const m = new RegExp(`(?:^|[^\\w$.])${escapeRegExp(variable)}\\s*(?::[^=]+)?=(?!=)\\s*(.+?);?\\s*$`).exec(line);
2052
+ return m !== null && LITERAL_RHS.test(m[1].trim());
2053
+ }
1786
2054
  //# sourceMappingURL=cross-file.js.map