repometry 0.3.0 → 0.4.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/package.json +1 -1
- package/target/main/resources/report/json/report-v2.schema.json +12 -1
- package/target/main/resources/viewer/files.css +16 -9
- package/target/main/resources/viewer/styles.css +13 -2
- package/target/main/ts/api/git/git.d.ts +1 -1
- package/target/main/ts/api/git/git.js +21 -5
- package/target/main/ts/api/git/inspection-source.d.ts +1 -1
- package/target/main/ts/api/git/inspection-source.js +10 -4
- package/target/main/ts/api/git/local.d.ts +1 -1
- package/target/main/ts/api/git/local.js +29 -10
- package/target/main/ts/api/worker/worker-protocol.js +34 -9
- package/target/main/ts/compose/analyze-snapshot.d.ts +1 -0
- package/target/main/ts/compose/analyze-snapshot.js +50 -17
- package/target/main/ts/compose/inspect.js +2 -2
- package/target/main/ts/compose/plugin-list.js +2 -1
- package/target/main/ts/compose/security-plugins.d.ts +1 -0
- package/target/main/ts/compose/security-plugins.js +7 -0
- package/target/main/ts/core/engine/manifest.js +3 -2
- package/target/main/ts/core/engine/plugin.d.ts +2 -0
- package/target/main/ts/core/model/line-coverage.d.ts +2 -0
- package/target/main/ts/core/model/line-coverage.js +19 -1
- package/target/main/ts/core/model/source.d.ts +3 -0
- package/target/main/ts/core/model/source.js +4 -1
- package/target/main/ts/core/usecases/inspect.js +2 -2
- package/target/main/ts/plugins/coverage/coverage.d.ts +4 -3
- package/target/main/ts/plugins/coverage/coverage.js +46 -17
- package/target/main/ts/plugins/language-js/js-security.d.ts +4 -0
- package/target/main/ts/plugins/language-js/js-security.js +508 -0
- package/target/main/ts/plugins/security/actions-security.d.ts +2 -0
- package/target/main/ts/plugins/security/actions-security.js +581 -0
- package/target/main/ts/plugins/security/dockerfile.d.ts +2 -0
- package/target/main/ts/plugins/security/dockerfile.js +488 -0
- package/target/main/ts/plugins/security/npm-supply-chain.d.ts +4 -0
- package/target/main/ts/plugins/security/npm-supply-chain.js +368 -0
- package/target/main/ts/plugins/security/secrets.d.ts +12 -0
- package/target/main/ts/plugins/security/secrets.js +254 -0
- package/target/main/ts/report/html/app/viewer-files.d.ts +7 -0
- package/target/main/ts/report/html/app/viewer-files.js +60 -10
- package/target/main/ts/report/html/app/viewer-filter.js +8 -0
- package/target/main/ts/report/html/app/viewer-source.d.ts +17 -0
- package/target/main/ts/report/html/app/viewer-source.js +71 -11
- package/target/main/ts/report/html/site/badges.d.ts +1 -4
- package/target/main/ts/report/html/site/badges.js +28 -19
- package/target/main/ts/report/json/projections/commit-summary.js +2 -11
- package/target/main/ts/report/json/projections/inspection-context.js +6 -12
- package/target/main/ts/report/json/projections/security-summary.d.ts +11 -0
- package/target/main/ts/report/json/projections/security-summary.js +22 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { extname } from 'node:path';
|
|
3
|
+
import { parse } from '@babel/parser';
|
|
4
|
+
import { pluginApi } from '../../core/engine/plugin.js';
|
|
5
|
+
import { findingResult } from '../../core/model/finding.js';
|
|
6
|
+
import { sourceExtensions, sourceFile, utf8 } from '../../core/model/snapshot.js';
|
|
7
|
+
import { babelTypes, parserDefaults } from './babel-provider.js';
|
|
8
|
+
import { children } from './function-facts.js';
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
10
|
+
const catalog = {
|
|
11
|
+
'eval': { level: 'high',
|
|
12
|
+
description: 'eval() or the Function constructor runs a string that is not a literal, so injected text becomes code.' },
|
|
13
|
+
'child-process-injection': { level: 'high',
|
|
14
|
+
description: 'child_process exec or execSync runs a shell command built from interpolated or concatenated values.' },
|
|
15
|
+
'vm-code-execution': { level: 'medium',
|
|
16
|
+
description: 'The vm module compiles or runs code that is not a literal, and vm is not a security boundary.' },
|
|
17
|
+
'dom-xss': { level: 'medium',
|
|
18
|
+
description: 'Markup that is not a literal is written through innerHTML, outerHTML, insertAdjacentHTML or document.write.' },
|
|
19
|
+
'tls-verification-disabled': { level: 'high',
|
|
20
|
+
description: 'TLS certificate verification is turned off with rejectUnauthorized or NODE_TLS_REJECT_UNAUTHORIZED.' },
|
|
21
|
+
'weak-hash': { level: 'medium', description: 'createHash or createHmac uses the broken MD5 or SHA-1 algorithm.' },
|
|
22
|
+
'insecure-random': { level: 'low',
|
|
23
|
+
description: 'Math.random() feeds a value named like a token, secret, password, nonce, salt, key or one-time code.' },
|
|
24
|
+
'http-url': { level: 'low', description: 'A string literal points at a plain http:// URL on a public host.' },
|
|
25
|
+
};
|
|
26
|
+
const tracked = new Set(['child_process', 'vm']);
|
|
27
|
+
const shellCalls = new Set(['exec', 'execSync']);
|
|
28
|
+
const vmCalls = new Set(['runInThisContext', 'runInNewContext', 'runInContext']);
|
|
29
|
+
const weakAlgorithms = new Set(['md5', 'sha1']);
|
|
30
|
+
const secretWords = new Set(['token', 'secret', 'password', 'passwd', 'passphrase', 'nonce', 'salt', 'otp']);
|
|
31
|
+
const keyQualifiers = new Set(['api', 'private', 'session', 'access', 'auth', 'encryption', 'signing', 'crypto', 'master']);
|
|
32
|
+
// A name ending in one of these holds a number about the secret, such as tokenIndex, not the secret itself.
|
|
33
|
+
const quantityWords = new Set(['index', 'idx', 'count', 'length', 'size', 'delay', 'timeout', 'ms']);
|
|
34
|
+
const wrappers = new Set(['TSAsExpression', 'TSSatisfiesExpression', 'TSNonNullExpression', 'TSTypeAssertion',
|
|
35
|
+
'ParenthesizedExpression', 'TypeCastExpression']);
|
|
36
|
+
const functionTypes = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'ObjectMethod',
|
|
37
|
+
'ClassMethod', 'ClassPrivateMethod']);
|
|
38
|
+
const jsSecurityRules = Object.entries(catalog)
|
|
39
|
+
.map(([id, { description }]) => ({ id, category: 'security', description }));
|
|
40
|
+
export const jsSecurity = {
|
|
41
|
+
id: 'js-security',
|
|
42
|
+
version: '1.0.0',
|
|
43
|
+
api: pluginApi,
|
|
44
|
+
kinds: ['security'],
|
|
45
|
+
claims: { extensions: sourceExtensions },
|
|
46
|
+
produces: [{ contract: findingResult.id, persistence: 'report' }],
|
|
47
|
+
tools: { '@babel/parser': require('@babel/parser/package.json').version, ...babelTypes },
|
|
48
|
+
rules: jsSecurityRules,
|
|
49
|
+
operations: [{
|
|
50
|
+
id: 'scan',
|
|
51
|
+
role: 'inspection',
|
|
52
|
+
inputs: { source: sourceFile },
|
|
53
|
+
output: findingResult,
|
|
54
|
+
execute(inputs) {
|
|
55
|
+
const file = inputs.read('source', sourceFile);
|
|
56
|
+
const ast = parseSource(file.path, file.bytes);
|
|
57
|
+
return { measurements: [], findings: ast === undefined ? [] : scan(file.path, ast) };
|
|
58
|
+
},
|
|
59
|
+
}],
|
|
60
|
+
};
|
|
61
|
+
/** Parses like the babel-syntax provider; a file it rejects is that plugin's diagnostic, not a security finding. */
|
|
62
|
+
function parseSource(path, bytes) {
|
|
63
|
+
const ext = extname(path);
|
|
64
|
+
const plugins = [];
|
|
65
|
+
if (['.ts', '.mts', '.cts', '.tsx'].includes(ext)) {
|
|
66
|
+
plugins.push(['typescript', { dts: /\.d\.[cm]?ts$/.test(path), disallowAmbiguousJSXLike: ext === '.mts' || ext === '.cts' }]);
|
|
67
|
+
}
|
|
68
|
+
if (ext === '.jsx' || ext === '.tsx')
|
|
69
|
+
plugins.push('jsx');
|
|
70
|
+
const sourceType = ext === '.mjs' || ext === '.mts' ? 'module'
|
|
71
|
+
: ext === '.cjs' || ext === '.cts' ? 'commonjs' : parserDefaults.sourceType;
|
|
72
|
+
try {
|
|
73
|
+
return parse(utf8(bytes), { ...parserDefaults, sourceType, plugins });
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function scan(path, ast) {
|
|
80
|
+
const bindings = collectBindings(ast);
|
|
81
|
+
const findings = [];
|
|
82
|
+
walk(ast, [], (node, ancestors) => {
|
|
83
|
+
const parent = ancestors.at(-1);
|
|
84
|
+
switch (node.type) {
|
|
85
|
+
case 'CallExpression':
|
|
86
|
+
case 'OptionalCallExpression':
|
|
87
|
+
inspectCall(node.callee, node.arguments, node, ancestors);
|
|
88
|
+
break;
|
|
89
|
+
case 'NewExpression':
|
|
90
|
+
inspectConstruct(node.callee, node.arguments, node);
|
|
91
|
+
break;
|
|
92
|
+
case 'AssignmentExpression':
|
|
93
|
+
inspectAssignment(node.left, node.right, node.operator, node);
|
|
94
|
+
break;
|
|
95
|
+
case 'ObjectProperty':
|
|
96
|
+
if (propertyKey(node) === 'rejectUnauthorized' && isFalse(node.value)) {
|
|
97
|
+
report('tls-verification-disabled', node, 'Setting rejectUnauthorized to false accepts any TLS certificate, '
|
|
98
|
+
+ 'which exposes the connection to man-in-the-middle attacks.');
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
case 'StringLiteral':
|
|
102
|
+
if (parent?.type !== 'TSLiteralType')
|
|
103
|
+
inspectUrl(node.value, node);
|
|
104
|
+
break;
|
|
105
|
+
case 'TemplateLiteral':
|
|
106
|
+
if (node.expressions.length === 0 && node.quasis[0]?.value.cooked != null)
|
|
107
|
+
inspectUrl(node.quasis[0].value.cooked, node);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return findings.sort((left, right) => (left.line ?? 0) - (right.line ?? 0));
|
|
112
|
+
function inspectCall(callee, args, node, ancestors) {
|
|
113
|
+
const target = unwrap(callee);
|
|
114
|
+
const [first] = args;
|
|
115
|
+
if (isGlobal(target, 'eval')) {
|
|
116
|
+
if (first !== undefined && !isStatic(first)) {
|
|
117
|
+
report('eval', node, 'eval() runs a string that is not a literal, so attacker-controlled input can execute as code.');
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (isGlobal(target, 'Function')) {
|
|
122
|
+
inspectFunctionConstructor(args, node);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (isMathRandom(target)) {
|
|
126
|
+
const name = sinkName(ancestors);
|
|
127
|
+
if (name !== undefined && isSecretName(name)) {
|
|
128
|
+
report('insecure-random', node, `Math.random() is predictable, so ${name} can be guessed; `
|
|
129
|
+
+ 'use crypto.randomBytes, randomUUID or getRandomValues instead.');
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const method = memberName(target);
|
|
134
|
+
if (method === 'createHash' || method === 'createHmac' || (target.type === 'Identifier'
|
|
135
|
+
&& (target.name === 'createHash' || target.name === 'createHmac'))) {
|
|
136
|
+
const algorithm = first === undefined ? undefined : staticString(first);
|
|
137
|
+
if (algorithm !== undefined && weakAlgorithms.has(algorithm.toLowerCase())) {
|
|
138
|
+
report('weak-hash', node, `${algorithm.toUpperCase()} is vulnerable to collision attacks and must not protect `
|
|
139
|
+
+ 'integrity, signatures or passwords; use SHA-256 or stronger.');
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (isDocumentWrite(target)) {
|
|
144
|
+
if (args.some(arg => !isStatic(arg) && !isSanitized(arg))) {
|
|
145
|
+
report('dom-xss', node, 'document.write() with markup that is not a literal lets injected HTML run scripts in the page.');
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (method === 'insertAdjacentHTML') {
|
|
150
|
+
const html = args[1];
|
|
151
|
+
if (html !== undefined && !isStatic(html) && !isSanitized(html)) {
|
|
152
|
+
report('dom-xss', node, 'insertAdjacentHTML() with markup that is not a literal lets injected HTML run scripts in the page.');
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const imported = resolve(target);
|
|
157
|
+
if (imported?.module === 'child_process' && shellCalls.has(imported.name)) {
|
|
158
|
+
if (first !== undefined && isBuiltString(first)) {
|
|
159
|
+
report('child-process-injection', node, `${imported.name}() passes a command built from interpolated values to a shell, `
|
|
160
|
+
+ 'so metacharacters in those values can run arbitrary commands; use execFile or spawn with an argument array.');
|
|
161
|
+
}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (imported?.module === 'vm' && vmCalls.has(imported.name) && first !== undefined && !isStatic(first)) {
|
|
165
|
+
report('vm-code-execution', node, `vm.${imported.name}() executes code that is not a literal, and the vm module `
|
|
166
|
+
+ 'does not isolate untrusted code from the host process.');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function inspectConstruct(callee, args, node) {
|
|
170
|
+
const target = unwrap(callee);
|
|
171
|
+
if (isGlobal(target, 'Function')) {
|
|
172
|
+
inspectFunctionConstructor(args, node);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const imported = resolve(target);
|
|
176
|
+
const [code] = args;
|
|
177
|
+
if (imported?.module === 'vm' && imported.name === 'Script' && code !== undefined && !isStatic(code)) {
|
|
178
|
+
report('vm-code-execution', node, 'new vm.Script() compiles code that is not a literal, and the vm module '
|
|
179
|
+
+ 'does not isolate untrusted code from the host process.');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function inspectFunctionConstructor(args, node) {
|
|
183
|
+
const body = args.at(-1);
|
|
184
|
+
if (body !== undefined && !isStatic(body)) {
|
|
185
|
+
report('eval', node, 'The Function constructor compiles a body that is not a literal, '
|
|
186
|
+
+ 'so attacker-controlled input can execute as code.');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function inspectAssignment(left, right, operator, node) {
|
|
190
|
+
const property = memberName(left);
|
|
191
|
+
if ((property === 'innerHTML' || property === 'outerHTML') && (operator === '=' || operator === '+=')) {
|
|
192
|
+
if (!isStatic(right) && !isSanitized(right)) {
|
|
193
|
+
report('dom-xss', node, `Assigning markup that is not a literal to ${property} lets injected HTML run scripts in the page.`);
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (property === 'rejectUnauthorized' && operator === '=' && isFalse(right)) {
|
|
198
|
+
report('tls-verification-disabled', node, 'Setting rejectUnauthorized to false accepts any TLS certificate, '
|
|
199
|
+
+ 'which exposes the connection to man-in-the-middle attacks.');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (property === 'NODE_TLS_REJECT_UNAUTHORIZED' && operator === '=' && isProcessEnv(unwrap(left))) {
|
|
203
|
+
const value = unwrap(right);
|
|
204
|
+
if ((value.type === 'StringLiteral' && value.value === '0') || (value.type === 'NumericLiteral' && value.value === 0)) {
|
|
205
|
+
report('tls-verification-disabled', node, 'NODE_TLS_REJECT_UNAUTHORIZED=0 disables certificate verification '
|
|
206
|
+
+ 'for every TLS connection in the process, exposing them to man-in-the-middle attacks.');
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function inspectUrl(value, node) {
|
|
211
|
+
if (!value.startsWith('http://'))
|
|
212
|
+
return;
|
|
213
|
+
const host = /^http:\/\/(?:[^@/?#\s]*@)?(\[[^\]]*\]|[^/:?#\s]*)/.exec(value)?.[1]?.toLowerCase() ?? '';
|
|
214
|
+
if (!isPublicHost(host))
|
|
215
|
+
return;
|
|
216
|
+
report('http-url', node, `The plain HTTP URL to ${host} sends data unencrypted, so it can be read or altered in transit; use https.`);
|
|
217
|
+
}
|
|
218
|
+
function resolve(node) {
|
|
219
|
+
if (node.type === 'Identifier')
|
|
220
|
+
return bindings.named.get(node.name);
|
|
221
|
+
const name = memberName(node);
|
|
222
|
+
if (name === undefined || (node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression'))
|
|
223
|
+
return undefined;
|
|
224
|
+
const object = unwrap(node.object);
|
|
225
|
+
const module = object.type === 'Identifier' ? bindings.namespaces.get(object.name) : requiredModule(object);
|
|
226
|
+
return module === undefined ? undefined : { module, name };
|
|
227
|
+
}
|
|
228
|
+
function report(rule, node, message) {
|
|
229
|
+
const line = node.loc?.start.line ?? 1;
|
|
230
|
+
findings.push({ tool: 'js-security', rule, category: 'security', level: catalog[rule].level, message, path,
|
|
231
|
+
line, endLine: Math.max(line, node.loc?.end.line ?? line) });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function collectBindings(ast) {
|
|
235
|
+
const bindings = { namespaces: new Map(), named: new Map() };
|
|
236
|
+
walk(ast, [], node => {
|
|
237
|
+
if (node.type === 'ImportDeclaration') {
|
|
238
|
+
const module = moduleName(node.source.value);
|
|
239
|
+
if (module === undefined || node.importKind === 'type')
|
|
240
|
+
return;
|
|
241
|
+
for (const specifier of node.specifiers) {
|
|
242
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
243
|
+
if (specifier.importKind === 'type')
|
|
244
|
+
continue;
|
|
245
|
+
const name = specifier.imported.type === 'Identifier' ? specifier.imported.name : specifier.imported.value;
|
|
246
|
+
bindings.named.set(specifier.local.name, { module, name });
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
bindings.namespaces.set(specifier.local.name, module);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
else if (node.type === 'TSImportEqualsDeclaration' && node.moduleReference.type === 'TSExternalModuleReference') {
|
|
254
|
+
const module = moduleName(node.moduleReference.expression.value);
|
|
255
|
+
if (module !== undefined && !node.isExport)
|
|
256
|
+
bindings.namespaces.set(node.id.name, module);
|
|
257
|
+
}
|
|
258
|
+
else if (node.type === 'VariableDeclarator' && node.init) {
|
|
259
|
+
bindDeclarator(node.id, unwrap(node.init), bindings);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
return bindings;
|
|
263
|
+
}
|
|
264
|
+
function bindDeclarator(id, init, bindings) {
|
|
265
|
+
const module = requiredModule(init);
|
|
266
|
+
if (module !== undefined) {
|
|
267
|
+
if (id.type === 'Identifier')
|
|
268
|
+
bindings.namespaces.set(id.name, module);
|
|
269
|
+
if (id.type !== 'ObjectPattern')
|
|
270
|
+
return;
|
|
271
|
+
for (const property of id.properties) {
|
|
272
|
+
if (property.type !== 'ObjectProperty')
|
|
273
|
+
continue;
|
|
274
|
+
const name = propertyKey(property);
|
|
275
|
+
const value = property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
|
|
276
|
+
if (name !== undefined && value.type === 'Identifier')
|
|
277
|
+
bindings.named.set(value.name, { module, name });
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (id.type !== 'Identifier')
|
|
282
|
+
return;
|
|
283
|
+
// const exec = require('child_process').exec; const run = promisify(exec);
|
|
284
|
+
const member = memberName(init);
|
|
285
|
+
if (member !== undefined && (init.type === 'MemberExpression' || init.type === 'OptionalMemberExpression')) {
|
|
286
|
+
const object = unwrap(init.object);
|
|
287
|
+
const owner = object.type === 'Identifier' ? bindings.namespaces.get(object.name) : requiredModule(object);
|
|
288
|
+
if (owner !== undefined)
|
|
289
|
+
bindings.named.set(id.name, { module: owner, name: member });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (init.type === 'CallExpression' && init.arguments[0] !== undefined) {
|
|
293
|
+
const callee = unwrap(init.callee);
|
|
294
|
+
if ((callee.type === 'Identifier' && callee.name === 'promisify') || memberName(callee) === 'promisify') {
|
|
295
|
+
const wrapped = unwrap(init.arguments[0]);
|
|
296
|
+
const original = wrapped.type === 'Identifier' ? bindings.named.get(wrapped.name) : undefined;
|
|
297
|
+
if (original !== undefined)
|
|
298
|
+
bindings.named.set(id.name, original);
|
|
299
|
+
else if (wrapped.type === 'MemberExpression') {
|
|
300
|
+
const object = unwrap(wrapped.object);
|
|
301
|
+
const owner = object.type === 'Identifier' ? bindings.namespaces.get(object.name) : requiredModule(object);
|
|
302
|
+
const name = memberName(wrapped);
|
|
303
|
+
if (owner !== undefined && name !== undefined)
|
|
304
|
+
bindings.named.set(id.name, { module: owner, name });
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/** The tracked module that `require('x')` or `await import('x')` loads, if any. */
|
|
310
|
+
function requiredModule(node) {
|
|
311
|
+
const call = node.type === 'AwaitExpression' ? unwrap(node.argument) : node;
|
|
312
|
+
if (call.type !== 'CallExpression')
|
|
313
|
+
return undefined;
|
|
314
|
+
const isLoader = (call.callee.type === 'Identifier' && call.callee.name === 'require') || call.callee.type === 'Import';
|
|
315
|
+
const [specifier] = call.arguments;
|
|
316
|
+
return isLoader && specifier?.type === 'StringLiteral' ? moduleName(specifier.value) : undefined;
|
|
317
|
+
}
|
|
318
|
+
function moduleName(specifier) {
|
|
319
|
+
const name = specifier.startsWith('node:') ? specifier.slice(5) : specifier;
|
|
320
|
+
return tracked.has(name) ? name : undefined;
|
|
321
|
+
}
|
|
322
|
+
function walk(node, ancestors, visit) {
|
|
323
|
+
visit(node, ancestors);
|
|
324
|
+
ancestors.push(node);
|
|
325
|
+
for (const child of children(node))
|
|
326
|
+
walk(child, ancestors, visit);
|
|
327
|
+
ancestors.pop();
|
|
328
|
+
}
|
|
329
|
+
function unwrap(node) {
|
|
330
|
+
let current = node;
|
|
331
|
+
while (wrappers.has(current.type))
|
|
332
|
+
current = current.expression;
|
|
333
|
+
return current;
|
|
334
|
+
}
|
|
335
|
+
/** A value fixed at write time: literals, expression-free templates and concatenations of those. */
|
|
336
|
+
function isStatic(node) {
|
|
337
|
+
const value = unwrap(node);
|
|
338
|
+
switch (value.type) {
|
|
339
|
+
case 'StringLiteral':
|
|
340
|
+
case 'NumericLiteral':
|
|
341
|
+
case 'BooleanLiteral':
|
|
342
|
+
case 'NullLiteral':
|
|
343
|
+
case 'BigIntLiteral':
|
|
344
|
+
case 'RegExpLiteral':
|
|
345
|
+
return true;
|
|
346
|
+
case 'TemplateLiteral':
|
|
347
|
+
return value.expressions.every(isStatic);
|
|
348
|
+
case 'BinaryExpression':
|
|
349
|
+
return value.operator === '+' && value.left.type !== 'PrivateName' && isStatic(value.left) && isStatic(value.right);
|
|
350
|
+
default:
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** A template with interpolations or a `+` concatenation whose parts are not all literal. */
|
|
355
|
+
function isBuiltString(node) {
|
|
356
|
+
const value = unwrap(node);
|
|
357
|
+
return (value.type === 'TemplateLiteral' || (value.type === 'BinaryExpression' && value.operator === '+')) && !isStatic(value);
|
|
358
|
+
}
|
|
359
|
+
function staticString(node) {
|
|
360
|
+
const value = unwrap(node);
|
|
361
|
+
if (value.type === 'StringLiteral')
|
|
362
|
+
return value.value;
|
|
363
|
+
if (value.type === 'TemplateLiteral' && value.expressions.length === 0)
|
|
364
|
+
return value.quasis[0]?.value.cooked ?? undefined;
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
function isFalse(node) {
|
|
368
|
+
const value = unwrap(node);
|
|
369
|
+
return value.type === 'BooleanLiteral' && !value.value;
|
|
370
|
+
}
|
|
371
|
+
/** Markup passed through a function named like a sanitizer or escaper, such as DOMPurify.sanitize(html). */
|
|
372
|
+
function isSanitized(node) {
|
|
373
|
+
const value = unwrap(node);
|
|
374
|
+
if (value.type !== 'CallExpression' && value.type !== 'OptionalCallExpression')
|
|
375
|
+
return false;
|
|
376
|
+
const callee = unwrap(value.callee);
|
|
377
|
+
const name = callee.type === 'Identifier' ? callee.name : memberName(callee);
|
|
378
|
+
return name !== undefined && /sanitize|escape/i.test(name);
|
|
379
|
+
}
|
|
380
|
+
function memberName(node) {
|
|
381
|
+
if (node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression')
|
|
382
|
+
return undefined;
|
|
383
|
+
if (!node.computed && node.property.type === 'Identifier')
|
|
384
|
+
return node.property.name;
|
|
385
|
+
if (node.computed && node.property.type === 'StringLiteral')
|
|
386
|
+
return node.property.value;
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
function propertyKey(node) {
|
|
390
|
+
if (node.type !== 'ObjectProperty' && node.type !== 'ClassProperty' && node.type !== 'ObjectMethod'
|
|
391
|
+
&& node.type !== 'ClassMethod' && node.type !== 'ClassPrivateProperty' && node.type !== 'ClassPrivateMethod')
|
|
392
|
+
return undefined;
|
|
393
|
+
const key = node.key;
|
|
394
|
+
if (key.type === 'PrivateName')
|
|
395
|
+
return key.id.name;
|
|
396
|
+
if (node.type !== 'ClassPrivateProperty' && node.type !== 'ClassPrivateMethod' && node.computed) {
|
|
397
|
+
return key.type === 'StringLiteral' ? key.value : undefined;
|
|
398
|
+
}
|
|
399
|
+
return key.type === 'Identifier' ? key.name : key.type === 'StringLiteral' ? key.value : undefined;
|
|
400
|
+
}
|
|
401
|
+
/** `eval`, `Function` or the same name read from globalThis, window, self or global. */
|
|
402
|
+
function isGlobal(node, name) {
|
|
403
|
+
if (node.type === 'Identifier')
|
|
404
|
+
return node.name === name;
|
|
405
|
+
if (memberName(node) !== name || (node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression'))
|
|
406
|
+
return false;
|
|
407
|
+
const object = unwrap(node.object);
|
|
408
|
+
return object.type === 'Identifier' && ['globalThis', 'window', 'self', 'global'].includes(object.name);
|
|
409
|
+
}
|
|
410
|
+
function isMathRandom(node) {
|
|
411
|
+
if (memberName(node) !== 'random' || (node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression'))
|
|
412
|
+
return false;
|
|
413
|
+
const object = unwrap(node.object);
|
|
414
|
+
return object.type === 'Identifier' && object.name === 'Math';
|
|
415
|
+
}
|
|
416
|
+
function isDocumentWrite(node) {
|
|
417
|
+
const name = memberName(node);
|
|
418
|
+
if ((name !== 'write' && name !== 'writeln') || (node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression'))
|
|
419
|
+
return false;
|
|
420
|
+
const object = unwrap(node.object);
|
|
421
|
+
return isGlobal(object, 'document');
|
|
422
|
+
}
|
|
423
|
+
function isProcessEnv(node) {
|
|
424
|
+
if (node.type !== 'MemberExpression' && node.type !== 'OptionalMemberExpression')
|
|
425
|
+
return false;
|
|
426
|
+
const env = unwrap(node.object);
|
|
427
|
+
if (memberName(env) !== 'env' || (env.type !== 'MemberExpression' && env.type !== 'OptionalMemberExpression'))
|
|
428
|
+
return false;
|
|
429
|
+
const process = unwrap(env.object);
|
|
430
|
+
return process.type === 'Identifier' && process.name === 'process';
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* The name a value is stored under, found by climbing from the expression to the nearest declarator, assignment,
|
|
434
|
+
* property or return. A returned value takes the enclosing function's name; any other statement ends the search.
|
|
435
|
+
*/
|
|
436
|
+
function sinkName(ancestors) {
|
|
437
|
+
let returning = false;
|
|
438
|
+
for (let index = ancestors.length - 1; index >= 0; index--) {
|
|
439
|
+
const node = ancestors[index];
|
|
440
|
+
if (functionTypes.has(node.type)) {
|
|
441
|
+
const own = functionOwnName(node);
|
|
442
|
+
if (own !== undefined)
|
|
443
|
+
return own;
|
|
444
|
+
returning = false;
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (returning)
|
|
448
|
+
continue;
|
|
449
|
+
switch (node.type) {
|
|
450
|
+
case 'VariableDeclarator':
|
|
451
|
+
return node.id.type === 'Identifier' ? node.id.name : undefined;
|
|
452
|
+
case 'AssignmentExpression': {
|
|
453
|
+
const left = unwrap(node.left);
|
|
454
|
+
return left.type === 'Identifier' ? left.name : memberName(left);
|
|
455
|
+
}
|
|
456
|
+
case 'ObjectProperty':
|
|
457
|
+
case 'ClassProperty':
|
|
458
|
+
case 'ClassPrivateProperty':
|
|
459
|
+
return propertyKey(node);
|
|
460
|
+
case 'JSXAttribute':
|
|
461
|
+
return node.name.type === 'JSXIdentifier' ? node.name.name : node.name.name.name;
|
|
462
|
+
case 'JSXElement':
|
|
463
|
+
case 'JSXFragment':
|
|
464
|
+
return undefined;
|
|
465
|
+
case 'ReturnStatement':
|
|
466
|
+
returning = true;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (isStatementLike(node))
|
|
470
|
+
return undefined;
|
|
471
|
+
}
|
|
472
|
+
return undefined;
|
|
473
|
+
}
|
|
474
|
+
function functionOwnName(node) {
|
|
475
|
+
if ((node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') && node.id)
|
|
476
|
+
return node.id.name;
|
|
477
|
+
return propertyKey(node);
|
|
478
|
+
}
|
|
479
|
+
function isStatementLike(node) {
|
|
480
|
+
return node.type.endsWith('Statement') || node.type.endsWith('Declaration') || node.type === 'Program'
|
|
481
|
+
|| node.type === 'SwitchCase' || node.type === 'CatchClause' || node.type === 'StaticBlock';
|
|
482
|
+
}
|
|
483
|
+
/** Splits camelCase, PascalCase and snake_case names into words and looks for secret-bearing ones. */
|
|
484
|
+
export function isSecretName(name) {
|
|
485
|
+
const words = name.replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
|
|
486
|
+
.toLowerCase().split(/[^a-z0-9]+/).filter(word => word.length > 0);
|
|
487
|
+
if (words.length === 0 || quantityWords.has(words.at(-1)))
|
|
488
|
+
return false;
|
|
489
|
+
// Bare `key` is too common for list and cache keys, so it counts only with a qualifier such as apiKey or sessionKey.
|
|
490
|
+
return words.some((word, index) => matches(secretWords, word)
|
|
491
|
+
|| ((word === 'key' || word === 'keys') && words.some((other, position) => position !== index && matches(keyQualifiers, other))));
|
|
492
|
+
}
|
|
493
|
+
function matches(set, word) {
|
|
494
|
+
return set.has(word) || (word.endsWith('s') && set.has(word.slice(0, -1)));
|
|
495
|
+
}
|
|
496
|
+
function isPublicHost(host) {
|
|
497
|
+
if (host === '' || host.startsWith('['))
|
|
498
|
+
return false;
|
|
499
|
+
if (!/^[a-z0-9.-]+$/.test(host) || !host.includes('.') || host.startsWith('.') || host.endsWith('.'))
|
|
500
|
+
return false;
|
|
501
|
+
if (host === '0.0.0.0' || /^127\.\d+\.\d+\.\d+$/.test(host) || host.endsWith('.localhost'))
|
|
502
|
+
return false;
|
|
503
|
+
const under = (domain) => host === domain || host.endsWith(`.${domain}`);
|
|
504
|
+
// XML namespaces and JSON Schema $schema ids are identifiers that are never fetched.
|
|
505
|
+
return !under('example.com') && !under('example.org') && !under('example.net') && !under('w3.org') && !under('json-schema.org')
|
|
506
|
+
&& !host.startsWith('schemas.');
|
|
507
|
+
}
|
|
508
|
+
//# sourceMappingURL=js-security.js.map
|