loadwise 0.5.0 → 0.6.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/README.md +543 -628
- package/bin/loadwise.js +5 -1
- package/lib/accessibility/frontend-detect.js +2 -1
- package/lib/accessibility/rule-ids.js +12 -9
- package/lib/accessibility/template-attributes.js +4 -2
- package/{editor/accessibility-examples.cjs → lib/editor/accessibility-examples.js} +20 -21
- package/lib/editor/actions.js +117 -0
- package/{editor/apply-fix.cjs → lib/editor/apply-fix.js} +1 -6
- package/{editor/control-flow-comparison.cjs → lib/editor/control-flow-comparison.js} +1 -3
- package/lib/editor/diagnostics.js +365 -0
- package/lib/editor/dismissals.js +111 -0
- package/lib/editor/editor-document.js +99 -0
- package/{editor/finding-comparison.cjs → lib/editor/finding-comparison.js} +4 -7
- package/lib/editor/fix-edits.js +200 -0
- package/lib/editor/language-server.js +437 -0
- package/{editor/maintainability-examples.cjs → lib/editor/maintainability-examples.js} +1 -3
- package/{editor/perf-site-comparison.cjs → lib/editor/perf-site-comparison.js} +1 -3
- package/lib/editor/perf-watch.js +78 -0
- package/{editor/performance-examples.cjs → lib/editor/performance-examples.js} +129 -128
- package/{editor/reliability-examples.cjs → lib/editor/reliability-examples.js} +100 -41
- package/lib/editor/scan-session.js +334 -0
- package/{editor/security-examples.cjs → lib/editor/security-examples.js} +28 -32
- package/lib/editor/server-commands.js +232 -0
- package/{editor/source-example.cjs → lib/editor/source-example.js} +3 -5
- package/{editor/standalone-performance-examples.cjs → lib/editor/standalone-performance-examples.js} +349 -333
- package/lib/editor/types/dismissal.js +1 -0
- package/lib/editor/types/fix-example.js +1 -0
- package/lib/editor/types/source-document.js +1 -0
- package/lib/editor/workspace-files.js +78 -0
- package/lib/editor/workspace-paths.js +22 -0
- package/lib/maintainability/analyze-source.js +2 -0
- package/lib/maintainability/apply-fix.js +1 -1
- package/lib/maintainability/cli.js +1 -1
- package/lib/maintainability/examples.js +58 -41
- package/lib/maintainability/rule-ids.js +29 -14
- package/lib/maintainability/scan-changed.js +3 -0
- package/lib/maintainability/scan.js +13 -9
- package/lib/maintainability/tsconfig-strict.js +69 -0
- package/lib/maintainability/type-safety-hygiene.js +204 -0
- package/lib/performance/analyze.js +2 -0
- package/lib/performance/cli-arguments.js +0 -2
- package/lib/performance/cli-policy.js +17 -9
- package/lib/performance/cli-scan.js +10 -8
- package/lib/performance/cli.js +4 -23
- package/lib/performance/editor-signal.js +1 -1
- package/lib/performance/finding-text.js +3 -3
- package/lib/performance/fix-cli.js +1 -1
- package/lib/performance/frontend-perf.js +7 -0
- package/lib/performance/frontend-render-perf.js +200 -0
- package/lib/performance/llm-max-tokens.js +56 -0
- package/lib/performance/native.js +46 -26
- package/lib/performance/polyglot.js +22 -1
- package/lib/performance/pooling.js +38 -23
- package/lib/performance/projects.js +42 -21
- package/lib/performance/rule-ids.js +236 -222
- package/lib/performance/site-fixes/database.js +3 -1
- package/lib/performance/site-fixes/integrity.js +15 -1
- package/lib/performance/source-inputs.js +18 -6
- package/lib/performance/unscanned-javascript.js +66 -19
- package/lib/reliability/analyze.js +12 -0
- package/lib/reliability/child-process.js +106 -0
- package/lib/reliability/error-values.js +177 -0
- package/lib/reliability/pool-timeout.js +79 -0
- package/lib/reliability/react-bugs.js +602 -0
- package/lib/reliability/resource-leaks.js +3 -6
- package/lib/reliability/rule-ids.js +169 -127
- package/lib/reliability/switch-exhaustive.js +284 -0
- package/lib/reliability/uncaught-exception.js +223 -0
- package/lib/security/auth-crypto/rule-ids.js +3 -2
- package/lib/security/cli.js +6 -5
- package/lib/security/deps/rule-ids.js +1 -0
- package/lib/security/examples/auth-crypto.cjs +5 -4
- package/lib/security/examples/deps.cjs +1 -0
- package/lib/security/examples/exposure.cjs +55 -42
- package/lib/security/examples/files.cjs +1 -0
- package/lib/security/examples/injection.cjs +1 -0
- package/lib/security/examples/moved.cjs +5 -4
- package/lib/security/examples/request-forgery.cjs +1 -0
- package/lib/security/exposure/client-env.js +113 -0
- package/lib/security/exposure/frontend-security.js +12 -10
- package/lib/security/exposure/index.js +14 -3
- package/lib/security/exposure/jsx-url.js +267 -0
- package/lib/security/exposure/next-image-config.js +34 -0
- package/lib/security/exposure/package-manifest.js +45 -0
- package/lib/security/exposure/react-version.js +45 -0
- package/lib/security/exposure/rule-ids.js +16 -12
- package/lib/security/files/index.js +1 -1
- package/lib/security/files/rule-ids.js +1 -0
- package/lib/security/injection/code.js +24 -10
- package/lib/security/injection/command.js +81 -28
- package/lib/security/injection/nosql.js +109 -22
- package/lib/security/injection/redos.js +152 -308
- package/lib/security/injection/regex-pattern.js +290 -0
- package/lib/security/injection/regex.js +30 -11
- package/lib/security/injection/rule-ids.js +2 -1
- package/lib/security/injection/shared.js +19 -1
- package/lib/security/injection/sql.js +83 -2
- package/lib/security/request-forgery/rule-ids.js +1 -0
- package/lib/security/rule-descriptions.js +36 -32
- package/lib/security/rule-ids.js +74 -67
- package/lib/security/taint/filename-guard.js +3 -1
- package/lib/security/taint/guards.js +84 -10
- package/lib/security/taint/index.js +124 -13
- package/lib/security/taint/propagate.js +10 -7
- package/lib/security/taint/sanitizers.js +7 -3
- package/lib/security/taint/sources.js +6 -3
- package/lib/security/taint/summaries.js +13 -1
- package/lib/security/taint/url-parts.js +14 -5
- package/lib/shared/agent-session.js +2 -2
- package/lib/shared/check.js +16 -6
- package/lib/shared/explain.js +5 -5
- package/lib/shared/fix-convergence.js +1 -1
- package/lib/shared/formats/finding-location.js +6 -2
- package/lib/shared/formats/sarif.js +3 -3
- package/lib/shared/heap-limit.js +20 -3
- package/lib/shared/pillars.js +7 -7
- package/lib/shared/rule-docs.js +12 -0
- package/lib/shared/scan-cache.js +0 -1
- package/lib/shared/workspaces.js +31 -9
- package/package.json +12 -19
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/schema/loadwise.config.schema.json +44 -0
- package/editor/README.md +0 -47
- package/examples/redis-keys.otlp.json +0 -22
- package/lib/maintainability/notify-editor.js +0 -1
- package/lib/performance/editor-report.js +0 -191
- package/lib/performance/editor-setup.js +0 -189
- package/lib/performance/editor-watch-supervisor.js +0 -104
- package/lib/performance/editor-watch.js +0 -193
- package/lib/performance/notify-editor.js +0 -1
- package/lib/performance/recursive-watch.js +0 -181
- /package/lib/{performance/types/editor-report.js → editor/types/comparison.js} +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// next-image-any-remote-host: `images.remotePatterns` in a Next.js config lets `hostname: '**'`
|
|
2
|
+
// match every host, so `/_next/image?url=...` fetches and serves images from any host an attacker
|
|
3
|
+
// names within the entry's protocol, port and pathname: a proxy that bills the app for the bandwidth.
|
|
4
|
+
// Before Next.js 16 (or with `dangerouslyAllowLocalIP`) it also reaches local addresses.
|
|
5
|
+
import { ts } from '../../performance/compiler.js';
|
|
6
|
+
import { nameText, unwrap } from './literals.js';
|
|
7
|
+
const ruleId = 'next-image-any-remote-host';
|
|
8
|
+
const message = "`hostname: '**'` in `images.remotePatterns` lets the Next.js image optimizer fetch and serve images from any host, so anyone can use `/_next/image` as an image proxy for any host the entry's protocol, port and pathname allow (before Next.js 16, local IP addresses too). List the exact hosts (or a `**.your-cdn.com` subdomain pattern) instead.";
|
|
9
|
+
// `*` matches one host label; `**` matches every host.
|
|
10
|
+
const anyHost = '**';
|
|
11
|
+
function propertyName(node) {
|
|
12
|
+
return node && ts.isPropertyAssignment(node) ? nameText(node.name) : undefined;
|
|
13
|
+
}
|
|
14
|
+
// hostname -> { } -> [ ] -> remotePatterns: -> { } -> images:
|
|
15
|
+
function isRemotePatternHost(property) {
|
|
16
|
+
const pattern = property.parent;
|
|
17
|
+
const patterns = pattern.parent;
|
|
18
|
+
if (!ts.isArrayLiteralExpression(patterns))
|
|
19
|
+
return false;
|
|
20
|
+
const remotePatterns = patterns.parent;
|
|
21
|
+
if (propertyName(remotePatterns) !== 'remotePatterns')
|
|
22
|
+
return false;
|
|
23
|
+
return propertyName(remotePatterns.parent.parent) === 'images';
|
|
24
|
+
}
|
|
25
|
+
function checkProperty(property, context) {
|
|
26
|
+
if (nameText(property.name) !== 'hostname')
|
|
27
|
+
return;
|
|
28
|
+
const value = unwrap(property.initializer);
|
|
29
|
+
if (!ts.isStringLiteralLike(value) || value.text !== anyHost)
|
|
30
|
+
return;
|
|
31
|
+
if (isRemotePatternHost(property))
|
|
32
|
+
context.report(property, ruleId, message);
|
|
33
|
+
}
|
|
34
|
+
export { checkProperty };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// The nearest package.json that answers a question about a file's project, read fresh on every
|
|
2
|
+
// call. Callers ask only after a candidate finding, so the walk is rare, and nothing goes stale
|
|
3
|
+
// when the editor's long-lived process sees a manifest change.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
const DEPENDENCY_FIELDS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
|
|
7
|
+
function readManifest(file) {
|
|
8
|
+
try {
|
|
9
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
10
|
+
return parsed !== null && typeof parsed === 'object' ? { ...parsed } : undefined;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
// A missing or malformed manifest answers nothing; the search continues upward.
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function declaredRange(manifest, packageName) {
|
|
18
|
+
for (const field of DEPENDENCY_FIELDS) {
|
|
19
|
+
const dependencies = manifest[field];
|
|
20
|
+
if (dependencies === null || typeof dependencies !== 'object')
|
|
21
|
+
continue;
|
|
22
|
+
const range = Object.entries(dependencies).find(([name]) => name === packageName)?.[1];
|
|
23
|
+
if (typeof range === 'string')
|
|
24
|
+
return range;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// The first manifest, from the file's directory upward, whose `answerOf` is defined; false when none.
|
|
28
|
+
function nearestManifestAnswer(fileName, answerOf) {
|
|
29
|
+
let directory = path.dirname(path.resolve(fileName));
|
|
30
|
+
for (;;) {
|
|
31
|
+
const manifest = readManifest(path.join(directory, 'package.json'));
|
|
32
|
+
const answer = manifest && answerOf(manifest);
|
|
33
|
+
if (answer !== undefined)
|
|
34
|
+
return answer;
|
|
35
|
+
const parent = path.dirname(directory);
|
|
36
|
+
if (parent === directory)
|
|
37
|
+
return false;
|
|
38
|
+
directory = parent;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Only the file's own project counts: a nested Vite app under an Astro workspace root is not Astro.
|
|
42
|
+
function projectDeclares(fileName, packageName) {
|
|
43
|
+
return nearestManifestAnswer(fileName, (manifest) => declaredRange(manifest, packageName) !== undefined);
|
|
44
|
+
}
|
|
45
|
+
export { declaredRange, nearestManifestAnswer, projectDeclares };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Whether JSX in a file renders `javascript:` URLs as written. React 19 replaces them in `href`,
|
|
2
|
+
// `xlinkHref`, `src`, `action` and `formAction` itself; React 18 and older only warn, and Preact
|
|
3
|
+
// never checks. The answer comes from the nearest package.json that declares react-dom, react or
|
|
4
|
+
// preact; when no version can be read the answer is no, so the rule stays silent rather than guess.
|
|
5
|
+
import { declaredRange, nearestManifestAnswer } from './package-manifest.js';
|
|
6
|
+
const FIRST_SANITIZING_REACT_MAJOR = 19;
|
|
7
|
+
// `^18.2.0`, `~18`, `>=18`, `>18.1`, `<19`, `18.x`, `=18.3.1`: an operator and the major it names.
|
|
8
|
+
const COMPARATOR = /^(<=?|>=?|[=^~])?v?(\d+)(?:\.(?:\d+|[xX*])){0,2}(?:[-+][\w.-]*)?$/;
|
|
9
|
+
const UPPER_BOUND_OPERATORS = new Set(['<', '<=']);
|
|
10
|
+
// The lowest major one `||` alternative allows. Space-separated comparators all apply, so the
|
|
11
|
+
// highest lower bound wins: `>=18 >=19` allows no React 18. In `18 - 19` only the start bounds it.
|
|
12
|
+
function alternativeMinimumMajor(alternative) {
|
|
13
|
+
const [start] = alternative.trim().split(/\s+-\s+/);
|
|
14
|
+
const comparators = start.replaceAll(/([<>=^~])\s+(?=v?\d)/g, '$1').split(/\s+/);
|
|
15
|
+
let minimum = 0;
|
|
16
|
+
for (const comparator of comparators) {
|
|
17
|
+
const match = COMPARATOR.exec(comparator);
|
|
18
|
+
if (!match)
|
|
19
|
+
return undefined;
|
|
20
|
+
const [, operator = '', major] = match;
|
|
21
|
+
if (!UPPER_BOUND_OPERATORS.has(operator))
|
|
22
|
+
minimum = Math.max(minimum, Number(major));
|
|
23
|
+
}
|
|
24
|
+
return minimum;
|
|
25
|
+
}
|
|
26
|
+
// The lowest major any alternative allows; undefined for tags, `*`, workspace or catalog links.
|
|
27
|
+
function minimumMajor(range) {
|
|
28
|
+
const majors = range.split('||').map(alternativeMinimumMajor);
|
|
29
|
+
if (majors.some((major) => major === undefined))
|
|
30
|
+
return undefined;
|
|
31
|
+
return Math.min(...majors.map(Number));
|
|
32
|
+
}
|
|
33
|
+
// undefined when this manifest declares none of the renderers.
|
|
34
|
+
function manifestAnswer(manifest) {
|
|
35
|
+
const reactRange = declaredRange(manifest, 'react-dom') ?? declaredRange(manifest, 'react');
|
|
36
|
+
if (reactRange !== undefined) {
|
|
37
|
+
const major = minimumMajor(reactRange);
|
|
38
|
+
return major !== undefined && major < FIRST_SANITIZING_REACT_MAJOR;
|
|
39
|
+
}
|
|
40
|
+
return declaredRange(manifest, 'preact') !== undefined ? true : undefined;
|
|
41
|
+
}
|
|
42
|
+
function rendersJavaScriptUrls(fileName) {
|
|
43
|
+
return nearestManifestAnswer(fileName, manifestAnswer);
|
|
44
|
+
}
|
|
45
|
+
export { rendersJavaScriptUrls };
|
|
@@ -1,28 +1,32 @@
|
|
|
1
1
|
// Rule id -> { needs }: the weakest recognition the rule accepts ('syntax', 'import' or 'types';
|
|
2
2
|
// see bindings.cjs).
|
|
3
|
+
// Keep sorted by rule id; test/shared/registry-order.test.ts checks it.
|
|
3
4
|
export default {
|
|
5
|
+
'angular-bypass-security-trust': { needs: 'types' },
|
|
6
|
+
'client-env-secret-exposed': { needs: 'syntax' },
|
|
4
7
|
'cookie-missing-security-flags': { needs: 'import' },
|
|
5
8
|
'cors-credentials-any-origin': { needs: 'import' },
|
|
6
9
|
'cors-origin-regex-unanchored': { needs: 'import' },
|
|
10
|
+
'database-error-in-response': { needs: 'syntax' },
|
|
11
|
+
'dom-xss-sink': { needs: 'syntax' },
|
|
7
12
|
'error-stack-in-response': { needs: 'syntax' },
|
|
13
|
+
'express-missing-helmet': { needs: 'types' },
|
|
14
|
+
'frontend-storage-secret-token': { needs: 'syntax' },
|
|
8
15
|
'graphql-introspection-enabled': { needs: 'syntax' },
|
|
16
|
+
'graphql-query-depth-unbounded': { needs: 'syntax' },
|
|
17
|
+
'jsx-javascript-url': { needs: 'syntax' },
|
|
9
18
|
'mass-assignment': { needs: 'types' },
|
|
10
19
|
'nest-validation-without-whitelist': { needs: 'import' },
|
|
20
|
+
'next-image-any-remote-host': { needs: 'syntax' },
|
|
21
|
+
'password-hash-in-response': { needs: 'types' },
|
|
22
|
+
'postmessage-missing-origin-check': { needs: 'syntax' },
|
|
11
23
|
'prototype-pollution': { needs: 'import' },
|
|
24
|
+
'react-dangerously-set-inner-html': { needs: 'syntax' },
|
|
12
25
|
'reflected-xss-response': { needs: 'syntax' },
|
|
13
26
|
'secret-in-log': { needs: 'syntax' },
|
|
14
|
-
'template-autoescape-disabled': { needs: 'import' },
|
|
15
|
-
'xml-external-entities': { needs: 'import' },
|
|
16
|
-
'password-hash-in-response': { needs: 'types' },
|
|
17
|
-
'storage-object-public-acl': { needs: 'syntax' },
|
|
18
27
|
'secret-in-url-query': { needs: 'syntax' },
|
|
19
|
-
'
|
|
20
|
-
'
|
|
21
|
-
'dom-xss-sink': { needs: 'syntax' },
|
|
22
|
-
'react-dangerously-set-inner-html': { needs: 'syntax' },
|
|
23
|
-
'angular-bypass-security-trust': { needs: 'types' },
|
|
28
|
+
'storage-object-public-acl': { needs: 'syntax' },
|
|
29
|
+
'template-autoescape-disabled': { needs: 'import' },
|
|
24
30
|
'vue-v-html-unsanitized': { needs: 'syntax' },
|
|
25
|
-
'
|
|
26
|
-
'postmessage-missing-origin-check': { needs: 'syntax' },
|
|
27
|
-
'express-missing-helmet': { needs: 'types' },
|
|
31
|
+
'xml-external-entities': { needs: 'import' },
|
|
28
32
|
};
|
|
@@ -97,4 +97,4 @@ function scanProjectFiles(roots, isChangedFile, isIgnored) {
|
|
|
97
97
|
findings.sort((first, second) => first.file.localeCompare(second.file) || first.line - second.line);
|
|
98
98
|
return { scannedFiles, findings };
|
|
99
99
|
}
|
|
100
|
-
export { hasInfrastructureFiles, scanProjectFiles };
|
|
100
|
+
export { hasInfrastructureFiles, isNamedConfigFile, scanProjectFiles };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Rule id -> { needs }: these rules read project files, not TypeScript syntax, so they need nothing
|
|
2
2
|
// from the compiler.
|
|
3
|
+
// Keep sorted by rule id; test/shared/registry-order.test.ts checks it.
|
|
3
4
|
export default {
|
|
4
5
|
'committed-secret': { needs: 'syntax' },
|
|
5
6
|
'compose-hardcoded-secret': { needs: 'syntax' },
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// `template-injection`: request data compiled as a server-side template, whose expressions run with
|
|
3
3
|
// the server's privileges.
|
|
4
4
|
import { ts } from '../../performance/compiler.js';
|
|
5
|
-
import { reportTraced, isGlobalName, calledExport } from './shared.js';
|
|
5
|
+
import { reportTraced, isGlobalName, calledExport, initializerOf, positionalArguments } from './shared.js';
|
|
6
6
|
const evaluatingGlobals = new Set(['eval', 'Function', 'setTimeout', 'setInterval', 'require']);
|
|
7
7
|
const vmFunctions = new Set(['runInNewContext', 'runInContext', 'runInThisContext', 'compileFunction', 'Script']);
|
|
8
8
|
const vmAndSerializeMethods = new Set([...vmFunctions, 'unserialize']);
|
|
@@ -26,19 +26,31 @@ function codeMessage(label) {
|
|
|
26
26
|
function templateMessage(label) {
|
|
27
27
|
return `\`${label}\` is compiled as a template, which lets a caller run template code on the server. Keep templates fixed and pass input only as data.`;
|
|
28
28
|
}
|
|
29
|
+
// `const run = eval;` makes `run(...)` an evaluator call, also through `const again = run`. The
|
|
30
|
+
// visited identifiers stop a cycle.
|
|
31
|
+
function resolveAlias(callee, context, visited = new Set()) {
|
|
32
|
+
if (!ts.isIdentifier(callee) || visited.has(callee))
|
|
33
|
+
return callee;
|
|
34
|
+
visited.add(callee);
|
|
35
|
+
const initializer = initializerOf(callee, context.checker);
|
|
36
|
+
return initializer && ts.isIdentifier(initializer) ? resolveAlias(initializer, context, visited) : callee;
|
|
37
|
+
}
|
|
29
38
|
// The arguments evaluated as code by this call or `new` expression.
|
|
30
39
|
function codeInputs(node, context) {
|
|
31
40
|
if (!node.arguments?.length)
|
|
32
41
|
return [];
|
|
33
42
|
const callee = ts.skipOuterExpressions(node.expression);
|
|
43
|
+
const evaluator = resolveAlias(callee, context);
|
|
34
44
|
if (ts.isCallExpression(node) && callee.kind === ts.SyntaxKind.ImportKeyword)
|
|
35
45
|
return [node.arguments[0]];
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
// `eval(...['1', extra])` evaluates only the first element.
|
|
47
|
+
const args = positionalArguments(node.arguments);
|
|
48
|
+
if (isGlobalName(evaluator, evaluatingGlobals, context.checker)) {
|
|
49
|
+
if (evaluator.text === 'Function')
|
|
50
|
+
return args;
|
|
39
51
|
if (ts.isNewExpression(node))
|
|
40
52
|
return [];
|
|
41
|
-
const [first] =
|
|
53
|
+
const [first] = args;
|
|
42
54
|
// A function passed to a timer is not evaluated as text.
|
|
43
55
|
return first && !ts.isFunctionLike(first) ? [first] : [];
|
|
44
56
|
}
|
|
@@ -47,9 +59,9 @@ function codeInputs(node, context) {
|
|
|
47
59
|
const library = context.libraryCall(node);
|
|
48
60
|
const name = calledExport(library);
|
|
49
61
|
if (library?.module === 'vm' && name !== undefined && vmFunctions.has(name))
|
|
50
|
-
return [
|
|
62
|
+
return [args[0]];
|
|
51
63
|
if (library?.module === 'node-serialize' && name === 'unserialize')
|
|
52
|
-
return [
|
|
64
|
+
return [args[0]];
|
|
53
65
|
return [];
|
|
54
66
|
}
|
|
55
67
|
function templateInput(call, context) {
|
|
@@ -81,14 +93,16 @@ function derivesFromParameter(expression, checker) {
|
|
|
81
93
|
// `eval`, `Function` and the vm functions only: a parameter reaching `require`, `import` or a timer
|
|
82
94
|
// is a common idiom.
|
|
83
95
|
function isEvaluatorCall(node, context) {
|
|
84
|
-
const callee = ts.skipOuterExpressions(node.expression);
|
|
96
|
+
const callee = resolveAlias(ts.skipOuterExpressions(node.expression), context);
|
|
85
97
|
if (isGlobalName(callee, parameterEvaluators, context.checker))
|
|
86
98
|
return true;
|
|
87
99
|
return context.libraryCall(node) !== undefined;
|
|
88
100
|
}
|
|
89
101
|
function checkCode(node, context) {
|
|
90
|
-
for (const
|
|
91
|
-
|
|
102
|
+
for (const argument of codeInputs(node, context)) {
|
|
103
|
+
// `new Function(...parts)` evaluates whatever the spread supplies.
|
|
104
|
+
const input = argument && ts.isSpreadElement(argument) ? argument.expression : argument;
|
|
105
|
+
if (!input || ts.isStringLiteralLike(input))
|
|
92
106
|
continue;
|
|
93
107
|
const trace = context.taint.trace(input, 'code');
|
|
94
108
|
if (trace)
|
|
@@ -6,7 +6,7 @@ import { ts } from '../../performance/compiler.js';
|
|
|
6
6
|
import { importedFrom } from '../bindings.js';
|
|
7
7
|
import { analyzeFragment } from '../../performance/pg-fragments.js';
|
|
8
8
|
import { operandsOf } from '../pg-identifier-guards.js';
|
|
9
|
-
import { reportTraced, calledExport, initializerOf } from './shared.js';
|
|
9
|
+
import { reportTraced, calledExport, initializerOf, fieldWritesOf, positionalArguments } from './shared.js';
|
|
10
10
|
const ruleId = 'command-injection';
|
|
11
11
|
const untracedReason = 'The interpolated value is a parameter with no traced caller; confirm no caller passes request data.';
|
|
12
12
|
const shellCommands = new Set(['exec', 'execSync']);
|
|
@@ -42,32 +42,81 @@ function childProcessExport(call, context) {
|
|
|
42
42
|
: undefined;
|
|
43
43
|
return binding?.module === 'child_process' ? binding.exported : undefined;
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
// An options object written inline or held in a `const`, also behind `const options = base`.
|
|
46
|
+
function objectLiteralOf(expression, context, visited = new Set()) {
|
|
47
|
+
const value = ts.skipOuterExpressions(expression);
|
|
48
|
+
if (ts.isObjectLiteralExpression(value))
|
|
49
|
+
return value;
|
|
50
|
+
if (!ts.isIdentifier(value) || visited.has(value))
|
|
51
|
+
return;
|
|
52
|
+
visited.add(value);
|
|
53
|
+
const initializer = initializerOf(value, context.checker);
|
|
54
|
+
return initializer && objectLiteralOf(initializer, context, visited);
|
|
55
|
+
}
|
|
56
|
+
function optionsOf(args, context) {
|
|
57
|
+
for (let index = args.length - 1; index >= 1; index -= 1)
|
|
58
|
+
if (objectLiteralOf(args[index], context))
|
|
59
|
+
return args[index];
|
|
51
60
|
return undefined;
|
|
52
61
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
62
|
+
function isShellValue(expression) {
|
|
63
|
+
const value = ts.skipOuterExpressions(expression);
|
|
64
|
+
return value.kind === ts.SyntaxKind.TrueKeyword || ts.isStringLiteralLike(value);
|
|
65
|
+
}
|
|
66
|
+
// The `shell` setting of an options value: true for `{ shell: true }` or `{ shell: '/bin/bash' }`,
|
|
67
|
+
// false when set to anything else, undefined when the options do not set it. A later property,
|
|
68
|
+
// spread or `options.shell = ...` write overrides an earlier one. `resolving` holds the values on
|
|
69
|
+
// the current path, so a cycle stops without hiding a value spread twice.
|
|
70
|
+
function shellSetting(expression, context, resolving) {
|
|
71
|
+
const value = ts.skipOuterExpressions(expression);
|
|
72
|
+
if (resolving.has(value))
|
|
73
|
+
return;
|
|
74
|
+
resolving.add(value);
|
|
75
|
+
try {
|
|
76
|
+
if (ts.isObjectLiteralExpression(value))
|
|
77
|
+
return literalShellSetting(value, context, resolving);
|
|
78
|
+
if (!ts.isIdentifier(value))
|
|
79
|
+
return;
|
|
80
|
+
const initializer = initializerOf(value, context.checker);
|
|
81
|
+
let setting = initializer ? shellSetting(initializer, context, resolving) : undefined;
|
|
82
|
+
for (const { name, assignment } of fieldWritesOf(value, context.checker))
|
|
83
|
+
if (name === 'shell')
|
|
84
|
+
setting = isShellValue(assignment.right);
|
|
85
|
+
return setting;
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
resolving.delete(value);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function literalShellSetting(options, context, resolving) {
|
|
92
|
+
let setting;
|
|
93
|
+
for (const property of options.properties) {
|
|
94
|
+
if (ts.isSpreadAssignment(property)) {
|
|
95
|
+
setting = shellSetting(property.expression, context, resolving) ?? setting;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (!ts.isPropertyAssignment(property))
|
|
99
|
+
continue;
|
|
100
|
+
if (!(ts.isIdentifier(property.name) || ts.isStringLiteralLike(property.name)) || property.name.text !== 'shell')
|
|
101
|
+
continue;
|
|
102
|
+
setting = isShellValue(property.initializer);
|
|
103
|
+
}
|
|
104
|
+
return setting;
|
|
105
|
+
}
|
|
106
|
+
function usesShell(args, context) {
|
|
107
|
+
const options = optionsOf(args, context);
|
|
108
|
+
return options !== undefined && shellSetting(options, context, new Set()) === true;
|
|
60
109
|
}
|
|
61
|
-
function argumentList(
|
|
62
|
-
const list =
|
|
110
|
+
function argumentList(args) {
|
|
111
|
+
const list = args[1] && ts.skipOuterExpressions(args[1]);
|
|
63
112
|
return list && ts.isArrayLiteralExpression(list) ? list : undefined;
|
|
64
113
|
}
|
|
65
114
|
// The script of `spawn('sh', ['-c', script])`.
|
|
66
|
-
function shellScript(
|
|
67
|
-
const program =
|
|
115
|
+
function shellScript(args) {
|
|
116
|
+
const program = args[0] && ts.skipOuterExpressions(args[0]);
|
|
68
117
|
if (!program || !ts.isStringLiteralLike(program) || !shellPrograms.test(program.text))
|
|
69
118
|
return;
|
|
70
|
-
const list = argumentList(
|
|
119
|
+
const list = argumentList(args);
|
|
71
120
|
if (!list)
|
|
72
121
|
return;
|
|
73
122
|
const flag = list.elements.findIndex((element) => ts.isStringLiteralLike(element) && commandFlags.has(element.text));
|
|
@@ -84,22 +133,24 @@ function shellInputs(call, context) {
|
|
|
84
133
|
const library = exported ? undefined : context.libraryCall(call);
|
|
85
134
|
const name = exported ?? calledExport(library);
|
|
86
135
|
const module = exported ? 'child_process' : library?.module;
|
|
136
|
+
// `exec(...['echo fixed', options])` runs only the first element.
|
|
137
|
+
const args = positionalArguments(call.arguments);
|
|
87
138
|
if (module === 'shelljs' && name === 'exec')
|
|
88
|
-
return [
|
|
139
|
+
return [args[0]];
|
|
89
140
|
if (name === undefined)
|
|
90
141
|
return [];
|
|
91
142
|
if (module === 'execa' && execaCommands.has(name))
|
|
92
|
-
return [
|
|
143
|
+
return [args[0]];
|
|
93
144
|
if (module === 'child_process' && shellCommands.has(name))
|
|
94
|
-
return [
|
|
145
|
+
return [args[0]];
|
|
95
146
|
const spawns = (module === 'child_process' && spawnCommands.has(name)) || (module === 'execa' && execaSpawns.has(name));
|
|
96
147
|
if (!spawns)
|
|
97
148
|
return [];
|
|
98
|
-
if (usesShell(
|
|
99
|
-
const list = argumentList(
|
|
100
|
-
return [
|
|
149
|
+
if (usesShell(args, context)) {
|
|
150
|
+
const list = argumentList(args);
|
|
151
|
+
return [args[0], ...(list ? list.elements : [args[1]])];
|
|
101
152
|
}
|
|
102
|
-
return [shellScript(
|
|
153
|
+
return [shellScript(args)];
|
|
103
154
|
}
|
|
104
155
|
// An interpolated part that is a parameter no traced caller reaches.
|
|
105
156
|
function untracedPart(input, context) {
|
|
@@ -111,8 +162,10 @@ function untracedPart(input, context) {
|
|
|
111
162
|
return operandsOf(value).find((part) => analyzeFragment(part, context.checker) === undefined && context.taint.isUntracedParameter(part));
|
|
112
163
|
}
|
|
113
164
|
function check(call, context) {
|
|
114
|
-
for (const
|
|
115
|
-
|
|
165
|
+
for (const argument of shellInputs(call, context)) {
|
|
166
|
+
// `exec(...[command])` runs whatever the spread supplies.
|
|
167
|
+
const input = argument && ts.isSpreadElement(argument) ? argument.expression : argument;
|
|
168
|
+
if (!input)
|
|
116
169
|
continue;
|
|
117
170
|
const trace = context.taint.trace(input, 'shell');
|
|
118
171
|
if (trace) {
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// a MongoDB filter or filter value on a Mongoose model or a MongoDB collection, and request text
|
|
3
3
|
// used as a `$where` expression.
|
|
4
4
|
import { ts } from '../../performance/compiler.js';
|
|
5
|
-
import { reportTraced, methodName, createdBy, calledExport, initializerOf } from './shared.js';
|
|
5
|
+
import { reportTraced, methodName, createdBy, calledExport, initializerOf, fieldWritesOf } from './shared.js';
|
|
6
6
|
import { isProjectSource, returnedExpressions } from '../taint/summaries.js';
|
|
7
7
|
const ruleId = 'nosql-operator-injection';
|
|
8
|
+
const stageAddingMethods = new Set(['push', 'unshift']);
|
|
8
9
|
const filterFirstMethods = new Set([
|
|
9
10
|
'find',
|
|
10
11
|
'findOne',
|
|
@@ -111,17 +112,24 @@ function isMongoReceiver(call, context, method) {
|
|
|
111
112
|
return false;
|
|
112
113
|
return createdBy(callee.expression, (created) => isMongooseModelFactory(created, context) || (method !== 'where' && isNamedCollection(created)), context.checker);
|
|
113
114
|
}
|
|
114
|
-
|
|
115
|
+
// An identifier initialized from a literal (`const pipeline = [...]`) stands for that literal.
|
|
116
|
+
function resolveLocalLiteral(node, checker) {
|
|
117
|
+
const value = ts.skipOuterExpressions(node);
|
|
118
|
+
const initializer = ts.isIdentifier(value) ? initializerOf(value, checker) : undefined;
|
|
119
|
+
return initializer ? ts.skipOuterExpressions(initializer) : value;
|
|
120
|
+
}
|
|
121
|
+
function filterArguments(call, method, checker) {
|
|
115
122
|
if (filterFirstMethods.has(method))
|
|
116
123
|
return [call.arguments[0]];
|
|
117
124
|
if (method === 'distinct')
|
|
118
125
|
return [call.arguments[1]];
|
|
119
126
|
if (method !== 'aggregate')
|
|
120
127
|
return [];
|
|
121
|
-
const pipeline = call.arguments[0] &&
|
|
128
|
+
const pipeline = call.arguments[0] && resolveLocalLiteral(call.arguments[0], checker);
|
|
122
129
|
if (!pipeline || !ts.isArrayLiteralExpression(pipeline))
|
|
123
130
|
return [];
|
|
124
|
-
|
|
131
|
+
const stages = [...pipeline.elements, ...pushedStages(call.arguments[0], call, checker)];
|
|
132
|
+
return stages.flatMap((stage) => {
|
|
125
133
|
const value = ts.skipOuterExpressions(stage);
|
|
126
134
|
if (!ts.isObjectLiteralExpression(value))
|
|
127
135
|
return [];
|
|
@@ -129,6 +137,53 @@ function filterArguments(call, method) {
|
|
|
129
137
|
return match && ts.isPropertyAssignment(match) ? [match.initializer] : [];
|
|
130
138
|
});
|
|
131
139
|
}
|
|
140
|
+
// `pipeline.push(stage)` after `const pipeline = [...]`: the stages added to a local pipeline
|
|
141
|
+
// before `aggregate` runs it.
|
|
142
|
+
function pushedStages(node, aggregate, checker) {
|
|
143
|
+
const value = ts.skipOuterExpressions(node);
|
|
144
|
+
const declaration = ts.isIdentifier(value) ? checker.getSymbolAtLocation(value)?.valueDeclaration : undefined;
|
|
145
|
+
const scope = declaration && ts.findAncestor(declaration, (child) => ts.isFunctionLike(child) || ts.isSourceFile(child));
|
|
146
|
+
if (!scope)
|
|
147
|
+
return [];
|
|
148
|
+
const stages = [];
|
|
149
|
+
(function visit(child) {
|
|
150
|
+
if (ts.isCallExpression(child) &&
|
|
151
|
+
ts.isPropertyAccessExpression(child.expression) &&
|
|
152
|
+
stageAddingMethods.has(child.expression.name.text)) {
|
|
153
|
+
const receiver = ts.skipOuterExpressions(child.expression.expression);
|
|
154
|
+
if (ts.isIdentifier(receiver) &&
|
|
155
|
+
checker.getSymbolAtLocation(receiver)?.valueDeclaration === declaration &&
|
|
156
|
+
runsBefore(child, aggregate, declaration))
|
|
157
|
+
stages.push(...child.arguments);
|
|
158
|
+
}
|
|
159
|
+
ts.forEachChild(child, visit);
|
|
160
|
+
})(scope);
|
|
161
|
+
return stages;
|
|
162
|
+
}
|
|
163
|
+
// A push inside a nested function runs only if that function is called, so it counts only when the
|
|
164
|
+
// same function also runs the aggregate. A push written after the call reaches it only on a later
|
|
165
|
+
// pass of a loop around both that keeps the same pipeline: any such loop counts, not just the
|
|
166
|
+
// innermost one, and `for (const pipeline = []; ...)` creates the pipeline once for every pass.
|
|
167
|
+
function runsBefore(push, aggregate, declaration) {
|
|
168
|
+
const pushFunction = ts.findAncestor(push.parent, (node) => ts.isFunctionLike(node) || ts.isSourceFile(node));
|
|
169
|
+
if (!pushFunction || !encloses(pushFunction, aggregate))
|
|
170
|
+
return false;
|
|
171
|
+
if (push.getStart() < aggregate.getStart())
|
|
172
|
+
return true;
|
|
173
|
+
for (let node = aggregate.parent; node; node = node.parent) {
|
|
174
|
+
const createdOnce = ts.isForStatement(node) && node.initializer !== undefined && encloses(node.initializer, declaration);
|
|
175
|
+
if (encloses(node, declaration) && !createdOnce)
|
|
176
|
+
return false;
|
|
177
|
+
if (ts.isIterationStatement(node, false) && encloses(node, push))
|
|
178
|
+
return true;
|
|
179
|
+
if (createdOnce)
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
function encloses(outer, inner) {
|
|
185
|
+
return outer.getStart() <= inner.getStart() && inner.getEnd() <= outer.getEnd();
|
|
186
|
+
}
|
|
132
187
|
function propertyKey(property) {
|
|
133
188
|
const name = property.name;
|
|
134
189
|
if (!name)
|
|
@@ -183,28 +238,43 @@ function checkFilterObject(filter, context, contextualReason) {
|
|
|
183
238
|
reportValue(property.name, context, contextualReason);
|
|
184
239
|
continue;
|
|
185
240
|
}
|
|
186
|
-
if (
|
|
187
|
-
|
|
188
|
-
const value = ts.skipOuterExpressions(property.initializer);
|
|
189
|
-
if (key === '$where') {
|
|
190
|
-
reportWhere(value, context, contextualReason);
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
if (key !== undefined && logicalOperators.has(key) && ts.isArrayLiteralExpression(value)) {
|
|
194
|
-
checkLogicalFilterBranches(value, context, contextualReason);
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
// A nested object is an operator expression (`{ $eq: value }`) or an exact sub-document match.
|
|
198
|
-
if (key?.startsWith('$') || ts.isObjectLiteralExpression(value))
|
|
199
|
-
continue;
|
|
200
|
-
reportValue(value, context, contextualReason);
|
|
241
|
+
if (ts.isPropertyAssignment(property))
|
|
242
|
+
checkFilterField(key, property.initializer, context, contextualReason);
|
|
201
243
|
}
|
|
202
244
|
}
|
|
245
|
+
// One `key: value` of a filter, written in the literal or assigned later (`filter.$where = ...`).
|
|
246
|
+
function checkFilterField(key, initializer, context, contextualReason) {
|
|
247
|
+
const value = ts.skipOuterExpressions(initializer);
|
|
248
|
+
if (key === '$where')
|
|
249
|
+
return reportWhere(value, context, contextualReason);
|
|
250
|
+
if (key !== undefined && logicalOperators.has(key) && ts.isArrayLiteralExpression(value))
|
|
251
|
+
return checkLogicalFilterBranches(value, context, contextualReason);
|
|
252
|
+
// A nested object is an operator expression (`{ $eq: value }`) or an exact sub-document match, so
|
|
253
|
+
// only a spread request object, whose keys become operators, is reported inside it.
|
|
254
|
+
if (ts.isObjectLiteralExpression(value))
|
|
255
|
+
return reportNestedSpreads(value, context, contextualReason);
|
|
256
|
+
// `{ $or: req.body.branches }`: a value the request supplies under an operator key is as
|
|
257
|
+
// attacker-shaped as a top-level one.
|
|
258
|
+
reportValue(value, context, contextualReason);
|
|
259
|
+
}
|
|
260
|
+
function reportNestedSpreads(nested, context, contextualReason) {
|
|
261
|
+
for (const property of nested.properties)
|
|
262
|
+
if (ts.isSpreadAssignment(property))
|
|
263
|
+
reportWhole(property.expression, context, contextualReason);
|
|
264
|
+
}
|
|
203
265
|
function checkLogicalFilterBranches(value, context, contextualReason) {
|
|
204
266
|
for (const element of value.elements) {
|
|
205
267
|
const branch = ts.skipOuterExpressions(element);
|
|
206
|
-
|
|
207
|
-
|
|
268
|
+
// `const branch = { name: req.body.name }; find({ $or: [branch] })` gets the inline checks.
|
|
269
|
+
const literal = resolveLocalLiteral(branch, context.checker);
|
|
270
|
+
if (!ts.isObjectLiteralExpression(literal)) {
|
|
271
|
+
reportWhole(branch, context, contextualReason);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
checkFilterObject(literal, context, contextualReason);
|
|
275
|
+
if (ts.isIdentifier(branch))
|
|
276
|
+
for (const { name, assignment } of fieldWritesOf(branch, context.checker))
|
|
277
|
+
checkFilterField(name, assignment.right, context, contextualReason);
|
|
208
278
|
}
|
|
209
279
|
}
|
|
210
280
|
function checkFilter(filter, context) {
|
|
@@ -214,6 +284,13 @@ function checkFilter(filter, context) {
|
|
|
214
284
|
const value = ts.skipOuterExpressions(filter);
|
|
215
285
|
if (ts.isObjectLiteralExpression(value))
|
|
216
286
|
return checkFilterObject(value, context, reason);
|
|
287
|
+
const local = ts.isIdentifier(value) ? initializerOf(value, context.checker) : undefined;
|
|
288
|
+
const localFilter = local && ts.skipOuterExpressions(local);
|
|
289
|
+
if (localFilter && ts.isObjectLiteralExpression(localFilter) && ts.isIdentifier(value)) {
|
|
290
|
+
checkFilterObject(localFilter, context, reason);
|
|
291
|
+
for (const { name, assignment } of fieldWritesOf(value, context.checker))
|
|
292
|
+
checkFilterField(name, assignment.right, context, reason);
|
|
293
|
+
}
|
|
217
294
|
for (const built of builtFilters(value, context))
|
|
218
295
|
checkFilterObject(built, context, reason);
|
|
219
296
|
reportWhole(value, context, reason);
|
|
@@ -255,12 +332,22 @@ function check(call, context) {
|
|
|
255
332
|
const method = methodName(call);
|
|
256
333
|
if (!method)
|
|
257
334
|
return;
|
|
258
|
-
const filters = filterArguments(call, method).filter((filter) => filter !== undefined);
|
|
335
|
+
const filters = filterArguments(call, method, context.checker).filter((filter) => filter !== undefined);
|
|
259
336
|
if (!filters.length)
|
|
260
337
|
return;
|
|
261
338
|
if (!isMongoReceiver(call, context, method))
|
|
262
339
|
return filters.forEach((filter) => checkWhereOnly(filter, context));
|
|
263
340
|
for (const filter of filters)
|
|
264
341
|
checkFilter(filter, context);
|
|
342
|
+
// Mongoose `where(path, value)`: the second argument is the path's condition.
|
|
343
|
+
const [, condition] = call.arguments;
|
|
344
|
+
if (method !== 'where' || call.arguments.length !== 2 || !condition)
|
|
345
|
+
return;
|
|
346
|
+
// `where('name', { ...req.body })` spreads request keys into the condition, as in `find`.
|
|
347
|
+
const conditionValue = ts.skipOuterExpressions(condition);
|
|
348
|
+
if (ts.isObjectLiteralExpression(conditionValue))
|
|
349
|
+
reportNestedSpreads(conditionValue, context, sanitizerReason(context));
|
|
350
|
+
else
|
|
351
|
+
reportValue(condition, context, sanitizerReason(context));
|
|
265
352
|
}
|
|
266
353
|
export { check, ruleId };
|