circle-ir 4.20.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 (170) hide show
  1. package/dist/analysis/config-loader.d.ts.map +1 -1
  2. package/dist/analysis/config-loader.js +17 -2
  3. package/dist/analysis/config-loader.js.map +1 -1
  4. package/dist/analysis/constant-propagation/patterns.d.ts.map +1 -1
  5. package/dist/analysis/constant-propagation/patterns.js +3 -0
  6. package/dist/analysis/constant-propagation/patterns.js.map +1 -1
  7. package/dist/analysis/constant-propagation/propagator.d.ts +46 -1
  8. package/dist/analysis/constant-propagation/propagator.d.ts.map +1 -1
  9. package/dist/analysis/constant-propagation/propagator.js +407 -56
  10. package/dist/analysis/constant-propagation/propagator.js.map +1 -1
  11. package/dist/analysis/findings.d.ts.map +1 -1
  12. package/dist/analysis/findings.js +7 -5
  13. package/dist/analysis/findings.js.map +1 -1
  14. package/dist/analysis/interprocedural.d.ts +16 -0
  15. package/dist/analysis/interprocedural.d.ts.map +1 -1
  16. package/dist/analysis/interprocedural.js +545 -197
  17. package/dist/analysis/interprocedural.js.map +1 -1
  18. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts +58 -0
  19. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts.map +1 -0
  20. package/dist/analysis/passes/csrf-samesite-none-session-pass.js +159 -0
  21. package/dist/analysis/passes/csrf-samesite-none-session-pass.js.map +1 -0
  22. package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
  23. package/dist/analysis/passes/interprocedural-pass.js +83 -10
  24. package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
  25. package/dist/analysis/passes/language-sources-pass.d.ts +0 -18
  26. package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
  27. package/dist/analysis/passes/language-sources-pass.js +439 -16
  28. package/dist/analysis/passes/language-sources-pass.js.map +1 -1
  29. package/dist/analysis/passes/param-source-flow-gate-pass.d.ts.map +1 -1
  30. package/dist/analysis/passes/param-source-flow-gate-pass.js +71 -1
  31. package/dist/analysis/passes/param-source-flow-gate-pass.js.map +1 -1
  32. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js +1 -1
  33. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js.map +1 -1
  34. package/dist/analysis/passes/scan-secrets-pass.js +2 -2
  35. package/dist/analysis/passes/scan-secrets-pass.js.map +1 -1
  36. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts +66 -0
  37. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts.map +1 -0
  38. package/dist/analysis/passes/sensitive-data-in-log-pass.js +462 -0
  39. package/dist/analysis/passes/sensitive-data-in-log-pass.js.map +1 -0
  40. package/dist/analysis/passes/sink-filter-pass.d.ts +7 -1
  41. package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
  42. package/dist/analysis/passes/sink-filter-pass.js +372 -14
  43. package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
  44. package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
  45. package/dist/analysis/passes/taint-propagation-pass.js +289 -13
  46. package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
  47. package/dist/analysis/project-callee-escape-gate.js +1 -1
  48. package/dist/analysis/project-callee-escape-gate.js.map +1 -1
  49. package/dist/analysis/rules.d.ts.map +1 -1
  50. package/dist/analysis/rules.js +9 -0
  51. package/dist/analysis/rules.js.map +1 -1
  52. package/dist/analysis/sbom.d.ts +49 -0
  53. package/dist/analysis/sbom.d.ts.map +1 -1
  54. package/dist/analysis/sbom.js +126 -1
  55. package/dist/analysis/sbom.js.map +1 -1
  56. package/dist/analysis/taint-matcher.d.ts.map +1 -1
  57. package/dist/analysis/taint-matcher.js +85 -16
  58. package/dist/analysis/taint-matcher.js.map +1 -1
  59. package/dist/analysis/taint-propagation.d.ts +1 -0
  60. package/dist/analysis/taint-propagation.d.ts.map +1 -1
  61. package/dist/analysis/taint-propagation.js +32 -0
  62. package/dist/analysis/taint-propagation.js.map +1 -1
  63. package/dist/analysis/test-to-production-taint-gate.d.ts +13 -0
  64. package/dist/analysis/test-to-production-taint-gate.d.ts.map +1 -0
  65. package/dist/analysis/test-to-production-taint-gate.js +30 -0
  66. package/dist/analysis/test-to-production-taint-gate.js.map +1 -0
  67. package/dist/analyzer.d.ts +15 -3
  68. package/dist/analyzer.d.ts.map +1 -1
  69. package/dist/analyzer.js +56 -2
  70. package/dist/analyzer.js.map +1 -1
  71. package/dist/browser/circle-ir.js +4972 -2470
  72. package/dist/core/circle-ir-core.cjs +2969 -2223
  73. package/dist/core/circle-ir-core.js +2969 -2219
  74. package/dist/core/extractors/calls.d.ts +10 -1
  75. package/dist/core/extractors/calls.d.ts.map +1 -1
  76. package/dist/core/extractors/calls.js +148 -6
  77. package/dist/core/extractors/calls.js.map +1 -1
  78. package/dist/core/extractors/cfg.d.ts.map +1 -1
  79. package/dist/core/extractors/cfg.js +12 -0
  80. package/dist/core/extractors/cfg.js.map +1 -1
  81. package/dist/core/extractors/dfg.d.ts.map +1 -1
  82. package/dist/core/extractors/dfg.js +151 -18
  83. package/dist/core/extractors/dfg.js.map +1 -1
  84. package/dist/core/extractors/types.d.ts +5 -0
  85. package/dist/core/extractors/types.d.ts.map +1 -1
  86. package/dist/core/extractors/types.js +149 -4
  87. package/dist/core/extractors/types.js.map +1 -1
  88. package/dist/core/parser.d.ts.map +1 -1
  89. package/dist/core/parser.js +3 -1
  90. package/dist/core/parser.js.map +1 -1
  91. package/dist/core/ts-grammar-workarounds.d.ts +18 -0
  92. package/dist/core/ts-grammar-workarounds.d.ts.map +1 -0
  93. package/dist/core/ts-grammar-workarounds.js +40 -0
  94. package/dist/core/ts-grammar-workarounds.js.map +1 -0
  95. package/dist/facts/facts2.d.ts +12 -3
  96. package/dist/facts/facts2.d.ts.map +1 -1
  97. package/dist/facts/facts2.js +24 -7
  98. package/dist/facts/facts2.js.map +1 -1
  99. package/dist/facts/index.d.ts +3 -1
  100. package/dist/facts/index.d.ts.map +1 -1
  101. package/dist/facts/index.js +2 -1
  102. package/dist/facts/index.js.map +1 -1
  103. package/dist/facts/schema2.d.ts +3 -3
  104. package/dist/facts/schema2.js +3 -3
  105. package/dist/facts/t6.d.ts +84 -0
  106. package/dist/facts/t6.d.ts.map +1 -0
  107. package/dist/facts/t6.js +226 -0
  108. package/dist/facts/t6.js.map +1 -0
  109. package/dist/graph-facts/dead-code.d.ts.map +1 -1
  110. package/dist/graph-facts/dead-code.js +3 -2
  111. package/dist/graph-facts/dead-code.js.map +1 -1
  112. package/dist/graph-facts/index.d.ts +1 -1
  113. package/dist/graph-facts/index.d.ts.map +1 -1
  114. package/dist/graph-facts/index.js +1 -1
  115. package/dist/graph-facts/index.js.map +1 -1
  116. package/dist/graph-facts/reach.d.ts +31 -4
  117. package/dist/graph-facts/reach.d.ts.map +1 -1
  118. package/dist/graph-facts/reach.js +120 -4
  119. package/dist/graph-facts/reach.js.map +1 -1
  120. package/dist/index.d.ts +8 -6
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.js +11 -7
  123. package/dist/index.js.map +1 -1
  124. package/dist/navigation/index.d.ts +1 -0
  125. package/dist/navigation/index.d.ts.map +1 -1
  126. package/dist/navigation/index.js +1 -0
  127. package/dist/navigation/index.js.map +1 -1
  128. package/dist/navigation/java-graph.d.ts +31 -9
  129. package/dist/navigation/java-graph.d.ts.map +1 -1
  130. package/dist/navigation/java-graph.js +174 -11
  131. package/dist/navigation/java-graph.js.map +1 -1
  132. package/dist/navigation/java-registrations.d.ts +93 -0
  133. package/dist/navigation/java-registrations.d.ts.map +1 -0
  134. package/dist/navigation/java-registrations.js +254 -0
  135. package/dist/navigation/java-registrations.js.map +1 -0
  136. package/dist/navigation/navigation-index.d.ts +65 -3
  137. package/dist/navigation/navigation-index.d.ts.map +1 -1
  138. package/dist/navigation/navigation-index.js +181 -11
  139. package/dist/navigation/navigation-index.js.map +1 -1
  140. package/dist/navigation/ts-call-edges.d.ts +6 -0
  141. package/dist/navigation/ts-call-edges.d.ts.map +1 -1
  142. package/dist/navigation/ts-call-edges.js.map +1 -1
  143. package/dist/navigation/ts-entry-tables.d.ts +1 -1
  144. package/dist/navigation/ts-entry-tables.d.ts.map +1 -1
  145. package/dist/navigation/ts-entry-tables.js.map +1 -1
  146. package/dist/navigation/xml-scan.d.ts +40 -0
  147. package/dist/navigation/xml-scan.d.ts.map +1 -0
  148. package/dist/navigation/xml-scan.js +269 -0
  149. package/dist/navigation/xml-scan.js.map +1 -0
  150. package/dist/stable-ids/index.d.ts +3 -3
  151. package/dist/stable-ids/index.d.ts.map +1 -1
  152. package/dist/stable-ids/index.js +16 -4
  153. package/dist/stable-ids/index.js.map +1 -1
  154. package/dist/stable-ids/nodes.d.ts +16 -6
  155. package/dist/stable-ids/nodes.d.ts.map +1 -1
  156. package/dist/stable-ids/nodes.js +17 -7
  157. package/dist/stable-ids/nodes.js.map +1 -1
  158. package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
  159. package/dist/stable-ids/ts-parse-errors.js +21 -2
  160. package/dist/stable-ids/ts-parse-errors.js.map +1 -1
  161. package/dist/stable-ids/typescript.d.ts +5 -0
  162. package/dist/stable-ids/typescript.d.ts.map +1 -1
  163. package/dist/stable-ids/typescript.js +41 -8
  164. package/dist/stable-ids/typescript.js.map +1 -1
  165. package/dist/types/index.d.ts +21 -3
  166. package/dist/types/index.d.ts.map +1 -1
  167. package/dist/types/index.js +1 -0
  168. package/dist/types/index.js.map +1 -1
  169. package/docs/SPEC.md +9 -0
  170. package/package.json +3 -3
