circle-ir 4.23.0 → 4.24.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.
- package/configs/sink-semantics.json +6 -0
- package/dist/analysis/config-loader.d.ts.map +1 -1
- package/dist/analysis/config-loader.js +18 -5
- package/dist/analysis/config-loader.js.map +1 -1
- package/dist/analysis/findings.d.ts +8 -1
- package/dist/analysis/findings.d.ts.map +1 -1
- package/dist/analysis/findings.js +67 -5
- package/dist/analysis/findings.js.map +1 -1
- package/dist/analysis/hono-env.d.ts +24 -0
- package/dist/analysis/hono-env.d.ts.map +1 -0
- package/dist/analysis/hono-env.js +105 -0
- package/dist/analysis/hono-env.js.map +1 -0
- package/dist/analysis/html/html-extractor.d.ts +6 -0
- package/dist/analysis/html/html-extractor.d.ts.map +1 -1
- package/dist/analysis/html/html-extractor.js +16 -0
- package/dist/analysis/html/html-extractor.js.map +1 -1
- package/dist/analysis/interprocedural.d.ts.map +1 -1
- package/dist/analysis/interprocedural.js +51 -2
- package/dist/analysis/interprocedural.js.map +1 -1
- package/dist/analysis/js-helper-return.d.ts +7 -0
- package/dist/analysis/js-helper-return.d.ts.map +1 -1
- package/dist/analysis/js-helper-return.js +165 -0
- package/dist/analysis/js-helper-return.js.map +1 -1
- package/dist/analysis/js-spawn-argv.d.ts +34 -0
- package/dist/analysis/js-spawn-argv.d.ts.map +1 -0
- package/dist/analysis/js-spawn-argv.js +156 -0
- package/dist/analysis/js-spawn-argv.js.map +1 -0
- package/dist/analysis/maven-declared.d.ts.map +1 -1
- package/dist/analysis/maven-declared.js +5 -1
- package/dist/analysis/maven-declared.js.map +1 -1
- package/dist/analysis/passes/cli-main-reflection-suppress-pass.d.ts +1 -1
- package/dist/analysis/passes/cli-main-reflection-suppress-pass.d.ts.map +1 -1
- package/dist/analysis/passes/cli-main-reflection-suppress-pass.js +3 -1
- package/dist/analysis/passes/cli-main-reflection-suppress-pass.js.map +1 -1
- package/dist/analysis/passes/cross-file-pass.d.ts.map +1 -1
- package/dist/analysis/passes/cross-file-pass.js +2 -1
- package/dist/analysis/passes/cross-file-pass.js.map +1 -1
- package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
- package/dist/analysis/passes/interprocedural-pass.js +113 -2
- package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
- package/dist/analysis/passes/language-sources-pass.d.ts +15 -0
- package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
- package/dist/analysis/passes/language-sources-pass.js +1414 -33
- package/dist/analysis/passes/language-sources-pass.js.map +1 -1
- package/dist/analysis/passes/module-side-effect-pass.d.ts +9 -0
- package/dist/analysis/passes/module-side-effect-pass.d.ts.map +1 -1
- package/dist/analysis/passes/module-side-effect-pass.js +30 -3
- package/dist/analysis/passes/module-side-effect-pass.js.map +1 -1
- package/dist/analysis/passes/orphan-module-pass.d.ts +6 -1
- package/dist/analysis/passes/orphan-module-pass.d.ts.map +1 -1
- package/dist/analysis/passes/orphan-module-pass.js +6 -1
- package/dist/analysis/passes/orphan-module-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 +2 -1
- package/dist/analysis/passes/param-source-flow-gate-pass.js.map +1 -1
- package/dist/analysis/passes/scan-secrets-pass.d.ts +8 -0
- package/dist/analysis/passes/scan-secrets-pass.d.ts.map +1 -1
- package/dist/analysis/passes/scan-secrets-pass.js +78 -10
- package/dist/analysis/passes/scan-secrets-pass.js.map +1 -1
- package/dist/analysis/passes/sink-filter-pass.d.ts +17 -0
- package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
- package/dist/analysis/passes/sink-filter-pass.js +687 -4
- package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
- package/dist/analysis/passes/taint-propagation-pass.d.ts +19 -0
- package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
- package/dist/analysis/passes/taint-propagation-pass.js +40 -1
- package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
- package/dist/analysis/require-entry-path.d.ts +4 -1
- package/dist/analysis/require-entry-path.d.ts.map +1 -1
- package/dist/analysis/require-entry-path.js +150 -7
- package/dist/analysis/require-entry-path.js.map +1 -1
- package/dist/analysis/rules.d.ts +7 -0
- package/dist/analysis/rules.d.ts.map +1 -1
- package/dist/analysis/rules.js +65 -0
- package/dist/analysis/rules.js.map +1 -1
- package/dist/analysis/sbom.d.ts +32 -4
- package/dist/analysis/sbom.d.ts.map +1 -1
- package/dist/analysis/sbom.js +57 -19
- 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 -8
- package/dist/analysis/taint-matcher.js.map +1 -1
- package/dist/analysis/taint-propagation.d.ts +7 -0
- package/dist/analysis/taint-propagation.d.ts.map +1 -1
- package/dist/analysis/taint-propagation.js +84 -0
- package/dist/analysis/taint-propagation.js.map +1 -1
- package/dist/analysis/test-to-production-taint-gate.d.ts.map +1 -1
- package/dist/analysis/test-to-production-taint-gate.js +6 -2
- package/dist/analysis/test-to-production-taint-gate.js.map +1 -1
- package/dist/analyzer.d.ts +30 -5
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +93 -13
- package/dist/analyzer.js.map +1 -1
- package/dist/browser/circle-ir.js +11388 -8828
- package/dist/core/circle-ir-core.cjs +307 -27
- package/dist/core/circle-ir-core.js +307 -27
- package/dist/core/extractors/calls.d.ts.map +1 -1
- package/dist/core/extractors/calls.js +83 -1
- package/dist/core/extractors/calls.js.map +1 -1
- package/dist/core/extractors/meta.d.ts +3 -2
- package/dist/core/extractors/meta.d.ts.map +1 -1
- package/dist/core/extractors/meta.js +3 -2
- package/dist/core/extractors/meta.js.map +1 -1
- package/dist/core/extractors/runtime-registrations.js +5 -2
- package/dist/core/extractors/runtime-registrations.js.map +1 -1
- package/dist/core/extractors/types.d.ts +3 -2
- package/dist/core/extractors/types.d.ts.map +1 -1
- package/dist/core/extractors/types.js +22 -20
- package/dist/core/extractors/types.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/parser.d.ts +11 -6
- package/dist/core/parser.d.ts.map +1 -1
- package/dist/core/parser.js +29 -5
- package/dist/core/parser.js.map +1 -1
- package/dist/core/partial-parse-recovery.d.ts +26 -0
- package/dist/core/partial-parse-recovery.d.ts.map +1 -0
- package/dist/core/partial-parse-recovery.js +129 -0
- package/dist/core/partial-parse-recovery.js.map +1 -0
- package/dist/graph/code-graph.d.ts +1 -0
- package/dist/graph/code-graph.d.ts.map +1 -1
- package/dist/graph/code-graph.js +1 -0
- package/dist/graph/code-graph.js.map +1 -1
- package/dist/graph/import-graph.d.ts +14 -4
- package/dist/graph/import-graph.d.ts.map +1 -1
- package/dist/graph/import-graph.js +72 -9
- package/dist/graph/import-graph.js.map +1 -1
- package/dist/graph-facts/dead-code.d.ts +2 -0
- package/dist/graph-facts/dead-code.d.ts.map +1 -1
- package/dist/graph-facts/dead-code.js +126 -1
- 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/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/languages/plugins/javascript.d.ts.map +1 -1
- package/dist/languages/plugins/javascript.js +1 -1
- package/dist/languages/plugins/javascript.js.map +1 -1
- package/dist/languages/registry.d.ts.map +1 -1
- package/dist/languages/registry.js +12 -0
- package/dist/languages/registry.js.map +1 -1
- package/dist/navigation/index.d.ts +1 -1
- package/dist/navigation/index.d.ts.map +1 -1
- package/dist/navigation/index.js.map +1 -1
- package/dist/navigation/navigation-index.js +3 -1
- package/dist/navigation/navigation-index.js.map +1 -1
- package/dist/navigation/python-graph.d.ts +3 -1
- package/dist/navigation/python-graph.d.ts.map +1 -1
- package/dist/navigation/python-graph.js +236 -23
- package/dist/navigation/python-graph.js.map +1 -1
- package/dist/navigation/ts-call-edges.d.ts +18 -0
- package/dist/navigation/ts-call-edges.d.ts.map +1 -1
- package/dist/navigation/ts-call-edges.js +207 -18
- package/dist/navigation/ts-call-edges.js.map +1 -1
- package/dist/path-set/path-set.d.ts +5 -2
- package/dist/path-set/path-set.d.ts.map +1 -1
- package/dist/path-set/path-set.js +4 -1
- package/dist/path-set/path-set.js.map +1 -1
- package/dist/resolution/cross-file.d.ts +12 -0
- package/dist/resolution/cross-file.d.ts.map +1 -1
- package/dist/resolution/cross-file.js +357 -3
- package/dist/resolution/cross-file.js.map +1 -1
- package/dist/stable-ids/ts-parse-errors.d.ts +2 -0
- package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
- package/dist/stable-ids/ts-parse-errors.js +43 -1
- package/dist/stable-ids/ts-parse-errors.js.map +1 -1
- package/dist/types/config.d.ts +2 -1
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/index.d.ts +74 -5
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/docs/SPEC.md +1 -1
- package/package.json +2 -2
|
@@ -19,6 +19,7 @@ import { runsArgvAsCommand } from '../arg-executing-programs.js';
|
|
|
19
19
|
import { CSHARP_SANITIZER_RES, CSharpCredit, csharpSanitizersFor } from './csharp-sanitizer-credit.js';
|
|
20
20
|
import { JS_TAINTED_PATTERNS } from './language-sources-pass.js';
|
|
21
21
|
import { LIBRARY_API_SURFACE_TAG } from '../library-api-surface-downgrade.js';
|
|
22
|
+
import { SHELL_OPTION_ON, SHELL_PROGRAM } from '../js-spawn-argv.js';
|
|
22
23
|
import { isNonExecutableSourceLine } from '../non-executable-lines.js';
|
|
23
24
|
import { sanitizerCoversSink } from '../sanitizer-index.js';
|
|
24
25
|
import { identifierOutsideSanitizerCalls, identifierOutsideSanitizerCallsInConcatOperand, } from '../taint-propagation.js';
|
|
@@ -235,6 +236,36 @@ const OS_EXEC_RECEIVER_RE = /\b(?:Runtime|ProcessBuilder|DefaultExecutor|Executo
|
|
|
235
236
|
// real finding drowns and hit@3 collapses to 16% while recall stays at 79% —
|
|
236
237
|
// a ranking failure caused by FP volume, not a detection gap.
|
|
237
238
|
const JAVA_STDOUT_PRINT_RE = /\bSystem\s*\.\s*(?:out|err)\s*\.\s*(?:println|print|printf|write|append)\s*\(/;
|
|
239
|
+
// Stage 15e, second shape — the same print through a variable. A CLI tool
|
|
240
|
+
// prints via a `PrintStream ps` parameter, a `PrintStream` field (a small file
|
|
241
|
+
// logger) or its own `Logger out`. The receiver type resolves, so `sink.class`
|
|
242
|
+
// names it. `java.io.PrintStream` writes to a console or a file, and a logger
|
|
243
|
+
// to a log; neither is an HTML document. The set is matched by simple type
|
|
244
|
+
// name only: a project's own class named `Logger` or `Log` is trusted to be a
|
|
245
|
+
// logger whatever it writes to.
|
|
246
|
+
//
|
|
247
|
+
// A `PrintStream` can wrap the servlet response's output stream, and a servlet
|
|
248
|
+
// can hand that stream to a helper in another file. So the sinks are kept when
|
|
249
|
+
// the file itself calls `getOutputStream()`, or when any Java file of the
|
|
250
|
+
// project does (`javaOpensResponseStream`, passed in by `analyzeProject`).
|
|
251
|
+
const JAVA_NON_HTML_PRINT_RECEIVERS = new Set([
|
|
252
|
+
'PrintStream', 'Logger', 'Log',
|
|
253
|
+
]);
|
|
254
|
+
const JAVA_RESPONSE_OUTPUT_STREAM_RE = /\bgetOutputStream\s*\(/;
|
|
255
|
+
/**
|
|
256
|
+
* True when any Java file in `files` calls `getOutputStream()`, so a
|
|
257
|
+
* `PrintStream` anywhere in the project may write to a servlet response.
|
|
258
|
+
* `analyzeProject` computes this before its per-file loop and passes it to
|
|
259
|
+
* each file as `AnalyzerOptions.javaOpensResponseStream`; a caller that
|
|
260
|
+
* analyses files itself (`AnalyzerOptions.analyzeFile`) sets it the same way.
|
|
261
|
+
*/
|
|
262
|
+
export function javaProjectOpensResponseStream(files) {
|
|
263
|
+
for (const f of files) {
|
|
264
|
+
if (f.language === 'java' && JAVA_RESPONSE_OUTPUT_STREAM_RE.test(f.code))
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
238
269
|
// ---------------------------------------------------------------------------
|
|
239
270
|
// Stage 11 — Java command_injection (CWE-78) FP reduction.
|
|
240
271
|
// (cognium-dev #179 Sink 1 — Sprint 44)
|
|
@@ -1125,6 +1156,433 @@ const REPLACE_ALL_TO_PLACEHOLDER_RE = /\.replaceAll\s*\([\s\S]*?,\s*"[\s,?]*\?[\
|
|
|
1125
1156
|
// text/html. Absent this signal anywhere in the file, `body(...)` is not an HTML
|
|
1126
1157
|
// output context.
|
|
1127
1158
|
const HTML_CONTENT_TYPE_RE = /text\/html|TEXT_HTML/;
|
|
1159
|
+
// JS/TS response writers (`res.end`, `res.send`, `res.write`,
|
|
1160
|
+
// `c.body`) are XSS sinks because a browser renders their body as HTML. When
|
|
1161
|
+
// the handler sets the response's own Content-Type to a non-HTML type first
|
|
1162
|
+
// (`res.writeHead(200, {'Content-Type': 'application/json'})`,
|
|
1163
|
+
// `res.setHeader(...)`, `res.type('json')`, `c.body(x, 200, {headers})`), the
|
|
1164
|
+
// browser does not render the body as markup, so the write is not an XSS sink.
|
|
1165
|
+
// A type that a browser can render as a document (HTML, XHTML, SVG, XML) keeps
|
|
1166
|
+
// the sink; so does an unset type or one we cannot read.
|
|
1167
|
+
const JS_RESPONSE_WRITE_METHODS = new Set(['end', 'send', 'write', 'body']);
|
|
1168
|
+
const JS_CONTENT_TYPE_SETTERS = new Set([
|
|
1169
|
+
'writeHead', 'setHeader', 'setHeaders', 'set', 'header', 'append', 'type', 'contentType', 'body',
|
|
1170
|
+
]);
|
|
1171
|
+
// Calls on the response that leave its Content-Type alone: body writes
|
|
1172
|
+
// (Express's `send` / `json` set a type only when none is set), status and
|
|
1173
|
+
// cookie writes, header reads and event hooks. Any other call on the response
|
|
1174
|
+
// after the type is set may change it (`removeHeader`, `attachment`,
|
|
1175
|
+
// `render`, `redirect`, `format`, …).
|
|
1176
|
+
const JS_TYPE_KEEPING_METHODS = new Set([
|
|
1177
|
+
'end', 'send', 'write', 'json', 'status', 'cookie', 'clearCookie', 'vary',
|
|
1178
|
+
'get', 'getHeader', 'getHeaders', 'getHeaderNames', 'hasHeader',
|
|
1179
|
+
'on', 'once', 'off', 'flushHeaders', 'setTimeout', 'cork', 'uncork',
|
|
1180
|
+
]);
|
|
1181
|
+
const JS_CONTENT_TYPE_KEY_RE = /^\s*['"`]content-type['"`]\s*$/i;
|
|
1182
|
+
const JS_CONTENT_TYPE_PROP_RE = /['"`]content-type['"`]\s*:\s*([^,}\n]+)/gi;
|
|
1183
|
+
const JS_RENDERABLE_TYPE_RE = /html|svg|xml/i;
|
|
1184
|
+
const JS_STRING_LITERAL_RE = /^\s*(['"`])([^'"`$]*)\1\s*$/;
|
|
1185
|
+
// A static file server picks the type from the served file's extension
|
|
1186
|
+
// (`MIME[path.extname(file)]`, `mime.getType(file)`, `mime.lookup(file)`); the
|
|
1187
|
+
// body is that file on disk, never request text rendered into a page. Group 1
|
|
1188
|
+
// or 2 is the path the type is looked up for.
|
|
1189
|
+
const JS_MIME_LOOKUP_RE = /\[\s*(?:[\w$]+\.)?extname\s*\(\s*([A-Za-z_$][\w$]*)\s*\)\s*\]|\b(?:getType|lookup|contentType)\s*\(\s*(?:(?:[\w$]+\.)?extname\s*\(\s*)?([A-Za-z_$][\w$]*)\s*(?:\)\s*)?\)/;
|
|
1190
|
+
// What may stand around the lookup: the map or module it is read from and one
|
|
1191
|
+
// literal fallback (`TYPES[...] || 'application/octet-stream'`).
|
|
1192
|
+
const JS_MIME_LOOKUP_REST_RE = /^\s*(?:[A-Za-z_$][\w$.]*\s*)?(?:(?:\|\||\?\?)\s*(['"`])([^'"`$]*)\1\s*)?$/;
|
|
1193
|
+
const JS_FILE_READ_METHODS = 'readFileSync|readFile|createReadStream';
|
|
1194
|
+
/** Lines of the innermost function passed as an argument that holds `line`. */
|
|
1195
|
+
function jsEnclosingCallbackRange(calls, line) {
|
|
1196
|
+
let best = null;
|
|
1197
|
+
for (const call of calls) {
|
|
1198
|
+
for (const arg of call.arguments) {
|
|
1199
|
+
const expr = arg.expression;
|
|
1200
|
+
if (!/^\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*=>|[A-Za-z_$][\w$]*\s*=>)/.test(expr))
|
|
1201
|
+
continue;
|
|
1202
|
+
const start = call.location.line;
|
|
1203
|
+
const end = start + (expr.match(/\n/g)?.length ?? 0);
|
|
1204
|
+
if (line < start || line > end)
|
|
1205
|
+
continue;
|
|
1206
|
+
if (!best || end - start < best[1] - best[0])
|
|
1207
|
+
best = [start, end];
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return best;
|
|
1211
|
+
}
|
|
1212
|
+
/** The Content-Type expressions one call sets on its receiver. */
|
|
1213
|
+
function jsContentTypeExpressions(call) {
|
|
1214
|
+
const args = call.arguments.map(a => a.expression);
|
|
1215
|
+
if (call.method_name === 'type' || call.method_name === 'contentType') {
|
|
1216
|
+
return args.length > 0 ? [args[0]] : [];
|
|
1217
|
+
}
|
|
1218
|
+
if (['setHeader', 'set', 'header', 'append'].includes(call.method_name) &&
|
|
1219
|
+
args.length >= 2 && JS_CONTENT_TYPE_KEY_RE.test(args[0])) {
|
|
1220
|
+
return [args[1]];
|
|
1221
|
+
}
|
|
1222
|
+
const out = [];
|
|
1223
|
+
for (const a of args) {
|
|
1224
|
+
for (const m of a.matchAll(JS_CONTENT_TYPE_PROP_RE))
|
|
1225
|
+
out.push(m[1].trim());
|
|
1226
|
+
}
|
|
1227
|
+
return out;
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* True when the header object or list `expr` may set a Content-Type we cannot
|
|
1231
|
+
* read: a variable, a call, a spread or computed key (after any literal
|
|
1232
|
+
* `content-type` key, since a later key wins), or a raw header list that names
|
|
1233
|
+
* the type or holds a non-literal entry.
|
|
1234
|
+
*/
|
|
1235
|
+
function jsHeaderValueMayChangeType(expr) {
|
|
1236
|
+
const value = expr.trim();
|
|
1237
|
+
if (/^(?:\d+|(['"`])[^'"`$]*\1)$/.test(value))
|
|
1238
|
+
return false;
|
|
1239
|
+
const headers = /^new\s+Headers\s*\(([\s\S]*)\)$/.exec(value);
|
|
1240
|
+
if (headers)
|
|
1241
|
+
return headers[1].trim() !== '' && jsHeaderValueMayChangeType(headers[1]);
|
|
1242
|
+
if (value.startsWith('{')) {
|
|
1243
|
+
const keys = [...value.matchAll(/['"`]content-type['"`]\s*:/gi)];
|
|
1244
|
+
const tail = keys.length > 0 ? value.slice(keys[keys.length - 1].index) : value;
|
|
1245
|
+
return /\.\.\.|[{,]\s*\[/.test(tail);
|
|
1246
|
+
}
|
|
1247
|
+
if (value.startsWith('[')) {
|
|
1248
|
+
return /content-type/i.test(value) || /[[,]\s*[^'"`\s\]]/.test(value);
|
|
1249
|
+
}
|
|
1250
|
+
return true;
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* True when the call `call` on the response may change its Content-Type to a
|
|
1254
|
+
* value we cannot read, or unset it: `res.writeHead(200, headers)`,
|
|
1255
|
+
* `res.setHeader(name, value)` with a non-literal name, `res.set(headers)`,
|
|
1256
|
+
* `res.setHeaders(map)`, a header object with a spread after its
|
|
1257
|
+
* `content-type` key, `res.removeHeader('Content-Type')`, or any method not
|
|
1258
|
+
* known to leave the type alone.
|
|
1259
|
+
*/
|
|
1260
|
+
function jsHeaderWriteMayChangeType(call) {
|
|
1261
|
+
const args = call.arguments.map(a => a.expression);
|
|
1262
|
+
switch (call.method_name) {
|
|
1263
|
+
case 'writeHead':
|
|
1264
|
+
case 'body':
|
|
1265
|
+
return args.slice(1).some(jsHeaderValueMayChangeType);
|
|
1266
|
+
case 'setHeader':
|
|
1267
|
+
case 'set':
|
|
1268
|
+
case 'header':
|
|
1269
|
+
case 'append':
|
|
1270
|
+
if (args.length >= 2)
|
|
1271
|
+
return !JS_STRING_LITERAL_RE.test(args[0]);
|
|
1272
|
+
return args.length === 1 && jsHeaderValueMayChangeType(args[0]);
|
|
1273
|
+
case 'setHeaders':
|
|
1274
|
+
return args.length > 0 && jsHeaderValueMayChangeType(args[0]);
|
|
1275
|
+
case 'removeHeader':
|
|
1276
|
+
return args.length === 0 || !JS_STRING_LITERAL_RE.test(args[0]) || JS_CONTENT_TYPE_KEY_RE.test(args[0]);
|
|
1277
|
+
case 'type':
|
|
1278
|
+
case 'contentType':
|
|
1279
|
+
return false;
|
|
1280
|
+
default:
|
|
1281
|
+
return !JS_TYPE_KEEPING_METHODS.has(call.method_name);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* True when the call argument `expr` hands over the response `receiver` itself
|
|
1286
|
+
* (`setType(res)`, `send({ res })`), so the callee may change its type. A
|
|
1287
|
+
* member read (`res.locals`) or a function argument does not count.
|
|
1288
|
+
*/
|
|
1289
|
+
function jsArgumentPassesReceiver(expr, receiver) {
|
|
1290
|
+
if (/^\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*=>|[A-Za-z_$][\w$]*\s*=>)/.test(expr))
|
|
1291
|
+
return false;
|
|
1292
|
+
const text = expr.replace(/'[^'\n]*'|"[^"\n]*"|`[^`]*`/g, '""');
|
|
1293
|
+
const name = receiver.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1294
|
+
return new RegExp(`(?<![\\w$.])${name}(?![\\w$.[]|\\?\\.)`).test(text);
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* The receiver and method of a call. A bracket call (`res['setHeader'](…)`)
|
|
1298
|
+
* has no receiver in the IR and its whole callee as the method name; read it
|
|
1299
|
+
* as `res` / `setHeader`. A computed name (`res[m](…)`) gives method `''`,
|
|
1300
|
+
* which no method set holds, so it counts as a call that may change the type.
|
|
1301
|
+
*/
|
|
1302
|
+
function jsCallTarget(call) {
|
|
1303
|
+
if (call.receiver)
|
|
1304
|
+
return { receiver: call.receiver, method: call.method_name };
|
|
1305
|
+
const m = /^([A-Za-z_$][\w$.]*)\s*(?:\?\.)?\[([\s\S]*)\]$/.exec(call.method_name);
|
|
1306
|
+
if (!m)
|
|
1307
|
+
return { receiver: null, method: call.method_name };
|
|
1308
|
+
const lit = JS_STRING_LITERAL_RE.exec(m[2]);
|
|
1309
|
+
return { receiver: m[1], method: lit ? lit[2] : '' };
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Names that hold the same response as `receiver`: `const out = res`,
|
|
1313
|
+
* `res = out`, and chains of those. A call on one may change the type the
|
|
1314
|
+
* write sends.
|
|
1315
|
+
*/
|
|
1316
|
+
function jsReceiverAliases(code, receiver) {
|
|
1317
|
+
const aliases = new Set();
|
|
1318
|
+
const queue = [receiver];
|
|
1319
|
+
while (queue.length > 0) {
|
|
1320
|
+
const name = queue.pop().replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1321
|
+
const found = [
|
|
1322
|
+
...code.matchAll(new RegExp(`(?<![\\w$.])([A-Za-z_$][\\w$]*)\\s*=\\s*${name}(?![\\w$.([]|\\s*\\?\\.)`, 'g')),
|
|
1323
|
+
...code.matchAll(new RegExp(`(?<![\\w$.])${name}\\s*=(?!=)\\s*([A-Za-z_$][\\w$]*)(?![\\w$.([]|\\s*\\?\\.)`, 'g')),
|
|
1324
|
+
];
|
|
1325
|
+
for (const m of found) {
|
|
1326
|
+
if (m[1] !== receiver && !aliases.has(m[1])) {
|
|
1327
|
+
aliases.add(m[1]);
|
|
1328
|
+
queue.push(m[1]);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
return aliases;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* How a call's receiver text relates to the response `name`: `'self'` when it
|
|
1336
|
+
* is the response or a chain of calls on it (`res.status(200)`, which returns
|
|
1337
|
+
* the response), `'member'` when it is another chain rooted at it
|
|
1338
|
+
* (`res.locals.x`), `null` when it is not rooted at it.
|
|
1339
|
+
*/
|
|
1340
|
+
function jsReceiverChainKind(text, name) {
|
|
1341
|
+
if (text === name)
|
|
1342
|
+
return 'self';
|
|
1343
|
+
if (!text.startsWith(name) || !/^\s*(?:\??\.|\[)/.test(text.slice(name.length)))
|
|
1344
|
+
return null;
|
|
1345
|
+
let i = name.length;
|
|
1346
|
+
while (i < text.length) {
|
|
1347
|
+
const seg = /^\s*\??\.\s*[A-Za-z_$][\w$]*\s*\(/.exec(text.slice(i));
|
|
1348
|
+
if (!seg)
|
|
1349
|
+
return 'member';
|
|
1350
|
+
i += seg[0].length;
|
|
1351
|
+
for (let depth = 1; depth > 0; i++) {
|
|
1352
|
+
if (i >= text.length)
|
|
1353
|
+
return 'member';
|
|
1354
|
+
if (text[i] === '(')
|
|
1355
|
+
depth++;
|
|
1356
|
+
else if (text[i] === ')')
|
|
1357
|
+
depth--;
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return 'self';
|
|
1361
|
+
}
|
|
1362
|
+
/** `code` with string, template and comment text blanked, offsets kept. */
|
|
1363
|
+
function jsMaskStringsAndComments(code) {
|
|
1364
|
+
return code.replace(/\/\/[^\n]*|\/\*[\s\S]*?\*\/|'(?:\\.|[^'\\\n])*'|"(?:\\.|[^"\\\n])*"|`(?:\\[\s\S]|[^`\\])*`/g, m => m.replace(/[^\n]/g, ' '));
|
|
1365
|
+
}
|
|
1366
|
+
/**
|
|
1367
|
+
* Offsets in `code` before `to` where one of `names` (the response or another
|
|
1368
|
+
* name for it) is used as a value after the response is declared: handed to a
|
|
1369
|
+
* call (`asHtml(out)`), wrapped (`{ res }`, `[res]`, `(res)`), or anything
|
|
1370
|
+
* other than a member access, a parameter, an assignment target or a plain
|
|
1371
|
+
* alias (`const out = res`). The value may reach code that changes the type.
|
|
1372
|
+
*/
|
|
1373
|
+
function jsReceiverEscapes(code, names, to) {
|
|
1374
|
+
const text = jsMaskStringsAndComments(code.slice(0, to));
|
|
1375
|
+
const esc = (n) => n.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1376
|
+
const uses = [...text.matchAll(new RegExp(`(?<![\\w$.])(?:${names.map(esc).join('|')})(?![\\w$])`, 'g'))];
|
|
1377
|
+
// A parameter list: the `)` that closes it starts a body, arrow or return type.
|
|
1378
|
+
const isParameter = (at) => {
|
|
1379
|
+
let depth = 0;
|
|
1380
|
+
for (let i = at; i < text.length; i++) {
|
|
1381
|
+
if (text[i] === '(')
|
|
1382
|
+
depth++;
|
|
1383
|
+
else if (text[i] === ')' && --depth < 0)
|
|
1384
|
+
return /^\s*(?:=>|\{|:)/.test(text.slice(i + 1));
|
|
1385
|
+
else if (depth === 0 && /[;{}]/.test(text[i]))
|
|
1386
|
+
return false;
|
|
1387
|
+
}
|
|
1388
|
+
return false;
|
|
1389
|
+
};
|
|
1390
|
+
const declared = uses.filter(m => isParameter(m.index)).pop()?.index ?? -1;
|
|
1391
|
+
return uses
|
|
1392
|
+
.filter(m => m.index > declared)
|
|
1393
|
+
.filter(m => {
|
|
1394
|
+
const after = text.slice(m.index + m[0].length);
|
|
1395
|
+
if (/^\s*(?:\??\.|\[)|^\s*=(?!=)/.test(after))
|
|
1396
|
+
return false;
|
|
1397
|
+
const before = text.slice(0, m.index);
|
|
1398
|
+
return !(/(?<![=!<>])=\s*$/.test(before) && /^\s*(?:[;\n]|$)/.test(after));
|
|
1399
|
+
})
|
|
1400
|
+
.map(m => m.index);
|
|
1401
|
+
}
|
|
1402
|
+
/** Offset in `code` of a call's start (1-based line, 0-based column). */
|
|
1403
|
+
function jsCallOffset(lineStarts, call) {
|
|
1404
|
+
return (lineStarts[call.location.line - 1] ?? 0) + call.location.column;
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* True when the statement starting at `from` runs on every path to `to`: it
|
|
1408
|
+
* is a plain statement (not the body of a braceless `if` / `else` / loop, nor
|
|
1409
|
+
* an operand of `&&` / `||` / `?:`), and no block that holds it closes before
|
|
1410
|
+
* `to` (so it is not inside an `if`, an early-return branch, or a callback).
|
|
1411
|
+
*/
|
|
1412
|
+
function jsStatementDominates(code, from, to) {
|
|
1413
|
+
if (from >= to)
|
|
1414
|
+
return false;
|
|
1415
|
+
let b = from - 1;
|
|
1416
|
+
while (b >= 0 && !';{}\n'.includes(code[b]))
|
|
1417
|
+
b--;
|
|
1418
|
+
if (!/^\s*(?:await\s+)?$/.test(code.slice(b + 1, from)))
|
|
1419
|
+
return false;
|
|
1420
|
+
if (b >= 0 && code[b] === '\n') {
|
|
1421
|
+
const prev = code.slice(0, b).split('\n').map(l => l.trim()).filter(l => l && !l.startsWith('//')).pop() ?? '';
|
|
1422
|
+
if (/(?:\)|\belse|\bdo|&&|\|\||[?:=,(+\-*/]|=>)$/.test(prev))
|
|
1423
|
+
return false;
|
|
1424
|
+
}
|
|
1425
|
+
let depth = 0;
|
|
1426
|
+
for (let i = from; i < to; i++) {
|
|
1427
|
+
const ch = code[i];
|
|
1428
|
+
if (ch === '\'' || ch === '"' || ch === '`') {
|
|
1429
|
+
for (i++; i < to && code[i] !== ch; i++)
|
|
1430
|
+
if (code[i] === '\\')
|
|
1431
|
+
i++;
|
|
1432
|
+
}
|
|
1433
|
+
else if (ch === '/' && code[i + 1] === '/') {
|
|
1434
|
+
while (i < to && code[i] !== '\n')
|
|
1435
|
+
i++;
|
|
1436
|
+
}
|
|
1437
|
+
else if (ch === '/' && code[i + 1] === '*') {
|
|
1438
|
+
const end = code.indexOf('*/', i + 2);
|
|
1439
|
+
i = end < 0 ? to : end + 1;
|
|
1440
|
+
}
|
|
1441
|
+
else if (ch === '{') {
|
|
1442
|
+
depth++;
|
|
1443
|
+
}
|
|
1444
|
+
else if (ch === '}' && --depth < 0) {
|
|
1445
|
+
return false;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
return true;
|
|
1449
|
+
}
|
|
1450
|
+
/**
|
|
1451
|
+
* True when the non-literal type `expr` is a mime lookup of the file that
|
|
1452
|
+
* `sinkCall` writes: `MIME[path.extname(file)]`, `mime.getType(file)`, … where
|
|
1453
|
+
* `file` is read from disk into the body (`res.end(fs.readFileSync(file))`).
|
|
1454
|
+
*/
|
|
1455
|
+
function jsTypeIsServedFileMime(code, expr, setterAt, sinkCall) {
|
|
1456
|
+
let value = expr.trim();
|
|
1457
|
+
if (/^[A-Za-z_$][\w$]*$/.test(value)) {
|
|
1458
|
+
const name = value;
|
|
1459
|
+
const before = code.slice(0, setterAt);
|
|
1460
|
+
const decls = [...before.matchAll(new RegExp(`\\b(?:const|let|var)\\s+${name}\\s*=\\s*([^;\\n]+)`, 'g'))];
|
|
1461
|
+
const writes = before.match(new RegExp(`(?<![\\w$.])${name}\\s*=(?!=)`, 'g'))?.length ?? 0;
|
|
1462
|
+
if (decls.length !== 1 || writes !== 1)
|
|
1463
|
+
return false;
|
|
1464
|
+
value = decls[0][1].trim();
|
|
1465
|
+
}
|
|
1466
|
+
const m = JS_MIME_LOOKUP_RE.exec(value);
|
|
1467
|
+
if (!m)
|
|
1468
|
+
return false;
|
|
1469
|
+
const rest = JS_MIME_LOOKUP_REST_RE.exec(value.slice(0, m.index) + value.slice(m.index + m[0].length));
|
|
1470
|
+
if (!rest || (rest[2] !== undefined && JS_RENDERABLE_TYPE_RE.test(rest[2])))
|
|
1471
|
+
return false;
|
|
1472
|
+
const file = (m[1] ?? m[2]);
|
|
1473
|
+
const reads = (v) => new RegExp(`\\b(?:${JS_FILE_READ_METHODS})\\s*\\(\\s*${v}\\b`);
|
|
1474
|
+
return sinkCall.arguments.some(a => {
|
|
1475
|
+
if (reads(file).test(a.expression))
|
|
1476
|
+
return true;
|
|
1477
|
+
const body = a.expression.trim();
|
|
1478
|
+
if (!/^[A-Za-z_$][\w$]*$/.test(body))
|
|
1479
|
+
return false;
|
|
1480
|
+
const read = `(?:${JS_FILE_READ_METHODS})\\s*\\(\\s*${file}\\b`;
|
|
1481
|
+
return new RegExp(`\\b(?:const|let|var)\\s+${body}\\s*=\\s*(?:await\\s+)?[\\w$.]*${read}`).test(code) ||
|
|
1482
|
+
new RegExp(`\\b${read}[^;]*?\\(\\s*[\\w$]+\\s*,\\s*${body}\\s*\\)\\s*=>`).test(code) ||
|
|
1483
|
+
new RegExp(`\\b${read}[^;]*?function\\s*\\(\\s*[\\w$]+\\s*,\\s*${body}\\s*\\)`).test(code);
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1487
|
+
* True when the response written by the xss sink `sinkCall` has an explicit
|
|
1488
|
+
* Content-Type that a browser does not render as a document. Every type set
|
|
1489
|
+
* on that response before the write must be non-renderable, at least one
|
|
1490
|
+
* must be set on every path to the write, and no call after it (on any path)
|
|
1491
|
+
* may change the type: a header write we cannot read, a removal, a method not
|
|
1492
|
+
* known to leave the type alone, a call chained on or through a member of the
|
|
1493
|
+
* response, or the response (or another name for it) handed on or wrapped.
|
|
1494
|
+
*/
|
|
1495
|
+
function jsResponseTypeIsNotRenderable(code, calls, sinkCall) {
|
|
1496
|
+
const receiver = sinkCall.receiver;
|
|
1497
|
+
if (!receiver)
|
|
1498
|
+
return false;
|
|
1499
|
+
const line = sinkCall.location.line;
|
|
1500
|
+
const range = jsEnclosingCallbackRange(calls, line);
|
|
1501
|
+
const inScope = (c) => range
|
|
1502
|
+
? c.location.line >= range[0] && c.location.line <= range[1]
|
|
1503
|
+
: (c.in_method ?? null) === (sinkCall.in_method ?? null);
|
|
1504
|
+
const lineStarts = [0];
|
|
1505
|
+
for (let i = 0; i < code.length; i++)
|
|
1506
|
+
if (code[i] === '\n')
|
|
1507
|
+
lineStarts.push(i + 1);
|
|
1508
|
+
const sinkAt = jsCallOffset(lineStarts, sinkCall);
|
|
1509
|
+
// A helper called inside the write may set the type: `res.end(render(res))`.
|
|
1510
|
+
const aliases = jsReceiverAliases(code, receiver);
|
|
1511
|
+
if (sinkCall.arguments.some(a => [receiver, ...aliases].some(n => jsArgumentPassesReceiver(a.expression, n)))) {
|
|
1512
|
+
return false;
|
|
1513
|
+
}
|
|
1514
|
+
// Offsets of the last type set that runs on every path to the write, and of
|
|
1515
|
+
// the last call that may change the type to one we cannot read.
|
|
1516
|
+
let lastSetAt = -1;
|
|
1517
|
+
let lastChangeAt = -1;
|
|
1518
|
+
// The response handed on or wrapped (`asHtml(out)`, `{ res }`, `[res]`,
|
|
1519
|
+
// `(res)`): we cannot follow it, so it may change the type from there.
|
|
1520
|
+
for (const at of jsReceiverEscapes(code, [receiver, ...aliases], sinkAt)) {
|
|
1521
|
+
lastChangeAt = Math.max(lastChangeAt, at);
|
|
1522
|
+
}
|
|
1523
|
+
for (const setter of calls) {
|
|
1524
|
+
if (setter.location.line > line || !inScope(setter))
|
|
1525
|
+
continue;
|
|
1526
|
+
const setterAt = jsCallOffset(lineStarts, setter);
|
|
1527
|
+
if (setterAt > sinkAt)
|
|
1528
|
+
continue;
|
|
1529
|
+
const target = jsCallTarget(setter);
|
|
1530
|
+
let call = target.receiver === setter.receiver && target.method === setter.method_name
|
|
1531
|
+
? setter
|
|
1532
|
+
: { ...setter, receiver: target.receiver, method_name: target.method };
|
|
1533
|
+
const recv = call.receiver;
|
|
1534
|
+
if (recv !== null && [...aliases].some(a => jsReceiverChainKind(recv, a) !== null)) {
|
|
1535
|
+
// Another name for the response (`const out = res`): any call on it
|
|
1536
|
+
// that may touch the type, a type set included, keeps the sink.
|
|
1537
|
+
if (!JS_TYPE_KEEPING_METHODS.has(call.method_name)) {
|
|
1538
|
+
lastChangeAt = Math.max(lastChangeAt, setterAt);
|
|
1539
|
+
}
|
|
1540
|
+
continue;
|
|
1541
|
+
}
|
|
1542
|
+
const kind = recv === null ? null : jsReceiverChainKind(recv, receiver);
|
|
1543
|
+
if (kind === 'member') {
|
|
1544
|
+
// A call on a member of the response (`res.locals.x.f()`): we cannot
|
|
1545
|
+
// tell what it touches, so unless it leaves the type alone, keep it.
|
|
1546
|
+
if (!JS_TYPE_KEEPING_METHODS.has(call.method_name)) {
|
|
1547
|
+
lastChangeAt = Math.max(lastChangeAt, setterAt);
|
|
1548
|
+
}
|
|
1549
|
+
continue;
|
|
1550
|
+
}
|
|
1551
|
+
// A call chained on the response (`res.status(200).type('html')`) acts
|
|
1552
|
+
// on the response itself.
|
|
1553
|
+
if (kind === 'self' && recv !== receiver)
|
|
1554
|
+
call = { ...call, receiver };
|
|
1555
|
+
if (call.receiver !== receiver) {
|
|
1556
|
+
// A helper handed the response may set its type: `setJsonType(res)`.
|
|
1557
|
+
if (setter.arguments.some(a => jsArgumentPassesReceiver(a.expression, receiver))) {
|
|
1558
|
+
lastChangeAt = Math.max(lastChangeAt, setterAt);
|
|
1559
|
+
}
|
|
1560
|
+
continue;
|
|
1561
|
+
}
|
|
1562
|
+
// The write itself, or an earlier `c.body`, which is another response.
|
|
1563
|
+
if (call.method_name === 'body' ? setter !== sinkCall : setter === sinkCall)
|
|
1564
|
+
continue;
|
|
1565
|
+
if (jsHeaderWriteMayChangeType(call))
|
|
1566
|
+
lastChangeAt = Math.max(lastChangeAt, setterAt);
|
|
1567
|
+
if (!JS_CONTENT_TYPE_SETTERS.has(call.method_name))
|
|
1568
|
+
continue;
|
|
1569
|
+
const types = jsContentTypeExpressions(call);
|
|
1570
|
+
if (types.length === 0)
|
|
1571
|
+
continue;
|
|
1572
|
+
const ok = types.every(expr => {
|
|
1573
|
+
const lit = JS_STRING_LITERAL_RE.exec(expr);
|
|
1574
|
+
if (lit)
|
|
1575
|
+
return !JS_RENDERABLE_TYPE_RE.test(lit[2]);
|
|
1576
|
+
return jsTypeIsServedFileMime(code, expr, setterAt, sinkCall);
|
|
1577
|
+
});
|
|
1578
|
+
if (!ok)
|
|
1579
|
+
return false;
|
|
1580
|
+
if (setter === sinkCall || jsStatementDominates(code, setterAt, sinkAt)) {
|
|
1581
|
+
lastSetAt = Math.max(lastSetAt, setterAt);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
return lastSetAt >= 0 && lastChangeAt < lastSetAt;
|
|
1585
|
+
}
|
|
1128
1586
|
// skillsregistry#49 — the classless `fetch` SSRF sink (config-loader) exists for
|
|
1129
1587
|
// the global Web `fetch` API, but `method: 'fetch'` matches any `.fetch(` call —
|
|
1130
1588
|
// including SDK/ORM entity lookups like discord.js `client.channels.fetch(id)` /
|
|
@@ -1144,8 +1602,6 @@ const FETCH_MEMBER_RECEIVER_RE = /[\w$)\]]\s*\.\s*fetch\s*\(/;
|
|
|
1144
1602
|
// sink" test. Real `collection.find({ ... })` passes an object literal and is
|
|
1145
1603
|
// unaffected.
|
|
1146
1604
|
const NOSQL_ARRAY_METHODS = new Set(['find', 'filter', 'some', 'every', 'findIndex', 'findLast']);
|
|
1147
|
-
/** A shell or interpreter that would run its arguments as a command. */
|
|
1148
|
-
const SHELL_PROGRAM = /(?:^|[\\/])(?:sh|bash|zsh|dash|ksh|fish|cmd(?:\.exe)?|powershell(?:\.exe)?|pwsh|node|python3?|perl|ruby|php|osascript|env)$/i;
|
|
1149
1605
|
const FUNCTION_FIRST_ARG_RE = /\.\s*(?:find|filter|some|every|findIndex|findLast)\s*\(\s*(?:async\s+)?(?:function\b|(?:\([^()]*\)|[A-Za-z_$][\w$]*)\s*=>)/;
|
|
1150
1606
|
// cognium-dev#1000 — `recv.find(name)` with a single bare identifier argument.
|
|
1151
1607
|
const NAMED_ARRAY_PREDICATE_RE = /([A-Za-z_$][\w$]*)\s*\.\s*(find|filter|some|every|findIndex|findLast)\s*\(\s*([A-Za-z_$][\w$]*)\s*\)/;
|
|
@@ -1508,6 +1964,162 @@ function isSchemeHostPrefix(prefix) {
|
|
|
1508
1964
|
* host-guarded above (following one-hop derivations), and the guard has a
|
|
1509
1965
|
* throwing/returning body (reject polarity — the safe path is everything below).
|
|
1510
1966
|
*/
|
|
1967
|
+
/** Names bound to the in-process supertest helper (`require('supertest')` / default import). */
|
|
1968
|
+
function jsSupertestRequestBindingNames(code) {
|
|
1969
|
+
const names = new Set();
|
|
1970
|
+
for (const line of code.split('\n')) {
|
|
1971
|
+
let m = line.match(/\b(?:const|let|var)\s+(\w+)\s*=\s*require\s*\(\s*['"]supertest['"]\s*\)/);
|
|
1972
|
+
if (m)
|
|
1973
|
+
names.add(m[1]);
|
|
1974
|
+
m = line.match(/\bimport\s+(\w+)\s+from\s+['"]supertest['"]/);
|
|
1975
|
+
if (m)
|
|
1976
|
+
names.add(m[1]);
|
|
1977
|
+
m = line.match(/\bimport\s+\*\s+as\s+(\w+)\s+from\s+['"]supertest['"]/);
|
|
1978
|
+
if (m)
|
|
1979
|
+
names.add(m[1]);
|
|
1980
|
+
m = line.match(/\bimport\s+(\w+)\s*,\s*\{[^}]*\}\s*from\s+['"]supertest['"]/);
|
|
1981
|
+
if (m)
|
|
1982
|
+
names.add(m[1]);
|
|
1983
|
+
}
|
|
1984
|
+
return names;
|
|
1985
|
+
}
|
|
1986
|
+
/**
|
|
1987
|
+
* Stage 15f1 (#1332) — true for `/^…$/` regex literals whose body is a finite
|
|
1988
|
+
* allowlist: explicit anchors, no unescaped `.` wildcard. Alternation inside
|
|
1989
|
+
* the anchored body is allowed (BenchProctor express_ts safe cmdi/xpathi shapes).
|
|
1990
|
+
*/
|
|
1991
|
+
function isJsAnchoredFiniteAllowlistRegexBody(src) {
|
|
1992
|
+
if (!src.startsWith('^') || !src.endsWith('$'))
|
|
1993
|
+
return false;
|
|
1994
|
+
const inner = src.slice(1, -1);
|
|
1995
|
+
if (inner === '')
|
|
1996
|
+
return false;
|
|
1997
|
+
const stripped = inner.replace(/\[(?:[^\]\\]|\\.)*\]/g, '');
|
|
1998
|
+
const cleaned = stripped.replace(/\\./g, '');
|
|
1999
|
+
return !cleaned.includes('.');
|
|
2000
|
+
}
|
|
2001
|
+
/**
|
|
2002
|
+
* Returns 1-indexed lines whose sinks are covered by a reject-guard:
|
|
2003
|
+
* `if (!/^allowlist$/.test(var)) { … return|throw; … }` then uses `var`
|
|
2004
|
+
* (or one-hop assignment from it) below. Mirrors `jsSsrfHostGuardedLines`.
|
|
2005
|
+
*/
|
|
2006
|
+
function jsAnchoredAllowlistRejectGuardedLines(code) {
|
|
2007
|
+
const covered = new Set();
|
|
2008
|
+
const lines = code.split('\n');
|
|
2009
|
+
const rejectRegexTest = /\bif\s*\(\s*!\s*\/((?:[^/\\]|\\.)*)\/[a-z]*\s*\.\s*test\s*\(\s*(?:String\s*\(\s*)?([A-Za-z_$][\w$]*)(?:\s*\))?\s*\)\s*\)/;
|
|
2010
|
+
const terminatorRe = /\b(throw|return)\b/;
|
|
2011
|
+
const assignRe = /^\s*(?:const|let|var\s+)?\s*([A-Za-z_$][\w$]*)\s*=(?!=)\s*(.*)$/;
|
|
2012
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2013
|
+
const rm = rejectRegexTest.exec(lines[i] ?? '');
|
|
2014
|
+
if (!rm || !isJsAnchoredFiniteAllowlistRegexBody(rm[1] ?? ''))
|
|
2015
|
+
continue;
|
|
2016
|
+
const guardedVar = rm[2];
|
|
2017
|
+
if (!guardedVar)
|
|
2018
|
+
continue;
|
|
2019
|
+
let hasTerminator = terminatorRe.test(lines[i] ?? '');
|
|
2020
|
+
if (!hasTerminator) {
|
|
2021
|
+
for (let j = i + 1; j < Math.min(lines.length, i + 6); j++) {
|
|
2022
|
+
const ln = lines[j] ?? '';
|
|
2023
|
+
if (terminatorRe.test(ln)) {
|
|
2024
|
+
hasTerminator = true;
|
|
2025
|
+
break;
|
|
2026
|
+
}
|
|
2027
|
+
// Closing brace of the guard `if` — not `});` inside the block body.
|
|
2028
|
+
if (/^\s*}\s*$/.test(ln) || /^\s*}\s*else\b/.test(ln))
|
|
2029
|
+
break;
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
if (!hasTerminator)
|
|
2033
|
+
continue;
|
|
2034
|
+
const guarded = new Set([guardedVar]);
|
|
2035
|
+
const mentionsGuarded = (text) => {
|
|
2036
|
+
for (const name of guarded)
|
|
2037
|
+
if (new RegExp(`\\b${name}\\b`).test(text))
|
|
2038
|
+
return true;
|
|
2039
|
+
return false;
|
|
2040
|
+
};
|
|
2041
|
+
for (let p = 0; p <= i; p++) {
|
|
2042
|
+
const pre = assignRe.exec(lines[p] ?? '');
|
|
2043
|
+
if (pre && mentionsGuarded(pre[2]))
|
|
2044
|
+
guarded.add(pre[1]);
|
|
2045
|
+
}
|
|
2046
|
+
for (let l = i + 1; l < lines.length; l++) {
|
|
2047
|
+
const lineText = lines[l] ?? '';
|
|
2048
|
+
const assign = assignRe.exec(lineText);
|
|
2049
|
+
if (assign) {
|
|
2050
|
+
if (mentionsGuarded(assign[2]))
|
|
2051
|
+
guarded.add(assign[1]);
|
|
2052
|
+
else if (assign[1] !== guardedVar)
|
|
2053
|
+
guarded.delete(assign[1]);
|
|
2054
|
+
}
|
|
2055
|
+
if (mentionsGuarded(lineText))
|
|
2056
|
+
covered.add(l + 1);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
return covered;
|
|
2060
|
+
}
|
|
2061
|
+
/**
|
|
2062
|
+
* Stage 19b (#1347) — Python: `if not re.fullmatch(<allowlist>, var): return`
|
|
2063
|
+
* before subprocess / exec-shaped sinks. Covers interop IL-77 safe mirrors
|
|
2064
|
+
* where a numeric allowlist rejects attacker input before crossing to node.
|
|
2065
|
+
*/
|
|
2066
|
+
function pythonFullmatchRejectGuardedLines(code) {
|
|
2067
|
+
const covered = new Set();
|
|
2068
|
+
const lines = code.split('\n');
|
|
2069
|
+
const rejectFullmatch = /\bif\s+not\s+re\s*\.\s*(?:fullmatch|match)\s*\(\s*r["']((?:[^"'\\]|\\.)*)["']\s*,\s*([A-Za-z_]\w*)\s*\)/;
|
|
2070
|
+
const terminatorRe = /\b(throw|return)\b/;
|
|
2071
|
+
const assignRe = /^\s*([A-Za-z_]\w*)\s*=(?!=)\s*(.*)$/;
|
|
2072
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2073
|
+
const rm = rejectFullmatch.exec(lines[i] ?? '');
|
|
2074
|
+
if (!rm || !isImplicitlyAnchoredAllowlistRegex(rm[1] ?? ''))
|
|
2075
|
+
continue;
|
|
2076
|
+
const guardedVar = rm[2];
|
|
2077
|
+
if (!guardedVar)
|
|
2078
|
+
continue;
|
|
2079
|
+
let hasTerminator = terminatorRe.test(lines[i] ?? '');
|
|
2080
|
+
if (!hasTerminator) {
|
|
2081
|
+
for (let j = i + 1; j < Math.min(lines.length, i + 6); j++) {
|
|
2082
|
+
const ln = lines[j] ?? '';
|
|
2083
|
+
if (terminatorRe.test(ln)) {
|
|
2084
|
+
hasTerminator = true;
|
|
2085
|
+
break;
|
|
2086
|
+
}
|
|
2087
|
+
if (/^\s*elif\b/.test(ln) || /^\s*else\s*:/.test(ln))
|
|
2088
|
+
break;
|
|
2089
|
+
if (ln.trim() !== '' && !/^\s+/.test(ln))
|
|
2090
|
+
break;
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
if (!hasTerminator)
|
|
2094
|
+
continue;
|
|
2095
|
+
const guarded = new Set([guardedVar]);
|
|
2096
|
+
const mentionsGuarded = (text) => {
|
|
2097
|
+
for (const name of guarded) {
|
|
2098
|
+
if (new RegExp(`\\b${escapeRegex(name)}\\b`).test(text))
|
|
2099
|
+
return true;
|
|
2100
|
+
}
|
|
2101
|
+
return false;
|
|
2102
|
+
};
|
|
2103
|
+
for (let p = 0; p <= i; p++) {
|
|
2104
|
+
const pre = assignRe.exec(lines[p] ?? '');
|
|
2105
|
+
if (pre && mentionsGuarded(pre[2]))
|
|
2106
|
+
guarded.add(pre[1]);
|
|
2107
|
+
}
|
|
2108
|
+
for (let l = i + 1; l < lines.length; l++) {
|
|
2109
|
+
const lineText = lines[l] ?? '';
|
|
2110
|
+
const assign = assignRe.exec(lineText);
|
|
2111
|
+
if (assign) {
|
|
2112
|
+
if (mentionsGuarded(assign[2]))
|
|
2113
|
+
guarded.add(assign[1]);
|
|
2114
|
+
else if (assign[1] !== guardedVar)
|
|
2115
|
+
guarded.delete(assign[1]);
|
|
2116
|
+
}
|
|
2117
|
+
if (mentionsGuarded(lineText))
|
|
2118
|
+
covered.add(l + 1);
|
|
2119
|
+
}
|
|
2120
|
+
}
|
|
2121
|
+
return covered;
|
|
2122
|
+
}
|
|
1511
2123
|
function jsSsrfHostGuardedLines(code) {
|
|
1512
2124
|
const covered = new Set();
|
|
1513
2125
|
const lines = code.split('\n');
|
|
@@ -2310,8 +2922,16 @@ function guardTestsValue(line, values) {
|
|
|
2310
2922
|
return values.some(v => new RegExp(`(?<![\\w$.])${v.replace(/[.$]/g, '\\$&')}(?![\\w$])`).test(line));
|
|
2311
2923
|
}
|
|
2312
2924
|
export class SinkFilterPass {
|
|
2925
|
+
javaOpensResponseStream;
|
|
2313
2926
|
name = 'sink-filter';
|
|
2314
2927
|
category = 'security';
|
|
2928
|
+
/**
|
|
2929
|
+
* @param javaOpensResponseStream another Java file of the project calls
|
|
2930
|
+
* `getOutputStream()` (Stage 15e); see `javaProjectOpensResponseStream`.
|
|
2931
|
+
*/
|
|
2932
|
+
constructor(javaOpensResponseStream = false) {
|
|
2933
|
+
this.javaOpensResponseStream = javaOpensResponseStream;
|
|
2934
|
+
}
|
|
2315
2935
|
run(ctx) {
|
|
2316
2936
|
const { graph, language } = ctx;
|
|
2317
2937
|
const { calls, dfg } = graph.ir;
|
|
@@ -2574,6 +3194,14 @@ export class SinkFilterPass {
|
|
|
2574
3194
|
return true;
|
|
2575
3195
|
return !jsLibxmljsNoentDisablesXxe(ctx.code, sink.line);
|
|
2576
3196
|
});
|
|
3197
|
+
// Stage 7c — JS/TS xss: response written with a non-HTML Content-Type
|
|
3198
|
+
//. See JS_RESPONSE_WRITE_METHODS above.
|
|
3199
|
+
filtered = filtered.filter(sink => {
|
|
3200
|
+
if (sink.type !== 'xss' || !JS_RESPONSE_WRITE_METHODS.has(sink.method ?? ''))
|
|
3201
|
+
return true;
|
|
3202
|
+
const sinkCall = (callsByLine.get(sink.line) ?? []).find(c => c.method_name === sink.method);
|
|
3203
|
+
return !sinkCall || !jsResponseTypeIsNotRenderable(ctx.code, calls, sinkCall);
|
|
3204
|
+
});
|
|
2577
3205
|
}
|
|
2578
3206
|
// Stage 8 — JavaScript open_redirect / crlf / header_injection FP reduction.
|
|
2579
3207
|
// Suppresses res.redirect(url) and res.setHeader(...) sinks when an
|
|
@@ -3302,11 +3930,21 @@ export class SinkFilterPass {
|
|
|
3302
3930
|
// and an unresolved real writer print would lose the latter. The sink
|
|
3303
3931
|
// carries no column, and every other stage here judges on line text, so
|
|
3304
3932
|
// this follows the existing convention rather than inventing a new one.
|
|
3933
|
+
//
|
|
3934
|
+
// A sink WITH a resolved class carries the receiver's type. It is dropped
|
|
3935
|
+
// only when that type is a PrintStream or a logger and neither the file
|
|
3936
|
+
// nor any other Java file of the project opens the response's output
|
|
3937
|
+
// stream (JAVA_NON_HTML_PRINT_RECEIVERS).
|
|
3305
3938
|
if (language === 'java') {
|
|
3306
3939
|
const sourceLines = ctx.code.split('\n');
|
|
3940
|
+
const writesResponseStream = this.javaOpensResponseStream
|
|
3941
|
+
|| JAVA_RESPONSE_OUTPUT_STREAM_RE.test(ctx.code);
|
|
3307
3942
|
filtered = filtered.filter(sink => {
|
|
3308
|
-
if (sink.type !== 'xss'
|
|
3943
|
+
if (sink.type !== 'xss')
|
|
3309
3944
|
return true;
|
|
3945
|
+
if (sink.class !== undefined) {
|
|
3946
|
+
return writesResponseStream || !JAVA_NON_HTML_PRINT_RECEIVERS.has(sink.class);
|
|
3947
|
+
}
|
|
3310
3948
|
return !JAVA_STDOUT_PRINT_RE.test(sourceLines[sink.line - 1] ?? '');
|
|
3311
3949
|
});
|
|
3312
3950
|
}
|
|
@@ -3403,7 +4041,7 @@ export class SinkFilterPass {
|
|
|
3403
4041
|
|| (literal !== null && !SHELL_PROGRAM.test(literal[2]) && !runsArgvAsCommand(literal[2])); // #1066
|
|
3404
4042
|
if (!fixed)
|
|
3405
4043
|
return true;
|
|
3406
|
-
if (
|
|
4044
|
+
if (SHELL_OPTION_ON.test(stmt))
|
|
3407
4045
|
return true;
|
|
3408
4046
|
if (/['"`]-(?:e|p|-eval|-print)['"`]/.test(stmt))
|
|
3409
4047
|
return true;
|
|
@@ -3562,6 +4200,42 @@ export class SinkFilterPass {
|
|
|
3562
4200
|
filtered = filtered.filter(sink => !(sink.type === 'ssrf' && guardedLines.has(sink.line)));
|
|
3563
4201
|
}
|
|
3564
4202
|
}
|
|
4203
|
+
// Stage 15f1 — JS/TS: anchored allowlist reject-guard on user input
|
|
4204
|
+
// (cognium-dev#1332 BenchProctor express_ts). `if (!/^tokens$/.test(x)) return`
|
|
4205
|
+
// before cmdi/xpathi/sqli/xss sinks drops the sink when the line references
|
|
4206
|
+
// the guarded (or one-hop derived) variable.
|
|
4207
|
+
if (['javascript', 'typescript'].includes(language)) {
|
|
4208
|
+
const allowlistGuarded = jsAnchoredAllowlistRejectGuardedLines(ctx.code);
|
|
4209
|
+
if (allowlistGuarded.size > 0) {
|
|
4210
|
+
const CREDITED = new Set([
|
|
4211
|
+
'command_injection',
|
|
4212
|
+
'xpath_injection',
|
|
4213
|
+
'sql_injection',
|
|
4214
|
+
'xss',
|
|
4215
|
+
'unsafe_deserialization',
|
|
4216
|
+
'code_injection',
|
|
4217
|
+
'path_traversal',
|
|
4218
|
+
]);
|
|
4219
|
+
filtered = filtered.filter(sink => !(CREDITED.has(sink.type) && allowlistGuarded.has(sink.line)));
|
|
4220
|
+
}
|
|
4221
|
+
}
|
|
4222
|
+
// Stage 15f2 — JS/TS ssrf: supertest drives an in-process Express app, not a
|
|
4223
|
+
// network URL (#1326). Drop the classless `request(app)` SSRF sink when the
|
|
4224
|
+
// callee is bound to `require('supertest')` / a default import from
|
|
4225
|
+
// supertest.
|
|
4226
|
+
if (['javascript', 'typescript'].includes(language)) {
|
|
4227
|
+
const supertestNames = jsSupertestRequestBindingNames(ctx.code);
|
|
4228
|
+
if (supertestNames.size > 0) {
|
|
4229
|
+
filtered = filtered.filter(sink => {
|
|
4230
|
+
if (sink.type !== 'ssrf' || sink.method !== 'request')
|
|
4231
|
+
return true;
|
|
4232
|
+
const callee = (sink.code ?? '').match(/^([A-Za-z_$][\w$]*)\s*\(/);
|
|
4233
|
+
if (callee && supertestNames.has(callee[1]))
|
|
4234
|
+
return false;
|
|
4235
|
+
return true;
|
|
4236
|
+
});
|
|
4237
|
+
}
|
|
4238
|
+
}
|
|
3565
4239
|
// Stage 15k — C# sanitizer suppression (cognium-dev C#/.NET Phase-1).
|
|
3566
4240
|
// C# rides the text-scan propagation, which is not sanitizer-aware; apply
|
|
3567
4241
|
// sanitizers here (sink-type-aware, C#-scoped). Drop a sink when its line
|
|
@@ -3939,6 +4613,15 @@ export class SinkFilterPass {
|
|
|
3939
4613
|
return false;
|
|
3940
4614
|
});
|
|
3941
4615
|
}
|
|
4616
|
+
// Stage 19b — Python command/code injection: inline `re.fullmatch` reject-guard
|
|
4617
|
+
// (#1347 interop IL-77 safe py→node mirror).
|
|
4618
|
+
if (language === 'python') {
|
|
4619
|
+
const guardedLines = pythonFullmatchRejectGuardedLines(ctx.code);
|
|
4620
|
+
if (guardedLines.size > 0) {
|
|
4621
|
+
const CREDITED = new Set(['command_injection', 'code_injection']);
|
|
4622
|
+
filtered = filtered.filter(sink => !(CREDITED.has(sink.type) && guardedLines.has(sink.line)));
|
|
4623
|
+
}
|
|
4624
|
+
}
|
|
3942
4625
|
return { sources, sinks: filtered, sanitizers };
|
|
3943
4626
|
}
|
|
3944
4627
|
}
|