loadwise 0.7.0 → 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 +23 -15
- package/dist/bin/loadwise.js +12 -24
- 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/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/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 +5 -3
- 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 +7 -2
- package/dist/lib/editor/language-server.js +64 -25
- package/dist/lib/editor/native-setup.js +3 -2
- package/dist/lib/editor/perf-watch.js +8 -6
- 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 +36 -9
- package/dist/lib/editor/setup-server.js +1 -1
- package/dist/lib/editor/setup-vim.js +6 -4
- 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 +9 -1
- package/dist/lib/maintainability/cli.js +4 -0
- 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/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/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/unused/module-usage.js +43 -21
- 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/cli-output.js +1 -0
- package/dist/lib/performance/cli/cli-scan.js +21 -2
- package/dist/lib/performance/cli.js +8 -3
- 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 +61 -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 +56 -2
- 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.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 +72 -57
- 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/mongoose-lean.js +175 -10
- package/dist/lib/performance/request/phase5-rules.js +74 -13
- 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/frontend-fixes.js +350 -10
- 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 +18 -4
- 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/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 +6 -2
- 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 +2 -1
- package/dist/lib/shared/agent/agent-verify.js +4 -1
- package/dist/lib/shared/agent/git-hook.js +17 -6
- 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 +96 -12
- 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 +5 -3
- package/dist/lib/shared/fix/fix-imports.js +4 -171
- package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
- package/dist/lib/shared/fix/fix-presentation.js +5 -3
- package/dist/lib/shared/fix/fix-safety.js +7 -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.js +31 -7
- 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 +6 -2
- package/dist/lib/shared/reporting/baseline.js +7 -5
- package/dist/lib/shared/reporting/pillars.js +9 -3
- package/dist/lib/shared/reporting/source-report.js +1 -0
- package/dist/lib/shared/system/glob.js +34 -8
- package/dist/lib/shared/system/sentry.js +26 -7
- package/dist/lib/shared/workspaces.js +15 -3
- package/package.json +5 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- 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 +554 -76
- /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
|
@@ -9,6 +9,7 @@ const bundleVersion = /^\/\*+!?\s*\*?\s*(?:@license\s+)?[\w@][\w@./-]*[ \t]+v?\d
|
|
|
9
9
|
// The whole comment webpack's minifier writes when it extracts licenses; the same words in prose are authored.
|
|
10
10
|
const bundleLicenseNotice = /^\/\*! For license information please see \S+\.LICENSE\.txt(?:\?\S*)? \*\/$/;
|
|
11
11
|
const loggerNames = new Set(['console', 'logger', 'log']);
|
|
12
|
+
const loggingMethods = new Set(['log', 'info', 'warn', 'error', 'debug', 'trace', 'fatal', 'dir', 'table']);
|
|
12
13
|
const cleanupMethods = new Set([
|
|
13
14
|
'close',
|
|
14
15
|
'destroy',
|
|
@@ -57,7 +58,11 @@ function isLoggingCall(statement) {
|
|
|
57
58
|
const call = callOf(statement);
|
|
58
59
|
if (!call)
|
|
59
60
|
return false;
|
|
60
|
-
const
|
|
61
|
+
const names = calleeNames(call);
|
|
62
|
+
const [base, owner] = names;
|
|
63
|
+
const method = names.at(-1) ?? '';
|
|
64
|
+
if (!loggingMethods.has(method))
|
|
65
|
+
return false;
|
|
61
66
|
return loggerNames.has(base) || (base === 'this' && /^_?(?:logger|log)$/i.test(owner ?? ''));
|
|
62
67
|
}
|
|
63
68
|
function isCleanupCall(statement) {
|
|
@@ -284,13 +289,18 @@ function onlySerializesTelemetry(tryBlock) {
|
|
|
284
289
|
});
|
|
285
290
|
return hasStringify && !hasOtherCall;
|
|
286
291
|
}
|
|
292
|
+
const explanatoryComment = /\b(?:error|errors|exception|exceptions|fails?|failed|failing|failure|failures|throw|throws|thrown|reject|rejects|rejected|safe|ignore|ignored|ignoring|fall back|fallback|optional|best[- ]effort|non[- ]blocking|retry|retries|swallow|swallowed|expected|benign|harmless|no[- ]?op|will recover|defaults?|missing|continu\w*|skipp\w*|lint|eslint|loadwise)\b/i;
|
|
293
|
+
function hasExplanatoryLeadingComment(sourceFile, pos) {
|
|
294
|
+
const ranges = ts.getLeadingCommentRanges(sourceFile.text, pos);
|
|
295
|
+
return Boolean(ranges?.some((range) => explanatoryComment.test(sourceFile.text.slice(range.pos, range.end))));
|
|
296
|
+
}
|
|
287
297
|
function swallowKind(sourceFile, clause) {
|
|
288
298
|
const tryStatement = clause.parent;
|
|
289
299
|
const statements = clause.block.statements;
|
|
290
300
|
if (hasComment(sourceFile, clause.block))
|
|
291
301
|
return;
|
|
292
|
-
// A comment directly above the try, such as "non-blocking", explains the catch
|
|
293
|
-
if (
|
|
302
|
+
// A comment directly above the try, such as "non-blocking", explains the catch only when it mentions error/fallback handling.
|
|
303
|
+
if (hasExplanatoryLeadingComment(sourceFile, tryStatement.pos))
|
|
294
304
|
return;
|
|
295
305
|
if (probesOptionalModule(tryStatement.tryBlock) || insideCleanupPath(tryStatement))
|
|
296
306
|
return;
|
|
@@ -341,30 +351,106 @@ function swallowedErrorApplies(sourceFile, file, isGenerated) {
|
|
|
341
351
|
!hasBundleBanner(sourceFile, file) &&
|
|
342
352
|
!(isGenerated ?? generatedSource(sourceFile, file)));
|
|
343
353
|
}
|
|
354
|
+
function isPromiseTypeNode(type) {
|
|
355
|
+
return (type !== undefined &&
|
|
356
|
+
ts.isTypeReferenceNode(type) &&
|
|
357
|
+
ts.isIdentifier(type.typeName) &&
|
|
358
|
+
(type.typeName.text === 'Promise' || type.typeName.text === 'PromiseLike'));
|
|
359
|
+
}
|
|
360
|
+
function declaresPromiseParameter(node, name) {
|
|
361
|
+
if (!ts.isFunctionLike(node))
|
|
362
|
+
return false;
|
|
363
|
+
return node.parameters.some((param) => ts.isIdentifier(param.name) && param.name.text === name && isPromiseTypeNode(param.type));
|
|
364
|
+
}
|
|
365
|
+
function declaresPromiseVariable(scope, name) {
|
|
366
|
+
if (!ts.isBlock(scope) && !ts.isSourceFile(scope))
|
|
367
|
+
return false;
|
|
368
|
+
return scope.statements.some((stmt) => ts.isVariableStatement(stmt) &&
|
|
369
|
+
stmt.declarationList.declarations.some((decl) => ts.isIdentifier(decl.name) &&
|
|
370
|
+
decl.name.text === name &&
|
|
371
|
+
(isPromiseTypeNode(decl.type) || (decl.initializer && isPromiseReceiver(decl.initializer, false)))));
|
|
372
|
+
}
|
|
373
|
+
function isPromiseTypedName(name, scopeNode) {
|
|
374
|
+
for (let current = scopeNode; current; current = current.parent) {
|
|
375
|
+
if (declaresPromiseParameter(current, name) || declaresPromiseVariable(current, name))
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
function isPromiseReceiver(expression, allowLookup = true) {
|
|
381
|
+
const receiver = withoutWrappers(expression);
|
|
382
|
+
if (ts.isNewExpression(receiver) && ts.isIdentifier(receiver.expression) && receiver.expression.text === 'Promise') {
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
if (ts.isCallExpression(receiver)) {
|
|
386
|
+
const callee = withoutWrappers(receiver.expression);
|
|
387
|
+
if (ts.isPropertyAccessExpression(callee)) {
|
|
388
|
+
const target = withoutWrappers(callee.expression);
|
|
389
|
+
if (ts.isIdentifier(target) && target.text === 'Promise')
|
|
390
|
+
return true;
|
|
391
|
+
if (['then', 'catch', 'finally'].includes(callee.name.text))
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
if (ts.isIdentifier(callee) && callee.text === 'fetch')
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
if (allowLookup && ts.isIdentifier(receiver)) {
|
|
398
|
+
return isPromiseTypedName(receiver.text, receiver);
|
|
399
|
+
}
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
344
402
|
function checkSwallowedErrors(sourceFile, file, isGenerated) {
|
|
345
403
|
if (!swallowedErrorApplies(sourceFile, file, isGenerated))
|
|
346
404
|
return [];
|
|
347
405
|
const findings = [];
|
|
348
406
|
walkNodes(sourceFile, (node) => {
|
|
349
|
-
if (
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
407
|
+
if (ts.isCatchClause(node)) {
|
|
408
|
+
const kind = swallowKind(sourceFile, node);
|
|
409
|
+
if (!kind)
|
|
410
|
+
return;
|
|
411
|
+
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
412
|
+
findings.push({
|
|
413
|
+
file,
|
|
414
|
+
line: position.line + 1,
|
|
415
|
+
column: position.character + 1,
|
|
416
|
+
ruleId: 'swallowed-error',
|
|
417
|
+
message: (kind === 'empty'
|
|
418
|
+
? 'This catch block is empty.'
|
|
419
|
+
: 'This catch block only logs the error and the code after it continues.') + advice,
|
|
420
|
+
...(kind === 'logs' &&
|
|
421
|
+
parsesInput(node.parent.tryBlock) && {
|
|
422
|
+
contextualReason: 'The try block parses input, so the logged failure may just mean no valid value was supplied.',
|
|
423
|
+
}),
|
|
424
|
+
});
|
|
353
425
|
return;
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
426
|
+
}
|
|
427
|
+
if (ts.isCallExpression(node) &&
|
|
428
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
429
|
+
node.expression.name.text === 'catch' &&
|
|
430
|
+
node.arguments.length >= 1 &&
|
|
431
|
+
isPromiseReceiver(node.expression.expression)) {
|
|
432
|
+
const callback = node.arguments[0];
|
|
433
|
+
if ((!ts.isArrowFunction(callback) && !ts.isFunctionExpression(callback)) || !ts.isBlock(callback.body))
|
|
434
|
+
return;
|
|
435
|
+
if (hasComment(sourceFile, callback.body))
|
|
436
|
+
return;
|
|
437
|
+
const statements = callback.body.statements;
|
|
438
|
+
const kind = !statements.length
|
|
439
|
+
? 'empty'
|
|
440
|
+
: statements.every(isLoggingCall) && !logExplainsFallback(statements)
|
|
441
|
+
? 'logs'
|
|
442
|
+
: undefined;
|
|
443
|
+
if (!kind)
|
|
444
|
+
return;
|
|
445
|
+
const position = sourceFile.getLineAndCharacterOfPosition(callback.getStart(sourceFile));
|
|
446
|
+
findings.push({
|
|
447
|
+
file,
|
|
448
|
+
line: position.line + 1,
|
|
449
|
+
column: position.character + 1,
|
|
450
|
+
ruleId: 'swallowed-error',
|
|
451
|
+
message: (kind === 'empty' ? 'This catch callback is empty.' : 'This catch callback only logs the error.') + advice,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
368
454
|
});
|
|
369
455
|
return findings;
|
|
370
456
|
}
|
|
@@ -43,9 +43,15 @@ function testCallback(call, checker, bindings) {
|
|
|
43
43
|
const name = runnerName(runner, checker, bindings);
|
|
44
44
|
if (!name || !testNames.has(name))
|
|
45
45
|
return;
|
|
46
|
-
const
|
|
47
|
-
if (isTestCallback(
|
|
48
|
-
return
|
|
46
|
+
const last = unwrapExpression(call.arguments.at(-1));
|
|
47
|
+
if (isTestCallback(last))
|
|
48
|
+
return last;
|
|
49
|
+
// Jest's timeout and Vitest's legacy timeout/options overloads follow the callback.
|
|
50
|
+
if (call.arguments.length !== 3)
|
|
51
|
+
return;
|
|
52
|
+
const middle = unwrapExpression(call.arguments[1]);
|
|
53
|
+
if (isTestCallback(middle))
|
|
54
|
+
return middle;
|
|
49
55
|
}
|
|
50
56
|
// A test double written as an object literal listens to nothing, whatever its name.
|
|
51
57
|
function isObjectLiteralBinding(identifier, checker) {
|
|
@@ -163,10 +169,11 @@ function usesJest(sourceFile, checker) {
|
|
|
163
169
|
found ||= node.moduleSpecifier.text === '@jest/globals';
|
|
164
170
|
if (ts.isPropertyAccessExpression(node))
|
|
165
171
|
found ||= isUnshadowedGlobal(node.expression, 'jest', checker);
|
|
172
|
+
return found;
|
|
166
173
|
});
|
|
167
174
|
return found;
|
|
168
175
|
}
|
|
169
|
-
function isIgnoredAfterEach(call, checker, bindings) {
|
|
176
|
+
function isIgnoredAfterEach(call, checker, bindings, hasJest) {
|
|
170
177
|
const callee = unwrapExpression(call.expression);
|
|
171
178
|
const name = isAccess(callee) ? staticMemberName(callee) : runnerName(callee, checker, bindings);
|
|
172
179
|
if (name !== 'afterEach')
|
|
@@ -178,12 +185,12 @@ function isIgnoredAfterEach(call, checker, bindings) {
|
|
|
178
185
|
// A member named `afterEach` on anything but a runner is a local registrar, which `jest` says nothing about.
|
|
179
186
|
if (isAccess(callee) && !testNames.has(runnerName(owner, checker, bindings) ?? ''))
|
|
180
187
|
return false;
|
|
181
|
-
return
|
|
188
|
+
return hasJest();
|
|
182
189
|
}
|
|
183
|
-
function runsOutsideTeardown(registration, checker, bindings) {
|
|
190
|
+
function runsOutsideTeardown(registration, checker, bindings, hasJest) {
|
|
184
191
|
if (!ts.isCallExpression(registration))
|
|
185
192
|
return false;
|
|
186
|
-
if (isIgnoredAfterEach(registration, checker, bindings))
|
|
193
|
+
if (isIgnoredAfterEach(registration, checker, bindings, hasJest))
|
|
187
194
|
return true;
|
|
188
195
|
const callee = unwrapExpression(registration.expression);
|
|
189
196
|
if (ts.isIdentifier(callee))
|
|
@@ -193,7 +200,7 @@ function runsOutsideTeardown(registration, checker, bindings) {
|
|
|
193
200
|
const member = staticMemberName(callee);
|
|
194
201
|
return member !== undefined && (eventMethods.has(member) || nonTeardownNames.has(member));
|
|
195
202
|
}
|
|
196
|
-
function releaseExecution(call, callback, checker, bindings) {
|
|
203
|
+
function releaseExecution(call, callback, checker, bindings, hasJest) {
|
|
197
204
|
const waitRegions = [];
|
|
198
205
|
let catchOnly = false;
|
|
199
206
|
const closures = [];
|
|
@@ -217,7 +224,8 @@ function releaseExecution(call, callback, checker, bindings) {
|
|
|
217
224
|
if (!registration)
|
|
218
225
|
return;
|
|
219
226
|
const isHook = ts.isCallExpression(registration) && isCleanupHook(registration, callback, checker, bindings);
|
|
220
|
-
if (!isHook &&
|
|
227
|
+
if (!isHook &&
|
|
228
|
+
registrations.some((candidate) => candidate && runsOutsideTeardown(candidate, checker, bindings, hasJest)))
|
|
221
229
|
return;
|
|
222
230
|
return { action: registration, waitRegions: [], protectsOnly: !isHook };
|
|
223
231
|
}
|
|
@@ -229,13 +237,77 @@ function containsOwnStatement(body, matches) {
|
|
|
229
237
|
});
|
|
230
238
|
return found;
|
|
231
239
|
}
|
|
232
|
-
|
|
233
|
-
|
|
240
|
+
function isSafeLiteralValue(expression) {
|
|
241
|
+
const inner = unwrapExpression(expression);
|
|
242
|
+
if (ts.isLiteralExpression(inner) ||
|
|
243
|
+
inner.kind === ts.SyntaxKind.TrueKeyword ||
|
|
244
|
+
inner.kind === ts.SyntaxKind.FalseKeyword ||
|
|
245
|
+
inner.kind === ts.SyntaxKind.NullKeyword ||
|
|
246
|
+
(ts.isIdentifier(inner) && inner.text === 'undefined'))
|
|
247
|
+
return true;
|
|
248
|
+
if (ts.isPrefixUnaryExpression(inner) &&
|
|
249
|
+
(inner.operator === ts.SyntaxKind.MinusToken || inner.operator === ts.SyntaxKind.PlusToken))
|
|
250
|
+
return ts.isNumericLiteral(inner.operand);
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
function isSafeLiteralHandler(argument) {
|
|
254
|
+
const fn = argument && unwrapExpression(argument);
|
|
255
|
+
if (!fn || (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)))
|
|
256
|
+
return false;
|
|
257
|
+
// Plain identifier parameters bind without evaluating anything; initializers, destructuring and rest could throw.
|
|
258
|
+
if (fn.parameters.some((parameter) => !ts.isIdentifier(parameter.name) || parameter.initializer || parameter.dotDotDotToken))
|
|
259
|
+
return false;
|
|
260
|
+
if (fn.asteriskToken || fn.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword))
|
|
261
|
+
return false;
|
|
262
|
+
if (!ts.isBlock(fn.body))
|
|
263
|
+
return isSafeLiteralValue(fn.body);
|
|
264
|
+
return fn.body.statements.every((statement) => ts.isEmptyStatement(statement) ||
|
|
265
|
+
(ts.isReturnStatement(statement) && (!statement.expression || isSafeLiteralValue(statement.expression))));
|
|
266
|
+
}
|
|
267
|
+
function isNativePromiseType(type) {
|
|
268
|
+
if (type.isUnion())
|
|
269
|
+
return Boolean(type.types?.length && type.types.every(isNativePromiseType));
|
|
270
|
+
const symbol = type.getSymbol() ?? type.aliasSymbol;
|
|
271
|
+
return (symbol?.name === 'Promise' &&
|
|
272
|
+
Boolean(symbol.declarations?.some((decl) => decl.getSourceFile().isDeclarationFile)));
|
|
273
|
+
}
|
|
274
|
+
function isSafeSettledArgument(argument, checker) {
|
|
275
|
+
const unwrapped = unwrapExpression(argument);
|
|
276
|
+
if (ts.isArrayLiteralExpression(unwrapped)) {
|
|
277
|
+
return unwrapped.elements.every((element) => {
|
|
278
|
+
if (ts.isSpreadElement(element))
|
|
279
|
+
return false;
|
|
280
|
+
const item = unwrapExpression(element);
|
|
281
|
+
return ts.isIdentifier(item) || isSafeLiteralValue(item);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
if (!ts.isIdentifier(unwrapped))
|
|
285
|
+
return false;
|
|
286
|
+
const symbolName = checker.getTypeAtLocation(unwrapped).getSymbol()?.name;
|
|
287
|
+
return symbolName === 'Array' || symbolName === 'ReadonlyArray';
|
|
288
|
+
}
|
|
289
|
+
// `await ready().catch(handler)`, `await Promise.allSettled([p1, p2])`, or a native `promise.then(ok, err)`
|
|
290
|
+
// with non-throwing literal-return handlers does not reject.
|
|
291
|
+
function catchesOwnRejection(awaited, checker) {
|
|
234
292
|
const call = unwrapExpression(awaited.expression);
|
|
235
293
|
if (!ts.isCallExpression(call))
|
|
236
294
|
return false;
|
|
237
295
|
const callee = unwrapExpression(call.expression);
|
|
238
|
-
if (!isAccess(callee)
|
|
296
|
+
if (!isAccess(callee))
|
|
297
|
+
return false;
|
|
298
|
+
const method = staticMemberName(callee);
|
|
299
|
+
if (method === 'allSettled') {
|
|
300
|
+
return (call.arguments.length === 1 &&
|
|
301
|
+
isUnshadowedGlobal(callee.expression, 'Promise', checker) &&
|
|
302
|
+
isSafeSettledArgument(call.arguments[0], checker));
|
|
303
|
+
}
|
|
304
|
+
if (method === 'then') {
|
|
305
|
+
return (call.arguments.length >= 2 &&
|
|
306
|
+
isNativePromiseType(checker.getTypeAtLocation(callee.expression)) &&
|
|
307
|
+
isSafeLiteralHandler(call.arguments[0]) &&
|
|
308
|
+
isSafeLiteralHandler(call.arguments[1]));
|
|
309
|
+
}
|
|
310
|
+
if (method !== 'catch')
|
|
239
311
|
return false;
|
|
240
312
|
const handler = unwrapExpression(call.arguments[0]);
|
|
241
313
|
if (!handler)
|
|
@@ -245,8 +317,8 @@ function catchesOwnRejection(awaited) {
|
|
|
245
317
|
return !containsOwnStatement(handler.body, ts.isThrowStatement);
|
|
246
318
|
}
|
|
247
319
|
// True when a rejection is handled and execution still reaches the release.
|
|
248
|
-
function rejectionReaches(awaited, release) {
|
|
249
|
-
if (catchesOwnRejection(awaited))
|
|
320
|
+
function rejectionReaches(awaited, release, checker) {
|
|
321
|
+
if (catchesOwnRejection(awaited, checker))
|
|
250
322
|
return true;
|
|
251
323
|
for (let child = awaited, parent = child.parent; parent; child = parent, parent = parent.parent) {
|
|
252
324
|
if (!ts.isTryStatement(parent) || parent.tryBlock !== child || !parent.catchClause)
|
|
@@ -257,7 +329,7 @@ function rejectionReaches(awaited, release) {
|
|
|
257
329
|
}
|
|
258
330
|
return false;
|
|
259
331
|
}
|
|
260
|
-
function cleanupAfterAwait(declaration, method, { callback, awaits }, checker, bindings) {
|
|
332
|
+
function cleanupAfterAwait(declaration, method, { callback, awaits }, { checker, bindings, hasJest }) {
|
|
261
333
|
const waitsAfterCreation = awaits.filter((awaited) => awaited.pos >= declaration.end);
|
|
262
334
|
if (waitsAfterCreation.length === 0)
|
|
263
335
|
return;
|
|
@@ -270,7 +342,7 @@ function cleanupAfterAwait(declaration, method, { callback, awaits }, checker, b
|
|
|
270
342
|
return;
|
|
271
343
|
continue;
|
|
272
344
|
}
|
|
273
|
-
const release = releaseExecution(call, callback, checker, bindings);
|
|
345
|
+
const release = releaseExecution(call, callback, checker, bindings, hasJest);
|
|
274
346
|
if (release)
|
|
275
347
|
releases.push(release);
|
|
276
348
|
}
|
|
@@ -278,7 +350,7 @@ function cleanupAfterAwait(declaration, method, { callback, awaits }, checker, b
|
|
|
278
350
|
const hasProtectedCleanup = releases.some(({ action, waitRegions, catchOnly }) => (!catchOnly && action.end <= awaited.end) || waitRegions.some((region) => contains(region, awaited)));
|
|
279
351
|
if (hasProtectedCleanup)
|
|
280
352
|
continue;
|
|
281
|
-
const late = releases.find(({ action, protectsOnly }) => !protectsOnly && action.getStart() >= awaited.end && !rejectionReaches(awaited, action));
|
|
353
|
+
const late = releases.find(({ action, protectsOnly }) => !protectsOnly && action.getStart() >= awaited.end && !rejectionReaches(awaited, action, checker));
|
|
282
354
|
if (late)
|
|
283
355
|
return late.action;
|
|
284
356
|
}
|
|
@@ -292,6 +364,8 @@ function checkTestCleanupAfterAwait(sourceFile, file, checker) {
|
|
|
292
364
|
const findings = [];
|
|
293
365
|
const callbacks = new Set();
|
|
294
366
|
const bindings = new Map();
|
|
367
|
+
let cachedUsesJest;
|
|
368
|
+
const hasJest = () => (cachedUsesJest ??= usesJest(sourceFile, checker));
|
|
295
369
|
// A describe callback holds tests but does not execute their bodies.
|
|
296
370
|
walkNodes(sourceFile, (container) => {
|
|
297
371
|
if (!ts.isCallExpression(container))
|
|
@@ -315,7 +389,7 @@ function checkTestCleanupAfterAwait(sourceFile, file, checker) {
|
|
|
315
389
|
const method = cleanupMethod(declaration, checker, bindings);
|
|
316
390
|
if (!method)
|
|
317
391
|
continue;
|
|
318
|
-
const cleanup = cleanupAfterAwait(declaration, method, { callback, awaits }, checker, bindings);
|
|
392
|
+
const cleanup = cleanupAfterAwait(declaration, method, { callback, awaits }, { checker, bindings, hasJest });
|
|
319
393
|
if (!cleanup)
|
|
320
394
|
continue;
|
|
321
395
|
findings.push(findingAt(sourceFile, file, cleanup, 'test-cleanup-after-await', `The test only cleans up \`${declaration.name.getText(sourceFile)}\` after an await. A rejection can leave it running. Register cleanup before awaiting or use a finally block.`));
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { ts } from '../performance/engine/compiler.js';
|
|
2
2
|
// Visits every node in source order with the selected compiler, so the native engine's trees walk the same way. An
|
|
3
|
-
// explicit stack keeps operator chains thousands deep off the call stack.
|
|
3
|
+
// explicit stack keeps operator chains thousands deep off the call stack. A visitor that returns `true` stops the walk.
|
|
4
4
|
function walkNodes(root, visit) {
|
|
5
5
|
const pending = [root];
|
|
6
6
|
for (let node = pending.pop(); node; node = pending.pop()) {
|
|
7
|
-
visit(node)
|
|
7
|
+
if (visit(node) === true)
|
|
8
|
+
return;
|
|
8
9
|
const children = [];
|
|
9
10
|
ts.forEachChild(node, (child) => {
|
|
10
11
|
children.push(child);
|
|
@@ -127,15 +127,31 @@ function isBreachRangePrefix(top, context) {
|
|
|
127
127
|
if (!declaration || !ts.isVariableDeclaration(declaration) || !ts.isIdentifier(declaration.name))
|
|
128
128
|
return false;
|
|
129
129
|
const scope = enclosingFunction(declaration) ?? context.sourceFile;
|
|
130
|
-
|
|
130
|
+
if (ts.canHaveModifiers(declaration.parent.parent) &&
|
|
131
|
+
ts.getModifiers(declaration.parent.parent)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword))
|
|
132
|
+
return false;
|
|
133
|
+
const uses = referencesOf(declaration.name, scope, context.checker);
|
|
134
|
+
let hasPrefix = false;
|
|
135
|
+
for (const reference of uses) {
|
|
136
|
+
if (ts.findAncestor(reference, ts.isTypeQueryNode))
|
|
137
|
+
continue;
|
|
131
138
|
const access = reference.parent;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
if (!ts.isPropertyAccessExpression(access) || access.expression !== reference)
|
|
140
|
+
return false;
|
|
141
|
+
const call = access.parent;
|
|
142
|
+
if (!ts.isCallExpression(call) || call.expression !== access)
|
|
143
|
+
return false;
|
|
144
|
+
if (isRangePrefixCall(call)) {
|
|
145
|
+
hasPrefix = true;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
// The range API returns suffixes for local comparison with the remainder of the digest.
|
|
149
|
+
if (!['slice', 'substring'].includes(access.name.text) ||
|
|
150
|
+
call.arguments.length !== 1 ||
|
|
151
|
+
call.arguments[0].getText() !== String(rangePrefixLength))
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
return hasPrefix;
|
|
139
155
|
}
|
|
140
156
|
function isKeyDerivationOrDigestAuth(top, context) {
|
|
141
157
|
const fn = enclosingFunction(top);
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { calleeName } from '../../performance/analysis/call-syntax.js';
|
|
4
4
|
import { ts } from '../../performance/engine/compiler.js';
|
|
5
5
|
import { isEffectivelySet, unwrapExpression } from '../../shared/analysis/ast-semantics.js';
|
|
6
|
+
import { signingAlgorithm, signingSourceHash } from './signing-algorithm.js';
|
|
6
7
|
import { importedFrom } from '../bindings.js';
|
|
7
8
|
import { fileMentions, libraryInfo, libraryFunction, resolveConstant, propertyKeyName, findProperty, hasSpread, enclosingFunction, functionName, isFunctionExpressionLike, referencesOf, } from './shared.js';
|
|
8
9
|
const nestJwt = '@nestjs/jwt';
|
|
@@ -59,13 +60,25 @@ function isDecodeCall(call, context) {
|
|
|
59
60
|
return false;
|
|
60
61
|
return (info.method ?? info.exported) === 'decode';
|
|
61
62
|
}
|
|
62
|
-
function
|
|
63
|
+
function isVerifyCall(node, context) {
|
|
64
|
+
if (verifyNames.has(calleeName(node) ?? ''))
|
|
65
|
+
return true;
|
|
66
|
+
const info = libraryInfo(node, context);
|
|
67
|
+
if (!info?.module)
|
|
68
|
+
return false;
|
|
69
|
+
if (info.module !== 'jsonwebtoken' && info.module !== '@nestjs/jwt')
|
|
70
|
+
return false;
|
|
71
|
+
const name = info.method ?? info.exported;
|
|
72
|
+
return name === 'verify' || name === 'verifyAsync';
|
|
73
|
+
}
|
|
74
|
+
function isVerifiedInside(scope, tokenText, decodeCall, context) {
|
|
63
75
|
let verified = false;
|
|
64
76
|
(function visit(node) {
|
|
65
77
|
if (verified)
|
|
66
78
|
return;
|
|
67
79
|
if (ts.isCallExpression(node) &&
|
|
68
|
-
|
|
80
|
+
node.end <= decodeCall.getStart() &&
|
|
81
|
+
isVerifyCall(node, context) &&
|
|
69
82
|
node.arguments[0]?.getText() === tokenText)
|
|
70
83
|
verified = true;
|
|
71
84
|
ts.forEachChild(node, visit);
|
|
@@ -186,7 +199,7 @@ function checkJwtDecode(call, context) {
|
|
|
186
199
|
if (!isDecodeCall(call, context))
|
|
187
200
|
return;
|
|
188
201
|
const scope = enclosingFunction(call) ?? context.sourceFile;
|
|
189
|
-
if (isVerifiedInside(scope, call.arguments[0].getText()))
|
|
202
|
+
if (isVerifiedInside(scope, call.arguments[0].getText(), call, context))
|
|
190
203
|
return;
|
|
191
204
|
if (isTokenSignedHere(call.arguments[0], context))
|
|
192
205
|
return;
|
|
@@ -308,9 +321,11 @@ function checkJwtVerify(call, context) {
|
|
|
308
321
|
isEffectivelySet(options, 'algorithms', context.checker) !== false)
|
|
309
322
|
return;
|
|
310
323
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
324
|
+
const reason = keyLooksAsymmetric(call.arguments[1], context)
|
|
325
|
+
? undefined
|
|
326
|
+
: 'The key is a shared secret, so an unpinned algorithm is less exploitable here.';
|
|
327
|
+
const algorithm = signingAlgorithm(call, context);
|
|
328
|
+
context.report(call, 'jwt-verify-without-algorithms', messages.verify, reason, algorithm ? { signingAlgorithm: algorithm, signingSourceHash: signingSourceHash(context.sourceFile) } : undefined);
|
|
314
329
|
}
|
|
315
330
|
function isNoneLiteral(node) {
|
|
316
331
|
return ts.isStringLiteralLike(node) && node.text.toLowerCase() === noneAlgorithm;
|
|
@@ -196,6 +196,46 @@ function targetNames(literal) {
|
|
|
196
196
|
names.push(lastSegment(parent.left));
|
|
197
197
|
return names.filter((name) => Boolean(name));
|
|
198
198
|
}
|
|
199
|
+
const runtimeConfigFile = /(?:^|[\\/])config(?:[\\/]|$)/i;
|
|
200
|
+
function isProcessEnvAccess(node) {
|
|
201
|
+
return ((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) &&
|
|
202
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
203
|
+
ts.isIdentifier(node.expression.expression) &&
|
|
204
|
+
node.expression.expression.text === 'process' &&
|
|
205
|
+
node.expression.name.text === 'env');
|
|
206
|
+
}
|
|
207
|
+
// The left operand must itself yield the env value; `'Bearer ' + process.env.X` is always truthy.
|
|
208
|
+
function suppliesEnvValue(node) {
|
|
209
|
+
if (ts.isParenthesizedExpression(node) || ts.isNonNullExpression(node))
|
|
210
|
+
return suppliesEnvValue(node.expression);
|
|
211
|
+
// `x || 'configured'` is always truthy, so a fallback after it never runs.
|
|
212
|
+
if (ts.isBinaryExpression(node) &&
|
|
213
|
+
node.operatorToken.kind === ts.SyntaxKind.BarBarToken &&
|
|
214
|
+
ts.isStringLiteralLike(node.right) &&
|
|
215
|
+
node.right.text !== '')
|
|
216
|
+
return false;
|
|
217
|
+
if (ts.isBinaryExpression(node) &&
|
|
218
|
+
(node.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
|
|
219
|
+
node.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken))
|
|
220
|
+
return suppliesEnvValue(node.left) || suppliesEnvValue(node.right);
|
|
221
|
+
if (ts.isCallExpression(node))
|
|
222
|
+
return suppliesEnvValue(node.expression);
|
|
223
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node))
|
|
224
|
+
return isProcessEnvAccess(node) || suppliesEnvValue(node.expression);
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
function isEnvFallback(node) {
|
|
228
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
229
|
+
if (ts.isBinaryExpression(current) &&
|
|
230
|
+
(current.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
|
|
231
|
+
current.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken)) {
|
|
232
|
+
return node.pos >= current.right.pos && node.end <= current.right.end && suppliesEnvValue(current.left);
|
|
233
|
+
}
|
|
234
|
+
if (ts.isStatement(current) || ts.isFunctionLike(current))
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
199
239
|
function checkNamedSecret(literal, context) {
|
|
200
240
|
const value = literal.text;
|
|
201
241
|
if (value.length < shortestNamedSecret || /\s/.test(value))
|
|
@@ -207,7 +247,10 @@ function checkNamedSecret(literal, context) {
|
|
|
207
247
|
return;
|
|
208
248
|
if (publicKeyShape.test(value))
|
|
209
249
|
return;
|
|
210
|
-
if (
|
|
250
|
+
if (hasStrongPlaceholder(value))
|
|
251
|
+
return;
|
|
252
|
+
const deployableFallback = runtimeConfigFile.test(context.sourceFile.fileName) || isEnvFallback(literal);
|
|
253
|
+
if (!deployableFallback && isPlaceholder(value, name))
|
|
211
254
|
return;
|
|
212
255
|
if (characterClasses(value) < 2)
|
|
213
256
|
return;
|
|
@@ -92,7 +92,9 @@ function referencesOf(identifier, scope, checker) {
|
|
|
92
92
|
if (ts.isIdentifier(node) &&
|
|
93
93
|
node !== identifier &&
|
|
94
94
|
node.text === identifier.text &&
|
|
95
|
-
|
|
95
|
+
(ts.isShorthandPropertyAssignment(node.parent)
|
|
96
|
+
? checker.getShorthandAssignmentValueSymbol(node.parent)
|
|
97
|
+
: checker.getSymbolAtLocation(node)) === symbol)
|
|
96
98
|
references.push(node);
|
|
97
99
|
ts.forEachChild(node, visit);
|
|
98
100
|
})(scope);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { hash } from 'node:crypto';
|
|
2
|
+
import { ts } from '../../performance/engine/compiler.js';
|
|
3
|
+
import { effectiveProperty } from '../../shared/analysis/ast-semantics.js';
|
|
4
|
+
import { libraryFunction } from './shared.js';
|
|
5
|
+
const defaultSigningAlgorithm = 'HS256';
|
|
6
|
+
function algorithmInSignOptions(call) {
|
|
7
|
+
const argument = call.arguments[2];
|
|
8
|
+
if (!argument)
|
|
9
|
+
return defaultSigningAlgorithm;
|
|
10
|
+
const options = ts.skipOuterExpressions(argument);
|
|
11
|
+
if (ts.isArrowFunction(options) || ts.isFunctionExpression(options))
|
|
12
|
+
return defaultSigningAlgorithm;
|
|
13
|
+
if (!ts.isObjectLiteralExpression(options) || effectiveProperty(options, 'algorithms') !== undefined)
|
|
14
|
+
return;
|
|
15
|
+
let algorithm = defaultSigningAlgorithm;
|
|
16
|
+
for (const property of options.properties) {
|
|
17
|
+
if (!ts.isPropertyAssignment(property) || ts.isComputedPropertyName(property.name))
|
|
18
|
+
return;
|
|
19
|
+
if (property.name.text === 'header')
|
|
20
|
+
return;
|
|
21
|
+
if (property.name.text !== 'algorithm')
|
|
22
|
+
continue;
|
|
23
|
+
const value = ts.skipOuterExpressions(property.initializer);
|
|
24
|
+
if (!ts.isStringLiteralLike(value) || !/^(?:HS|RS|PS|ES)(?:256|384|512)$/.test(value.text))
|
|
25
|
+
return;
|
|
26
|
+
algorithm = value.text;
|
|
27
|
+
}
|
|
28
|
+
return algorithm;
|
|
29
|
+
}
|
|
30
|
+
// Only a locally resolved constant or imported key has a stable binding we can compare across calls.
|
|
31
|
+
function keyBinding(argument, context) {
|
|
32
|
+
const key = ts.skipOuterExpressions(argument);
|
|
33
|
+
if (!ts.isIdentifier(key))
|
|
34
|
+
return;
|
|
35
|
+
const [declaration] = context.checker.getSymbolAtLocation(key)?.declarations ?? [];
|
|
36
|
+
if (!declaration)
|
|
37
|
+
return;
|
|
38
|
+
if (ts.isImportSpecifier(declaration) || ts.isImportClause(declaration))
|
|
39
|
+
return declaration;
|
|
40
|
+
if (ts.isVariableDeclaration(declaration) && declaration.parent.flags & ts.NodeFlags.Const)
|
|
41
|
+
return declaration;
|
|
42
|
+
}
|
|
43
|
+
const sourceHashes = new WeakMap();
|
|
44
|
+
export function signingSourceHash(sourceFile) {
|
|
45
|
+
const existing = sourceHashes.get(sourceFile);
|
|
46
|
+
if (existing)
|
|
47
|
+
return existing;
|
|
48
|
+
const digest = hash('sha256', sourceFile.text);
|
|
49
|
+
sourceHashes.set(sourceFile, digest);
|
|
50
|
+
return digest;
|
|
51
|
+
}
|
|
52
|
+
const algorithmsByFile = new WeakMap();
|
|
53
|
+
export function signingAlgorithm(verify, context) {
|
|
54
|
+
const key = keyBinding(verify.arguments[1], context);
|
|
55
|
+
if (!key)
|
|
56
|
+
return;
|
|
57
|
+
let byKey = algorithmsByFile.get(context.sourceFile);
|
|
58
|
+
if (!byKey) {
|
|
59
|
+
const result = new Map();
|
|
60
|
+
function visit(node) {
|
|
61
|
+
if (ts.isCallExpression(node) &&
|
|
62
|
+
node.arguments.length >= 2 &&
|
|
63
|
+
libraryFunction(node, context, ['jsonwebtoken']) === 'sign') {
|
|
64
|
+
const binding = keyBinding(node.arguments[1], context);
|
|
65
|
+
if (binding) {
|
|
66
|
+
const algorithms = result.get(binding) ?? new Set();
|
|
67
|
+
algorithms.add(algorithmInSignOptions(node));
|
|
68
|
+
result.set(binding, algorithms);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
ts.forEachChild(node, visit);
|
|
72
|
+
}
|
|
73
|
+
visit(context.sourceFile);
|
|
74
|
+
algorithmsByFile.set(context.sourceFile, result);
|
|
75
|
+
byKey = result;
|
|
76
|
+
}
|
|
77
|
+
const algorithms = byKey.get(key);
|
|
78
|
+
return algorithms?.size === 1 ? [...algorithms][0] : undefined;
|
|
79
|
+
}
|