@@ -21,7 +21,7 @@ import { JS_TAINTED_PATTERNS } from './language-sources-pass.js';
21
21
  import { LIBRARY_API_SURFACE_TAG } from '../library-api-surface-downgrade.js';
22
22
  import { isNonExecutableSourceLine } from '../non-executable-lines.js';
23
23
  import { sanitizerCoversSink } from '../sanitizer-index.js';
24
- import { identifierOutsideSanitizerCalls } from '../taint-propagation.js';
24
+ import { identifierOutsideSanitizerCalls, identifierOutsideSanitizerCallsInConcatOperand, } from '../taint-propagation.js';
25
25
  import { parseJavaArrayAccessExpression } from '../java-array-element-keys.js';
26
26
  import { resolveIdentifierType, resolveCallReturnType, isArgvContainerType, } from '../arg-type-resolver.js';
27
27
  /** Escape regex metacharacters for safe pattern building. */
@@ -47,7 +47,9 @@ const JS_XSS_SANITIZERS = [
47
47
  /\bencodeURI\s*\(/,
48
48
  /\bcreateSafeHTML\s*\(/,
49
49
  /\btrustAsHtml\s*\(/, // Angular
50
- /\bbypassSecurityTrust/, // Angular
50
+ // Not `bypassSecurityTrust*` (#1171): it switches Angular's sanitizer off and is
51
+ // registered as the xss sink itself (config-loader, #184), so listing it here
52
+ // filtered every such sink out on its own line.
51
53
  ];
52
54
  // ---------------------------------------------------------------------------
53
55
  // Stage 16 — JS log_injection (CWE-117) sanitizer patterns.
@@ -726,9 +728,13 @@ export function synthesizeCrossFileArgvWrapperFlows(fileAnalyses, projectGraph,
726
728
  continue;
727
729
  const wrapperMethod = ctorCall.in_method;
728
730
  const t = resolveJavaArgvIdentifierType(varName, wrapperMethod, sinkLine, ir.types, projectGraph);
729
- if (!isArgvContainerType(t))
731
+ const assembledLocal = wrapperMethod != null
732
+ && javaAssembledArgvLocalFromInstanceFields(wrapperMethod, sinkLine, ir.types, lines);
733
+ const typedArgv = isArgvContainerType(t);
734
+ if (!typedArgv && !assembledLocal)
730
735
  continue;
731
- if (sameFileCallerPassesShellArgv(varName, wrapperMethod, sinkLine, ir.types, ir.calls, lines)) {
736
+ if (typedArgv
737
+ && sameFileCallerPassesShellArgv(varName, wrapperMethod, sinkLine, ir.types, ir.calls, lines)) {
732
738
  continue;
733
739
  }
734
740
  const calleeMethod = ir.types
@@ -741,7 +747,7 @@ export function synthesizeCrossFileArgvWrapperFlows(fileAnalyses, projectGraph,
741
747
  const fieldArgv = pos < 0
742
748
  && wrapperMethod
743
749
  && javaFieldArgvAtSink(projectGraph, ir.types, varName, wrapperMethod, sinkLine);
744
- if (pos < 0 && !fieldArgv)
750
+ if (!assembledLocal && pos < 0 && !fieldArgv)
745
751
  continue;
746
752
  const relatedCalleeFiles = javaRelatedCalleeFiles(projectGraph, fa.file, hostType);
747
753
  const shellArgvCtx = { projectGraph, sourceLinesByFile };
@@ -752,7 +758,17 @@ export function synthesizeCrossFileArgvWrapperFlows(fileAnalyses, projectGraph,
752
758
  return;
753
759
  const setterName = `set${varName.charAt(0).toUpperCase()}${varName.slice(1)}`;
754
760
  let shellProof = false;
755
- if (pos >= 0) {
761
+ if (assembledLocal && hostType) {
762
+ if (!wrapperMethod || !wrapperMethodMatchesTarget(wrapperMethod, targetMethod))
763
+ return;
764
+ const callerMethod = ctx.types
765
+ .flatMap(ty => ty.methods)
766
+ .find(mm => call.location.line >= mm.start_line && call.location.line <= mm.end_line);
767
+ if (!callerMethod)
768
+ return;
769
+ shellProof = javaCallerReceiverConstructedWithShell(call, hostType, callerMethod.start_line, callerMethod.end_line, ctx.lines, ctx.types, shellArgvCtx);
770
+ }
771
+ else if (pos >= 0) {
756
772
  if (!wrapperMethod || !wrapperMethodMatchesTarget(wrapperMethod, targetMethod))
757
773
  return;
758
774
  if (call.arguments.length <= pos)
@@ -850,6 +866,66 @@ export function synthesizeCrossFileArgvWrapperFlows(fileAnalyses, projectGraph,
850
866
  }
851
867
  }
852
868
  }
869
+ /** cognium-dev#1199 — local `argv` list built from instance fields before ProcessBuilder. */
870
+ function javaAssembledArgvLocalFromInstanceFields(wrapperMethod, sinkLine, types, sourceLines) {
871
+ const method = types
872
+ .flatMap(t => t.methods)
873
+ .find(mm => mm.name === wrapperMethod && sinkLine >= mm.start_line && sinkLine <= mm.end_line);
874
+ if (!method)
875
+ return false;
876
+ let sawAddExec = false;
877
+ let sawAddAll = false;
878
+ const addExecRe = /\bargv\s*\.\s*add\s*\(\s*(?:this\.)?\w+\s*\)/;
879
+ const addAllRe = /\bargv\s*\.\s*addAll\s*\(\s*(?:this\.)?\w+\s*\)/;
880
+ for (let l = method.start_line; l < sinkLine && l <= method.end_line; l++) {
881
+ const line = sourceLines[l - 1] ?? '';
882
+ if (addExecRe.test(line))
883
+ sawAddExec = true;
884
+ if (addAllRe.test(line))
885
+ sawAddAll = true;
886
+ }
887
+ return sawAddExec && sawAddAll;
888
+ }
889
+ function javaIsConstructorCallToType(targetMethod, call, hostTypeName) {
890
+ if (targetMethod.includes('<init>') && targetMethod.includes(hostTypeName))
891
+ return true;
892
+ return call.method_name === hostTypeName && (call.receiver === null || call.receiver === undefined);
893
+ }
894
+ /** cognium-dev#1199 — `recv.execute()` only counts when `recv` was `new HostType("sh", …)` above. */
895
+ function javaCallerReceiverConstructedWithShell(call, hostTypeName, methodStart, methodEnd, sourceLines, types, shellArgvCtx) {
896
+ const recv = call.receiver?.trim();
897
+ if (!recv || recv === 'this')
898
+ return false;
899
+ const ctorRe = new RegExp(`\\b${escapeReSf(hostTypeName)}\\s+${escapeReSf(recv)}\\s*=\\s*new\\s+${escapeReSf(hostTypeName)}\\s*\\(\\s*([^)]+)\\)`);
900
+ for (let l = methodStart; l < call.location.line && l <= methodEnd; l++) {
901
+ const line = (sourceLines[l - 1] ?? '').trim();
902
+ const m = ctorRe.exec(line);
903
+ if (m
904
+ && javaExpressionLooksLikeShellArgv(m[1].trim(), l, types, sourceLines, shellArgvCtx)) {
905
+ return true;
906
+ }
907
+ }
908
+ return false;
909
+ }
910
+ function projectCallerPassesShellForAssembledArgv(hostTypeName, wrapperMethod, getCallerContext, forEachCrossFileCall, shellArgvCtx) {
911
+ let found = false;
912
+ forEachCrossFileCall((callerFile, call, targetMethod) => {
913
+ if (!wrapperMethod || !wrapperMethodMatchesTarget(wrapperMethod, targetMethod))
914
+ return;
915
+ const ctx = getCallerContext(callerFile);
916
+ if (!ctx)
917
+ return;
918
+ const callerMethod = ctx.types
919
+ .flatMap(t => t.methods)
920
+ .find(mm => call.location.line >= mm.start_line && call.location.line <= mm.end_line);
921
+ if (!callerMethod)
922
+ return;
923
+ if (javaCallerReceiverConstructedWithShell(call, hostTypeName, callerMethod.start_line, callerMethod.end_line, ctx.lines, ctx.types, shellArgvCtx)) {
924
+ found = true;
925
+ }
926
+ });
927
+ return found;
928
+ }
853
929
  /** cognium-dev#626 — drop argv-wrapper flows with no shell-argv caller (incl. cross-file). */
854
930
  export function filterJavaArgvWrapperFlowsWithoutShellCaller(fileAnalyses, projectGraph, sourceLinesByFile) {
855
931
  const getCallerContext = (file) => {
@@ -898,12 +974,23 @@ export function filterJavaArgvWrapperFlowsWithoutShellCaller(fileAnalyses, proje
898
974
  if (!varName || argExpr !== varName || /[(.]/.test(argExpr))
899
975
  return true;
900
976
  const t = resolveJavaArgvIdentifierType(varName, ctorCall.in_method, flow.sink_line, ir.types, projectGraph);
901
- if (!isArgvContainerType(t))
902
- return true;
903
977
  const hostType = ctorCall.in_method
904
978
  ? javaHostTypeAtSink(ir.types, ctorCall.in_method, flow.sink_line)
905
979
  : undefined;
906
980
  const relatedCalleeFiles = javaRelatedCalleeFiles(projectGraph, fa.file, hostType);
981
+ if (!isArgvContainerType(t)) {
982
+ if (ctorCall.in_method
983
+ && hostType
984
+ && javaAssembledArgvLocalFromInstanceFields(ctorCall.in_method, flow.sink_line, ir.types, lines)) {
985
+ const shellArgvCtx = { projectGraph, sourceLinesByFile };
986
+ return projectCallerPassesShellForAssembledArgv(hostType, ctorCall.in_method ?? '', getCallerContext, cb => {
987
+ for (const calleeFile of relatedCalleeFiles) {
988
+ forEachCrossFileCall(calleeFile, cb);
989
+ }
990
+ }, shellArgvCtx);
991
+ }
992
+ return true;
993
+ }
907
994
  return projectCallerPassesShellArgvForWrapper(varName, ctorCall.in_method, flow.sink_line, ir.types, ir.calls, lines, projectGraph, sourceLinesByFile, getCallerContext, cb => {
908
995
  for (const calleeFile of relatedCalleeFiles) {
909
996
  forEachCrossFileCall(calleeFile, cb);
@@ -1245,7 +1332,7 @@ function regExpTemplateArgument(code, lines, line) {
1245
1332
  let offset = 0;
1246
1333
  for (let i = 0; i < line - 1; i++)
1247
1334
  offset += lines[i].length + 1;
1248
- const argument = /^(?:\s|\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*`((?:\\.|[^`\\])*)`(?=\s*[,)])/;
1335
+ const argument = /^(?:\s|\/\/[^\n]*(?:\n|$)|\/\*(?:[^*]|\*(?!\/))*\*\/)*`((?:\\.|[^`\\])*)`(?=\s*[,)])/;
1249
1336
  for (const start of text.matchAll(/new\s+RegExp\s*\(/g)) {
1250
1337
  // The literal must be the whole first argument: `` `…` + text `` is not constant.
1251
1338
  const m = argument.exec(code.slice(offset + start.index + start[0].length));
@@ -1477,6 +1564,54 @@ function jsSsrfHostGuardedLines(code) {
1477
1564
  }
1478
1565
  return covered;
1479
1566
  }
1567
+ /**
1568
+ * True when a libxmljs `parseXml` / `parseXmlString` call at `sinkLine` has
1569
+ * disabled external entities: `noent: false`, an inline options object without
1570
+ * `noent: true`, or a single-argument call (default `noent: false`).
1571
+ * Dynamic options (`parseXml(buf, opts)`) → false (unknown; keep the sink).
1572
+ * (cognium-dev#1222)
1573
+ */
1574
+ function jsLibxmljsNoentDisablesXxe(code, sinkLine) {
1575
+ const lines = code.split('\n');
1576
+ const start = Math.max(0, sinkLine - 2);
1577
+ const end = Math.min(lines.length, sinkLine + 4);
1578
+ const window = lines.slice(start, end).join('\n');
1579
+ if (!/\blibxml(?:js)?\s*\.\s*parseXml(?:String)?\s*\(/.test(window))
1580
+ return false;
1581
+ if (/\bnoent\s*:\s*true\b/.test(window))
1582
+ return false;
1583
+ const callIdx = window.search(/\bparseXml(?:String)?\s*\(/);
1584
+ if (callIdx < 0)
1585
+ return false;
1586
+ const afterCall = window.slice(callIdx);
1587
+ const openParen = afterCall.indexOf('(');
1588
+ if (openParen < 0)
1589
+ return false;
1590
+ let depth = 0;
1591
+ let closeIdx = -1;
1592
+ for (let i = openParen; i < afterCall.length; i++) {
1593
+ const ch = afterCall[i];
1594
+ if (ch === '(')
1595
+ depth++;
1596
+ else if (ch === ')') {
1597
+ depth--;
1598
+ if (depth === 0) {
1599
+ closeIdx = i;
1600
+ break;
1601
+ }
1602
+ }
1603
+ }
1604
+ if (closeIdx < 0)
1605
+ return false;
1606
+ const inner = afterCall.slice(openParen + 1, closeIdx);
1607
+ const commaIdx = inner.indexOf(',');
1608
+ if (commaIdx < 0)
1609
+ return true;
1610
+ const secondArg = inner.slice(commaIdx + 1).trim();
1611
+ if (/^[A-Za-z_$][\w$]*$/.test(secondArg))
1612
+ return false;
1613
+ return secondArg.startsWith('{');
1614
+ }
1480
1615
  /**
1481
1616
  * Is this `plugin_param` source a map read that provably yields `null` or a
1482
1617
  * string literal — a key that was never written, or any key of a literal-only
@@ -2138,6 +2273,41 @@ function isSameOriginQueryPrefix(prefix) {
2138
2273
  return false; // protocol-relative
2139
2274
  return prefix.includes('?'); // taint lands in the query
2140
2275
  }
2276
+ /**
2277
+ * The values a JS sink call reads: each dotted name (`target`, `req.query.next`)
2278
+ * in the argument list of `method(` on the sink line, outside string literals,
2279
+ * and not itself called. Empty when the call is not found on the line.
2280
+ */
2281
+ export function jsSinkArgumentValues(lineText, method) {
2282
+ if (!method)
2283
+ return [];
2284
+ const m = new RegExp(`\\b${method.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*\\(`).exec(lineText);
2285
+ if (!m)
2286
+ return [];
2287
+ let depth = 1, i = m.index + m[0].length;
2288
+ const start = i;
2289
+ for (; i < lineText.length && depth > 0; i++) {
2290
+ const ch = lineText[i];
2291
+ if (ch === '(')
2292
+ depth++;
2293
+ else if (ch === ')')
2294
+ depth--;
2295
+ }
2296
+ const args = lineText.slice(start, depth === 0 ? i - 1 : i)
2297
+ .replace(/(["'`])(?:\\.|(?!\1)[^\\])*\1/g, '""');
2298
+ const out = new Set();
2299
+ const re = /(?<![\w$.])([A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)*)(?![\w$])(?!\s*\()/g;
2300
+ for (let r = re.exec(args); r; r = re.exec(args)) {
2301
+ const name = r[1].replace(/\s+/g, '');
2302
+ if (!/^(?:true|false|null|undefined|this|new|typeof|await)$/.test(name))
2303
+ out.add(name);
2304
+ }
2305
+ return [...out];
2306
+ }
2307
+ /** True if a guard line mentions one of the sink's values as a whole name. */
2308
+ function guardTestsValue(line, values) {
2309
+ return values.some(v => new RegExp(`(?<![\\w$.])${v.replace(/[.$]/g, '\\$&')}(?![\\w$])`).test(line));
2310
+ }
2141
2311
  export class SinkFilterPass {
2142
2312
  name = 'sink-filter';
2143
2313
  category = 'security';
@@ -2218,7 +2388,7 @@ export class SinkFilterPass {
2218
2388
  // Stage 2 — clean array elements
2219
2389
  filtered = filterCleanArraySinks(filtered, calls, constProp.taintedArrayElements, constProp.symbols);
2220
2390
  // Stage 3 — clean variables
2221
- filtered = filterCleanVariableSinks(filtered, calls, constProp.tainted, constProp.symbols, dfg, constProp.sanitizedVars, constProp.synchronizedLines, language, constProp.sanitizedVarUncovered);
2391
+ filtered = filterCleanVariableSinks(filtered, calls, constProp.tainted, constProp.symbols, dfg, constProp.sanitizedVars, constProp.synchronizedLines, language, constProp.sanitizedVarUncovered, sanitizers);
2222
2392
  // Stage 4 — sanitized sinks. A sanitizer on one operand does not drop
2223
2393
  // the sink while another source operand is still raw (#641).
2224
2394
  filtered = filterSanitizedSinks(filtered, sanitizers, calls, sources);
@@ -2397,6 +2567,12 @@ export class SinkFilterPass {
2397
2567
  // Default: keep the sink (conservative when no taint info available)
2398
2568
  return true;
2399
2569
  });
2570
+ // Stage 7b — JS/TS libxmljs xxe: hardened `noent` drops the taint sink (#1222).
2571
+ filtered = filtered.filter(sink => {
2572
+ if (sink.type !== 'xxe')
2573
+ return true;
2574
+ return !jsLibxmljsNoentDisablesXxe(ctx.code, sink.line);
2575
+ });
2400
2576
  }
2401
2577
  // Stage 8 — JavaScript open_redirect / crlf / header_injection FP reduction.
2402
2578
  // Suppresses res.redirect(url) and res.setHeader(...) sinks when an
@@ -2413,10 +2589,16 @@ export class SinkFilterPass {
2413
2589
  }
2414
2590
  const sinkLineText = sourceLines[sink.line - 1] ?? '';
2415
2591
  // 8a. Conditional-allowlist guard: if (allowed.includes(x)) res.redirect(x);
2592
+ // The guard must test a value the sink uses: a check on an unrelated
2593
+ // variable (`if (name.includes(' '))`) no longer drops the sink. When the
2594
+ // sink's argument names no value (the call is not found on the line),
2595
+ // any guard in the window still drops it, as before.
2596
+ const sinkValues = jsSinkArgumentValues(sinkLineText, sink.method);
2416
2597
  const startLine = Math.max(0, sink.line - 7);
2417
2598
  for (let i = startLine; i < sink.line - 1; i++) {
2418
2599
  const line = sourceLines[i] ?? '';
2419
- if (/\bif\s*\(/.test(line) && guardPatterns.test(line)) {
2600
+ if (/\bif\s*\(/.test(line) && guardPatterns.test(line)
2601
+ && (sinkValues.length === 0 || guardTestsValue(line, sinkValues))) {
2420
2602
  return false;
2421
2603
  }
2422
2604
  }
@@ -3275,6 +3457,25 @@ export class SinkFilterPass {
3275
3457
  return !ORM_BUILDER_KEY_RE.test(sinkLineText);
3276
3458
  });
3277
3459
  }
3460
+ // Stage 15h2 — JS/TS nosql: string type reject-guard. (cognium-dev#1172)
3461
+ // `if (typeof req.body.id !== 'string') { …; return }` before
3462
+ // `coll.update({ _id: req.body.id }, …)` rules out an operator object
3463
+ // (`{ $ne: … }`), the NoSQL injection itself. Drop the sink only when every
3464
+ // request-derived reference in the filter argument is guarded that way.
3465
+ if (['javascript', 'typescript'].includes(language)) {
3466
+ const sourceLines = ctx.code.split('\n');
3467
+ filtered = filtered.filter(sink => {
3468
+ if (sink.type !== 'nosql_injection' || !sink.method)
3469
+ return true;
3470
+ const arg0 = jsCallFirstArgText(sourceLines, sink.line, sink.method);
3471
+ if (arg0 === null)
3472
+ return true;
3473
+ const refs = [...new Set([...arg0.matchAll(/\b(?:req|request)(?:\.[A-Za-z_$][\w$]*)+/g)].map(m => m[0]))];
3474
+ if (refs.length === 0)
3475
+ return true;
3476
+ return !refs.every(ref => jsTypeofStringRejectGuarded(sourceLines, sink.line, ref));
3477
+ });
3478
+ }
3278
3479
  // Stage 15j — JS/TS ssrf: client-component file. (cognium-ai#279 R-3)
3279
3480
  // A fetch/axios call in a browser-rendered client component is not SSRF.
3280
3481
  // Gated on ext + positive client signal + no server signal (zero-FN).
@@ -4116,6 +4317,64 @@ function argumentIdentifiers(expression) {
4116
4317
  }
4117
4318
  return out;
4118
4319
  }
4320
+ /** Split a Java string concat on top-level `+` (outside literals and parens). */
4321
+ function topLevelConcatOperands(expression) {
4322
+ const parts = [];
4323
+ let depth = 0;
4324
+ let quote = null;
4325
+ let escaped = false;
4326
+ let start = 0;
4327
+ for (let i = 0; i < expression.length; i++) {
4328
+ const c = expression[i];
4329
+ if (quote) {
4330
+ if (escaped) {
4331
+ escaped = false;
4332
+ continue;
4333
+ }
4334
+ if (c === '\\') {
4335
+ escaped = true;
4336
+ continue;
4337
+ }
4338
+ if (c === quote)
4339
+ quote = null;
4340
+ continue;
4341
+ }
4342
+ if (c === '"' || c === "'") {
4343
+ quote = c;
4344
+ continue;
4345
+ }
4346
+ if (c === '(')
4347
+ depth++;
4348
+ else if (c === ')')
4349
+ depth--;
4350
+ else if (c === '+' && depth === 0) {
4351
+ parts.push(expression.slice(start, i));
4352
+ start = i + 1;
4353
+ }
4354
+ }
4355
+ parts.push(expression.slice(start));
4356
+ return parts;
4357
+ }
4358
+ function concatOperandVariableNames(expr, sanitizers, sinkType) {
4359
+ const out = [];
4360
+ for (const operand of topLevelConcatOperands(expr)) {
4361
+ const trimmed = operand.trim();
4362
+ if (isStringLiteralExpression(trimmed))
4363
+ continue;
4364
+ let ids = argumentIdentifiers(trimmed);
4365
+ if (sanitizers && sanitizers.length > 0 && sinkType) {
4366
+ ids = ids.filter(varName => {
4367
+ const wrappedOnly = identifierOutsideSanitizerCallsInConcatOperand(varName, trimmed, sanitizers, sinkType);
4368
+ return wrappedOnly !== false;
4369
+ });
4370
+ }
4371
+ for (const id of ids) {
4372
+ if (!out.includes(id))
4373
+ out.push(id);
4374
+ }
4375
+ }
4376
+ return out;
4377
+ }
4119
4378
  function isStringLiteralExpression(expr) {
4120
4379
  const trimmed = expr.trim();
4121
4380
  if (trimmed.length < 2)
@@ -4167,7 +4426,7 @@ function filterCleanArraySinks(sinks, calls, taintedArrayElements, symbols) {
4167
4426
  return true;
4168
4427
  });
4169
4428
  }
4170
- export function filterCleanVariableSinks(sinks, calls, taintedVars, symbols, dfg, sanitizedVars, synchronizedLines, language, sanitizedVarUncovered) {
4429
+ export function filterCleanVariableSinks(sinks, calls, taintedVars, symbols, dfg, sanitizedVars, synchronizedLines, language, sanitizedVarUncovered, sanitizers) {
4171
4430
  const fieldNames = new Set();
4172
4431
  if (dfg) {
4173
4432
  for (const def of dfg.defs) {
@@ -4225,11 +4484,36 @@ export function filterCleanVariableSinks(sinks, calls, taintedVars, symbols, dfg
4225
4484
  // Java only. The same `+` test on other languages kept sinks in
4226
4485
  // minified scripts (a numeric add whose first identifier is a
4227
4486
  // constant). #627 is the Java `exec(cmd + tainted)` shape.
4228
- const concatNames = language === 'java' && isStringConcat(expr) ? argumentIdentifiers(expr) : [];
4229
- const names = concatNames.length > 0 ? concatNames : [arg.variable];
4487
+ const rawConcatNames = language === 'java' && isStringConcat(expr)
4488
+ ? concatOperandVariableNames(expr, sanitizers, sink.type)
4489
+ : [];
4490
+ if (language === 'java' && isStringConcat(expr) && rawConcatNames.length === 0)
4491
+ continue;
4492
+ const names = rawConcatNames.length > 0 ? rawConcatNames : [arg.variable];
4493
+ const concatOperandCheck = rawConcatNames.length > 0;
4230
4494
  let argClean = true;
4231
4495
  for (const varName of names) {
4232
4496
  const scopedName = methodName ? `${methodName}:${varName}` : varName;
4497
+ if (concatOperandCheck &&
4498
+ sanitizers &&
4499
+ sanitizers.length > 0 &&
4500
+ sink.type &&
4501
+ !taintedVars.has(scopedName) &&
4502
+ !taintedVars.has(varName) &&
4503
+ !sanitizedVars?.has(scopedName) &&
4504
+ !sanitizedVars?.has(varName)) {
4505
+ let outsideOnly = null;
4506
+ for (const operand of topLevelConcatOperands(expr)) {
4507
+ const trimmed = operand.trim();
4508
+ if (!trimmed.includes(varName))
4509
+ continue;
4510
+ outsideOnly = identifierOutsideSanitizerCallsInConcatOperand(varName, trimmed, sanitizers, sink.type);
4511
+ if (outsideOnly !== null)
4512
+ break;
4513
+ }
4514
+ if (outsideOnly === true)
4515
+ continue;
4516
+ }
4233
4517
  if (fieldNames.has(varName) && !isInSynchronizedBlock) {
4234
4518
  argClean = false;
4235
4519
  break;
@@ -4388,4 +4672,78 @@ function isXPathQuoteEscapedOnLine(lineText, varName) {
4388
4672
  }
4389
4673
  return seen > 0;
4390
4674
  }
4675
+ /**
4676
+ * cognium-dev#1172 — text of the first argument of the call to `method` that
4677
+ * starts on 1-based `line` (it may span lines), or null if it can't be read.
4678
+ */
4679
+ function jsCallFirstArgText(lines, line, method) {
4680
+ const text = lines.slice(line - 1, line + 11).join('\n');
4681
+ const at = text.search(new RegExp(`\\b${method.replace(/\$/g, '\\$')}\\s*\\(`));
4682
+ if (at < 0)
4683
+ return null;
4684
+ let i = text.indexOf('(', at) + 1;
4685
+ const start = i;
4686
+ let depth = 0;
4687
+ let str = null;
4688
+ for (; i < text.length; i++) {
4689
+ const ch = text[i];
4690
+ if (str) {
4691
+ if (ch === '\\')
4692
+ i++;
4693
+ else if (ch === str)
4694
+ str = null;
4695
+ continue;
4696
+ }
4697
+ if (ch === '"' || ch === "'" || ch === '`') {
4698
+ str = ch;
4699
+ continue;
4700
+ }
4701
+ if (ch === '(' || ch === '{' || ch === '[')
4702
+ depth++;
4703
+ else if (ch === ')' || ch === '}' || ch === ']') {
4704
+ if (depth === 0)
4705
+ return text.slice(start, i);
4706
+ depth--;
4707
+ }
4708
+ else if (ch === ',' && depth === 0)
4709
+ return text.slice(start, i);
4710
+ }
4711
+ return null;
4712
+ }
4713
+ /**
4714
+ * cognium-dev#1172 — true when, within the 40 lines before `sinkLine`, an
4715
+ * `if (… typeof <ref> !== 'string' …)` whose condition is only that check or
4716
+ * an `||` of checks rejects with return / throw / a `res.status(…)` reply.
4717
+ */
4718
+ function jsTypeofStringRejectGuarded(lines, sinkLine, ref) {
4719
+ const esc = ref.replace(/[.$]/g, m => '\\' + m);
4720
+ const guardRe = new RegExp(`\\bif\\s*\\((?:[^()]*\\|\\|\\s*)?typeof\\s+${esc}\\s*!==?\\s*(['"])string\\1\\s*(?:\\|\\|[^()]*)?\\)`);
4721
+ const terminator = /\b(?:return|throw)\b|\bres\s*\.\s*status\s*\(/;
4722
+ for (let i = Math.max(0, sinkLine - 41); i < sinkLine - 1; i++) {
4723
+ if (!guardRe.test(lines[i] ?? ''))
4724
+ continue;
4725
+ const after = (lines[i] ?? '').slice((lines[i] ?? '').search(/\)\s*(?:\{|$|[^\s])/));
4726
+ if (terminator.test(after))
4727
+ return true;
4728
+ // block body: scan to the closing brace for a terminator
4729
+ let depth = 0;
4730
+ let started = false;
4731
+ for (let j = i; j < Math.min(lines.length, i + 15, sinkLine - 1); j++) {
4732
+ const ln = j === i ? after : (lines[j] ?? '');
4733
+ if (j > i && terminator.test(ln))
4734
+ return true;
4735
+ for (const ch of ln) {
4736
+ if (ch === '{') {
4737
+ depth++;
4738
+ started = true;
4739
+ }
4740
+ else if (ch === '}')
4741
+ depth--;
4742
+ }
4743
+ if (started && depth <= 0)
4744
+ break;
4745
+ }
4746
+ }
4747
+ return false;
4748
+ }
4391
4749
  //# sourceMappingURL=sink-filter-pass.js.map