loadwise 0.9.0 → 0.10.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 +88 -19
- package/agent/hooks/check-edited-file.mjs +146 -28
- package/dist/bin/github-action.js +3 -74
- package/dist/bin/loadwise.js +12 -12
- package/dist/lib/accessibility/aria-tables.js +10 -6
- package/dist/lib/accessibility/document-reference-rules.js +13 -3
- package/dist/lib/accessibility/jsx-elements.js +34 -16
- package/dist/lib/accessibility/native-interactive.js +51 -13
- package/dist/lib/accessibility/rules.js +4 -2
- package/dist/lib/accessibility/site-fixes/aria-typo.js +65 -0
- package/dist/lib/accessibility/site-fixes/html-language.js +45 -9
- package/dist/lib/accessibility/template-elements.js +5 -3
- package/dist/lib/editor/actions.js +15 -9
- package/dist/lib/editor/diagnostics.js +110 -107
- package/dist/lib/editor/finding-comparison.js +4 -25
- package/dist/lib/editor/hover-markdown.js +104 -0
- package/dist/lib/editor/inline-fix.js +50 -0
- package/dist/lib/editor/inline-recommendation.js +27 -0
- package/dist/lib/editor/language-server.js +94 -18
- package/dist/lib/editor/scan-session.js +2 -1
- package/dist/lib/editor/standalone-performance-examples.js +48 -0
- package/dist/lib/maintainability/analysis/analyze-source.js +2 -0
- package/dist/lib/maintainability/architecture/architecture.js +50 -2
- package/dist/lib/maintainability/cli/inputs.js +12 -5
- package/dist/lib/maintainability/cli/site-fix.js +3 -0
- package/dist/lib/maintainability/cli.js +6 -3
- package/dist/lib/maintainability/examples.js +48 -0
- package/dist/lib/maintainability/quality/modern-node-callbacks.js +188 -0
- package/dist/lib/maintainability/quality/modern-syntax.js +301 -0
- package/dist/lib/maintainability/quality/tsconfig-strict.js +307 -15
- package/dist/lib/maintainability/rule-ids.js +18 -0
- package/dist/lib/maintainability/scan-changed.js +33 -2
- package/dist/lib/maintainability/scan.js +10 -1
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +15 -6
- package/dist/lib/maintainability/unused/module-usage-signatures.js +116 -0
- package/dist/lib/maintainability/unused/module-usage.js +67 -106
- package/dist/lib/maintainability/unused/unused-bindings.js +143 -8
- package/dist/lib/maintainability/unused/unused-dependencies.js +218 -0
- package/dist/lib/maintainability/unused/unused-modules.js +4 -0
- package/dist/lib/performance/analysis/missing-packages.js +3 -0
- package/dist/lib/performance/analysis/source-text.js +2 -2
- package/dist/lib/performance/analyze.js +4 -1
- package/dist/lib/performance/async/floating-promises.js +25 -4
- package/dist/lib/performance/cli/cli-output.js +9 -3
- package/dist/lib/performance/cli/cli-policy.js +25 -2
- package/dist/lib/performance/cli/cli-scan.js +25 -24
- package/dist/lib/performance/cli/scan-outcome.js +19 -1
- package/dist/lib/performance/cli/shared-security-inputs.js +27 -0
- package/dist/lib/performance/cli/watch.js +27 -8
- package/dist/lib/performance/collections/collection-lookups.js +27 -10
- package/dist/lib/performance/collections/collection-reduce.js +32 -2
- package/dist/lib/performance/collections/collections.js +28 -0
- package/dist/lib/performance/collections/repeated.js +25 -1
- package/dist/lib/performance/contracts/cli.js +22 -11
- package/dist/lib/performance/contracts/git-files.js +46 -1
- package/dist/lib/performance/datastores/pooling.js +1 -17
- package/dist/lib/performance/datastores/postgres.js +41 -33
- package/dist/lib/performance/engine/native.js +5 -1
- package/dist/lib/performance/frontend/angular-template-calls.js +187 -0
- package/dist/lib/performance/frontend/frontend-dom-perf.js +68 -8
- package/dist/lib/performance/frontend/frontend-perf.js +115 -171
- package/dist/lib/performance/frontend/frontend-render-perf.js +7 -3
- package/dist/lib/performance/frontend/jsx-element-perf.js +171 -0
- package/dist/lib/performance/frontend/next-fetch-waterfall.js +346 -29
- package/dist/lib/performance/frontend/rxjs-subscriptions.js +155 -0
- package/dist/lib/performance/frontend/seo/client-fetched-content.js +196 -0
- package/dist/lib/performance/frontend/seo/navigation-without-link.js +116 -0
- package/dist/lib/performance/frontend/seo/next-routes.js +114 -0
- package/dist/lib/performance/frontend/seo/route-metadata.js +158 -0
- package/dist/lib/performance/frontend/seo/seo-rules.js +9 -0
- package/dist/lib/performance/frontend/seo/sitewide-noindex.js +103 -0
- package/dist/lib/performance/frontend/template-tracking.js +86 -2
- package/dist/lib/performance/frontend/vue-options-hooks.js +108 -0
- package/dist/lib/performance/orm/prisma.js +1 -25
- package/dist/lib/performance/projects.js +3 -2
- package/dist/lib/performance/query-loops/service-loops.js +38 -8
- package/dist/lib/performance/request/json-stringify-output.js +82 -4
- package/dist/lib/performance/request/promise-schedulers.js +180 -0
- package/dist/lib/performance/request/request-handlers.js +43 -4
- package/dist/lib/performance/request/unbounded-promise-all.js +13 -52
- package/dist/lib/performance/rule-ids.js +26 -2
- package/dist/lib/performance/site-fix.js +1 -0
- package/dist/lib/reliability/react-bugs.js +9 -0
- package/dist/lib/reliability/react-state-aliases.js +94 -4
- package/dist/lib/reliability/react-state.js +24 -5
- package/dist/lib/security/auth-crypto/secrets.js +121 -16
- package/dist/lib/security/cli.js +1 -1
- package/dist/lib/security/credential-entropy.js +12 -0
- package/dist/lib/security/deps/lockfiles.js +3 -1
- package/dist/lib/security/examples/files.js +110 -0
- package/dist/lib/security/examples/request-forgery.js +1 -1
- package/dist/lib/security/exposure/frontend-security.js +11 -2
- package/dist/lib/security/exposure/prototype-pollution.js +73 -4
- package/dist/lib/security/files/azure-pipelines.js +69 -0
- package/dist/lib/security/files/bitbucket-pipelines.js +32 -0
- package/dist/lib/security/files/ci-platforms.js +16 -0
- package/dist/lib/security/files/ci-shared.js +260 -0
- package/dist/lib/security/files/circleci.js +81 -0
- package/dist/lib/security/files/dockerfile.js +188 -3
- package/dist/lib/security/files/github-actions.js +490 -0
- package/dist/lib/security/files/gitlab-ci.js +90 -0
- package/dist/lib/security/files/hcl.js +264 -0
- package/dist/lib/security/files/hosting-config.js +159 -0
- package/dist/lib/security/files/http-headers.js +65 -0
- package/dist/lib/security/files/index.js +62 -7
- package/dist/lib/security/files/jenkinsfile.js +482 -0
- package/dist/lib/security/files/json-nodes.js +67 -0
- package/dist/lib/security/files/nginx.js +501 -0
- package/dist/lib/security/files/package-scripts.js +175 -0
- package/dist/lib/security/files/rule-ids.js +29 -0
- package/dist/lib/security/files/secret-values.js +31 -5
- package/dist/lib/security/files/secrets.js +44 -13
- package/dist/lib/security/files/terraform.js +345 -0
- package/dist/lib/security/injection/nosql.js +2 -1
- package/dist/lib/security/injection/sql.js +2 -2
- package/dist/lib/security/lexical-declaration.js +28 -0
- package/dist/lib/security/pg-identifier-guards.js +90 -1
- package/dist/lib/security/request-forgery/member-chain.js +41 -0
- package/dist/lib/security/request-forgery/open-redirect.js +45 -8
- package/dist/lib/security/request-forgery/shared.js +18 -3
- package/dist/lib/security/rule-descriptions.js +29 -0
- package/dist/lib/security/rule-ids.js +29 -0
- package/dist/lib/security/taint/original-string-validation.js +229 -0
- package/dist/lib/security/taint/validators.js +1 -1
- package/dist/lib/shared/agent/review-delivery.js +3 -1
- package/dist/lib/shared/analysis/ast-semantics.js +17 -1
- package/dist/lib/shared/analysis/hot-path-graph.js +55 -19
- package/dist/lib/shared/analysis/next-env-config.js +232 -0
- package/dist/lib/shared/analysis/scan-cache-git.js +114 -0
- package/dist/lib/shared/analysis/scan-cache-loadwise-packages.js +54 -0
- package/dist/lib/shared/analysis/scan-cache-project-inputs.js +221 -0
- package/dist/lib/shared/analysis/scan-cache-sources.js +235 -0
- package/dist/lib/shared/analysis/scan-cache-storage.js +24 -0
- package/dist/lib/shared/analysis/scan-cache.js +158 -174
- package/dist/lib/shared/check-watch.js +3 -3
- package/dist/lib/shared/check.js +25 -4
- package/dist/lib/shared/config.js +1 -1
- package/dist/lib/shared/conventional-projects.js +40 -0
- package/dist/lib/shared/fix/fix-command.js +2 -0
- package/dist/lib/shared/fix/fix-usage.js +47 -0
- package/dist/lib/shared/fix/project-file-access.js +43 -20
- package/dist/lib/shared/fix.js +4 -35
- package/dist/lib/shared/formats/security-cwe.js +29 -0
- package/dist/lib/shared/project-root.js +1 -1
- package/dist/lib/shared/reporting/action-comparison.js +78 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +7 -2
- package/dist/lib/shared/reporting/check-text.js +7 -5
- package/dist/lib/shared/reporting/pillars.js +28 -8
- package/dist/lib/shared/reporting/source-report.js +4 -4
- package/dist/lib/shared/system/git.js +31 -16
- package/dist/lib/shared/system/glob.js +146 -19
- package/dist/lib/shared/system/terminal-style.js +2 -2
- package/dist/lib/shared/workspaces.js +6 -1
- package/package.json +4 -1
- package/schema/loadwise.config.schema.json +86 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { ts } from '../engine/compiler.js';
|
|
2
|
+
import { importedFrom } from '../../security/bindings.js';
|
|
3
|
+
import { hasPromiseType } from '../async/awaits.js';
|
|
4
|
+
import { mayWriteLimit } from './http-options-writes.js';
|
|
5
|
+
import { isAsyncFunction } from '../../shared/analysis/ast-helpers.js';
|
|
6
|
+
import { effectiveProperty, isUnshadowedGlobal, unwrapExpression } from '../../shared/analysis/ast-semantics.js';
|
|
7
|
+
import { constSource, containsMatchingNode, identifiersByName, valueSymbol } from './phase5-ast.js';
|
|
8
|
+
function mappingCallback(input) {
|
|
9
|
+
if (!ts.isCallExpression(input) ||
|
|
10
|
+
!ts.isPropertyAccessExpression(input.expression) ||
|
|
11
|
+
input.expression.name.text !== 'map')
|
|
12
|
+
return;
|
|
13
|
+
const mapper = unwrapExpression(input.arguments[0]);
|
|
14
|
+
if (mapper && (ts.isArrowFunction(mapper) || ts.isFunctionExpression(mapper)))
|
|
15
|
+
return mapper;
|
|
16
|
+
}
|
|
17
|
+
function mappedTask(input, checker) {
|
|
18
|
+
const mapper = mappingCallback(input);
|
|
19
|
+
if (!mapper)
|
|
20
|
+
return;
|
|
21
|
+
let result;
|
|
22
|
+
if (ts.isBlock(mapper.body)) {
|
|
23
|
+
const setup = mapper.body.statements.slice(0, -1);
|
|
24
|
+
if (!setup.every((statement) => ts.isVariableStatement(statement) &&
|
|
25
|
+
!containsMatchingNode(statement, (node) => ts.isCallExpression(node) && hasPromiseType(checker.getTypeAtLocation(node), checker))))
|
|
26
|
+
return;
|
|
27
|
+
const last = mapper.body.statements.at(-1);
|
|
28
|
+
if (last && ts.isReturnStatement(last))
|
|
29
|
+
result = last.expression;
|
|
30
|
+
}
|
|
31
|
+
else
|
|
32
|
+
result = mapper.body;
|
|
33
|
+
let task = unwrapExpression(result);
|
|
34
|
+
if (task && ts.isAwaitExpression(task))
|
|
35
|
+
task = unwrapExpression(task.expression);
|
|
36
|
+
return task && ts.isCallExpression(task) ? task : undefined;
|
|
37
|
+
}
|
|
38
|
+
// A queue with default options has no concurrency cap. Keep recognizing configurable limits
|
|
39
|
+
// when their values are supplied by the project, but exclude statically unlimited settings.
|
|
40
|
+
function hasConcurrencySetting(argument, key, checker) {
|
|
41
|
+
let setting = unwrapExpression(argument);
|
|
42
|
+
if (!setting)
|
|
43
|
+
return false;
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
function resolveAlias(value) {
|
|
46
|
+
while (ts.isIdentifier(value) && !seen.has(value)) {
|
|
47
|
+
seen.add(value);
|
|
48
|
+
const source = constSource(value, checker);
|
|
49
|
+
if (!source)
|
|
50
|
+
break;
|
|
51
|
+
if (ts.isObjectLiteralExpression(source) && mayWriteLimit(source, value, [key], [key], checker))
|
|
52
|
+
break;
|
|
53
|
+
value = source;
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
setting = resolveAlias(setting);
|
|
58
|
+
if (ts.isObjectLiteralExpression(setting)) {
|
|
59
|
+
const property = effectiveProperty(setting, key);
|
|
60
|
+
if (!property)
|
|
61
|
+
return false;
|
|
62
|
+
if (property === 'unknown')
|
|
63
|
+
return true;
|
|
64
|
+
if (ts.isShorthandPropertyAssignment(property))
|
|
65
|
+
setting = resolveAlias(property.name);
|
|
66
|
+
else if (ts.isPropertyAssignment(property))
|
|
67
|
+
setting = resolveAlias(unwrapExpression(property.initializer));
|
|
68
|
+
else if (ts.isGetAccessorDeclaration(property) && property.body?.statements.length === 1) {
|
|
69
|
+
const [statement] = property.body.statements;
|
|
70
|
+
if (!ts.isReturnStatement(statement) || !statement.expression)
|
|
71
|
+
return false;
|
|
72
|
+
setting = resolveAlias(unwrapExpression(statement.expression));
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return (!isUnshadowedGlobal(setting, 'Infinity', checker) &&
|
|
78
|
+
!isUnshadowedGlobal(setting, 'undefined', checker) &&
|
|
79
|
+
setting.kind !== ts.SyntaxKind.NullKeyword &&
|
|
80
|
+
!ts.isVoidExpression(setting));
|
|
81
|
+
}
|
|
82
|
+
function schedulesLimitedWork(task, input, checker) {
|
|
83
|
+
const callee = unwrapExpression(task.expression);
|
|
84
|
+
const member = ts.isPropertyAccessExpression(callee) ? callee : undefined;
|
|
85
|
+
const receiver = member ? unwrapExpression(member.expression) : callee;
|
|
86
|
+
if (!ts.isIdentifier(receiver))
|
|
87
|
+
return false;
|
|
88
|
+
// A new limiter for every map callback cannot bound work across items.
|
|
89
|
+
const mapper = mappingCallback(input);
|
|
90
|
+
const declaration = valueSymbol(receiver, checker)?.valueDeclaration;
|
|
91
|
+
if (!mapper ||
|
|
92
|
+
!declaration ||
|
|
93
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
94
|
+
!ts.isIdentifier(declaration.name) ||
|
|
95
|
+
ts.findAncestor(declaration, (node) => node === mapper))
|
|
96
|
+
return false;
|
|
97
|
+
const origin = constSource(receiver, checker);
|
|
98
|
+
if (!origin || (!ts.isCallExpression(origin) && !ts.isNewExpression(origin)))
|
|
99
|
+
return false;
|
|
100
|
+
const binding = importedFrom(origin.expression, checker);
|
|
101
|
+
const [work] = task.arguments;
|
|
102
|
+
if (!binding || !work || (!ts.isArrowFunction(work) && !ts.isFunctionExpression(work) && !ts.isIdentifier(work)))
|
|
103
|
+
return false;
|
|
104
|
+
if (binding.exported !== 'default' && binding.exported !== '*')
|
|
105
|
+
return false;
|
|
106
|
+
const optionKey = binding.module === 'bottleneck' && member?.name.text === 'schedule'
|
|
107
|
+
? 'maxConcurrent'
|
|
108
|
+
: (binding.module === 'p-limit' && !member) || (binding.module === 'p-queue' && member?.name.text === 'add')
|
|
109
|
+
? 'concurrency'
|
|
110
|
+
: undefined;
|
|
111
|
+
if (!optionKey || !hasConcurrencySetting(origin.arguments?.[0], optionKey, checker))
|
|
112
|
+
return false;
|
|
113
|
+
return !task.arguments
|
|
114
|
+
.slice(1)
|
|
115
|
+
.some((argument) => containsMatchingNode(argument, (node) => (ts.isCallExpression(node) || ts.isNewExpression(node)) &&
|
|
116
|
+
hasPromiseType(checker.getTypeAtLocation(node), checker)));
|
|
117
|
+
}
|
|
118
|
+
function semaphoreReceiver(expression, method) {
|
|
119
|
+
if (!ts.isCallExpression(expression) || expression.arguments.length !== 0)
|
|
120
|
+
return;
|
|
121
|
+
const member = expression.expression;
|
|
122
|
+
if (ts.isPropertyAccessExpression(member) && member.name.text === method && ts.isIdentifier(member.expression))
|
|
123
|
+
return member.expression;
|
|
124
|
+
}
|
|
125
|
+
function usesSharedSemaphore(input, checker) {
|
|
126
|
+
if (!ts.isCallExpression(input) || !ts.isPropertyAccessExpression(input.expression))
|
|
127
|
+
return false;
|
|
128
|
+
const mapper = mappingCallback(input);
|
|
129
|
+
if (!mapper ||
|
|
130
|
+
!isAsyncFunction(mapper) ||
|
|
131
|
+
mapper.parameters.some((parameter) => parameter.initializer || !ts.isIdentifier(parameter.name)))
|
|
132
|
+
return false;
|
|
133
|
+
if (!ts.isBlock(mapper.body) || mapper.body.statements.length !== 1)
|
|
134
|
+
return false;
|
|
135
|
+
const [guard] = mapper.body.statements;
|
|
136
|
+
if (!ts.isTryStatement(guard) || guard.finallyBlock?.statements.length !== 1)
|
|
137
|
+
return false;
|
|
138
|
+
const [acquire] = guard.tryBlock.statements;
|
|
139
|
+
const [release] = guard.finallyBlock.statements;
|
|
140
|
+
if (!acquire || !ts.isExpressionStatement(acquire) || !ts.isAwaitExpression(acquire.expression))
|
|
141
|
+
return false;
|
|
142
|
+
if (!ts.isExpressionStatement(release))
|
|
143
|
+
return false;
|
|
144
|
+
const semaphore = semaphoreReceiver(acquire.expression.expression, 'acquire');
|
|
145
|
+
const released = semaphoreReceiver(release.expression, 'release');
|
|
146
|
+
if (!semaphore || !released)
|
|
147
|
+
return false;
|
|
148
|
+
if (!checker.isArrayType(checker.getTypeAtLocation(input.expression.expression)))
|
|
149
|
+
return false;
|
|
150
|
+
const binding = valueSymbol(semaphore, checker);
|
|
151
|
+
const declaration = binding?.valueDeclaration;
|
|
152
|
+
if (!declaration ||
|
|
153
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
154
|
+
!ts.isIdentifier(declaration.name) ||
|
|
155
|
+
ts.findAncestor(declaration, (node) => node === mapper) ||
|
|
156
|
+
valueSymbol(released, checker) !== binding)
|
|
157
|
+
return false;
|
|
158
|
+
const origin = constSource(semaphore, checker);
|
|
159
|
+
if (!origin || !ts.isNewExpression(origin))
|
|
160
|
+
return false;
|
|
161
|
+
const imported = importedFrom(origin.expression, checker);
|
|
162
|
+
if (imported?.module !== 'async-sema' || imported.exported !== 'Sema')
|
|
163
|
+
return false;
|
|
164
|
+
const capacity = unwrapExpression(origin.arguments?.[0]);
|
|
165
|
+
const limit = capacity && ts.isIdentifier(capacity) ? constSource(capacity, checker) : capacity;
|
|
166
|
+
if (!limit || !ts.isNumericLiteral(limit) || !/^[1-9][0-9_]*$/.test(limit.getText()))
|
|
167
|
+
return false;
|
|
168
|
+
// Returning or launching an unawaited promise releases the permit before its work settles.
|
|
169
|
+
if (containsMatchingNode(guard.tryBlock, (node) => (ts.isCallExpression(node) || ts.isNewExpression(node)) &&
|
|
170
|
+
!ts.isAwaitExpression(node.parent) &&
|
|
171
|
+
hasPromiseType(checker.getTypeAtLocation(node), checker)) ||
|
|
172
|
+
guard.catchClause)
|
|
173
|
+
return false;
|
|
174
|
+
// Extra uses can release another permit, replace a method, or leak the receiver outside the proof.
|
|
175
|
+
return !(identifiersByName(semaphore.getSourceFile()).get(semaphore.text) ?? []).some((reference) => reference !== semaphore &&
|
|
176
|
+
reference !== released &&
|
|
177
|
+
reference !== declaration.name &&
|
|
178
|
+
valueSymbol(reference, checker) === binding);
|
|
179
|
+
}
|
|
180
|
+
export { mappedTask, schedulesLimitedWork, usesSharedSemaphore };
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { ts } from '../engine/compiler.js';
|
|
4
4
|
import { isProvenFastifyReceiver } from './fastify-receivers.js';
|
|
5
5
|
import { isMemberCall } from '../../shared/analysis/ast-helpers.js';
|
|
6
|
+
import { bindingScopeOf } from '../../security/lexical-declaration.js';
|
|
6
7
|
const ROUTE_DECORATORS = new Set([
|
|
7
8
|
'Get',
|
|
8
9
|
'Post',
|
|
@@ -55,6 +56,17 @@ const TRPC_PROCEDURE_BASE = /procedure$/i;
|
|
|
55
56
|
// Reading these off a result takes one element or a count, so the full list never reaches the
|
|
56
57
|
// response.
|
|
57
58
|
const NARROWING_MEMBERS = new Set(['length', 'slice', 'splice', 'at', 'find', 'findIndex', 'some', 'every']);
|
|
59
|
+
// Scalar result metadata drivers return next to the rows (pg, mysql2, Sequelize's findAndCountAll).
|
|
60
|
+
const QUERY_METADATA_FIELDS = new Set([
|
|
61
|
+
'rowCount',
|
|
62
|
+
'affectedRows',
|
|
63
|
+
'changedRows',
|
|
64
|
+
'insertId',
|
|
65
|
+
'command',
|
|
66
|
+
'oid',
|
|
67
|
+
'count',
|
|
68
|
+
'fields',
|
|
69
|
+
]);
|
|
58
70
|
const SIZE_PRESERVING_METHODS = new Set(['map', 'filter', 'sort', 'toSorted', 'reverse', 'flatMap']);
|
|
59
71
|
const TRANSPARENT_KINDS = [
|
|
60
72
|
'isParenthesizedExpression',
|
|
@@ -483,16 +495,43 @@ function followsResponse(value, handler, sendsReturnValue, depth) {
|
|
|
483
495
|
continue;
|
|
484
496
|
}
|
|
485
497
|
if (ts.isVariableDeclaration(parent) && parent.initializer === current) {
|
|
486
|
-
return (
|
|
487
|
-
|
|
488
|
-
usesReachResponse(parent.name, handler, sendsReturnValue, depth));
|
|
498
|
+
return (depth < MAX_ALIAS_DEPTH &&
|
|
499
|
+
boundAliases(parent.name).some((alias) => usesReachResponse(alias, handler, sendsReturnValue, depth)));
|
|
489
500
|
}
|
|
490
501
|
return false;
|
|
491
502
|
}
|
|
492
503
|
return false;
|
|
493
504
|
}
|
|
505
|
+
// The locals that carry the value on: the name itself, or the fields of `const { rows } = result`
|
|
506
|
+
// as `result.rows` would. Array patterns pick single elements and metadata fields are scalars, so they
|
|
507
|
+
// narrow the value.
|
|
508
|
+
function boundAliases(name) {
|
|
509
|
+
if (ts.isIdentifier(name))
|
|
510
|
+
return [name];
|
|
511
|
+
if (!ts.isObjectBindingPattern(name))
|
|
512
|
+
return [];
|
|
513
|
+
return name.elements.flatMap((element) => {
|
|
514
|
+
const property = element.propertyName ?? element.name;
|
|
515
|
+
if (!ts.isIdentifier(element.name) || !ts.isIdentifier(property))
|
|
516
|
+
return [];
|
|
517
|
+
if (NARROWING_MEMBERS.has(property.text) || QUERY_METADATA_FIELDS.has(property.text))
|
|
518
|
+
return [];
|
|
519
|
+
return [element.name];
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
// A same-named local in a nested scope, a binding in another branch, or an object key names
|
|
523
|
+
// something other than the alias.
|
|
524
|
+
function namesBinding(use, scope) {
|
|
525
|
+
const parent = use.parent;
|
|
526
|
+
if (ts.isPropertyAssignment(parent) && parent.name === use)
|
|
527
|
+
return false;
|
|
528
|
+
return bindingScopeOf(use) === scope;
|
|
529
|
+
}
|
|
494
530
|
function usesReachResponse(declaredName, handler, sendsReturnValue, depth) {
|
|
495
|
-
|
|
531
|
+
const scope = bindingScopeOf(declaredName);
|
|
532
|
+
if (!scope)
|
|
533
|
+
return false;
|
|
534
|
+
return identifiersNamed(handler.body, declaredName.text, declaredName).some((use) => namesBinding(use, scope) && followsResponse(use, handler, sendsReturnValue, depth + 1));
|
|
496
535
|
}
|
|
497
536
|
const USER_SCOPED_FIELDS = new Set([
|
|
498
537
|
'userId',
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ts } from '../engine/compiler.js';
|
|
2
|
-
import { importedFrom } from '../../security/bindings.js';
|
|
3
2
|
import { hasPromiseType } from '../async/awaits.js';
|
|
4
3
|
import { declaredTypeNode, smallLiteralLimit, syncCallback } from '../collections/collection-bindings.js';
|
|
5
4
|
import { hasSmallReadonlyTuple } from '../collections/collection-bounds.js';
|
|
6
5
|
import { standardInterface } from '../collections/repeated.js';
|
|
6
|
+
import { mappedTask, schedulesLimitedWork, usesSharedSemaphore } from './promise-schedulers.js';
|
|
7
7
|
import { effectiveProperty, isUnshadowedGlobal, staticMemberName, staticPropertyName, unwrapExpression, } from '../../shared/analysis/ast-semantics.js';
|
|
8
8
|
import { MAX_SOURCE_DEPTH, constSource, containsMatchingNode, enclosingLoop, identifiersByName, isGlobalMethodCall, valueSymbol, } from './phase5-ast.js';
|
|
9
9
|
import { isLoopStatement } from '../../shared/analysis/ast-helpers.js';
|
|
@@ -15,48 +15,6 @@ function isChunkingLoop(loopNode) {
|
|
|
15
15
|
return containsMatchingNode(loopNode, (node) => (ts.isPropertyAccessExpression(node) && node.name.text === 'slice') ||
|
|
16
16
|
(ts.isIdentifier(node) && /(?:chunk|batch)/i.test(node.text)), false);
|
|
17
17
|
}
|
|
18
|
-
function mappedTask(input, checker) {
|
|
19
|
-
if (!ts.isCallExpression(input) ||
|
|
20
|
-
!ts.isPropertyAccessExpression(input.expression) ||
|
|
21
|
-
input.expression.name.text !== 'map')
|
|
22
|
-
return;
|
|
23
|
-
const mapper = unwrapExpression(input.arguments[0]);
|
|
24
|
-
if (!mapper || (!ts.isArrowFunction(mapper) && !ts.isFunctionExpression(mapper)))
|
|
25
|
-
return;
|
|
26
|
-
let result;
|
|
27
|
-
if (ts.isBlock(mapper.body)) {
|
|
28
|
-
const setup = mapper.body.statements.slice(0, -1);
|
|
29
|
-
if (!setup.every((statement) => ts.isVariableStatement(statement) &&
|
|
30
|
-
!containsMatchingNode(statement, (node) => ts.isCallExpression(node) && hasPromiseType(checker.getTypeAtLocation(node), checker))))
|
|
31
|
-
return;
|
|
32
|
-
const last = mapper.body.statements.at(-1);
|
|
33
|
-
if (last && ts.isReturnStatement(last))
|
|
34
|
-
result = last.expression;
|
|
35
|
-
}
|
|
36
|
-
else
|
|
37
|
-
result = mapper.body;
|
|
38
|
-
let task = unwrapExpression(result);
|
|
39
|
-
if (task && ts.isAwaitExpression(task))
|
|
40
|
-
task = unwrapExpression(task.expression);
|
|
41
|
-
return task && ts.isCallExpression(task) ? task : undefined;
|
|
42
|
-
}
|
|
43
|
-
function schedulesLimitedWork(task, checker) {
|
|
44
|
-
const callee = unwrapExpression(task.expression);
|
|
45
|
-
const member = ts.isPropertyAccessExpression(callee) ? callee : undefined;
|
|
46
|
-
const receiver = member ? unwrapExpression(member.expression) : callee;
|
|
47
|
-
if (!ts.isIdentifier(receiver))
|
|
48
|
-
return false;
|
|
49
|
-
const origin = constSource(receiver, checker);
|
|
50
|
-
if (!origin || (!ts.isCallExpression(origin) && !ts.isNewExpression(origin)))
|
|
51
|
-
return false;
|
|
52
|
-
const binding = importedFrom(origin.expression, checker);
|
|
53
|
-
const [work] = task.arguments;
|
|
54
|
-
if (!binding || !work || (!ts.isArrowFunction(work) && !ts.isFunctionExpression(work) && !ts.isIdentifier(work)))
|
|
55
|
-
return false;
|
|
56
|
-
return ((binding.module === 'p-limit' && !member) ||
|
|
57
|
-
(binding.module === 'p-queue' && member?.name.text === 'add') ||
|
|
58
|
-
(binding.module === 'bottleneck' && member?.name.text === 'schedule'));
|
|
59
|
-
}
|
|
60
18
|
function isUndefinedSeparator(expression, checker, depth = 0) {
|
|
61
19
|
const value = unwrapExpression(expression);
|
|
62
20
|
if (!ts.isIdentifier(value))
|
|
@@ -526,10 +484,9 @@ function awaitsLocalSettledBatches(source, checker) {
|
|
|
526
484
|
const references = (identifiersByName(helper.body).get(result.text) ?? []).filter((reference) => reference !== declaration.name && reference !== result && valueSymbol(reference, checker) === binding);
|
|
527
485
|
return references.length > 0 && references.every((reference) => pushesSettledBatch(reference, checker));
|
|
528
486
|
}
|
|
529
|
-
function isAlreadyResolvedCollection(value, checker) {
|
|
487
|
+
function isAlreadyResolvedCollection(value, source, checker) {
|
|
530
488
|
if (!ts.isIdentifier(value))
|
|
531
489
|
return false;
|
|
532
|
-
const source = singleUseConstSource(value, checker);
|
|
533
490
|
if (!source || !ts.isAwaitExpression(source))
|
|
534
491
|
return false;
|
|
535
492
|
const array = checker.getTypeAtLocation(value);
|
|
@@ -550,7 +507,8 @@ function checkUnboundedPromiseAll(call, context, cliReason) {
|
|
|
550
507
|
const arg = promiseAllInput(call.arguments[0]);
|
|
551
508
|
if (ts.isArrayLiteralExpression(arg) || hasDeclaredReadonlyTupleBound(arg, context.checker))
|
|
552
509
|
return;
|
|
553
|
-
|
|
510
|
+
const singleUseSource = ts.isIdentifier(arg) ? singleUseConstSource(arg, context.checker) : undefined;
|
|
511
|
+
if (isAlreadyResolvedCollection(arg, singleUseSource, context.checker))
|
|
554
512
|
return;
|
|
555
513
|
if (ts.isIdentifier(arg) &&
|
|
556
514
|
(isFreshLiteralAlias(arg, context.checker) ||
|
|
@@ -560,21 +518,24 @@ function checkUnboundedPromiseAll(call, context, cliReason) {
|
|
|
560
518
|
const loop = enclosingLoop(call);
|
|
561
519
|
if (loop && isChunkingLoop(loop))
|
|
562
520
|
return;
|
|
563
|
-
const
|
|
521
|
+
const schedulerInput = (!settled && singleUseSource) || arg;
|
|
522
|
+
const task = mappedTask(schedulerInput, context.checker);
|
|
564
523
|
if (settled &&
|
|
565
524
|
(!task ||
|
|
566
525
|
!hasPromiseType(context.checker.getTypeAtLocation(task), context.checker) ||
|
|
567
526
|
task.arguments.some((argument) => hasPromiseType(context.checker.getTypeAtLocation(argument), context.checker))))
|
|
568
527
|
return;
|
|
569
|
-
if (task && schedulesLimitedWork(task, context.checker))
|
|
528
|
+
if (task && schedulesLimitedWork(task, schedulerInput, context.checker))
|
|
570
529
|
return;
|
|
571
530
|
if (isDbQueryInMap(arg))
|
|
572
531
|
return;
|
|
573
532
|
if (ts.isCallExpression(arg) && !isUnboundedCollection(arg, context.checker))
|
|
574
533
|
return;
|
|
575
|
-
const
|
|
576
|
-
context.report(call, 'unbounded-promise-all',
|
|
577
|
-
? '
|
|
578
|
-
:
|
|
534
|
+
const semaphore = !settled && usesSharedSemaphore(schedulerInput, context.checker);
|
|
535
|
+
context.report(call, 'unbounded-promise-all', semaphore
|
|
536
|
+
? 'The semaphore limits concurrent work. Mapping the collection still creates one promise per item before awaiting Promise.all.'
|
|
537
|
+
: settled
|
|
538
|
+
? 'Promise.allSettled runs over an unbounded collection without a concurrency limit, risking memory spikes or downstream saturation.'
|
|
539
|
+
: 'Promise.all runs over an unbounded collection without a concurrency limit, risking memory spikes or downstream saturation.', cliReason);
|
|
579
540
|
}
|
|
580
541
|
export { checkUnboundedPromiseAll };
|
|
@@ -3,6 +3,7 @@ const sourceRules = [
|
|
|
3
3
|
'angular-default-change-detection',
|
|
4
4
|
'angular-ngfor-missing-trackby',
|
|
5
5
|
'angular-subscribe-without-cleanup',
|
|
6
|
+
'angular-template-method-call',
|
|
6
7
|
'array-repeated-find',
|
|
7
8
|
'array-repeated-membership',
|
|
8
9
|
'array-shift-in-fifo-drain',
|
|
@@ -17,6 +18,8 @@ const sourceRules = [
|
|
|
17
18
|
'external-call-in-transaction',
|
|
18
19
|
'filter-for-existence',
|
|
19
20
|
'frontend-barrel-heavy-import',
|
|
21
|
+
'frontend-navigation-without-link',
|
|
22
|
+
'frontend-render-blocking-script',
|
|
20
23
|
'graphql-n-plus-one-resolver',
|
|
21
24
|
'guard-query-per-request',
|
|
22
25
|
'heavy-media-in-request-handler',
|
|
@@ -30,7 +33,10 @@ const sourceRules = [
|
|
|
30
33
|
'loop-copy-accumulator',
|
|
31
34
|
'mongoose-hydrate-for-to-object',
|
|
32
35
|
'mongoose-missing-lean',
|
|
36
|
+
'next-client-fetched-page-content',
|
|
37
|
+
'next-missing-route-metadata',
|
|
33
38
|
'next-server-fetch-waterfall',
|
|
39
|
+
'next-sitewide-noindex',
|
|
34
40
|
'node-event-listener-leak',
|
|
35
41
|
'node-sync-compression-in-async',
|
|
36
42
|
'node-sync-crypto-in-async',
|
|
@@ -67,6 +73,7 @@ const sourceRules = [
|
|
|
67
73
|
'react-index-as-key',
|
|
68
74
|
'react-inline-object-in-jsx-prop',
|
|
69
75
|
'react-missing-cleanup',
|
|
76
|
+
'react-unthrottled-input-fetch',
|
|
70
77
|
'rebuild-lookup-in-loop',
|
|
71
78
|
'redis-command-in-loop',
|
|
72
79
|
'redis-keys-command',
|
|
@@ -85,6 +92,7 @@ const sourceRules = [
|
|
|
85
92
|
'unbounded-list-query',
|
|
86
93
|
'unbounded-module-cache',
|
|
87
94
|
'vue-v-for-missing-key',
|
|
95
|
+
'vue-v-if-with-v-for',
|
|
88
96
|
'vue-watch-missing-cleanup',
|
|
89
97
|
'whole-payload-in-memory',
|
|
90
98
|
];
|
|
@@ -143,6 +151,7 @@ const impactByRule = {
|
|
|
143
151
|
'angular-default-change-detection': 'low',
|
|
144
152
|
'angular-ngfor-missing-trackby': 'low',
|
|
145
153
|
'angular-subscribe-without-cleanup': 'medium',
|
|
154
|
+
'angular-template-method-call': 'low',
|
|
146
155
|
'array-repeated-find': 'medium',
|
|
147
156
|
'array-repeated-membership': 'medium',
|
|
148
157
|
'array-shift-in-fifo-drain': 'low',
|
|
@@ -169,6 +178,8 @@ const impactByRule = {
|
|
|
169
178
|
'external-call-in-transaction': 'high',
|
|
170
179
|
'filter-for-existence': 'low',
|
|
171
180
|
'frontend-barrel-heavy-import': 'low',
|
|
181
|
+
'frontend-navigation-without-link': 'low',
|
|
182
|
+
'frontend-render-blocking-script': 'medium',
|
|
172
183
|
'go-fixed-regexp-in-loop': 'medium',
|
|
173
184
|
'go-sql-query-in-loop': 'high',
|
|
174
185
|
'go-string-concat-in-loop': 'medium',
|
|
@@ -198,7 +209,10 @@ const impactByRule = {
|
|
|
198
209
|
'mongo-disk-sort': 'medium',
|
|
199
210
|
'mongoose-hydrate-for-to-object': 'medium',
|
|
200
211
|
'mongoose-missing-lean': 'medium',
|
|
212
|
+
'next-client-fetched-page-content': 'medium',
|
|
213
|
+
'next-missing-route-metadata': 'medium',
|
|
201
214
|
'next-server-fetch-waterfall': 'medium',
|
|
215
|
+
'next-sitewide-noindex': 'high',
|
|
202
216
|
'node-event-listener-leak': 'high',
|
|
203
217
|
'node-sync-compression-in-async': 'medium',
|
|
204
218
|
'node-sync-crypto-in-async': 'high',
|
|
@@ -245,6 +259,7 @@ const impactByRule = {
|
|
|
245
259
|
'react-index-as-key': 'low',
|
|
246
260
|
'react-inline-object-in-jsx-prop': 'low',
|
|
247
261
|
'react-missing-cleanup': 'medium',
|
|
262
|
+
'react-unthrottled-input-fetch': 'medium',
|
|
248
263
|
'rebuild-lookup-in-loop': 'high',
|
|
249
264
|
'redis-command-in-loop': 'high',
|
|
250
265
|
'redis-keys-command': 'high',
|
|
@@ -270,6 +285,7 @@ const impactByRule = {
|
|
|
270
285
|
'unbounded-list-query': 'high',
|
|
271
286
|
'unbounded-module-cache': 'medium',
|
|
272
287
|
'vue-v-for-missing-key': 'low',
|
|
288
|
+
'vue-v-if-with-v-for': 'low',
|
|
273
289
|
'vue-watch-missing-cleanup': 'medium',
|
|
274
290
|
'whole-payload-in-memory': 'medium',
|
|
275
291
|
};
|
|
@@ -281,6 +297,7 @@ const descriptionByRule = {
|
|
|
281
297
|
'angular-default-change-detection': 'An Angular component uses default change detection instead of OnPush, so its subtree is re-checked on every event.',
|
|
282
298
|
'angular-ngfor-missing-trackby': 'An *ngFor has no trackBy, so a list replaced with new objects recreates the DOM of every item.',
|
|
283
299
|
'angular-subscribe-without-cleanup': 'An RxJS subscription in a component has no completing operator or ngOnDestroy unsubscribe and keeps running after destruction.',
|
|
300
|
+
'angular-template-method-call': 'An Angular template binding calls a component method that loops over a collection, so the loop reruns on every change detection pass.',
|
|
284
301
|
'array-repeated-find': 'An array is searched with find or findIndex repeatedly where a Map lookup would do.',
|
|
285
302
|
'array-repeated-membership': 'An array is tested with includes or indexOf repeatedly where a Set would do.',
|
|
286
303
|
'array-shift-in-fifo-drain': 'A FIFO drain repeatedly removes the first array element.',
|
|
@@ -307,6 +324,8 @@ const descriptionByRule = {
|
|
|
307
324
|
'external-call-in-transaction': 'An external call is awaited inside a database transaction, holding its connection and locks.',
|
|
308
325
|
'filter-for-existence': 'filter(...).length is used only to test whether any element matches.',
|
|
309
326
|
'frontend-barrel-heavy-import': 'A client module imports the whole of lodash or moment instead of subpaths or a lighter library.',
|
|
327
|
+
'frontend-navigation-without-link': 'An onClick handler only navigates to an internal path, so the element has no href crawlers can follow.',
|
|
328
|
+
'frontend-render-blocking-script': 'A script src in the document head is not async, deferred or a module, so it blocks parsing and first paint until it downloads and runs.',
|
|
310
329
|
'go-fixed-regexp-in-loop': 'A Go regexp with a fixed pattern is compiled inside a loop.',
|
|
311
330
|
'go-sql-query-in-loop': 'A Go database query executes inside a loop instead of batching.',
|
|
312
331
|
'go-string-concat-in-loop': 'String concatenation in a Go loop allocates repeatedly; use strings.Builder.',
|
|
@@ -336,7 +355,10 @@ const descriptionByRule = {
|
|
|
336
355
|
'mongo-disk-sort': 'A MongoDB sort spilled to disk.',
|
|
337
356
|
'mongoose-hydrate-for-to-object': 'A Mongoose query hydrates full Documents only to convert them immediately via .toObject() or .toJSON().',
|
|
338
357
|
'mongoose-missing-lean': 'A Mongoose read query is returned or serialized without .lean(), paying full Document hydration overhead.',
|
|
339
|
-
'next-
|
|
358
|
+
'next-client-fetched-page-content': 'A public Next.js page fetches the content it renders inside useEffect, so the server HTML lacks it.',
|
|
359
|
+
'next-missing-route-metadata': 'A Next.js App Router page and every layout above it set no metadata title or description.',
|
|
360
|
+
'next-server-fetch-waterfall': 'A Next.js page, layout or Pages Router data function awaits independent GET requests in sequence.',
|
|
361
|
+
'next-sitewide-noindex': 'A Next.js root layout, _app or _document marks every route noindex unconditionally.',
|
|
340
362
|
'node-event-listener-leak': 'An event listener is registered inside a repeated or per-request context without matching cleanup.',
|
|
341
363
|
'node-sync-compression-in-async': 'A synchronous zlib call blocks the event loop inside an async function.',
|
|
342
364
|
'node-sync-crypto-in-async': 'A synchronous crypto call blocks the event loop inside an async function.',
|
|
@@ -381,8 +403,9 @@ const descriptionByRule = {
|
|
|
381
403
|
'react-context-value-recreated': 'An inline object or array literal in Context.Provider value creates a new reference on every render.',
|
|
382
404
|
'react-fetch-in-effect-no-abort': 'A fetch in an effect has no AbortController or cleanup, so dependency changes can race and apply stale results.',
|
|
383
405
|
'react-index-as-key': 'A list item uses the map index as its React key, which breaks reconciliation when items are inserted or reordered.',
|
|
384
|
-
'react-inline-object-in-jsx-prop': 'An inline object
|
|
406
|
+
'react-inline-object-in-jsx-prop': 'An inline object, array or function is passed to a memo() component, creating a new reference each render and defeating the memo.',
|
|
385
407
|
'react-missing-cleanup': 'An effect starts a global listener, interval, socket or endless subscription and returns no cleanup, so it leaks on every remount.',
|
|
408
|
+
'react-unthrottled-input-fetch': 'A text input change handler calls fetch directly, so every keystroke sends a request.',
|
|
386
409
|
'rebuild-lookup-in-loop': 'A Set or Map is rebuilt from the same data on every loop iteration.',
|
|
387
410
|
'redis-command-in-loop': 'A per-key Redis command runs once per loop iteration.',
|
|
388
411
|
'redis-keys-command': 'A Redis KEYS call blocks the server while it scans the keyspace.',
|
|
@@ -408,6 +431,7 @@ const descriptionByRule = {
|
|
|
408
431
|
'unbounded-list-query': 'A request handler returns query results without an apparent row limit.',
|
|
409
432
|
'unbounded-module-cache': 'A long-lived cache accepts dynamic keys without an apparent bound or eviction.',
|
|
410
433
|
'vue-v-for-missing-key': 'A list rendered with h() or v-for has no key, so Vue cannot reliably reuse matching nodes.',
|
|
434
|
+
'vue-v-if-with-v-for': 'A v-if on the same element as v-for filters by the loop variable, re-checking every item each render instead of using a computed list.',
|
|
411
435
|
'vue-watch-missing-cleanup': 'onMounted registers a global listener, timer or observer with no unmount cleanup.',
|
|
412
436
|
'whole-payload-in-memory': 'An operation loads an entire file, stream, or response into memory.',
|
|
413
437
|
};
|
|
@@ -62,6 +62,7 @@ const builderModuleLoaders = [
|
|
|
62
62
|
() => import('../accessibility/site-fixes/positive-tabindex.js'),
|
|
63
63
|
() => import('../reliability/site-fixes/pool-timeout.js'),
|
|
64
64
|
() => import('../accessibility/site-fixes/buttons.js'),
|
|
65
|
+
() => import('../accessibility/site-fixes/aria-typo.js'),
|
|
65
66
|
() => import('../accessibility/site-fixes/html-language.js'),
|
|
66
67
|
() => import('../accessibility/site-fixes/email-autocomplete.js'),
|
|
67
68
|
() => import('../accessibility/site-fixes/viewport.js'),
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { ts } from '../performance/engine/compiler.js';
|
|
5
5
|
import { walkSkippingFunctions } from '../performance/frontend/frontend-helpers.js';
|
|
6
6
|
import { staticMemberName, unwrapExpression } from '../shared/analysis/ast-semantics.js';
|
|
7
|
+
import { nextPublishedEnvKeys } from '../shared/analysis/next-env-config.js';
|
|
7
8
|
import { reactEffectCall, stableEffectCallback } from './react-effect-bindings.js';
|
|
8
9
|
import { checkStateMutations } from './react-state.js';
|
|
9
10
|
import { stateSetterCalledBy } from './react-state-setter.js';
|
|
@@ -383,12 +384,20 @@ function isGuardedForServer(node, checker) {
|
|
|
383
384
|
function checkClientEnvironment(sourceFile, file, checker, findings) {
|
|
384
385
|
if (!isClientComponent(sourceFile) || !sourceFile.text.includes('process'))
|
|
385
386
|
return;
|
|
387
|
+
let published;
|
|
386
388
|
walkNodes(sourceFile, (node) => {
|
|
387
389
|
for (const read of environmentReadsIn(node, checker)) {
|
|
388
390
|
if (read.name !== undefined && browserSafeEnvironmentName.test(read.name))
|
|
389
391
|
continue;
|
|
390
392
|
if (isGuardedForServer(read.at, checker))
|
|
391
393
|
continue;
|
|
394
|
+
if (read.name !== undefined &&
|
|
395
|
+
ts.isPropertyAccessExpression(read.at) &&
|
|
396
|
+
!read.at.questionDotToken &&
|
|
397
|
+
ts.isPropertyAccessExpression(read.at.expression) &&
|
|
398
|
+
!read.at.expression.questionDotToken &&
|
|
399
|
+
(published ??= nextPublishedEnvKeys(sourceFile.fileName)).has(read.name))
|
|
400
|
+
continue;
|
|
392
401
|
findings.push(findingAt(sourceFile, file, read.at, 'next-server-env-in-client', read.name === undefined
|
|
393
402
|
? "`process.env` is read with a computed key in a 'use client' file, and Next.js inlines only literal NEXT_PUBLIC_ names into the browser bundle, so the value is unavailable there by default. Read it on the server and pass only a non-sensitive derived value to the Client Component, never the raw value."
|
|
394
403
|
: `\`process.env.${read.name}\` is read in a 'use client' file, but Next.js only exposes variables prefixed with NEXT_PUBLIC_ to the browser by default, so it is unavailable there unless the env option in next.config.js publishes it. Read it on the server and pass only a non-sensitive derived value to the Client Component, never the raw value. Rename it with the NEXT_PUBLIC_ prefix only if it is safe to publish.`));
|
|
@@ -277,9 +277,97 @@ function isWrittenByAnotherFunction(name, checker, writersByName) {
|
|
|
277
277
|
const owners = writersOf(name, checker, writersByName).get(symbol);
|
|
278
278
|
return owners !== undefined && (owners.size > 1 || !owners.has(declaringFunction));
|
|
279
279
|
}
|
|
280
|
+
// Array lookups that hand back one of the list's own elements, so changing the element changes the state value.
|
|
281
|
+
const elementLookupMethods = new Set(['find', 'findLast', 'at']);
|
|
282
|
+
// Bounds the walk up a class hierarchy; real element classes sit a few levels below `Array`.
|
|
283
|
+
const maxBaseDepth = 8;
|
|
284
|
+
// Only a getter named by an array index, or by a computed key that may be one, can answer a numeric element read. A
|
|
285
|
+
// symbol key, such as `[Symbol.iterator]` or a declared `unique symbol`, never is.
|
|
286
|
+
function mayAnswerNumericRead(name, checker) {
|
|
287
|
+
const key = ts.isComputedPropertyName(name) ? unwrapExpression(name.expression) : name;
|
|
288
|
+
if (ts.isNumericLiteral(key) || ts.isStringLiteral(key))
|
|
289
|
+
return isArrayIndexKey(key.text);
|
|
290
|
+
if (!ts.isComputedPropertyName(name))
|
|
291
|
+
return false;
|
|
292
|
+
return !(checker.getTypeAtLocation(key).flags & ts.TypeFlags.ESSymbolLike);
|
|
293
|
+
}
|
|
294
|
+
// A static accessor belongs to the class, not to the instances whose elements are read.
|
|
295
|
+
function isInstanceGetter(member) {
|
|
296
|
+
return (ts.isGetAccessorDeclaration(member) &&
|
|
297
|
+
!member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword));
|
|
298
|
+
}
|
|
299
|
+
function declaresCopyingGetter(declaration, numericOnly, checker) {
|
|
300
|
+
const members = ts.isClassLike(declaration) || ts.isInterfaceDeclaration(declaration) || ts.isTypeLiteralNode(declaration)
|
|
301
|
+
? declaration.members
|
|
302
|
+
: ts.isObjectLiteralExpression(declaration)
|
|
303
|
+
? declaration.properties
|
|
304
|
+
: [];
|
|
305
|
+
return members.some((member) => isInstanceGetter(member) &&
|
|
306
|
+
!returnsOwnData(member) &&
|
|
307
|
+
(!numericOnly || mayAnswerNumericRead(member.name, checker)));
|
|
308
|
+
}
|
|
309
|
+
// A getter such as `get 0()` may build a fresh value on every read. Element lookups by built-in methods read through
|
|
310
|
+
// it too, and so may an index the analysis cannot resolve. The receiver's type, the members of a union or
|
|
311
|
+
// intersection, and their base classes are searched, for getters a numeric key can reach when `numericOnly` is set.
|
|
312
|
+
function declaresGetter(receiver, checker, numericOnly) {
|
|
313
|
+
const type = checker.getNonNullableType(checker.getTypeAtLocation(receiver));
|
|
314
|
+
// Only a union or intersection has members; the native engine's `types` throws on any other type.
|
|
315
|
+
const isComposite = Boolean(type.flags & (ts.TypeFlags.Union | ts.TypeFlags.Intersection));
|
|
316
|
+
const pending = (isComposite && type.types ? type.types : [type]).map((member) => ({ type: member, depth: 0 }));
|
|
317
|
+
const visited = new Set();
|
|
318
|
+
for (let next = pending.pop(); next; next = pending.pop()) {
|
|
319
|
+
if (visited.has(next.type))
|
|
320
|
+
continue;
|
|
321
|
+
visited.add(next.type);
|
|
322
|
+
const declarations = next.type.getSymbol()?.declarations ?? [];
|
|
323
|
+
if (declarations.some((declaration) => declaresCopyingGetter(declaration, numericOnly, checker)))
|
|
324
|
+
return true;
|
|
325
|
+
const isClassOrInterface = declarations.some((declaration) => ts.isClassLike(declaration) || ts.isInterfaceDeclaration(declaration));
|
|
326
|
+
if (!isClassOrInterface || next.depth + 1 >= maxBaseDepth)
|
|
327
|
+
continue;
|
|
328
|
+
for (const base of checker.getBaseTypes(next.type))
|
|
329
|
+
pending.push({ type: base, depth: next.depth + 1 });
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
function isNumericKey(key, checker) {
|
|
334
|
+
const type = checker.getNonNullableType(checker.getTypeAtLocation(key));
|
|
335
|
+
const members = type.isUnion() && type.types ? type.types : [type];
|
|
336
|
+
return members.every((member) => Boolean(member.flags & ts.TypeFlags.NumberLike));
|
|
337
|
+
}
|
|
338
|
+
// `items[i]` reads an element unless a getter may answer it.
|
|
339
|
+
function readsElement(access, checker) {
|
|
340
|
+
const read = getterRead(access, checker);
|
|
341
|
+
if (read !== undefined)
|
|
342
|
+
return !read;
|
|
343
|
+
const numericOnly = ts.isElementAccessExpression(access) && isNumericKey(access.argumentExpression, checker);
|
|
344
|
+
return !declaresGetter(access.expression, checker, numericOnly);
|
|
345
|
+
}
|
|
346
|
+
// The list `items` in `items.find(...)`, `items.at(i)` or `items[i]`: the element shares the list's state value.
|
|
347
|
+
// A getter such as `get 0()` may build a fresh value, so a read through one is not an element.
|
|
348
|
+
function elementLookupReceiver(value, checker) {
|
|
349
|
+
let receiver;
|
|
350
|
+
if (ts.isElementAccessExpression(value) &&
|
|
351
|
+
mayBeIndexedList(value.expression, checker) &&
|
|
352
|
+
readsElement(value, checker))
|
|
353
|
+
receiver = value.expression;
|
|
354
|
+
else if (ts.isCallExpression(value)) {
|
|
355
|
+
const callee = unwrapExpression(value.expression);
|
|
356
|
+
const method = isAccess(callee) ? staticMemberName(callee) : undefined;
|
|
357
|
+
if (isAccess(callee) &&
|
|
358
|
+
method !== undefined &&
|
|
359
|
+
elementLookupMethods.has(method) &&
|
|
360
|
+
mayBeIndexedList(callee.expression, checker) &&
|
|
361
|
+
isBuiltInMember(callee, indexedListOwner, checker) &&
|
|
362
|
+
!declaresGetter(callee.expression, checker, true))
|
|
363
|
+
receiver = callee.expression;
|
|
364
|
+
}
|
|
365
|
+
const root = receiver && unwrapExpression(receiver);
|
|
366
|
+
return root && ts.isIdentifier(root) ? root : undefined;
|
|
367
|
+
}
|
|
280
368
|
// `const alias = items` makes `alias` another name for the same state value, until it is assigned something else.
|
|
281
|
-
// Only a plain name
|
|
282
|
-
// a replacement.
|
|
369
|
+
// Only a plain name, or an element looked up in it (`items.find(...)`, `items[i]`), shares the value. The state
|
|
370
|
+
// binding itself stops referring to the state value when it is assigned a replacement.
|
|
283
371
|
function recordAlias(node, checker, aliases, writersByName) {
|
|
284
372
|
let name;
|
|
285
373
|
let value;
|
|
@@ -299,10 +387,12 @@ function recordAlias(node, checker, aliases, writersByName) {
|
|
|
299
387
|
if (!aliasSymbol)
|
|
300
388
|
return;
|
|
301
389
|
const named = unwrapExpression(value);
|
|
302
|
-
const copied = ts.isIdentifier(named) ? named :
|
|
390
|
+
const copied = ts.isIdentifier(named) ? named : elementLookupReceiver(named, checker);
|
|
303
391
|
const namedSymbol = copied && checker.getSymbolAtLocation(copied);
|
|
304
392
|
const held = namedSymbol && referentOf(namedSymbol, aliases);
|
|
305
|
-
const
|
|
393
|
+
const shared = held instanceof WithheldReferent ? copied && withheldValueAt(copied, held, checker) : held;
|
|
394
|
+
// `rows = rows[0]` gives the state binding one of its elements: a value inside the state, not the state itself.
|
|
395
|
+
const referent = copied !== named && shared === aliasSymbol ? undefined : shared;
|
|
306
396
|
// Giving a binding its own value back is not an alias, so writes elsewhere do not matter to it.
|
|
307
397
|
const isWithheld = referent !== undefined &&
|
|
308
398
|
referent !== uncertainReferent &&
|