circle-ir 4.20.0 → 4.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analysis/config-loader.d.ts.map +1 -1
- package/dist/analysis/config-loader.js +17 -2
- package/dist/analysis/config-loader.js.map +1 -1
- package/dist/analysis/constant-propagation/patterns.d.ts.map +1 -1
- package/dist/analysis/constant-propagation/patterns.js +3 -0
- package/dist/analysis/constant-propagation/patterns.js.map +1 -1
- package/dist/analysis/constant-propagation/propagator.d.ts +46 -1
- package/dist/analysis/constant-propagation/propagator.d.ts.map +1 -1
- package/dist/analysis/constant-propagation/propagator.js +407 -56
- package/dist/analysis/constant-propagation/propagator.js.map +1 -1
- package/dist/analysis/findings.d.ts.map +1 -1
- package/dist/analysis/findings.js +7 -5
- package/dist/analysis/findings.js.map +1 -1
- package/dist/analysis/interprocedural.d.ts +16 -0
- package/dist/analysis/interprocedural.d.ts.map +1 -1
- package/dist/analysis/interprocedural.js +545 -197
- package/dist/analysis/interprocedural.js.map +1 -1
- package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts +58 -0
- package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts.map +1 -0
- package/dist/analysis/passes/csrf-samesite-none-session-pass.js +159 -0
- package/dist/analysis/passes/csrf-samesite-none-session-pass.js.map +1 -0
- package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
- package/dist/analysis/passes/interprocedural-pass.js +83 -10
- package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
- package/dist/analysis/passes/language-sources-pass.d.ts +21 -19
- package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
- package/dist/analysis/passes/language-sources-pass.js +703 -25
- package/dist/analysis/passes/language-sources-pass.js.map +1 -1
- package/dist/analysis/passes/param-source-flow-gate-pass.d.ts.map +1 -1
- package/dist/analysis/passes/param-source-flow-gate-pass.js +71 -1
- package/dist/analysis/passes/param-source-flow-gate-pass.js.map +1 -1
- package/dist/analysis/passes/prompt-injection-safety-gate-pass.js +1 -1
- package/dist/analysis/passes/prompt-injection-safety-gate-pass.js.map +1 -1
- package/dist/analysis/passes/scan-secrets-pass.js +2 -2
- package/dist/analysis/passes/scan-secrets-pass.js.map +1 -1
- package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts +66 -0
- package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts.map +1 -0
- package/dist/analysis/passes/sensitive-data-in-log-pass.js +462 -0
- package/dist/analysis/passes/sensitive-data-in-log-pass.js.map +1 -0
- package/dist/analysis/passes/sink-filter-pass.d.ts +7 -1
- package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
- package/dist/analysis/passes/sink-filter-pass.js +372 -14
- package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
- package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
- package/dist/analysis/passes/taint-propagation-pass.js +289 -13
- package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
- package/dist/analysis/project-callee-escape-gate.js +1 -1
- package/dist/analysis/project-callee-escape-gate.js.map +1 -1
- package/dist/analysis/rules.d.ts.map +1 -1
- package/dist/analysis/rules.js +9 -0
- package/dist/analysis/rules.js.map +1 -1
- package/dist/analysis/sbom.d.ts +49 -0
- package/dist/analysis/sbom.d.ts.map +1 -1
- package/dist/analysis/sbom.js +126 -1
- package/dist/analysis/sbom.js.map +1 -1
- package/dist/analysis/taint-matcher.d.ts.map +1 -1
- package/dist/analysis/taint-matcher.js +85 -16
- package/dist/analysis/taint-matcher.js.map +1 -1
- package/dist/analysis/taint-propagation.d.ts +1 -0
- package/dist/analysis/taint-propagation.d.ts.map +1 -1
- package/dist/analysis/taint-propagation.js +32 -0
- package/dist/analysis/taint-propagation.js.map +1 -1
- package/dist/analysis/test-to-production-taint-gate.d.ts +13 -0
- package/dist/analysis/test-to-production-taint-gate.d.ts.map +1 -0
- package/dist/analysis/test-to-production-taint-gate.js +30 -0
- package/dist/analysis/test-to-production-taint-gate.js.map +1 -0
- package/dist/analyzer.d.ts +15 -3
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +56 -2
- package/dist/analyzer.js.map +1 -1
- package/dist/browser/circle-ir.js +5149 -2477
- package/dist/core/circle-ir-core.cjs +2969 -2223
- package/dist/core/circle-ir-core.js +2969 -2219
- package/dist/core/extractors/calls.d.ts +10 -1
- package/dist/core/extractors/calls.d.ts.map +1 -1
- package/dist/core/extractors/calls.js +148 -6
- package/dist/core/extractors/calls.js.map +1 -1
- package/dist/core/extractors/cfg.d.ts.map +1 -1
- package/dist/core/extractors/cfg.js +12 -0
- package/dist/core/extractors/cfg.js.map +1 -1
- package/dist/core/extractors/dfg.d.ts.map +1 -1
- package/dist/core/extractors/dfg.js +151 -18
- package/dist/core/extractors/dfg.js.map +1 -1
- package/dist/core/extractors/types.d.ts +5 -0
- package/dist/core/extractors/types.d.ts.map +1 -1
- package/dist/core/extractors/types.js +149 -4
- package/dist/core/extractors/types.js.map +1 -1
- package/dist/core/parser.d.ts.map +1 -1
- package/dist/core/parser.js +3 -1
- package/dist/core/parser.js.map +1 -1
- package/dist/core/ts-grammar-workarounds.d.ts +18 -0
- package/dist/core/ts-grammar-workarounds.d.ts.map +1 -0
- package/dist/core/ts-grammar-workarounds.js +40 -0
- package/dist/core/ts-grammar-workarounds.js.map +1 -0
- package/dist/facts/facts2.d.ts +12 -3
- package/dist/facts/facts2.d.ts.map +1 -1
- package/dist/facts/facts2.js +24 -7
- package/dist/facts/facts2.js.map +1 -1
- package/dist/facts/index.d.ts +3 -1
- package/dist/facts/index.d.ts.map +1 -1
- package/dist/facts/index.js +2 -1
- package/dist/facts/index.js.map +1 -1
- package/dist/facts/schema2.d.ts +3 -3
- package/dist/facts/schema2.js +3 -3
- package/dist/facts/t6.d.ts +84 -0
- package/dist/facts/t6.d.ts.map +1 -0
- package/dist/facts/t6.js +226 -0
- package/dist/facts/t6.js.map +1 -0
- package/dist/graph-facts/dead-code.d.ts.map +1 -1
- package/dist/graph-facts/dead-code.js +3 -2
- package/dist/graph-facts/dead-code.js.map +1 -1
- package/dist/graph-facts/index.d.ts +1 -1
- package/dist/graph-facts/index.d.ts.map +1 -1
- package/dist/graph-facts/index.js +1 -1
- package/dist/graph-facts/index.js.map +1 -1
- package/dist/graph-facts/reach.d.ts +31 -4
- package/dist/graph-facts/reach.d.ts.map +1 -1
- package/dist/graph-facts/reach.js +120 -4
- package/dist/graph-facts/reach.js.map +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -7
- package/dist/index.js.map +1 -1
- package/dist/navigation/index.d.ts +1 -0
- package/dist/navigation/index.d.ts.map +1 -1
- package/dist/navigation/index.js +1 -0
- package/dist/navigation/index.js.map +1 -1
- package/dist/navigation/java-graph.d.ts +31 -9
- package/dist/navigation/java-graph.d.ts.map +1 -1
- package/dist/navigation/java-graph.js +174 -11
- package/dist/navigation/java-graph.js.map +1 -1
- package/dist/navigation/java-registrations.d.ts +93 -0
- package/dist/navigation/java-registrations.d.ts.map +1 -0
- package/dist/navigation/java-registrations.js +254 -0
- package/dist/navigation/java-registrations.js.map +1 -0
- package/dist/navigation/navigation-index.d.ts +65 -3
- package/dist/navigation/navigation-index.d.ts.map +1 -1
- package/dist/navigation/navigation-index.js +181 -11
- package/dist/navigation/navigation-index.js.map +1 -1
- package/dist/navigation/ts-call-edges.d.ts +6 -0
- package/dist/navigation/ts-call-edges.d.ts.map +1 -1
- package/dist/navigation/ts-call-edges.js.map +1 -1
- package/dist/navigation/ts-entry-tables.d.ts +1 -1
- package/dist/navigation/ts-entry-tables.d.ts.map +1 -1
- package/dist/navigation/ts-entry-tables.js.map +1 -1
- package/dist/navigation/xml-scan.d.ts +40 -0
- package/dist/navigation/xml-scan.d.ts.map +1 -0
- package/dist/navigation/xml-scan.js +269 -0
- package/dist/navigation/xml-scan.js.map +1 -0
- package/dist/stable-ids/index.d.ts +3 -3
- package/dist/stable-ids/index.d.ts.map +1 -1
- package/dist/stable-ids/index.js +16 -4
- package/dist/stable-ids/index.js.map +1 -1
- package/dist/stable-ids/nodes.d.ts +16 -6
- package/dist/stable-ids/nodes.d.ts.map +1 -1
- package/dist/stable-ids/nodes.js +17 -7
- package/dist/stable-ids/nodes.js.map +1 -1
- package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
- package/dist/stable-ids/ts-parse-errors.js +21 -2
- package/dist/stable-ids/ts-parse-errors.js.map +1 -1
- package/dist/stable-ids/typescript.d.ts +5 -0
- package/dist/stable-ids/typescript.d.ts.map +1 -1
- package/dist/stable-ids/typescript.js +41 -8
- package/dist/stable-ids/typescript.js.map +1 -1
- package/dist/types/index.d.ts +21 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -1
- package/docs/SPEC.md +9 -0
- package/package.json +3 -3
|
@@ -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
|
-
|
|
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
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
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
|
-
//
|
|
578
|
-
//
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
if (
|
|
597
|
-
|
|
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
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
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
|
-
:
|
|
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: '
|
|
631
|
-
cwe: 'CWE-
|
|
632
|
-
location: `Tainted data (${taintedArgVars.join(', ')})
|
|
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.
|
|
686
|
+
confidence: 0.7,
|
|
635
687
|
method: call.method_name,
|
|
636
688
|
argPositions: taintedArgPositions,
|
|
637
689
|
}
|
|
638
|
-
:
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
def
|
|
667
|
-
|
|
668
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
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 =
|
|
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) {
|