loadwise 0.6.2 → 0.8.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 +175 -115
- package/dist/bin/loadwise.js +132 -67
- package/dist/lib/accessibility/analyze.js +5 -3
- package/dist/lib/accessibility/angular-inline.js +9 -11
- package/dist/lib/accessibility/angular-literal.js +39 -0
- package/dist/lib/accessibility/form-context.js +36 -0
- package/dist/lib/accessibility/frame-head.js +92 -0
- package/dist/lib/accessibility/frame-viewport.js +149 -0
- package/dist/lib/accessibility/frontend-detect.js +6 -5
- package/dist/lib/accessibility/jsx-elements.js +13 -3
- package/dist/lib/accessibility/markup-files.js +7 -2
- package/dist/lib/accessibility/rule-ids.js +3 -0
- package/dist/lib/accessibility/rules.js +128 -41
- package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
- package/dist/lib/accessibility/template-attributes.js +5 -3
- package/dist/lib/accessibility/template-elements.js +25 -6
- package/dist/lib/accessibility/template-file.js +40 -25
- package/dist/lib/accessibility/usage.js +10 -2
- package/dist/lib/editor/accessibility-examples.js +6 -1
- package/dist/lib/editor/actions.js +46 -7
- package/dist/lib/editor/apply-fix.js +69 -4
- package/dist/lib/editor/configured-setup.js +63 -0
- package/dist/lib/editor/diagnostics.js +206 -17
- package/dist/lib/editor/dismissals.js +85 -26
- package/dist/lib/editor/editor-document.js +5 -2
- package/dist/lib/editor/editor-launcher.js +170 -0
- package/dist/lib/editor/fix-edits.js +19 -0
- package/dist/lib/editor/jetbrains-setup.js +53 -0
- package/dist/lib/editor/language-server.js +80 -24
- package/dist/lib/editor/managed-config.js +37 -0
- package/dist/lib/editor/native-setup.js +86 -0
- package/dist/lib/editor/perf-watch.js +8 -6
- package/dist/lib/editor/release-vsix.js +61 -0
- package/dist/lib/editor/reliability-examples.js +15 -0
- package/dist/lib/editor/scan-session.js +29 -8
- package/dist/lib/editor/server-commands.js +38 -9
- package/dist/lib/editor/setup-server.js +87 -0
- package/dist/lib/editor/setup-vim.js +63 -0
- package/dist/lib/editor/setup.js +131 -0
- package/dist/lib/editor/stdio-setup.js +6 -0
- package/dist/lib/editor/workspace-files.js +61 -7
- package/dist/lib/editor/workspace-paths.js +41 -1
- package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
- package/dist/lib/maintainability/architecture/architecture.js +4 -4
- package/dist/lib/maintainability/cli/changed-lines.js +20 -2
- package/dist/lib/maintainability/cli/inputs.js +12 -3
- package/dist/lib/maintainability/cli.js +18 -5
- package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
- package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
- package/dist/lib/maintainability/examples.js +4 -0
- package/dist/lib/maintainability/quality/functions.js +9 -6
- package/dist/lib/maintainability/quality/limits.js +19 -0
- package/dist/lib/maintainability/quality/size-limits.js +8 -2
- package/dist/lib/maintainability/quality/stray-console.js +62 -1
- package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
- package/dist/lib/maintainability/rule-ids.js +3 -0
- package/dist/lib/maintainability/scan-changed.js +4 -1
- package/dist/lib/maintainability/scan-worker.js +6 -2
- package/dist/lib/maintainability/scan.js +21 -8
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
- package/dist/lib/maintainability/unused/module-usage.js +43 -21
- package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
- package/dist/lib/maintainability/unused/unused-modules.js +5 -10
- package/dist/lib/performance/analysis/source-inputs.js +7 -2
- package/dist/lib/performance/analysis/source-options.js +7 -6
- package/dist/lib/performance/analyze.js +16 -3
- package/dist/lib/performance/cli/changed-lines.js +8 -1
- package/dist/lib/performance/cli/cli-arguments.js +19 -8
- package/dist/lib/performance/cli/cli-output.js +4 -0
- package/dist/lib/performance/cli/cli-scan.js +21 -2
- package/dist/lib/performance/cli.js +16 -7
- package/dist/lib/performance/collections/memory-growth.js +55 -12
- package/dist/lib/performance/contracts/openapi.js +13 -4
- package/dist/lib/performance/datastores/pg-integrity.js +68 -22
- package/dist/lib/performance/datastores/pg-session-release.js +114 -0
- package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
- package/dist/lib/performance/datastores/pooling.js +62 -3
- package/dist/lib/performance/datastores/redis.js +94 -18
- package/dist/lib/performance/datastores/session-store.js +2 -1
- package/dist/lib/performance/db/checks.js +19 -8
- package/dist/lib/performance/db/gateway.js +3 -1
- package/dist/lib/performance/db/plan-estimates.js +4 -4
- package/dist/lib/performance/engine/native-syntax.js +3 -0
- package/dist/lib/performance/engine/native.js +11 -7
- package/dist/lib/performance/evidence/plans.js +7 -5
- package/dist/lib/performance/evidence/traces.js +21 -1
- package/dist/lib/performance/frontend/angular-teardown.js +96 -0
- package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
- package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
- package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
- package/dist/lib/performance/frontend/frontend-perf.js +149 -74
- package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
- package/dist/lib/performance/migrations/extract-code.js +13 -5
- package/dist/lib/performance/migrations/extract-sql.js +82 -38
- package/dist/lib/performance/migrations/operations.js +56 -13
- package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
- package/dist/lib/performance/migrations/postgres-registry.js +127 -26
- package/dist/lib/performance/migrations/postgres-settings.js +78 -0
- package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
- package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
- package/dist/lib/performance/migrations/rules-postgres.js +88 -39
- package/dist/lib/performance/orm/offset-pagination.js +6 -4
- package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
- package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
- package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
- package/dist/lib/performance/polyglot.js +111 -14
- package/dist/lib/performance/projects.js +3 -3
- package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
- package/dist/lib/performance/request/backend-mastery.js +151 -42
- package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
- package/dist/lib/performance/request/fastify-receivers.js +93 -0
- package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
- package/dist/lib/performance/request/mongoose-lean.js +298 -46
- package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
- package/dist/lib/performance/request/phase5-ast.js +6 -7
- package/dist/lib/performance/request/phase5-rules.js +204 -23
- package/dist/lib/performance/request/request-handlers.js +120 -23
- package/dist/lib/performance/request/sql-select-star.js +150 -0
- package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
- package/dist/lib/performance/request/upload-limits.js +5 -1
- package/dist/lib/performance/site-fix.js +4 -4
- package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
- package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
- package/dist/lib/performance/site-fixes/collections.js +1 -2
- package/dist/lib/performance/site-fixes/frontend-fixes.js +358 -14
- package/dist/lib/performance/site-fixes/helpers.js +3 -4
- package/dist/lib/performance/site-fixes/integrity.js +2 -4
- package/dist/lib/performance/site-fixes/migration-context.js +8 -1
- package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
- package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
- package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
- package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
- package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
- package/dist/lib/performance/sql/mysql-tokens.js +38 -12
- package/dist/lib/reliability/boundary-access.js +114 -57
- package/dist/lib/reliability/react-bugs.js +29 -7
- package/dist/lib/reliability/resource-leak-intervals.js +258 -0
- package/dist/lib/reliability/resource-leaks.js +209 -195
- package/dist/lib/reliability/rule-ids.js +10 -1
- package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
- package/dist/lib/reliability/swallowed-errors.js +107 -21
- package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
- package/dist/lib/reliability/usage.js +16 -5
- package/dist/lib/reliability/walk.js +3 -2
- package/dist/lib/security/auth-crypto/hashing.js +24 -8
- package/dist/lib/security/auth-crypto/jwt.js +21 -6
- package/dist/lib/security/auth-crypto/secrets.js +44 -1
- package/dist/lib/security/auth-crypto/shared.js +3 -1
- package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
- package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
- package/dist/lib/security/cli.js +21 -6
- package/dist/lib/security/cookie-credentials.js +115 -0
- package/dist/lib/security/cookie-csrf.js +241 -0
- package/dist/lib/security/deps/advisory.js +50 -109
- package/dist/lib/security/deps/cli.js +1 -1
- package/dist/lib/security/deps/cvss-score.js +14 -0
- package/dist/lib/security/deps/fixed-version.js +97 -0
- package/dist/lib/security/deps/lockfiles.js +135 -74
- package/dist/lib/security/deps/osv.js +11 -3
- package/dist/lib/security/deps/scan.js +47 -20
- package/dist/lib/security/deps/yarn-lock.js +158 -0
- package/dist/lib/security/examples/injection.cjs +4 -0
- package/dist/lib/security/exposure/cookies.js +17 -2
- package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
- package/dist/lib/security/exposure/frontend-security.js +21 -14
- package/dist/lib/security/exposure/jsx-url.js +18 -33
- package/dist/lib/security/exposure/logging.js +49 -10
- package/dist/lib/security/exposure/prototype-pollution.js +126 -56
- package/dist/lib/security/exposure/react-html.js +26 -0
- package/dist/lib/security/exposure/reflected-xss.js +34 -5
- package/dist/lib/security/files/compose.js +126 -1
- package/dist/lib/security/files/dockerfile.js +62 -4
- package/dist/lib/security/files/index.js +1 -1
- package/dist/lib/security/injection/command.js +46 -18
- package/dist/lib/security/injection/index.js +2 -0
- package/dist/lib/security/injection/ldap.js +86 -0
- package/dist/lib/security/injection/nosql.js +28 -4
- package/dist/lib/security/injection/redos.js +1 -1
- package/dist/lib/security/injection/rule-ids.js +1 -0
- package/dist/lib/security/injection/shared.js +5 -1
- package/dist/lib/security/injection/sql.js +28 -13
- package/dist/lib/security/lexical-declaration.js +113 -0
- package/dist/lib/security/pg-dynamic-query.js +28 -1
- package/dist/lib/security/pg-identifier-guards.js +107 -3
- package/dist/lib/security/phase5-rules.js +26 -135
- package/dist/lib/security/polyglot.js +2 -2
- package/dist/lib/security/redirect-settings.js +98 -0
- package/dist/lib/security/request-forgery/open-redirect.js +18 -5
- package/dist/lib/security/request-forgery/path-traversal.js +84 -6
- package/dist/lib/security/request-forgery/shared.js +1 -7
- package/dist/lib/security/request-forgery/ssrf.js +176 -14
- package/dist/lib/security/rule-descriptions.js +5 -1
- package/dist/lib/security/rule-ids.js +7 -0
- package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
- package/dist/lib/security/site-fixes/exposure.js +21 -3
- package/dist/lib/security/site-fixes/files.js +11 -13
- package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
- package/dist/lib/security/site-fixes/injection.js +17 -4
- package/dist/lib/security/site-fixes/jwt.js +1 -20
- package/dist/lib/security/site-fixes/request-forgery.js +37 -29
- package/dist/lib/security/taint/binding-inputs.js +236 -0
- package/dist/lib/security/taint/filename-guard.js +45 -45
- package/dist/lib/security/taint/guards.js +14 -8
- package/dist/lib/security/taint/index.js +126 -154
- package/dist/lib/security/taint/propagate.js +43 -9
- package/dist/lib/security/taint/record-writes.js +161 -0
- package/dist/lib/security/taint/resolution-cache.js +13 -7
- package/dist/lib/security/taint/sanitizers.js +89 -5
- package/dist/lib/security/taint/trace-nodes.js +61 -0
- package/dist/lib/security/taint/validators.js +89 -9
- package/dist/lib/shared/agent/agent-init.js +28 -5
- package/dist/lib/shared/agent/agent-prompt.js +74 -0
- package/dist/lib/shared/agent/agent-session.js +6 -1
- package/dist/lib/shared/agent/agent-verify.js +58 -11
- package/dist/lib/shared/agent/git-hook.js +17 -6
- package/dist/lib/shared/agent/review-delivery.js +1 -1
- package/dist/lib/shared/agent/review-mcp.js +8 -2
- package/dist/lib/shared/analysis/ast-semantics.js +2 -2
- package/dist/lib/shared/analysis/scan-cache.js +99 -14
- package/dist/lib/shared/check.js +172 -20
- package/dist/lib/shared/cli-dispatch.js +17 -0
- package/dist/lib/shared/config.js +27 -10
- package/dist/lib/shared/explain.js +1 -1
- package/dist/lib/shared/fix/fix-convergence.js +19 -31
- package/dist/lib/shared/fix/fix-handoff.js +16 -11
- package/dist/lib/shared/fix/fix-imports.js +4 -162
- package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
- package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
- package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
- package/dist/lib/shared/fix/fix-presentation.js +9 -6
- package/dist/lib/shared/fix/fix-safety.js +32 -1
- package/dist/lib/shared/fix/fix-scope.js +82 -0
- package/dist/lib/shared/fix/fix-terminal.js +28 -1
- package/dist/lib/shared/fix/fix-typecheck.js +8 -4
- package/dist/lib/shared/fix/import-text-edits.js +148 -0
- package/dist/lib/shared/fix/project-file-access.js +17 -26
- package/dist/lib/shared/fix/read-fix-text.js +20 -0
- package/dist/lib/shared/fix.js +241 -104
- package/dist/lib/shared/formats/finding-location.js +4 -1
- package/dist/lib/shared/formats/github.js +3 -3
- package/dist/lib/shared/formats/sarif.js +9 -3
- package/dist/lib/shared/report-fp.js +97 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
- package/dist/lib/shared/reporting/baseline.js +7 -5
- package/dist/lib/shared/reporting/check-text.js +50 -0
- package/dist/lib/shared/reporting/pillars.js +54 -19
- package/dist/lib/shared/reporting/source-report.js +3 -0
- package/dist/lib/shared/system/atomic-write.js +5 -2
- package/dist/lib/shared/system/glob.js +34 -8
- package/dist/lib/shared/system/sentry.js +222 -0
- package/dist/lib/shared/system/shell-argument.js +5 -1
- package/dist/lib/shared/system/terminal-safe.js +3 -2
- package/dist/lib/shared/system/terminal-style.js +131 -0
- package/dist/lib/shared/workspaces.js +15 -3
- package/docs/assets/cli-workflow.gif +0 -0
- package/integrations/sublime/plugin.py +22 -0
- package/integrations/zed/Cargo.lock +845 -0
- package/integrations/zed/Cargo.toml +11 -0
- package/integrations/zed/extension.toml +11 -0
- package/integrations/zed/src/lib.rs +25 -0
- package/package.json +15 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/commands/loadwise.md +11 -6
- package/plugin/hooks/check-edited-file.mjs +27 -5
- package/polyglot/performance/rules/csharp.yml +34 -5
- package/polyglot/performance/rules/java.yml +24 -1
- package/polyglot/performance/rules/python.yml +74 -2
- package/polyglot/performance/sgconfig.yml +1 -0
- package/polyglot/reliability/rules/csharp.yml +12 -6
- package/polyglot/reliability/rules/go.yml +86 -6
- package/polyglot/reliability/rules/java.yml +22 -1
- package/polyglot/reliability/sgconfig.yml +1 -0
- package/polyglot/security/rules/go.yml +100 -22
- package/polyglot/security/rules/python.yml +74 -6
- package/polyglot/security/rules/rust.yml +65 -0
- package/polyglot/security/sgconfig.yml +1 -0
- package/schema/loadwise.config.schema.json +556 -76
- /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// `execFile` and execa only with a `shell` option or an explicit `sh -c`. Argument injection into a
|
|
4
4
|
// fixed executable is out of scope.
|
|
5
5
|
import { ts } from '../../performance/engine/compiler.js';
|
|
6
|
+
import { staticPropertyName } from '../../shared/analysis/ast-semantics.js';
|
|
6
7
|
import { importedFrom } from '../bindings.js';
|
|
7
8
|
import { analyzeFragment } from '../../performance/datastores/pg-fragments.js';
|
|
8
9
|
import { operandsOf } from '../pg-identifier-guards.js';
|
|
@@ -59,29 +60,52 @@ function optionsOf(args, context) {
|
|
|
59
60
|
return args[index];
|
|
60
61
|
return undefined;
|
|
61
62
|
}
|
|
62
|
-
function isShellValue(expression) {
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
function isShellValue(expression, context) {
|
|
64
|
+
let value = ts.skipOuterExpressions(expression);
|
|
65
|
+
const seen = new Set();
|
|
66
|
+
while (ts.isIdentifier(value)) {
|
|
67
|
+
const initializer = initializerOf(value, context.checker);
|
|
68
|
+
if (!initializer || seen.has(initializer))
|
|
69
|
+
return false;
|
|
70
|
+
seen.add(initializer);
|
|
71
|
+
value = initializer;
|
|
72
|
+
}
|
|
73
|
+
return value.kind === ts.SyntaxKind.TrueKeyword || (ts.isStringLiteralLike(value) && value.text.length > 0);
|
|
65
74
|
}
|
|
66
75
|
// The `shell` setting of an options value: true for `{ shell: true }` or `{ shell: '/bin/bash' }`,
|
|
67
76
|
// 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. `
|
|
69
|
-
// the
|
|
70
|
-
|
|
77
|
+
// spread or `options.shell = ...` write overrides an earlier one. A write after `before` (the
|
|
78
|
+
// analyzed call, or the spread that copies the value) is ignored when it runs in the same function,
|
|
79
|
+
// and disables the setting when it sits in another function, since source order is not execution
|
|
80
|
+
// order there. A write to an unresolved name may have set `shell`, so it disables the setting too. `resolving` holds the values on the current path, so a cycle stops without
|
|
81
|
+
// hiding a value spread twice.
|
|
82
|
+
function enclosingFunction(node) {
|
|
83
|
+
return ts.findAncestor(node, (ancestor) => ts.isFunctionLike(ancestor) || ts.isSourceFile(ancestor));
|
|
84
|
+
}
|
|
85
|
+
function shellSetting(expression, context, resolving, before) {
|
|
71
86
|
const value = ts.skipOuterExpressions(expression);
|
|
72
87
|
if (resolving.has(value))
|
|
73
|
-
return;
|
|
88
|
+
return false;
|
|
74
89
|
resolving.add(value);
|
|
75
90
|
try {
|
|
76
91
|
if (ts.isObjectLiteralExpression(value))
|
|
77
92
|
return literalShellSetting(value, context, resolving);
|
|
78
93
|
if (!ts.isIdentifier(value))
|
|
79
|
-
return;
|
|
94
|
+
return false;
|
|
80
95
|
const initializer = initializerOf(value, context.checker);
|
|
81
|
-
let setting = initializer ? shellSetting(initializer, context, resolving) :
|
|
82
|
-
for (const { name, assignment } of fieldWritesOf(value, context.checker))
|
|
96
|
+
let setting = initializer ? shellSetting(initializer, context, resolving, before) : false;
|
|
97
|
+
for (const { name, assignment } of fieldWritesOf(value, context.checker)) {
|
|
98
|
+
if (assignment.getStart() >= before.getStart()) {
|
|
99
|
+
if (enclosingFunction(assignment) !== enclosingFunction(before))
|
|
100
|
+
setting = false;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
83
103
|
if (name === 'shell')
|
|
84
|
-
setting =
|
|
104
|
+
setting =
|
|
105
|
+
assignment.operatorToken.kind === ts.SyntaxKind.EqualsToken && isShellValue(assignment.right, context);
|
|
106
|
+
else if (name === undefined)
|
|
107
|
+
setting = false;
|
|
108
|
+
}
|
|
85
109
|
return setting;
|
|
86
110
|
}
|
|
87
111
|
finally {
|
|
@@ -92,20 +116,24 @@ function literalShellSetting(options, context, resolving) {
|
|
|
92
116
|
let setting;
|
|
93
117
|
for (const property of options.properties) {
|
|
94
118
|
if (ts.isSpreadAssignment(property)) {
|
|
95
|
-
setting = shellSetting(property.expression, context, resolving) ?? setting;
|
|
119
|
+
setting = shellSetting(property.expression, context, resolving, property) ?? setting;
|
|
96
120
|
continue;
|
|
97
121
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
122
|
+
const name = staticPropertyName(property.name);
|
|
123
|
+
if (name === undefined || name === 'shell') {
|
|
124
|
+
const value = ts.isPropertyAssignment(property)
|
|
125
|
+
? property.initializer
|
|
126
|
+
: ts.isShorthandPropertyAssignment(property)
|
|
127
|
+
? property.name
|
|
128
|
+
: undefined;
|
|
129
|
+
setting = name === 'shell' && value !== undefined && isShellValue(value, context);
|
|
130
|
+
}
|
|
103
131
|
}
|
|
104
132
|
return setting;
|
|
105
133
|
}
|
|
106
134
|
function usesShell(args, context) {
|
|
107
135
|
const options = optionsOf(args, context);
|
|
108
|
-
return options !== undefined && shellSetting(options, context, new Set()) === true;
|
|
136
|
+
return options !== undefined && shellSetting(options, context, new Set(), options) === true;
|
|
109
137
|
}
|
|
110
138
|
function argumentList(args) {
|
|
111
139
|
const list = args[1] && ts.skipOuterExpressions(args[1]);
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// context.report.
|
|
3
3
|
import * as sql from './sql.js';
|
|
4
4
|
import * as nosql from './nosql.js';
|
|
5
|
+
import * as ldap from './ldap.js';
|
|
5
6
|
import * as command from './command.js';
|
|
6
7
|
import { checkCode, checkTemplate } from './code.js';
|
|
7
8
|
import { checkCall as checkRegex, checkRegexOperator } from './regex.js';
|
|
@@ -11,6 +12,7 @@ export const checks = {
|
|
|
11
12
|
CallExpression: [
|
|
12
13
|
sql.check,
|
|
13
14
|
nosql.check,
|
|
15
|
+
ldap.check,
|
|
14
16
|
command.check,
|
|
15
17
|
checkCode,
|
|
16
18
|
checkTemplate,
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// `ldap-filter-injection`: request-derived data reaches the filter slot of a resolved LDAP search
|
|
2
|
+
// call without RFC 4515 filter escaping or a filter builder.
|
|
3
|
+
import { ts } from '../../performance/engine/compiler.js';
|
|
4
|
+
import { importedFrom } from '../bindings.js';
|
|
5
|
+
import { createdBy, initializerOf, methodName, reportTraced, sourceSuffix } from './shared.js';
|
|
6
|
+
const ruleId = 'ldap-filter-injection';
|
|
7
|
+
const ldapModules = new Set(['ldapts', 'ldapjs']);
|
|
8
|
+
function isLdapClientFactory(creation, context) {
|
|
9
|
+
const callee = ts.skipOuterExpressions(creation.expression);
|
|
10
|
+
if (ts.isIdentifier(callee)) {
|
|
11
|
+
const binding = importedFrom(callee, context.checker);
|
|
12
|
+
if (!binding || !ldapModules.has(binding.module))
|
|
13
|
+
return false;
|
|
14
|
+
return ts.isNewExpression(creation) ? binding.exported === 'Client' : binding.exported === 'createClient';
|
|
15
|
+
}
|
|
16
|
+
if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression)) {
|
|
17
|
+
const binding = importedFrom(callee.expression, context.checker);
|
|
18
|
+
if (!binding || !ldapModules.has(binding.module))
|
|
19
|
+
return false;
|
|
20
|
+
if (binding.exported !== '*' && binding.exported !== 'default')
|
|
21
|
+
return false;
|
|
22
|
+
return ts.isNewExpression(creation) ? callee.name.text === 'Client' : callee.name.text === 'createClient';
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
function isLdapSearchCall(call, context) {
|
|
27
|
+
if (methodName(call) !== 'search')
|
|
28
|
+
return false;
|
|
29
|
+
const library = context.libraryCall(call);
|
|
30
|
+
if (library?.module && ldapModules.has(library.module))
|
|
31
|
+
return true;
|
|
32
|
+
const callee = ts.skipOuterExpressions(call.expression);
|
|
33
|
+
if (!ts.isPropertyAccessExpression(callee))
|
|
34
|
+
return false;
|
|
35
|
+
return createdBy(callee.expression, (creation) => isLdapClientFactory(creation, context), context.checker);
|
|
36
|
+
}
|
|
37
|
+
function objectLiteralOf(expression, context, visited = new Set()) {
|
|
38
|
+
if (!expression)
|
|
39
|
+
return;
|
|
40
|
+
const value = ts.skipOuterExpressions(expression);
|
|
41
|
+
if (ts.isObjectLiteralExpression(value))
|
|
42
|
+
return value;
|
|
43
|
+
if (!ts.isIdentifier(value) || visited.has(value))
|
|
44
|
+
return;
|
|
45
|
+
visited.add(value);
|
|
46
|
+
return objectLiteralOf(initializerOf(value, context.checker), context, visited);
|
|
47
|
+
}
|
|
48
|
+
// A later spread whose literal defines `filter` overrides earlier values. A spread that cannot be
|
|
49
|
+
// resolved to an object literal is skipped, so the earlier explicit filter is still reported.
|
|
50
|
+
// The memo keeps repeated spreads of the same layer from being re-walked exponentially.
|
|
51
|
+
function filterInLiteral(options, context, memo = new Map()) {
|
|
52
|
+
for (let index = options.properties.length - 1; index >= 0; index -= 1) {
|
|
53
|
+
const property = options.properties[index];
|
|
54
|
+
if (ts.isPropertyAssignment(property)) {
|
|
55
|
+
const name = ts.isIdentifier(property.name) || ts.isStringLiteralLike(property.name) ? property.name.text : undefined;
|
|
56
|
+
if (name === 'filter')
|
|
57
|
+
return property.initializer;
|
|
58
|
+
}
|
|
59
|
+
else if (ts.isShorthandPropertyAssignment(property) && property.name.text === 'filter') {
|
|
60
|
+
return property.name;
|
|
61
|
+
}
|
|
62
|
+
else if (ts.isSpreadAssignment(property)) {
|
|
63
|
+
const spread = objectLiteralOf(property.expression, context);
|
|
64
|
+
if (spread && !memo.has(spread))
|
|
65
|
+
memo.set(spread, filterInLiteral(spread, context, memo));
|
|
66
|
+
const spreadFilter = spread && memo.get(spread);
|
|
67
|
+
if (spreadFilter)
|
|
68
|
+
return spreadFilter;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function filterExpressionOf(optionsArg, context) {
|
|
73
|
+
const options = objectLiteralOf(optionsArg, context);
|
|
74
|
+
return options && filterInLiteral(options, context);
|
|
75
|
+
}
|
|
76
|
+
export function check(call, context) {
|
|
77
|
+
if (!isLdapSearchCall(call, context))
|
|
78
|
+
return;
|
|
79
|
+
const filterExpr = filterExpressionOf(call.arguments[1], context);
|
|
80
|
+
if (!filterExpr)
|
|
81
|
+
return;
|
|
82
|
+
const trace = context.taint.trace(filterExpr, 'ldap');
|
|
83
|
+
if (!trace)
|
|
84
|
+
return;
|
|
85
|
+
reportTraced(context, filterExpr, ruleId, `LDAP search filter includes request data${sourceSuffix(trace)}, risking LDAP filter injection. Use a filter builder or RFC 4515 filter escaping.`, trace);
|
|
86
|
+
}
|
|
@@ -227,15 +227,18 @@ function reportWhere(value, context, contextualReason) {
|
|
|
227
227
|
if (trace)
|
|
228
228
|
reportTraced(context, value, ruleId, whereMessage(trace.label), trace, contextualReason);
|
|
229
229
|
}
|
|
230
|
-
function checkFilterObject(filter, context, contextualReason) {
|
|
230
|
+
function checkFilterObject(filter, context, contextualReason, elementPredicate = false) {
|
|
231
231
|
for (const property of filter.properties) {
|
|
232
232
|
const key = propertyKey(property);
|
|
233
|
+
// A scalar element comparison such as $eq binds a value rather than interpreting it as a filter.
|
|
234
|
+
if (elementPredicate && key?.startsWith('$') && !logicalOperators.has(key))
|
|
235
|
+
continue;
|
|
233
236
|
if (ts.isSpreadAssignment(property)) {
|
|
234
237
|
reportWhole(property.expression, context, contextualReason);
|
|
235
238
|
continue;
|
|
236
239
|
}
|
|
237
240
|
if (ts.isShorthandPropertyAssignment(property)) {
|
|
238
|
-
|
|
241
|
+
checkFilterField(key, property.name, context, contextualReason);
|
|
239
242
|
continue;
|
|
240
243
|
}
|
|
241
244
|
if (ts.isPropertyAssignment(property))
|
|
@@ -252,11 +255,32 @@ function checkFilterField(key, initializer, context, contextualReason) {
|
|
|
252
255
|
// A nested object is an operator expression (`{ $eq: value }`) or an exact sub-document match, so
|
|
253
256
|
// only a spread request object, whose keys become operators, is reported inside it.
|
|
254
257
|
if (ts.isObjectLiteralExpression(value))
|
|
255
|
-
return
|
|
258
|
+
return checkFieldCondition(value, context, contextualReason);
|
|
256
259
|
// `{ $or: req.body.branches }`: a value the request supplies under an operator key is as
|
|
257
260
|
// attacker-shaped as a top-level one.
|
|
258
261
|
reportValue(value, context, contextualReason);
|
|
259
262
|
}
|
|
263
|
+
// A field condition can contain an element query. Objects under ordinary equality operators
|
|
264
|
+
// remain literal values, even if they happen to contain keys used by the query language.
|
|
265
|
+
function checkFieldCondition(nested, context, contextualReason) {
|
|
266
|
+
reportNestedSpreads(nested, context, contextualReason);
|
|
267
|
+
for (const property of nested.properties) {
|
|
268
|
+
if (propertyKey(property) !== '$elemMatch')
|
|
269
|
+
continue;
|
|
270
|
+
const expression = ts.isPropertyAssignment(property)
|
|
271
|
+
? property.initializer
|
|
272
|
+
: ts.isShorthandPropertyAssignment(property)
|
|
273
|
+
? property.name
|
|
274
|
+
: undefined;
|
|
275
|
+
if (!expression)
|
|
276
|
+
continue;
|
|
277
|
+
const element = ts.skipOuterExpressions(expression);
|
|
278
|
+
if (ts.isObjectLiteralExpression(element))
|
|
279
|
+
checkFilterObject(element, context, contextualReason, true);
|
|
280
|
+
else
|
|
281
|
+
reportValue(element, context, contextualReason);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
260
284
|
function reportNestedSpreads(nested, context, contextualReason) {
|
|
261
285
|
for (const property of nested.properties)
|
|
262
286
|
if (ts.isSpreadAssignment(property))
|
|
@@ -346,7 +370,7 @@ function check(call, context) {
|
|
|
346
370
|
// `where('name', { ...req.body })` spreads request keys into the condition, as in `find`.
|
|
347
371
|
const conditionValue = ts.skipOuterExpressions(condition);
|
|
348
372
|
if (ts.isObjectLiteralExpression(conditionValue))
|
|
349
|
-
|
|
373
|
+
checkFieldCondition(conditionValue, context, sanitizerReason(context));
|
|
350
374
|
else
|
|
351
375
|
reportValue(condition, context, sanitizerReason(context));
|
|
352
376
|
}
|
|
@@ -442,7 +442,7 @@ function checkCall(node, context) {
|
|
|
442
442
|
// few dozen characters, so a cut such as `slice(0, 254)` does not make it safe.
|
|
443
443
|
if (!kind || (kind === 'polynomial' && isTruncated(parts.subject)))
|
|
444
444
|
return;
|
|
445
|
-
const trace = context.taint.trace(parts.subject, 'regex');
|
|
445
|
+
const trace = context.taint.trace(parts.subject, 'regex-subject');
|
|
446
446
|
if (trace)
|
|
447
447
|
return reportTraced(context, node, ruleId, message(trace.label, kind), trace);
|
|
448
448
|
const parameterName = parameterSubjectName(parts.subject, context.checker);
|
|
@@ -7,6 +7,7 @@ export default {
|
|
|
7
7
|
'drizzle-sql-raw-dynamic': { needs: 'import' },
|
|
8
8
|
'escape-replace-incomplete': { needs: 'types' },
|
|
9
9
|
'knex-raw-dynamic-text': { needs: 'import' },
|
|
10
|
+
'ldap-filter-injection': { needs: 'import' },
|
|
10
11
|
'mysql-dynamic-query-text': { needs: 'import' },
|
|
11
12
|
'nosql-operator-injection': { needs: 'import' },
|
|
12
13
|
'prisma-raw-unsafe-dynamic': { needs: 'syntax' },
|
|
@@ -69,7 +69,11 @@ function fieldWritesOf(identifier, checker) {
|
|
|
69
69
|
if (!declaration || !ts.isVariableDeclaration(declaration))
|
|
70
70
|
return [];
|
|
71
71
|
const scope = ts.findAncestor(declaration, (node) => ts.isFunctionLike(node) || ts.isSourceFile(node));
|
|
72
|
-
return scope
|
|
72
|
+
return scope
|
|
73
|
+
? fieldWritesTo(declaration, scope, checker)
|
|
74
|
+
.filter(({ path }) => path.length === 1)
|
|
75
|
+
.map(({ path, assignment }) => ({ name: path[0], assignment }))
|
|
76
|
+
: [];
|
|
73
77
|
}
|
|
74
78
|
// The arguments in the positions the callee receives them: `f(...['a', b])` passes `'a'`, then `b`.
|
|
75
79
|
// A spread of anything else stays a spread, since the positions it fills are unknown.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// makes the finding default. Assembled text with no traced part is
|
|
6
6
|
// contextual: it is usually an internal identifier or a placeholder list, and only a reader can
|
|
7
7
|
// tell.
|
|
8
|
-
import { ts } from '../../performance/engine/compiler.js';
|
|
8
|
+
import { ts, isDefaultLibrary } from '../../performance/engine/compiler.js';
|
|
9
9
|
import { analyzeFragment } from '../../performance/datastores/pg-fragments.js';
|
|
10
10
|
import { operandsOf, isValidatedIdentifier } from '../pg-identifier-guards.js';
|
|
11
11
|
import { isPrunedType } from '../taint/sanitizers.js';
|
|
@@ -87,17 +87,24 @@ function isFixedText(value, call, context, depth) {
|
|
|
87
87
|
}
|
|
88
88
|
function isFixedPlaceholderCallback(callbackNode, call, context) {
|
|
89
89
|
const callback = ts.skipOuterExpressions(callbackNode);
|
|
90
|
-
if (!(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback))
|
|
90
|
+
if (!(ts.isArrowFunction(callback) || ts.isFunctionExpression(callback)))
|
|
91
|
+
return false;
|
|
92
|
+
const body = ts.isBlock(callback.body)
|
|
93
|
+
? callback.body.statements.length === 1 && ts.isReturnStatement(callback.body.statements[0])
|
|
94
|
+
? callback.body.statements[0].expression
|
|
95
|
+
: undefined
|
|
96
|
+
: callback.body;
|
|
97
|
+
if (!body)
|
|
91
98
|
return false;
|
|
92
99
|
const [element] = callback.parameters;
|
|
93
|
-
if (element && !(ts.isIdentifier(element.name) && !isNameUsedIn(element.name,
|
|
100
|
+
if (element && !(ts.isIdentifier(element.name) && !isNameUsedIn(element.name, body)))
|
|
94
101
|
return false;
|
|
95
|
-
if (isFixedPart(
|
|
102
|
+
if (isFixedPart(body, call, context))
|
|
96
103
|
return true;
|
|
97
104
|
// The second parameter is the element's index, a number even when the array is typed `any`.
|
|
98
105
|
const index = callback.parameters[1];
|
|
99
106
|
const isIndex = (part) => !!index && ts.isIdentifier(part) && context.checker.getSymbolAtLocation(part)?.valueDeclaration === index;
|
|
100
|
-
return unresolvedParts(
|
|
107
|
+
return unresolvedParts(body, call, context, maxVariableDepth - 1)?.every(isIndex) ?? false;
|
|
101
108
|
}
|
|
102
109
|
// `ids.map(() => '?').join(', ')`, `Array(n).fill('?').join(', ')`,
|
|
103
110
|
// `Array.from({ length: n }, () => '?').join(', ')`, or `ids.map((_, i) => '$' + (i + 1))`:
|
|
@@ -124,7 +131,7 @@ function isPlaceholderList(node, call, context) {
|
|
|
124
131
|
if (method === 'from' && list.arguments.length === 2) {
|
|
125
132
|
const receiver = receiverOf(list);
|
|
126
133
|
const root = receiver && ts.skipOuterExpressions(receiver);
|
|
127
|
-
if (root && ts.isIdentifier(root) && root.text === 'Array') {
|
|
134
|
+
if (root && ts.isIdentifier(root) && root.text === 'Array' && isBuiltInArrayFrom(list, root, context)) {
|
|
128
135
|
return isFixedPlaceholderCallback(list.arguments[1], call, context);
|
|
129
136
|
}
|
|
130
137
|
}
|
|
@@ -133,13 +140,19 @@ function isPlaceholderList(node, call, context) {
|
|
|
133
140
|
return isFixedPlaceholderCallback(list.arguments[0], call, context);
|
|
134
141
|
}
|
|
135
142
|
// A custom `map` or `fill` can return runtime text without calling its callback, so a method the
|
|
136
|
-
// project declares
|
|
137
|
-
// unresolved call (plain JavaScript, loose request types) keep it.
|
|
143
|
+
// project or a package declares loses the placeholder-list exemption; the language's own array
|
|
144
|
+
// methods and an unresolved call (plain JavaScript, loose request types) keep it.
|
|
138
145
|
function isBuiltInArrayReceiver(list, context) {
|
|
139
|
-
|
|
146
|
+
const callee = ts.skipOuterExpressions(list.expression);
|
|
147
|
+
if (!ts.isPropertyAccessExpression(callee))
|
|
140
148
|
return false;
|
|
141
|
-
const method = context.checker.getSymbolAtLocation(
|
|
142
|
-
return !method?.declarations?.some((declaration) => !declaration.getSourceFile()
|
|
149
|
+
const method = context.checker.getSymbolAtLocation(callee.name);
|
|
150
|
+
return !method?.declarations?.some((declaration) => !isDefaultLibrary(context.checker, declaration.getSourceFile()));
|
|
151
|
+
}
|
|
152
|
+
// A project-declared `Array` or `from` can return runtime text without calling its callback.
|
|
153
|
+
function isBuiltInArrayFrom(list, array, context) {
|
|
154
|
+
const isProjectDeclared = (symbol) => symbol?.declarations?.some((declaration) => !declaration.getSourceFile().isDeclarationFile);
|
|
155
|
+
return !isProjectDeclared(context.checker.getSymbolAtLocation(array)) && isBuiltInArrayReceiver(list, context);
|
|
143
156
|
}
|
|
144
157
|
// Any identifier with the same text counts as a use, shadowed or not, so the answer errs towards
|
|
145
158
|
// "used".
|
|
@@ -150,6 +163,8 @@ function isNameUsedIn(name, body) {
|
|
|
150
163
|
}
|
|
151
164
|
// `sequelize.escape(value)` quotes the value as an SQL literal.
|
|
152
165
|
function isSequelizeEscape(node, context) {
|
|
166
|
+
if (!context.libraryCall)
|
|
167
|
+
return false;
|
|
153
168
|
const escape = ts.skipOuterExpressions(node);
|
|
154
169
|
return (ts.isCallExpression(escape) &&
|
|
155
170
|
methodName(escape) === 'escape' &&
|
|
@@ -461,7 +476,7 @@ function isSequelizeInstance(receiver, library, context) {
|
|
|
461
476
|
const name = ts.isIdentifier(node) ? node.text : ts.isPropertyAccessExpression(node) ? node.name.text : undefined;
|
|
462
477
|
if (name === 'sequelize')
|
|
463
478
|
return true;
|
|
464
|
-
return createdBy(node, (created) => ts.isNewExpression(created) && hasModule(sequelizeModules, context.libraryCall(created)?.module), context.checker);
|
|
479
|
+
return createdBy(node, (created) => ts.isNewExpression(created) && hasModule(sequelizeModules, context.libraryCall?.(created)?.module), context.checker);
|
|
465
480
|
}
|
|
466
481
|
function sequelizeText(call, method, library, context) {
|
|
467
482
|
if (method === 'query' && isSequelizeInstance(receiverOf(call), library, context))
|
|
@@ -510,4 +525,4 @@ function check(call, context) {
|
|
|
510
525
|
if (sequelize)
|
|
511
526
|
checkText(sequelize, call, 'sequelize-dynamic-query-text', context);
|
|
512
527
|
}
|
|
513
|
-
export { check };
|
|
528
|
+
export { check, isPlaceholderList };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Lexical lookup of the declaration an identifier refers to, so constants resolve through real scoping.
|
|
2
|
+
import { ts } from '../performance/engine/compiler.js';
|
|
3
|
+
const OTHER = { kind: 'other' };
|
|
4
|
+
function bindingDeclaresName(name, target) {
|
|
5
|
+
if (ts.isIdentifier(name))
|
|
6
|
+
return name.text === target;
|
|
7
|
+
const elements = name.elements;
|
|
8
|
+
return elements.some((element) => !ts.isOmittedExpression(element) && bindingDeclaresName(element.name, target));
|
|
9
|
+
}
|
|
10
|
+
// `var` is function-scoped, so it can sit in a nested block; nested functions have their own scope.
|
|
11
|
+
function declaresHoistedVar(root, target) {
|
|
12
|
+
let found = false;
|
|
13
|
+
(function visit(node) {
|
|
14
|
+
if (found || (node !== root && ts.isFunctionLike(node)))
|
|
15
|
+
return;
|
|
16
|
+
if (ts.isVariableDeclarationList(node) &&
|
|
17
|
+
!(node.flags & ts.NodeFlags.BlockScoped) &&
|
|
18
|
+
node.declarations.some((decl) => bindingDeclaresName(decl.name, target))) {
|
|
19
|
+
found = true;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
ts.forEachChild(node, visit);
|
|
23
|
+
})(root);
|
|
24
|
+
return found;
|
|
25
|
+
}
|
|
26
|
+
function hasMergedNamespace(declaration) {
|
|
27
|
+
const container = declaration.parent;
|
|
28
|
+
if (!ts.isSourceFile(container) && !ts.isModuleBlock(container) && !ts.isBlock(container))
|
|
29
|
+
return false;
|
|
30
|
+
return container.statements.some((stmt) => stmt !== declaration &&
|
|
31
|
+
ts.isModuleDeclaration(stmt) &&
|
|
32
|
+
ts.isIdentifier(stmt.name) &&
|
|
33
|
+
ts.isIdentifier(declaration.name) &&
|
|
34
|
+
stmt.name.text === declaration.name.text);
|
|
35
|
+
}
|
|
36
|
+
function declarationFromList(list, target) {
|
|
37
|
+
for (const decl of list.declarations) {
|
|
38
|
+
if (!bindingDeclaresName(decl.name, target))
|
|
39
|
+
continue;
|
|
40
|
+
const isConstBinding = list.flags & ts.NodeFlags.Const && ts.isIdentifier(decl.name) && decl.initializer;
|
|
41
|
+
return isConstBinding && decl.initializer ? { kind: 'const', initializer: decl.initializer } : OTHER;
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
function declarationInFunction(fn, target) {
|
|
46
|
+
if (fn.parameters.some((param) => bindingDeclaresName(param.name, target)))
|
|
47
|
+
return OTHER;
|
|
48
|
+
if (ts.isFunctionExpression(fn) && fn.name && fn.name.text === target)
|
|
49
|
+
return OTHER;
|
|
50
|
+
if ('body' in fn && fn.body && declaresHoistedVar(fn.body, target))
|
|
51
|
+
return OTHER;
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
function declarationInLoopHead(loop, target) {
|
|
55
|
+
if (!loop.initializer || !ts.isVariableDeclarationList(loop.initializer))
|
|
56
|
+
return undefined;
|
|
57
|
+
return declarationFromList(loop.initializer, target);
|
|
58
|
+
}
|
|
59
|
+
function declarationInStatement(stmt, target) {
|
|
60
|
+
if (ts.isFunctionDeclaration(stmt) && stmt.name?.text === target)
|
|
61
|
+
return OTHER;
|
|
62
|
+
if (ts.isImportEqualsDeclaration(stmt) && stmt.name.text === target)
|
|
63
|
+
return OTHER;
|
|
64
|
+
if (!ts.isVariableStatement(stmt))
|
|
65
|
+
return undefined;
|
|
66
|
+
return declarationFromList(stmt.declarationList, target);
|
|
67
|
+
}
|
|
68
|
+
function declarationInScope(scope, target) {
|
|
69
|
+
const statements = ts.isCaseBlock(scope)
|
|
70
|
+
? scope.clauses.flatMap((clause) => clause.statements)
|
|
71
|
+
: scopeStatements(scope);
|
|
72
|
+
for (const stmt of statements) {
|
|
73
|
+
const found = declarationInStatement(stmt, target);
|
|
74
|
+
if (found)
|
|
75
|
+
return found;
|
|
76
|
+
}
|
|
77
|
+
if (ts.isSourceFile(scope) && declaresHoistedVar(scope, target))
|
|
78
|
+
return OTHER;
|
|
79
|
+
// Merged namespaces share exported members across blocks, so an outer lookup could pick an unrelated binding.
|
|
80
|
+
if (ts.isModuleBlock(scope) && hasMergedNamespace(scope.parent))
|
|
81
|
+
return OTHER;
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
function scopeStatements(scope) {
|
|
85
|
+
if (ts.isBlock(scope) || ts.isModuleBlock(scope) || ts.isSourceFile(scope))
|
|
86
|
+
return scope.statements;
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
function declarationInCatch(clause, target) {
|
|
90
|
+
const variable = clause.variableDeclaration;
|
|
91
|
+
return variable && bindingDeclaresName(variable.name, target) ? OTHER : undefined;
|
|
92
|
+
}
|
|
93
|
+
function declarationAtNode(node, target) {
|
|
94
|
+
if (ts.isFunctionLike(node))
|
|
95
|
+
return declarationInFunction(node, target);
|
|
96
|
+
if (ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node)) {
|
|
97
|
+
return declarationInLoopHead(node, target);
|
|
98
|
+
}
|
|
99
|
+
if (ts.isBlock(node) || ts.isModuleBlock(node) || ts.isSourceFile(node) || ts.isCaseBlock(node)) {
|
|
100
|
+
return declarationInScope(node, target);
|
|
101
|
+
}
|
|
102
|
+
if (ts.isCatchClause(node))
|
|
103
|
+
return declarationInCatch(node, target);
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
export function findLexicalDeclaration(identifier) {
|
|
107
|
+
for (let current = identifier.parent; current; current = current.parent) {
|
|
108
|
+
const found = declarationAtNode(current, identifier.text);
|
|
109
|
+
if (found)
|
|
110
|
+
return found;
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
@@ -5,6 +5,30 @@ import { ts } from '../performance/engine/compiler.js';
|
|
|
5
5
|
import { analyzeFragment } from '../performance/datastores/pg-fragments.js';
|
|
6
6
|
import { queryMethod, textArgument, usesDriverQuery, queryTypes } from '../performance/datastores/pg-integrity.js';
|
|
7
7
|
import { allRuntimePartsValidated } from './pg-identifier-guards.js';
|
|
8
|
+
import { isPlaceholderList } from './injection/sql.js';
|
|
9
|
+
function hasSeparateValues(call) {
|
|
10
|
+
if (call.arguments.length >= 2)
|
|
11
|
+
return true;
|
|
12
|
+
const first = call.arguments[0] && ts.skipOuterExpressions(call.arguments[0]);
|
|
13
|
+
if (!first || !ts.isObjectLiteralExpression(first))
|
|
14
|
+
return false;
|
|
15
|
+
return first.properties.some((property) => (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) &&
|
|
16
|
+
ts.isIdentifier(property.name) &&
|
|
17
|
+
property.name.text === 'values');
|
|
18
|
+
}
|
|
19
|
+
function isBoundPlaceholderFragment(operand, call, context) {
|
|
20
|
+
if (isPlaceholderList(operand, call, context))
|
|
21
|
+
return true;
|
|
22
|
+
const value = ts.skipOuterExpressions(operand);
|
|
23
|
+
if (!ts.isIdentifier(value))
|
|
24
|
+
return false;
|
|
25
|
+
const declaration = context.checker.getSymbolAtLocation(value)?.valueDeclaration;
|
|
26
|
+
return Boolean(declaration &&
|
|
27
|
+
ts.isVariableDeclaration(declaration) &&
|
|
28
|
+
Boolean(declaration.parent.flags & ts.NodeFlags.Const) &&
|
|
29
|
+
declaration.initializer &&
|
|
30
|
+
isPlaceholderList(declaration.initializer, call, context));
|
|
31
|
+
}
|
|
8
32
|
function check(call, context) {
|
|
9
33
|
if (!queryMethod(call))
|
|
10
34
|
return;
|
|
@@ -23,7 +47,10 @@ function check(call, context) {
|
|
|
23
47
|
return;
|
|
24
48
|
if (analyzeFragment(query, context.checker) !== undefined)
|
|
25
49
|
return;
|
|
26
|
-
|
|
50
|
+
const extraSafe = hasSeparateValues(call)
|
|
51
|
+
? (operand) => isBoundPlaceholderFragment(operand, call, context)
|
|
52
|
+
: undefined;
|
|
53
|
+
if (allRuntimePartsValidated(query, call, context.checker, extraSafe))
|
|
27
54
|
return;
|
|
28
55
|
context.report(query, 'pg-dynamic-query-text', 'A runtime value is concatenated into SQL text, which defeats parameter binding and the plan cache and allows injection. Pass data as `$1` placeholders with a values array; pick dynamic identifiers from a fixed allowlist.');
|
|
29
56
|
}
|