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,482 @@
|
|
|
1
|
+
const jenkinsfileName = /^(?:Jenkinsfile(?:\..+)?|.+\.jenkinsfile)$/i;
|
|
2
|
+
const shellSteps = new Set(['sh', 'bat', 'powershell', 'pwsh']);
|
|
3
|
+
// Environment variables whose value the author of a branch, tag or pull request chooses.
|
|
4
|
+
const untrustedEnvironment = new Set([
|
|
5
|
+
'BRANCH_NAME',
|
|
6
|
+
'CHANGE_AUTHOR',
|
|
7
|
+
'CHANGE_AUTHOR_DISPLAY_NAME',
|
|
8
|
+
'CHANGE_AUTHOR_EMAIL',
|
|
9
|
+
'CHANGE_BRANCH',
|
|
10
|
+
'CHANGE_FORK',
|
|
11
|
+
'CHANGE_TITLE',
|
|
12
|
+
'GIT_BRANCH',
|
|
13
|
+
'TAG_NAME',
|
|
14
|
+
]);
|
|
15
|
+
// Binding arguments that name a variable holding a secret. Usernames, key file paths and access key
|
|
16
|
+
// ids are not secret values.
|
|
17
|
+
const secretVariableKeys = new Set(['variable', 'passwordVariable', 'passphraseVariable', 'secretKeyVariable']);
|
|
18
|
+
// Bindings whose `variable` holds a file path, not the secret itself.
|
|
19
|
+
const pathBindings = new Set(['file', 'zip']);
|
|
20
|
+
const environmentReference = /(?<![\w.$])(?:env\s*\.\s*|env\s*\[\s*['"])?([A-Za-z_]\w*)/g;
|
|
21
|
+
const parameterReference = /(?<![\w.$])params\s*(?:\??\.\s*|\[\s*['"])([A-Za-z_]\w*)/g;
|
|
22
|
+
// An expression that compares or converts the value yields a fixed word or a number. Top-level
|
|
23
|
+
// ternaries are split into their results first, so `?` only matches one nested in brackets.
|
|
24
|
+
const derivedValue = /==|!=|=~|\?(?![.:])|\.(?:toInteger|toBoolean|size|length|isEmpty)\s*\(|\binstanceof\b/;
|
|
25
|
+
// A ternary nested in brackets can still return the credential, so only comparisons and size or type checks hide it.
|
|
26
|
+
const derivedFromCredential = /==|!=|=~|\.(?:toInteger|toBoolean|size|length|isEmpty)\s*\(|\binstanceof\b/;
|
|
27
|
+
// Boolean and choice parameters can only take values the pipeline declares.
|
|
28
|
+
const constrainedParameter = /\b(?:booleanParam|choice)\s*\(([^()]*)\)/g;
|
|
29
|
+
const parameterName = /\bname\s*:\s*['"]([\w.-]+)['"]/;
|
|
30
|
+
const maxNesting = 16;
|
|
31
|
+
// Bounds on per-step work, far above real pipelines, so crafted nesting cannot make the scan quadratic.
|
|
32
|
+
const maxArgumentTokens = 2000;
|
|
33
|
+
const maxScopeDepth = 64;
|
|
34
|
+
function isJenkinsfile(file) {
|
|
35
|
+
return jenkinsfileName.test(file.split(/[\\/]/).at(-1) ?? '');
|
|
36
|
+
}
|
|
37
|
+
// Index after a single-quoted string that starts at start; it ends at a line break when unterminated.
|
|
38
|
+
function singleQuotedEnd(text, start) {
|
|
39
|
+
for (let index = start + 1; index < text.length; index++) {
|
|
40
|
+
if (text[index] === '\\')
|
|
41
|
+
index++;
|
|
42
|
+
else if (text[index] === "'" || text[index] === '\n')
|
|
43
|
+
return index + 1;
|
|
44
|
+
}
|
|
45
|
+
return text.length;
|
|
46
|
+
}
|
|
47
|
+
// Index after a `${ ... }` expression, honouring nested braces and strings.
|
|
48
|
+
function interpolationEnd(text, start, nesting) {
|
|
49
|
+
let braces = 1;
|
|
50
|
+
let index = start;
|
|
51
|
+
while (index < text.length) {
|
|
52
|
+
const char = text[index];
|
|
53
|
+
if (char === "'") {
|
|
54
|
+
index = singleQuotedEnd(text, index);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (char === '"' && nesting < maxNesting) {
|
|
58
|
+
index = readGString(text, index, nesting + 1).end;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (char === '{')
|
|
62
|
+
braces++;
|
|
63
|
+
else if (char === '}' && --braces === 0)
|
|
64
|
+
return index + 1;
|
|
65
|
+
index++;
|
|
66
|
+
}
|
|
67
|
+
return index;
|
|
68
|
+
}
|
|
69
|
+
// Reads a double-quoted or triple-double-quoted GString and its interpolations.
|
|
70
|
+
function readGString(text, start, nesting) {
|
|
71
|
+
const triple = text.startsWith('"""', start);
|
|
72
|
+
const interpolations = [];
|
|
73
|
+
let index = start + (triple ? 3 : 1);
|
|
74
|
+
while (index < text.length) {
|
|
75
|
+
const char = text[index];
|
|
76
|
+
if (triple ? text.startsWith('"""', index) : char === '"') {
|
|
77
|
+
return { end: index + (triple ? 3 : 1), value: text.slice(start, index), interpolations };
|
|
78
|
+
}
|
|
79
|
+
if (!triple && char === '\n')
|
|
80
|
+
break;
|
|
81
|
+
if (char === '\\') {
|
|
82
|
+
index += 2;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (char === '$' && text[index + 1] === '{') {
|
|
86
|
+
const end = interpolationEnd(text, index + 2, nesting);
|
|
87
|
+
interpolations.push({ expression: text.slice(index + 2, end - 1), offset: index });
|
|
88
|
+
index = end;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const shortForm = char === '$' ? /[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*/y : undefined;
|
|
92
|
+
if (shortForm) {
|
|
93
|
+
shortForm.lastIndex = index + 1;
|
|
94
|
+
const match = shortForm.exec(text);
|
|
95
|
+
if (match) {
|
|
96
|
+
interpolations.push({ expression: match[0], offset: index });
|
|
97
|
+
index += 1 + match[0].length;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
index++;
|
|
102
|
+
}
|
|
103
|
+
return { end: index, value: text.slice(start, index), interpolations };
|
|
104
|
+
}
|
|
105
|
+
// Index after whitespace or a comment at index, or undefined when neither starts there.
|
|
106
|
+
function triviaEnd(text, index) {
|
|
107
|
+
const char = text[index];
|
|
108
|
+
if (char === ' ' || char === '\t' || char === '\r')
|
|
109
|
+
return index + 1;
|
|
110
|
+
if (char !== '/')
|
|
111
|
+
return undefined;
|
|
112
|
+
if (text[index + 1] === '/') {
|
|
113
|
+
const end = text.indexOf('\n', index);
|
|
114
|
+
return end === -1 ? text.length : end;
|
|
115
|
+
}
|
|
116
|
+
if (text[index + 1] !== '*')
|
|
117
|
+
return undefined;
|
|
118
|
+
const end = text.indexOf('*/', index + 2);
|
|
119
|
+
return end === -1 ? text.length : end + 2;
|
|
120
|
+
}
|
|
121
|
+
// A triple-quoted string, a single-quoted string or an interpolating GString starting at start.
|
|
122
|
+
function stringToken(text, start) {
|
|
123
|
+
if (text.startsWith("'''", start)) {
|
|
124
|
+
const close = text.indexOf("'''", start + 3);
|
|
125
|
+
const end = close === -1 ? text.length : close + 3;
|
|
126
|
+
return { end, token: { kind: 'string', text: text.slice(start + 3, end - 3), offset: start } };
|
|
127
|
+
}
|
|
128
|
+
if (text[start] === "'") {
|
|
129
|
+
const end = singleQuotedEnd(text, start);
|
|
130
|
+
return { end, token: { kind: 'string', text: text.slice(start + 1, end - 1), offset: start } };
|
|
131
|
+
}
|
|
132
|
+
if (text[start] !== '"')
|
|
133
|
+
return undefined;
|
|
134
|
+
const { end, value, interpolations } = readGString(text, start, 0);
|
|
135
|
+
return { end, token: { kind: 'string', text: value, offset: start, interpolations } };
|
|
136
|
+
}
|
|
137
|
+
function wordKind(char) {
|
|
138
|
+
if (/[A-Za-z_$]/.test(char))
|
|
139
|
+
return 'ident';
|
|
140
|
+
return char >= '0' && char <= '9' ? 'number' : 'punct';
|
|
141
|
+
}
|
|
142
|
+
// An identifier, a number or one punctuation character starting at start.
|
|
143
|
+
function wordToken(text, start) {
|
|
144
|
+
const kind = wordKind(text[start]);
|
|
145
|
+
if (kind === 'punct')
|
|
146
|
+
return { end: start + 1, token: { kind, text: text[start], offset: start } };
|
|
147
|
+
const part = kind === 'ident' ? /[\w$]/ : /[\w.]/;
|
|
148
|
+
let end = start;
|
|
149
|
+
while (end < text.length && part.test(text[end]))
|
|
150
|
+
end++;
|
|
151
|
+
return { end, token: { kind, text: text.slice(start, end), offset: start } };
|
|
152
|
+
}
|
|
153
|
+
function tokenize(text) {
|
|
154
|
+
const tokens = [];
|
|
155
|
+
let index = text.startsWith('#!') ? Math.max(text.indexOf('\n'), 0) : 0;
|
|
156
|
+
while (index < text.length) {
|
|
157
|
+
if (text[index] === '\n') {
|
|
158
|
+
tokens.push({ kind: 'newline', text: '\n', offset: index++ });
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const skipped = triviaEnd(text, index);
|
|
162
|
+
if (skipped !== undefined) {
|
|
163
|
+
index = skipped;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const { end, token } = stringToken(text, index) ?? wordToken(text, index);
|
|
167
|
+
tokens.push(token);
|
|
168
|
+
index = end;
|
|
169
|
+
}
|
|
170
|
+
return tokens;
|
|
171
|
+
}
|
|
172
|
+
const openers = { '(': ')', '[': ']', '{': '}' };
|
|
173
|
+
function structureOf(tokens) {
|
|
174
|
+
const closing = new Map();
|
|
175
|
+
const enclosingBrace = [];
|
|
176
|
+
const stack = [];
|
|
177
|
+
const braces = [];
|
|
178
|
+
tokens.forEach((token, index) => {
|
|
179
|
+
enclosingBrace.push(braces.at(-1) ?? -1);
|
|
180
|
+
if (token.kind !== 'punct')
|
|
181
|
+
return;
|
|
182
|
+
if (Object.hasOwn(openers, token.text)) {
|
|
183
|
+
stack.push(index);
|
|
184
|
+
if (token.text === '{')
|
|
185
|
+
braces.push(index);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const open = stack.at(-1);
|
|
189
|
+
if (open === undefined || openers[tokens[open].text] !== token.text)
|
|
190
|
+
return;
|
|
191
|
+
stack.pop();
|
|
192
|
+
if (token.text === '}')
|
|
193
|
+
braces.pop();
|
|
194
|
+
closing.set(open, index);
|
|
195
|
+
});
|
|
196
|
+
return { closing, enclosingBrace };
|
|
197
|
+
}
|
|
198
|
+
function isPunct(token, text) {
|
|
199
|
+
return token?.kind === 'punct' && token.text === text;
|
|
200
|
+
}
|
|
201
|
+
function nextSignificant(tokens, start) {
|
|
202
|
+
let index = start;
|
|
203
|
+
while (tokens[index]?.kind === 'newline')
|
|
204
|
+
index++;
|
|
205
|
+
return index;
|
|
206
|
+
}
|
|
207
|
+
// The secret variable names of a withCredentials([...]) { } call at index, bound to its body.
|
|
208
|
+
function bindWithCredentials(tokens, index, closing, bind) {
|
|
209
|
+
const argumentsEnd = closing.get(index + 1);
|
|
210
|
+
if (argumentsEnd === undefined || argumentsEnd - index > maxArgumentTokens)
|
|
211
|
+
return;
|
|
212
|
+
const body = nextSignificant(tokens, argumentsEnd + 1);
|
|
213
|
+
if (!isPunct(tokens[body], '{') || !closing.has(body))
|
|
214
|
+
return;
|
|
215
|
+
let binding = '';
|
|
216
|
+
for (let inner = index + 2; inner < argumentsEnd; inner++) {
|
|
217
|
+
const current = tokens[inner];
|
|
218
|
+
if (current.kind === 'ident' && isPunct(tokens[inner + 1], '('))
|
|
219
|
+
binding = current.text;
|
|
220
|
+
else if (isPunct(current, '['))
|
|
221
|
+
binding = '';
|
|
222
|
+
const value = tokens[inner + 2];
|
|
223
|
+
if (current.kind !== 'ident' || !isPunct(tokens[inner + 1], ':') || value?.kind !== 'string')
|
|
224
|
+
continue;
|
|
225
|
+
const isPathBinding = current.text === 'variable' && pathBindings.has(binding);
|
|
226
|
+
if (secretVariableKeys.has(current.text) && !isPathBinding && !value.interpolations?.length)
|
|
227
|
+
bind(body, value.text.replace(/^"/, ''));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// `NAME = credentials('id')` entries of the environment { } block opening at open, bound to brace,
|
|
231
|
+
// the block that holds the environment directive.
|
|
232
|
+
function bindEnvironmentCredentials(tokens, open, brace, closing, bind) {
|
|
233
|
+
const end = closing.get(open);
|
|
234
|
+
if (end === undefined)
|
|
235
|
+
return;
|
|
236
|
+
for (let inner = open + 1; inner < end; inner++) {
|
|
237
|
+
const current = tokens[inner];
|
|
238
|
+
// A nested block belongs to its own scan, so every token is read once.
|
|
239
|
+
if (current.kind === 'punct' && closing.has(inner)) {
|
|
240
|
+
inner = closing.get(inner) ?? inner;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const isCredentialCall = current.kind === 'ident' &&
|
|
244
|
+
isPunct(tokens[inner + 1], '=') &&
|
|
245
|
+
tokens[inner + 2]?.text === 'credentials' &&
|
|
246
|
+
isPunct(tokens[inner + 3], '(');
|
|
247
|
+
if (!isCredentialCall)
|
|
248
|
+
continue;
|
|
249
|
+
bind(brace, current.text);
|
|
250
|
+
bind(brace, current.text + '_PSW');
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// Names bound by withCredentials([...]) { } and by `NAME = credentials('id')` in an environment
|
|
254
|
+
// block, keyed by the index of the brace whose block can read them (-1 for the whole file).
|
|
255
|
+
function credentialScopes(tokens, { closing, enclosingBrace }) {
|
|
256
|
+
const scopes = new Map();
|
|
257
|
+
const bind = (brace, name) => {
|
|
258
|
+
const names = scopes.get(brace) ?? new Set();
|
|
259
|
+
names.add(name);
|
|
260
|
+
scopes.set(brace, names);
|
|
261
|
+
};
|
|
262
|
+
tokens.forEach((token, index) => {
|
|
263
|
+
if (token.kind !== 'ident')
|
|
264
|
+
return;
|
|
265
|
+
if (token.text === 'withCredentials' && isPunct(tokens[index + 1], '(')) {
|
|
266
|
+
bindWithCredentials(tokens, index, closing, bind);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const open = nextSignificant(tokens, index + 1);
|
|
270
|
+
if (token.text === 'environment' && isPunct(tokens[open], '{'))
|
|
271
|
+
bindEnvironmentCredentials(tokens, open, enclosingBrace[index], closing, bind);
|
|
272
|
+
});
|
|
273
|
+
return scopes;
|
|
274
|
+
}
|
|
275
|
+
// Credential names visible at a token: the bindings of its enclosing blocks, nearest first.
|
|
276
|
+
function visibleCredentials(index, scopes, { enclosingBrace }) {
|
|
277
|
+
const visible = new Set(scopes.get(-1));
|
|
278
|
+
let brace = enclosingBrace[index];
|
|
279
|
+
for (let level = 0; brace !== -1 && level < maxScopeDepth; level++) {
|
|
280
|
+
for (const name of scopes.get(brace) ?? [])
|
|
281
|
+
visible.add(name);
|
|
282
|
+
brace = enclosingBrace[brace];
|
|
283
|
+
}
|
|
284
|
+
return visible;
|
|
285
|
+
}
|
|
286
|
+
// The string tokens a shell step runs: positional arguments and `script:`, not `label:` or others.
|
|
287
|
+
function scriptStrings(tokens, step, closing) {
|
|
288
|
+
const parenthesized = isPunct(tokens[step + 1], '(');
|
|
289
|
+
const start = step + (parenthesized ? 2 : 1);
|
|
290
|
+
const strings = [];
|
|
291
|
+
let depth = 0;
|
|
292
|
+
let key;
|
|
293
|
+
let argumentStart = true;
|
|
294
|
+
const end = Math.min(parenthesized ? (closing.get(step + 1) ?? tokens.length) : tokens.length, start + maxArgumentTokens);
|
|
295
|
+
for (let index = start; index < end; index++) {
|
|
296
|
+
const token = tokens[index];
|
|
297
|
+
if (!parenthesized && depth === 0 && (token.kind === 'newline' || isPunct(token, ';') || isPunct(token, '}')))
|
|
298
|
+
break;
|
|
299
|
+
if (token.kind === 'newline')
|
|
300
|
+
continue;
|
|
301
|
+
if (token.kind === 'punct' && Object.hasOwn(openers, token.text))
|
|
302
|
+
depth++;
|
|
303
|
+
else if (token.kind === 'punct' && [')', ']', '}'].includes(token.text))
|
|
304
|
+
depth--;
|
|
305
|
+
if (depth === 0 && isPunct(token, ',')) {
|
|
306
|
+
key = undefined;
|
|
307
|
+
argumentStart = true;
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (argumentStart && depth === 0 && token.kind === 'ident' && isPunct(tokens[index + 1], ':')) {
|
|
311
|
+
key = token.text;
|
|
312
|
+
index++;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
argumentStart = false;
|
|
316
|
+
if (token.kind === 'string' && (key === undefined || key === 'script'))
|
|
317
|
+
strings.push(token);
|
|
318
|
+
}
|
|
319
|
+
return strings;
|
|
320
|
+
}
|
|
321
|
+
function isShellStep(tokens, index) {
|
|
322
|
+
const token = tokens[index];
|
|
323
|
+
if (token.kind !== 'ident' || !shellSteps.has(token.text))
|
|
324
|
+
return false;
|
|
325
|
+
const previous = tokens[index - 1];
|
|
326
|
+
if (isPunct(previous, '.') || previous?.text === 'def')
|
|
327
|
+
return false;
|
|
328
|
+
const next = tokens[index + 1];
|
|
329
|
+
return isPunct(next, '(') || next?.kind === 'string' || (next?.kind === 'ident' && isPunct(tokens[index + 2], ':'));
|
|
330
|
+
}
|
|
331
|
+
function constrainedParameters(text) {
|
|
332
|
+
const names = new Set();
|
|
333
|
+
for (const [, body] of text.matchAll(constrainedParameter)) {
|
|
334
|
+
const name = parameterName.exec(body)?.[1];
|
|
335
|
+
if (name)
|
|
336
|
+
names.add(name);
|
|
337
|
+
}
|
|
338
|
+
return names;
|
|
339
|
+
}
|
|
340
|
+
// Index after the quoted string that starts at start, honouring backslash escapes.
|
|
341
|
+
function quotedEnd(expression, start) {
|
|
342
|
+
for (let index = start + 1; index < expression.length; index++) {
|
|
343
|
+
if (expression[index] === '\\')
|
|
344
|
+
index++;
|
|
345
|
+
else if (expression[index] === expression[start])
|
|
346
|
+
return index + 1;
|
|
347
|
+
}
|
|
348
|
+
return expression.length;
|
|
349
|
+
}
|
|
350
|
+
// The two results of a top-level `condition ? first : second`, or undefined when there is none.
|
|
351
|
+
// Strings and brackets are skipped, and `?.`, `?[` and the Elvis `?:` are not ternaries.
|
|
352
|
+
function ternaryBranches(expression) {
|
|
353
|
+
let depth = 0;
|
|
354
|
+
let question = -1;
|
|
355
|
+
let open = 0;
|
|
356
|
+
for (let index = 0; index < expression.length; index++) {
|
|
357
|
+
const char = expression[index];
|
|
358
|
+
if (char === "'" || char === '"')
|
|
359
|
+
index = quotedEnd(expression, index) - 1;
|
|
360
|
+
else if ('([{'.includes(char))
|
|
361
|
+
depth++;
|
|
362
|
+
else if (')]}'.includes(char))
|
|
363
|
+
depth--;
|
|
364
|
+
else if (depth !== 0)
|
|
365
|
+
continue;
|
|
366
|
+
else if (char === '?' && '.[:'.includes(expression[index + 1] ?? '.'))
|
|
367
|
+
index++;
|
|
368
|
+
else if (char === '?') {
|
|
369
|
+
if (question < 0)
|
|
370
|
+
question = index;
|
|
371
|
+
open++;
|
|
372
|
+
}
|
|
373
|
+
else if (char === ':' && question >= 0 && --open === 0)
|
|
374
|
+
return [expression.slice(question + 1, index), expression.slice(index + 1)];
|
|
375
|
+
}
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
// The expression without parentheses around all of it, as in `(a ? b : c)`.
|
|
379
|
+
function withoutOuterParentheses(expression) {
|
|
380
|
+
const trimmed = expression.trim();
|
|
381
|
+
if (!trimmed.startsWith('('))
|
|
382
|
+
return trimmed;
|
|
383
|
+
let depth = 0;
|
|
384
|
+
for (let index = 0; index < trimmed.length; index++) {
|
|
385
|
+
const char = trimmed[index];
|
|
386
|
+
if (char === "'" || char === '"')
|
|
387
|
+
index = quotedEnd(trimmed, index) - 1;
|
|
388
|
+
else if (char === '(')
|
|
389
|
+
depth++;
|
|
390
|
+
else if (char === ')' && --depth === 0)
|
|
391
|
+
return index === trimmed.length - 1 ? trimmed.slice(1, -1) : trimmed;
|
|
392
|
+
}
|
|
393
|
+
return trimmed;
|
|
394
|
+
}
|
|
395
|
+
// The expressions whose value an interpolation can produce: each result of a ternary, but not its
|
|
396
|
+
// condition, which only picks one of them. Splitting stops after maxNesting results.
|
|
397
|
+
function resultExpressions(expression) {
|
|
398
|
+
const results = [];
|
|
399
|
+
const pending = [expression];
|
|
400
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
401
|
+
const branches = results.length + pending.length < maxNesting ? ternaryBranches(withoutOuterParentheses(next)) : undefined;
|
|
402
|
+
if (branches)
|
|
403
|
+
pending.push(branches[1], branches[0]);
|
|
404
|
+
else
|
|
405
|
+
results.push(next);
|
|
406
|
+
}
|
|
407
|
+
return results;
|
|
408
|
+
}
|
|
409
|
+
function untrustedReference(expression, constrained) {
|
|
410
|
+
if (derivedValue.test(expression))
|
|
411
|
+
return undefined;
|
|
412
|
+
for (const [, name] of expression.matchAll(parameterReference)) {
|
|
413
|
+
if (!constrained.has(name))
|
|
414
|
+
return `params.${name}`;
|
|
415
|
+
}
|
|
416
|
+
for (const [whole, name] of expression.matchAll(environmentReference)) {
|
|
417
|
+
if (untrustedEnvironment.has(name))
|
|
418
|
+
return whole.startsWith('env') ? `env.${name}` : name;
|
|
419
|
+
}
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
function boundCredential(expression, names) {
|
|
423
|
+
for (const [, name] of expression.matchAll(environmentReference))
|
|
424
|
+
if (names.has(name))
|
|
425
|
+
return name;
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
function positionOf(lineStarts, offset) {
|
|
429
|
+
let low = 0;
|
|
430
|
+
let high = lineStarts.length - 1;
|
|
431
|
+
while (low < high) {
|
|
432
|
+
const middle = (low + high + 1) >> 1;
|
|
433
|
+
if (lineStarts[middle] <= offset)
|
|
434
|
+
low = middle;
|
|
435
|
+
else
|
|
436
|
+
high = middle - 1;
|
|
437
|
+
}
|
|
438
|
+
return { line: low + 1, column: offset - lineStarts[low] + 1 };
|
|
439
|
+
}
|
|
440
|
+
function checkJenkinsfile(text) {
|
|
441
|
+
const tokens = tokenize(text);
|
|
442
|
+
const structure = structureOf(tokens);
|
|
443
|
+
const scopes = credentialScopes(tokens, structure);
|
|
444
|
+
const constrained = constrainedParameters(text);
|
|
445
|
+
const lineStarts = [0];
|
|
446
|
+
for (let index = text.indexOf('\n'); index !== -1; index = text.indexOf('\n', index + 1))
|
|
447
|
+
lineStarts.push(index + 1);
|
|
448
|
+
const findings = [];
|
|
449
|
+
tokens.forEach((token, index) => {
|
|
450
|
+
if (!isShellStep(tokens, index))
|
|
451
|
+
return;
|
|
452
|
+
const visible = visibleCredentials(index, scopes, structure);
|
|
453
|
+
for (const script of scriptStrings(tokens, index, structure.closing)) {
|
|
454
|
+
for (const { expression, offset } of script.interpolations ?? []) {
|
|
455
|
+
const position = positionOf(lineStarts, offset);
|
|
456
|
+
const results = resultExpressions(expression);
|
|
457
|
+
// A derived value such as ${TOKEN.length()} interpolates a number, not the secret.
|
|
458
|
+
const secret = results
|
|
459
|
+
.map((result) => (derivedFromCredential.test(result) ? undefined : boundCredential(result, visible)))
|
|
460
|
+
.find(Boolean);
|
|
461
|
+
if (secret) {
|
|
462
|
+
findings.push({
|
|
463
|
+
ruleId: 'jenkinsfile-credential-interpolation',
|
|
464
|
+
...position,
|
|
465
|
+
message: `The ${token.text} step interpolates the credential ${secret} with Groovy string interpolation, which puts the secret on the process command line and in the step arguments. Use a single-quoted script and let the shell read $${secret}.`,
|
|
466
|
+
});
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const untrusted = results.map((result) => untrustedReference(result, constrained)).find(Boolean);
|
|
470
|
+
if (!untrusted)
|
|
471
|
+
continue;
|
|
472
|
+
findings.push({
|
|
473
|
+
ruleId: 'jenkinsfile-script-injection',
|
|
474
|
+
...position,
|
|
475
|
+
message: `The ${token.text} step interpolates ${untrusted} into its script with Groovy string interpolation, so a crafted value runs as a shell command. Use a single-quoted script and read the value from the environment, for example '... "$${untrusted.replace(/^(?:env|params)\./, '')}"'.`,
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return findings;
|
|
481
|
+
}
|
|
482
|
+
export { checkJenkinsfile, isJenkinsfile };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Reads a JSON configuration file (vercel.json, package.json) into plain values that keep the line
|
|
2
|
+
// of each string. The TypeScript JSON parser tolerates comments and recovers from syntax errors.
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
// The configuration these checks read is a few levels deep. A file nested deeper than this is
|
|
5
|
+
// skipped, which keeps the recursive parser off the call stack limit.
|
|
6
|
+
const maxDepth = 64;
|
|
7
|
+
const other = { kind: 'other' };
|
|
8
|
+
// The bracket depth outside strings, read in one pass. The compiler's JSON parser recurses per
|
|
9
|
+
// level, so deeper text would exhaust the call stack before conversion starts.
|
|
10
|
+
function nestingDepth(text) {
|
|
11
|
+
let depth = 0;
|
|
12
|
+
let deepest = 0;
|
|
13
|
+
for (let index = 0; index < text.length; index++) {
|
|
14
|
+
const char = text[index];
|
|
15
|
+
if (char === '"')
|
|
16
|
+
index = stringClose(text, index);
|
|
17
|
+
else if (char === '{' || char === '[')
|
|
18
|
+
deepest = Math.max(deepest, ++depth);
|
|
19
|
+
else if (char === '}' || char === ']')
|
|
20
|
+
depth--;
|
|
21
|
+
}
|
|
22
|
+
return deepest;
|
|
23
|
+
}
|
|
24
|
+
// Index of the quote or line break that ends the JSON string opening at start.
|
|
25
|
+
function stringClose(text, start) {
|
|
26
|
+
let index = start + 1;
|
|
27
|
+
while (index < text.length && text[index] !== '"' && text[index] !== '\n')
|
|
28
|
+
index += text[index] === '\\' ? 2 : 1;
|
|
29
|
+
return index;
|
|
30
|
+
}
|
|
31
|
+
function parseJsonValue(fileName, text) {
|
|
32
|
+
if (nestingDepth(text) > maxDepth)
|
|
33
|
+
return other;
|
|
34
|
+
const source = ts.parseJsonText(fileName, text);
|
|
35
|
+
function convert(node) {
|
|
36
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
37
|
+
const entries = new Map();
|
|
38
|
+
for (const property of node.properties) {
|
|
39
|
+
if (!ts.isPropertyAssignment(property) ||
|
|
40
|
+
!(ts.isStringLiteral(property.name) || ts.isIdentifier(property.name)))
|
|
41
|
+
continue;
|
|
42
|
+
if (!entries.has(property.name.text))
|
|
43
|
+
entries.set(property.name.text, convert(property.initializer));
|
|
44
|
+
}
|
|
45
|
+
return { kind: 'object', entries };
|
|
46
|
+
}
|
|
47
|
+
if (ts.isArrayLiteralExpression(node))
|
|
48
|
+
return { kind: 'array', items: node.elements.map((element) => convert(element)) };
|
|
49
|
+
if (ts.isStringLiteral(node)) {
|
|
50
|
+
const { line, character } = source.getLineAndCharacterOfPosition(node.getStart(source));
|
|
51
|
+
return { kind: 'string', value: node.text, line: line + 1, column: character + 1 };
|
|
52
|
+
}
|
|
53
|
+
return other;
|
|
54
|
+
}
|
|
55
|
+
const [statement] = source.statements;
|
|
56
|
+
return statement && ts.isExpressionStatement(statement) ? convert(statement.expression) : other;
|
|
57
|
+
}
|
|
58
|
+
function member(value, key) {
|
|
59
|
+
return value?.kind === 'object' ? value.entries.get(key) : undefined;
|
|
60
|
+
}
|
|
61
|
+
function itemsOf(value) {
|
|
62
|
+
return value?.kind === 'array' ? value.items : [];
|
|
63
|
+
}
|
|
64
|
+
function stringOf(value) {
|
|
65
|
+
return value?.kind === 'string' ? value : undefined;
|
|
66
|
+
}
|
|
67
|
+
export { itemsOf, member, parseJsonValue, stringOf };
|