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
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// Scans a project's lockfile for packages with known vulnerabilities and reports one finding per
|
|
2
2
|
// package@version.
|
|
3
|
+
import path from 'node:path';
|
|
3
4
|
import { findLockfile, readLockfilePackages, manifestLocation } from './lockfiles.js';
|
|
4
5
|
import { findVulnerabilities } from './osv.js';
|
|
5
|
-
import { severityLevel, highestSeverity, fixedVersion,
|
|
6
|
+
import { severityLevel, highestSeverity, fixedVersion, advisoryGroups } from './advisory.js';
|
|
6
7
|
const ruleId = 'dependency-known-vulnerability';
|
|
7
8
|
const devOnlyReason = 'development-only dependency';
|
|
8
9
|
const advisoryUrl = 'https://osv.dev/vulnerability/';
|
|
@@ -12,18 +13,38 @@ const overrideHints = {
|
|
|
12
13
|
pnpm: (name, fixed) => `add "pnpm": { "overrides": { "${name}": "${fixed}" } } to package.json, then run pnpm install`,
|
|
13
14
|
yarn: (name, fixed) => `add "resolutions": { "${name}": "${fixed}" } to package.json, then run yarn install`,
|
|
14
15
|
};
|
|
15
|
-
function fixAdvice({ name, direct }, fixed, manager) {
|
|
16
|
+
function fixAdvice({ name, direct, declarations, patched }, aliases, fixed, { manager, filename }) {
|
|
17
|
+
if (patched)
|
|
18
|
+
return 'Patched via Yarn patch: protocol; confirm whether the local patch already remediates this advisory before replacing or updating the dependency.';
|
|
16
19
|
if (!fixed)
|
|
17
|
-
return '
|
|
20
|
+
return 'The advisory records do not establish a fixed version; review the advisories, or replace or remove the package.';
|
|
21
|
+
const owner = (directory) => path.relative(path.dirname(filename), directory).split(path.sep).join('/') || '.';
|
|
22
|
+
const owners = [...new Set(declarations.map(({ directory }) => owner(directory)))];
|
|
23
|
+
const scope = owners.some((owner) => owner !== '.') ? ` Declaring project directories: ${owners.join(', ')}.` : '';
|
|
24
|
+
if (aliases.length && owners.length > 1) {
|
|
25
|
+
const keys = declarations.map((declaration) => `${owner(declaration.directory)}/package.json (${declaration.name})`);
|
|
26
|
+
return `Update the declarations resolving to ${name}@${fixed} in ${[...new Set(keys)].join(', ')}.`;
|
|
27
|
+
}
|
|
28
|
+
if (aliases.length)
|
|
29
|
+
return `Review the npm alias ${aliases.join(', ')} and update its registry target to ${name}@${fixed}.` + scope;
|
|
30
|
+
if (scope)
|
|
31
|
+
return `Update the declaration of ${name} to ${fixed} in each listed project.` + scope;
|
|
18
32
|
return direct
|
|
19
33
|
? `Fix: ${installCommands[manager]} ${name}@${fixed}`
|
|
20
34
|
: `Fix: transitive dependency; ${overrideHints[manager](name, fixed)}`;
|
|
21
35
|
}
|
|
22
36
|
function buildFinding(entry, vulnerabilities, lockfile) {
|
|
23
|
-
const advisories =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
const advisories = advisoryGroups(vulnerabilities).map((group) => ({
|
|
38
|
+
id: group[0].id,
|
|
39
|
+
severity: highestSeverity(group.map(severityLevel)),
|
|
40
|
+
summary: group[0].summary,
|
|
41
|
+
url: advisoryUrl + group[0].id,
|
|
42
|
+
}));
|
|
43
|
+
const severity = highestSeverity(advisories.map((advisory) => advisory.severity));
|
|
44
|
+
const impactLevel = highestSeverity(advisories.map((advisory) => advisory.severity ?? 'medium'));
|
|
45
|
+
const fixed = fixedVersion(vulnerabilities, entry.name, entry.version);
|
|
46
|
+
const aliases = [...new Set(entry.declarations.map(({ name }) => name).filter((name) => name !== entry.name))];
|
|
47
|
+
const location = (entry.direct && manifestLocation(entry.declarations)) || {
|
|
27
48
|
file: lockfile.filename,
|
|
28
49
|
line: entry.line,
|
|
29
50
|
};
|
|
@@ -34,27 +55,33 @@ function buildFinding(entry, vulnerabilities, lockfile) {
|
|
|
34
55
|
column: 1,
|
|
35
56
|
ruleId,
|
|
36
57
|
message: `${entry.name}@${entry.version} has ${advisories.length} known ${advisories.length === 1 ? 'vulnerability' : 'vulnerabilities'} ` +
|
|
37
|
-
`(${ids}), highest severity ${severity}. ${fixAdvice(entry, fixed, lockfile
|
|
38
|
-
severity,
|
|
39
|
-
impactLevel
|
|
40
|
-
package: {
|
|
58
|
+
`(${ids}), highest known severity ${severity ?? 'unknown'}. ${fixAdvice(entry, aliases, fixed, lockfile)}`,
|
|
59
|
+
severity: severity ?? 'unknown',
|
|
60
|
+
impactLevel,
|
|
61
|
+
package: {
|
|
62
|
+
name: entry.name,
|
|
63
|
+
version: entry.version,
|
|
64
|
+
direct: entry.direct,
|
|
65
|
+
dev: entry.dev,
|
|
66
|
+
...(entry.patched && { patched: true }),
|
|
67
|
+
...(aliases.length && { aliases }),
|
|
68
|
+
},
|
|
41
69
|
fixedVersion: fixed,
|
|
42
|
-
advisories: advisories.map((advisory) => ({
|
|
43
|
-
id: advisory.id,
|
|
44
|
-
severity: severityLevel(advisory),
|
|
45
|
-
summary: advisory.summary,
|
|
46
|
-
url: advisoryUrl + advisory.id,
|
|
47
|
-
})),
|
|
70
|
+
advisories: advisories.map((advisory) => ({ ...advisory, severity: advisory.severity ?? 'unknown' })),
|
|
48
71
|
};
|
|
49
|
-
|
|
50
|
-
|
|
72
|
+
const reasons = [
|
|
73
|
+
entry.dev ? devOnlyReason : undefined,
|
|
74
|
+
entry.patched ? 'patched via Yarn patch: protocol' : undefined,
|
|
75
|
+
].filter(Boolean);
|
|
76
|
+
if (reasons.length)
|
|
77
|
+
finding.contextualReason = reasons.join('; ');
|
|
51
78
|
return finding;
|
|
52
79
|
}
|
|
53
80
|
// `options` carries the injected fetch, cache directory and clock for tests; see osv.cjs.
|
|
54
81
|
async function scanDependencies(directory, options) {
|
|
55
82
|
const lockfile = findLockfile(directory);
|
|
56
83
|
if (!lockfile)
|
|
57
|
-
throw new Error(`No package-lock.json, pnpm-lock.yaml or yarn.lock found in ${directory} or above it.`);
|
|
84
|
+
throw new Error(`No package-lock.json, npm-shrinkwrap.json, pnpm-lock.yaml or yarn.lock found in ${directory} or above it.`);
|
|
58
85
|
const packages = readLockfilePackages(lockfile);
|
|
59
86
|
const vulnerable = await findVulnerabilities(packages, options);
|
|
60
87
|
const findings = packages
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
function descriptor(name, range) {
|
|
2
|
+
return name + '@' + range.replace(/^npm:/, '');
|
|
3
|
+
}
|
|
4
|
+
function packageSelector(value) {
|
|
5
|
+
const boundary = value.indexOf('@', 1);
|
|
6
|
+
if (boundary < 1)
|
|
7
|
+
return undefined;
|
|
8
|
+
return { name: value.slice(0, boundary), range: value.slice(boundary + 1) };
|
|
9
|
+
}
|
|
10
|
+
function addYarnEdge(entry, line, isPeerEdge) {
|
|
11
|
+
const edge = /^ "?([^"\s:]+)"?:?\s+(?:"([^"]+)"|(\S+))$/.exec(line);
|
|
12
|
+
if (!edge) {
|
|
13
|
+
entry.unknownEdges = true;
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const edgeDescriptor = descriptor(edge[1], edge[2] ?? edge[3]);
|
|
17
|
+
entry.dependencies.push(edgeDescriptor);
|
|
18
|
+
if (!isPeerEdge)
|
|
19
|
+
entry.installDependencies.push(edgeDescriptor);
|
|
20
|
+
}
|
|
21
|
+
function yarnEntries(text) {
|
|
22
|
+
const entries = [];
|
|
23
|
+
let entry;
|
|
24
|
+
let readingEdges = false;
|
|
25
|
+
let readingPeers = false;
|
|
26
|
+
for (const [index, line] of text.split(/\r?\n/).entries()) {
|
|
27
|
+
if (/^\S.*:$/.test(line) && !line.startsWith('#')) {
|
|
28
|
+
const selectors = line
|
|
29
|
+
.slice(0, -1)
|
|
30
|
+
.split(',')
|
|
31
|
+
.map((part) => part.trim().replaceAll(/^"|"$/g, ''));
|
|
32
|
+
entry = {
|
|
33
|
+
selectors,
|
|
34
|
+
dependencies: [],
|
|
35
|
+
installDependencies: [],
|
|
36
|
+
version: '',
|
|
37
|
+
line: index + 1,
|
|
38
|
+
unknownEdges: false,
|
|
39
|
+
};
|
|
40
|
+
entries.push(entry);
|
|
41
|
+
readingEdges = false;
|
|
42
|
+
readingPeers = false;
|
|
43
|
+
}
|
|
44
|
+
else if (entry && /^ \S/.test(line)) {
|
|
45
|
+
const version = /^ version:? "?([^"\s]+)"?$/.exec(line);
|
|
46
|
+
if (version)
|
|
47
|
+
entry.version = version[1];
|
|
48
|
+
readingEdges = /^ (?:dependencies|optionalDependencies|peerDependencies):$/.test(line);
|
|
49
|
+
readingPeers = line === ' peerDependencies:';
|
|
50
|
+
}
|
|
51
|
+
else if (entry && readingEdges && line.trim() && !line.trimStart().startsWith('#')) {
|
|
52
|
+
addYarnEdge(entry, line, readingPeers);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return entries;
|
|
56
|
+
}
|
|
57
|
+
function reachable(selectors, entries) {
|
|
58
|
+
const reached = new Set();
|
|
59
|
+
const pending = [...selectors];
|
|
60
|
+
let complete = true;
|
|
61
|
+
for (let key = pending.pop(); key !== undefined; key = pending.pop()) {
|
|
62
|
+
const entry = entries.get(key);
|
|
63
|
+
if (!entry) {
|
|
64
|
+
complete = false;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (reached.has(entry))
|
|
68
|
+
continue;
|
|
69
|
+
reached.add(entry);
|
|
70
|
+
if (entry.unknownEdges)
|
|
71
|
+
complete = false;
|
|
72
|
+
pending.push(...entry.dependencies);
|
|
73
|
+
}
|
|
74
|
+
return { reached, complete };
|
|
75
|
+
}
|
|
76
|
+
function manifestSelectors(fields, keys) {
|
|
77
|
+
return keys.flatMap((key) => Object.entries(fields?.[key] ?? {}).flatMap(([name, range]) => typeof range === 'string' ? [descriptor(name, range)] : []));
|
|
78
|
+
}
|
|
79
|
+
const nonRegistryRange = /^(?:link|file|workspace|portal|git|github|https?):/;
|
|
80
|
+
function resolveYarnTarget(parsed) {
|
|
81
|
+
if (parsed.range.startsWith('patch:')) {
|
|
82
|
+
const locator = parsed.range.slice('patch:'.length).split('#')[0];
|
|
83
|
+
let decoded = locator;
|
|
84
|
+
try {
|
|
85
|
+
decoded = decodeURIComponent(locator);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Keep raw locator when percent-encoding is malformed.
|
|
89
|
+
}
|
|
90
|
+
const inner = packageSelector(decoded);
|
|
91
|
+
if (!inner)
|
|
92
|
+
return undefined;
|
|
93
|
+
const innerRange = inner.range.replace(/^npm:/, '');
|
|
94
|
+
if (nonRegistryRange.test(innerRange) || innerRange.startsWith('patch:'))
|
|
95
|
+
return undefined;
|
|
96
|
+
return { name: inner.name, patched: true, source: descriptor(inner.name, innerRange) };
|
|
97
|
+
}
|
|
98
|
+
if (nonRegistryRange.test(parsed.range))
|
|
99
|
+
return undefined;
|
|
100
|
+
const aliasTarget = parsed.range.startsWith('npm:') ? packageSelector(parsed.range.slice(4)) : undefined;
|
|
101
|
+
return { name: aliasTarget?.name ?? parsed.name, patched: false };
|
|
102
|
+
}
|
|
103
|
+
export function yarnPackages(text, root, manifest) {
|
|
104
|
+
const entries = yarnEntries(text);
|
|
105
|
+
const bySelector = new Map();
|
|
106
|
+
const workspaceRoots = [];
|
|
107
|
+
for (const entry of entries) {
|
|
108
|
+
for (const selector of entry.selectors) {
|
|
109
|
+
const parsed = packageSelector(selector);
|
|
110
|
+
if (!parsed)
|
|
111
|
+
continue;
|
|
112
|
+
bySelector.set(descriptor(parsed.name, parsed.range), entry);
|
|
113
|
+
// A non-root workspace may introduce production edges absent from the root manifest.
|
|
114
|
+
if (parsed.range.startsWith('workspace:') && parsed.range !== 'workspace:.')
|
|
115
|
+
workspaceRoots.push(descriptor(parsed.name, parsed.range));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const productionRoots = manifestSelectors(manifest, ['dependencies', 'optionalDependencies', 'peerDependencies']);
|
|
119
|
+
const devRoots = manifestSelectors(manifest, ['devDependencies']);
|
|
120
|
+
const declaredSelectors = new Set([...productionRoots, ...devRoots]);
|
|
121
|
+
const production = reachable([...productionRoots, ...workspaceRoots], bySelector);
|
|
122
|
+
const development = reachable(devRoots, bySelector);
|
|
123
|
+
// Classic lockfiles omit workspace declarations, so a root dev pin may also serve a workspace.
|
|
124
|
+
const complete = production.complete && !(manifest?.workspaces && !/^__metadata:/m.test(text));
|
|
125
|
+
const patchSources = new Set();
|
|
126
|
+
for (const entry of entries) {
|
|
127
|
+
const parsed = packageSelector(entry.selectors[0]);
|
|
128
|
+
const source = parsed && entry.version ? resolveYarnTarget(parsed)?.source : undefined;
|
|
129
|
+
if (source)
|
|
130
|
+
patchSources.add(source);
|
|
131
|
+
}
|
|
132
|
+
// Peer requirements inherit their provider from an ancestor, so they never install the source entry themselves.
|
|
133
|
+
const referencedSelectors = new Set([...declaredSelectors, ...entries.flatMap((entry) => entry.installDependencies)]);
|
|
134
|
+
// Berry keeps the unpatched base entry only as the patch's source; it is not installed unless something else uses it.
|
|
135
|
+
const isPatchSourceOnly = (entry) => entry.selectors.some((selector) => patchSources.has(selector.replace(/@npm:/, '@'))) &&
|
|
136
|
+
!entry.selectors.some((selector) => referencedSelectors.has(selector.replace(/@npm:/, '@')));
|
|
137
|
+
return entries.flatMap((entry) => {
|
|
138
|
+
const parsed = packageSelector(entry.selectors[0]);
|
|
139
|
+
const target = parsed && entry.version ? resolveYarnTarget(parsed) : undefined;
|
|
140
|
+
if (!target || (!target.patched && isPatchSourceOnly(entry)))
|
|
141
|
+
return [];
|
|
142
|
+
const names = entry.selectors.flatMap((selector) => {
|
|
143
|
+
const declared = packageSelector(selector);
|
|
144
|
+
return declared && declaredSelectors.has(descriptor(declared.name, declared.range)) ? [declared.name] : [];
|
|
145
|
+
});
|
|
146
|
+
return [
|
|
147
|
+
{
|
|
148
|
+
name: target.name,
|
|
149
|
+
version: entry.version,
|
|
150
|
+
direct: names.length > 0,
|
|
151
|
+
dev: complete && development.reached.has(entry) && !production.reached.has(entry),
|
|
152
|
+
...(target.patched && { patched: true }),
|
|
153
|
+
line: entry.line,
|
|
154
|
+
declarations: [...new Set(names)].map((name) => ({ directory: root, name })),
|
|
155
|
+
},
|
|
156
|
+
];
|
|
157
|
+
});
|
|
158
|
+
}
|
|
@@ -22,6 +22,10 @@ const examples = {
|
|
|
22
22
|
"const rows = await knex('users').whereRaw('lower(email) = ?', [email]).orderByRaw('?? desc', [column]);",
|
|
23
23
|
'Bind values with `?` and identifiers with `??`; choose identifiers from an allowlist.',
|
|
24
24
|
],
|
|
25
|
+
'ldap-filter-injection': [
|
|
26
|
+
"const filter = `(uid=${escapeFilterValue(req.query.uid)})`;\nconst { searchEntries } = await client.search(baseDn, { scope: 'sub', filter });",
|
|
27
|
+
'Escape each interpolated value for an LDAP filter (RFC 4515) or build the filter with the library helper.',
|
|
28
|
+
],
|
|
25
29
|
'mysql-dynamic-query-text': [
|
|
26
30
|
"const [rows] = await pool.execute('SELECT id FROM users WHERE email = ?', [email]);",
|
|
27
31
|
'Pass values as placeholders; choose table and column names from a fixed allowlist.',
|
|
@@ -79,15 +79,30 @@ function assess(options, { httpOnlyDefault, scriptReadable }) {
|
|
|
79
79
|
missing.push('secure');
|
|
80
80
|
if (isFalse(secure?.value))
|
|
81
81
|
definite = true;
|
|
82
|
-
|
|
82
|
+
const sameSite = options && findProperty(options, 'sameSite');
|
|
83
|
+
if (!sameSite || isFalse(sameSite.value))
|
|
83
84
|
missing.push('sameSite');
|
|
84
85
|
return { missing, definite };
|
|
85
86
|
}
|
|
87
|
+
const flagGuidance = {
|
|
88
|
+
httpOnly: '`httpOnly: true` so scripts cannot read it',
|
|
89
|
+
secure: '`secure: true` in production',
|
|
90
|
+
sameSite: "`sameSite: 'lax'` or stricter",
|
|
91
|
+
};
|
|
92
|
+
function joinList(items) {
|
|
93
|
+
if (items.length <= 1)
|
|
94
|
+
return items[0] ?? '';
|
|
95
|
+
return `${items.slice(0, -1).join(', ')} and ${items[items.length - 1]}`;
|
|
96
|
+
}
|
|
97
|
+
export function isScriptReadableCookieName(expression) {
|
|
98
|
+
return scriptReadableNamePattern.test(nameOf(expression));
|
|
99
|
+
}
|
|
86
100
|
function report(node, context, assessment) {
|
|
87
101
|
if (!assessment.missing.length)
|
|
88
102
|
return;
|
|
89
103
|
const flags = assessment.missing.join(', ');
|
|
90
|
-
const
|
|
104
|
+
const guidance = joinList(assessment.missing.map((flag) => flagGuidance[flag] ?? flag));
|
|
105
|
+
const message = `This session cookie is set without ${flags}. Add ${guidance}.`;
|
|
91
106
|
context.report(node, ruleId, message, assessment.definite ? undefined : deploymentReason);
|
|
92
107
|
}
|
|
93
108
|
// `res.cookie(name, value, options)`, `reply.setCookie(...)`, `ctx.cookies.set(...)`,
|
|
@@ -13,7 +13,6 @@ const portSuffixPattern = /^(?:[():?]|\\d|\[0-9\]|[+*]|\{\d+(?:,\d*)?\}|\\\/|\\:
|
|
|
13
13
|
// What may precede the host: a scheme separator, a subdomain dot or a group such as `(www\.)?` or
|
|
14
14
|
// `(^|\.)`.
|
|
15
15
|
const boundaryBeforeHost = /(?:\^|\\\/\\\/|\/\/|\\\.|\[\.\]|\)[?*+]?)$/;
|
|
16
|
-
const subdomainBoundary = '(?:\\/\\/|\\.)';
|
|
17
16
|
const originNamePattern = /^(?:req(?:uest)?)?origin$/i;
|
|
18
17
|
const originHeaderPattern = /headers(?:\.origin|\[\s*['"]origin['"]\s*\])|\.(?:get|header)\(\s*['"]origin['"]\s*\)/i;
|
|
19
18
|
// A `new RegExp(...)` argument named like a pattern is meant to be a regex, so its author owns the
|
|
@@ -88,10 +87,10 @@ function fixedRegexLiteral(literalText) {
|
|
|
88
87
|
const [flaws] = branches.map(branchFlaws);
|
|
89
88
|
if (branches.length !== 1 || !flaws)
|
|
90
89
|
return;
|
|
91
|
-
if (
|
|
90
|
+
if (flaws.start || (flaws.end && !flaws.endFixable))
|
|
92
91
|
return;
|
|
93
92
|
const escaped = source.replaceAll(hostPattern, (host, offset) => source[offset - 1] === '\\' ? host : host.replaceAll(/(^|[^\\])\.(?![*+?{])/g, '$1\\.'));
|
|
94
|
-
const anchored = `${
|
|
93
|
+
const anchored = `${escaped}${flaws.end ? '$' : ''}`;
|
|
95
94
|
return anchored === source ? undefined : `/${anchored}/${flags}`;
|
|
96
95
|
}
|
|
97
96
|
// Site-fix builder: rewrites the flagged regex literal.
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// field; other dynamic values are contextual.
|
|
11
11
|
import { ts, isDefaultLibrary } from '../../performance/engine/compiler.js';
|
|
12
12
|
import { staticMemberName } from '../../shared/analysis/ast-semantics.js';
|
|
13
|
+
import { hasDomPurifyAt, isJsonScript } from './react-html.js';
|
|
13
14
|
import { moduleName } from '../bindings.js';
|
|
14
15
|
import { calleeName } from '../../performance/schema/ts-helpers.js';
|
|
15
16
|
import { bodyOf, declaredNameOf, typeAnnotationOf } from '../declarations.js';
|
|
@@ -313,10 +314,10 @@ function assess(expression, context, depth = 0) {
|
|
|
313
314
|
return assessMember(node, context, depth);
|
|
314
315
|
return ts.isIdentifier(node) ? assessIdentifier(node, context, depth) : unknownValue;
|
|
315
316
|
}
|
|
316
|
-
function reportAssessment(node, context, { ruleId, sink, advice }, assessment) {
|
|
317
|
+
function reportAssessment(node, context, { ruleId, sink, advice, hasDomPurify }, assessment) {
|
|
317
318
|
if (!assessment)
|
|
318
319
|
return;
|
|
319
|
-
context.report(node, ruleId, `${assessment.label} reaches ${sink} unsanitized, which allows XSS if it holds attacker-controlled input. ${advice}`, assessment.reason);
|
|
320
|
+
context.report(node, ruleId, `${assessment.label} reaches ${sink} unsanitized, which allows XSS if it holds attacker-controlled input. ${advice}`, assessment.reason, hasDomPurify ? { hasDomPurify } : undefined);
|
|
320
321
|
}
|
|
321
322
|
function reportDynamicValue(node, context, report) {
|
|
322
323
|
reportAssessment(node, context, report, assess(report.value, context));
|
|
@@ -377,12 +378,29 @@ function isSensitiveKey(key) {
|
|
|
377
378
|
const normalized = key.replaceAll(/([a-z0-9])([A-Z])/g, '$1_$2').toLowerCase();
|
|
378
379
|
return SENSITIVE_KEY_WORDS.test(normalized) && !NON_SECRET_KEY_WORDS.test(normalized);
|
|
379
380
|
}
|
|
381
|
+
// Only a `const` keeps its narrowed type across calls, so a mutable read or an awaited value is never trusted.
|
|
382
|
+
function isConstEmptyString(node, checker) {
|
|
383
|
+
if (!ts.isIdentifier(node))
|
|
384
|
+
return false;
|
|
385
|
+
const declaration = checker.getSymbolAtLocation(node)?.valueDeclaration;
|
|
386
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !(declaration.parent.flags & ts.NodeFlags.Const))
|
|
387
|
+
return false;
|
|
388
|
+
const type = checker.getTypeAtLocation(node);
|
|
389
|
+
return Boolean(type.flags & ts.TypeFlags.StringLiteral) && type.value === '';
|
|
390
|
+
}
|
|
380
391
|
function checkStorage(call, member, context) {
|
|
381
392
|
if (!declaredInDomInterface(memberDeclarations(member, context), context, STORAGE_INTERFACES) || !call.arguments[0])
|
|
382
393
|
return;
|
|
383
394
|
const key = literalKeyText(call.arguments[0], context.checker);
|
|
384
395
|
if (!key || !isSensitiveKey(key))
|
|
385
396
|
return;
|
|
397
|
+
const stored = call.arguments[1] && unwrap(call.arguments[1]);
|
|
398
|
+
if (stored) {
|
|
399
|
+
if (ts.isStringLiteralLike(stored) && stored.text === '')
|
|
400
|
+
return;
|
|
401
|
+
if (isConstEmptyString(stored, context.checker))
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
386
404
|
context.report(call, 'frontend-storage-secret-token', `Storing a credential under \`${key}\` in web storage exposes it to any script running on the page (XSS exfiltration). ${STORAGE_ADVICE}`);
|
|
387
405
|
}
|
|
388
406
|
function propertyKey(property) {
|
|
@@ -521,18 +539,6 @@ function isReactAttribute(attribute, context) {
|
|
|
521
539
|
const typedByReact = declarations.some((declaration) => REACT_TYPES_PATH.test(declaration.getSourceFile().fileName.replaceAll('\\', '/')));
|
|
522
540
|
return typedByReact || importsPackage(attribute.getSourceFile(), REACT_PACKAGES);
|
|
523
541
|
}
|
|
524
|
-
// `<script type="application/ld+json" dangerouslySetInnerHTML=... />` carries data, not markup.
|
|
525
|
-
function isJsonScript(attribute) {
|
|
526
|
-
const element = attribute.parent.parent;
|
|
527
|
-
if (!ts.isIdentifier(element.tagName) || element.tagName.text !== 'script')
|
|
528
|
-
return false;
|
|
529
|
-
return element.attributes.properties.some((property) => ts.isJsxAttribute(property) &&
|
|
530
|
-
ts.isIdentifier(property.name) &&
|
|
531
|
-
property.name.text === 'type' &&
|
|
532
|
-
!!property.initializer &&
|
|
533
|
-
ts.isStringLiteral(property.initializer) &&
|
|
534
|
-
property.initializer.text.includes('json'));
|
|
535
|
-
}
|
|
536
542
|
function htmlProperty(object) {
|
|
537
543
|
return object.properties.find((property) => (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) &&
|
|
538
544
|
propertyKey(property) === '__html');
|
|
@@ -553,6 +559,7 @@ function checkJsxAttribute(attribute, context) {
|
|
|
553
559
|
reportDynamicValue(attribute, context, {
|
|
554
560
|
ruleId: 'react-dangerously-set-inner-html',
|
|
555
561
|
sink: '`dangerouslySetInnerHTML`',
|
|
562
|
+
hasDomPurify: hasDomPurifyAt(attribute, context.checker),
|
|
556
563
|
value: ts.isShorthandPropertyAssignment(property) ? property.name : property.initializer,
|
|
557
564
|
advice: 'Sanitize with `DOMPurify.sanitize(...)` or render JSX elements instead.',
|
|
558
565
|
});
|
|
@@ -122,32 +122,20 @@ function protocolBinding(node, context) {
|
|
|
122
122
|
const input = parsed.arguments?.[0] && unwrap(parsed.arguments[0]);
|
|
123
123
|
return input && ts.isIdentifier(input) ? declarationsOf(input, context)[0] : undefined;
|
|
124
124
|
}
|
|
125
|
-
function
|
|
126
|
-
const bindings = new Set();
|
|
127
|
-
const visit = (node) => {
|
|
128
|
-
if (ts.isPropertyAccessExpression(node)) {
|
|
129
|
-
const binding = protocolBinding(node, context);
|
|
130
|
-
if (binding)
|
|
131
|
-
bindings.add(binding);
|
|
132
|
-
}
|
|
133
|
-
ts.forEachChild(node, visit);
|
|
134
|
-
};
|
|
135
|
-
visit(condition);
|
|
136
|
-
return bindings;
|
|
137
|
-
}
|
|
138
|
-
function approvedBracketProtocolBinding(condition, context) {
|
|
125
|
+
function approvedProtocol(condition, context) {
|
|
139
126
|
const comparison = unwrap(condition);
|
|
140
|
-
if (!ts.isBinaryExpression(comparison)
|
|
127
|
+
if (!ts.isBinaryExpression(comparison))
|
|
141
128
|
return;
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (scheme.text === 'http:' || scheme.text === 'https:')
|
|
148
|
-
return protocolBinding(
|
|
129
|
+
const operator = comparison.operatorToken.kind;
|
|
130
|
+
if (operator !== ts.SyntaxKind.EqualsEqualsEqualsToken && operator !== ts.SyntaxKind.ExclamationEqualsEqualsToken)
|
|
131
|
+
return;
|
|
132
|
+
const comparedBinding = (member, literal) => {
|
|
133
|
+
const scheme = unwrap(literal);
|
|
134
|
+
if (ts.isStringLiteralLike(scheme) && (scheme.text === 'http:' || scheme.text === 'https:'))
|
|
135
|
+
return protocolBinding(unwrap(member), context);
|
|
149
136
|
};
|
|
150
|
-
|
|
137
|
+
const binding = comparedBinding(comparison.left, comparison.right) ?? comparedBinding(comparison.right, comparison.left);
|
|
138
|
+
return binding && { binding, onTrue: operator === ts.SyntaxKind.EqualsEqualsEqualsToken };
|
|
151
139
|
}
|
|
152
140
|
function isGlobalUrl(callee, context) {
|
|
153
141
|
const name = unwrap(callee);
|
|
@@ -155,17 +143,14 @@ function isGlobalUrl(callee, context) {
|
|
|
155
143
|
}
|
|
156
144
|
// A branch that renders the very binding the condition checked the protocol of is guarded.
|
|
157
145
|
function conditionalLeadSource(node, context, next) {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
const branchBinding = (branch) => {
|
|
146
|
+
const guard = approvedProtocol(node.condition, context);
|
|
147
|
+
function branchSource(branch, truth) {
|
|
161
148
|
const value = unwrap(branch);
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const falseBinding = branchBinding(node.whenFalse);
|
|
168
|
-
return whenTrue ?? (falseBinding !== undefined && dotBindings.has(falseBinding) ? undefined : next(node.whenFalse));
|
|
149
|
+
if (guard?.onTrue === truth && ts.isIdentifier(value) && declarationsOf(value, context)[0] === guard.binding)
|
|
150
|
+
return;
|
|
151
|
+
return next(branch);
|
|
152
|
+
}
|
|
153
|
+
return branchSource(node.whenTrue, true) ?? branchSource(node.whenFalse, false);
|
|
169
154
|
}
|
|
170
155
|
function templateLeadSource(node, state, next) {
|
|
171
156
|
if (!state.prefixCut)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// secret-in-log: a credential-like value passed to a logger call.
|
|
2
2
|
import { ts } from '../../performance/engine/compiler.js';
|
|
3
|
+
import { isUnshadowedGlobal } from '../../shared/analysis/ast-semantics.js';
|
|
3
4
|
import { classifyExecutionContext } from '../../shared/analysis/execution-context.js';
|
|
4
5
|
import { unwrap, nameText, chainRootName } from './literals.js';
|
|
5
6
|
const ruleId = 'secret-in-log';
|
|
@@ -70,27 +71,62 @@ function isWholeRequest(node, checker) {
|
|
|
70
71
|
requestObjectNames.has(expression.name.text) &&
|
|
71
72
|
requestPattern.test(chainRootName(expression) ?? ''));
|
|
72
73
|
}
|
|
73
|
-
function
|
|
74
|
+
function isNullishArgument(argument, checker) {
|
|
75
|
+
if (!argument)
|
|
76
|
+
return true;
|
|
77
|
+
const value = unwrap(argument);
|
|
78
|
+
if (value.kind === ts.SyntaxKind.NullKeyword)
|
|
79
|
+
return true;
|
|
80
|
+
return ts.isIdentifier(value) && isUnshadowedGlobal(value, 'undefined', checker);
|
|
81
|
+
}
|
|
82
|
+
function collectSecrets(expression, secrets, checker, serialized = false) {
|
|
74
83
|
const node = unwrap(expression);
|
|
75
84
|
if (!node)
|
|
76
85
|
return;
|
|
86
|
+
if (ts.isCallExpression(node) && isJsonStringify(node)) {
|
|
87
|
+
const callee = unwrap(node.expression);
|
|
88
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
89
|
+
ts.isIdentifier(callee.expression) &&
|
|
90
|
+
isUnshadowedGlobal(callee.expression, 'JSON', checker) &&
|
|
91
|
+
isNullishArgument(node.arguments[1], checker)) {
|
|
92
|
+
const payload = node.arguments[0] && unwrap(node.arguments[0]);
|
|
93
|
+
if (payload && (ts.isObjectLiteralExpression(payload) || ts.isArrayLiteralExpression(payload))) {
|
|
94
|
+
collectSecrets(payload, secrets, checker, true);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
77
99
|
if (ts.isTemplateExpression(node))
|
|
78
|
-
return node.templateSpans.forEach((span) => collectSecrets(span.expression, secrets));
|
|
100
|
+
return node.templateSpans.forEach((span) => collectSecrets(span.expression, secrets, checker, serialized));
|
|
79
101
|
if (ts.isBinaryExpression(node) && joiningOperators().has(node.operatorToken.kind)) {
|
|
80
|
-
collectSecrets(node.left, secrets);
|
|
81
|
-
return collectSecrets(node.right, secrets);
|
|
102
|
+
collectSecrets(node.left, secrets, checker, serialized);
|
|
103
|
+
return collectSecrets(node.right, secrets, checker, serialized);
|
|
82
104
|
}
|
|
83
105
|
if (ts.isArrayLiteralExpression(node))
|
|
84
|
-
return node.elements.forEach((element) => collectSecrets(element, secrets));
|
|
85
|
-
if (ts.isObjectLiteralExpression(node))
|
|
86
|
-
|
|
106
|
+
return node.elements.forEach((element) => collectSecrets(element, secrets, checker, serialized));
|
|
107
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
108
|
+
// A custom toJSON decides what JSON.stringify emits, so the sibling properties may never be logged.
|
|
109
|
+
// Raw logger arguments are inspected instead, so toJSON does not protect them.
|
|
110
|
+
if (serialized && node.properties.some((property) => property.name && nameText(property.name) === 'toJSON'))
|
|
111
|
+
return;
|
|
112
|
+
// A later spread may overwrite an earlier property (`{ token, ...redacted }`), so serialized
|
|
113
|
+
// properties written before a spread are skipped rather than risk a false positive.
|
|
114
|
+
const lastSpreadIndex = serialized
|
|
115
|
+
? node.properties.findLastIndex((property) => ts.isSpreadAssignment(property))
|
|
116
|
+
: -1;
|
|
117
|
+
return node.properties.forEach((property, index) => {
|
|
118
|
+
if (index < lastSpreadIndex)
|
|
119
|
+
return;
|
|
120
|
+
collectProperty(property, secrets, checker, serialized);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
87
123
|
if (!ts.isIdentifier(node) && !ts.isPropertyAccessExpression(node) && !ts.isElementAccessExpression(node))
|
|
88
124
|
return;
|
|
89
125
|
const name = lastSegment(node) ?? '';
|
|
90
126
|
if (secretNamePattern.test(name))
|
|
91
127
|
secrets.push({ node, name });
|
|
92
128
|
}
|
|
93
|
-
function collectProperty(property, secrets) {
|
|
129
|
+
function collectProperty(property, secrets, checker, serialized) {
|
|
94
130
|
if (ts.isShorthandPropertyAssignment(property)) {
|
|
95
131
|
if (secretNamePattern.test(property.name.text))
|
|
96
132
|
secrets.push({ node: property, name: property.name.text });
|
|
@@ -102,11 +138,14 @@ function collectProperty(property, secrets) {
|
|
|
102
138
|
return;
|
|
103
139
|
const value = unwrap(property.initializer);
|
|
104
140
|
const key = nameText(property.name);
|
|
141
|
+
// JSON.stringify omits properties whose value is undefined, so nothing is logged for them.
|
|
142
|
+
if (serialized && ts.isIdentifier(value) && isUnshadowedGlobal(value, 'undefined', checker))
|
|
143
|
+
return;
|
|
105
144
|
const isReference = ts.isIdentifier(value) || ts.isPropertyAccessExpression(value) || ts.isElementAccessExpression(value);
|
|
106
145
|
if (isReference && key && secretNamePattern.test(key))
|
|
107
146
|
secrets.push({ node: property, name: key });
|
|
108
147
|
else
|
|
109
|
-
collectSecrets(value, secrets);
|
|
148
|
+
collectSecrets(value, secrets, checker, serialized);
|
|
110
149
|
}
|
|
111
150
|
function requestObjectText(argument) {
|
|
112
151
|
const expression = unwrap(argument);
|
|
@@ -153,7 +192,7 @@ function check(call, context) {
|
|
|
153
192
|
continue;
|
|
154
193
|
}
|
|
155
194
|
const secrets = [];
|
|
156
|
-
collectSecrets(argument, secrets);
|
|
195
|
+
collectSecrets(argument, secrets, context.checker);
|
|
157
196
|
for (const { node, name } of secrets) {
|
|
158
197
|
if (commandOutput && isNewCredential(node, call, context.checker))
|
|
159
198
|
continue;
|