eslint-plugin-secure-coding 3.1.3 → 3.2.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/CHANGELOG.md +10 -0
- package/README.md +77 -55
- package/package.json +18 -3
- package/src/index.js +39 -6
- package/src/oxlint.d.ts +20 -0
- package/src/oxlint.js +21 -0
- package/src/rules/detect-non-literal-regexp/index.js +9 -1
- package/src/rules/detect-object-injection/index.js +169 -3
- package/src/rules/detect-weak-password-validation/index.js +3 -0
- package/src/rules/no-directive-injection/index.js +7 -3
- package/src/rules/no-electron-security-issues/index.js +6 -1
- package/src/rules/no-format-string-injection/index.js +32 -23
- package/src/rules/no-graphql-injection/index.js +8 -2
- package/src/rules/no-hardcoded-credentials/index.js +343 -51
- package/src/rules/no-hardcoded-session-tokens/index.js +3 -0
- package/src/rules/no-improper-sanitization/index.js +31 -3
- package/src/rules/no-improper-type-validation/index.js +6 -3
- package/src/rules/no-insecure-comparison/index.js +38 -2
- package/src/rules/no-ldap-injection/index.js +6 -2
- package/src/rules/no-missing-authentication/index.js +5 -2
- package/src/rules/no-pii-in-logs/index.js +3 -0
- package/src/rules/no-privilege-escalation/index.js +5 -2
- package/src/rules/no-redos-vulnerable-regex/index.js +79 -0
- package/src/rules/no-sensitive-data-exposure/index.js +3 -0
- package/src/rules/no-unchecked-loop-condition/index.js +5 -2
- package/src/rules/no-unlimited-resource-allocation/index.js +9 -8
- package/src/rules/no-unsafe-deserialization/index.js +7 -5
- package/src/rules/no-unsafe-regex-construction/index.js +10 -3
- package/src/rules/no-weak-password-recovery/index.js +5 -2
- package/src/rules/no-xpath-injection/index.js +6 -2
- package/src/rules/no-xxe-injection/index.js +48 -46
- package/src/rules/require-backend-authorization/index.js +6 -3
- package/src/rules/require-secure-defaults/index.js +3 -0
|
@@ -27,71 +27,97 @@ const CREDENTIAL_PATTERNS = {
|
|
|
27
27
|
// Secret keys (base64-like or hex strings)
|
|
28
28
|
secretKey: /^(?:[A-Za-z0-9+/]{32,}={0,2}|[A-Fa-f0-9]{32,})$/,
|
|
29
29
|
};
|
|
30
|
-
// Note: CREDENTIAL_VARIABLE_NAMES is reserved for future use when we want to
|
|
31
|
-
// check variable names in addition to values
|
|
32
|
-
// @coverage-note: Not currently used, reserved for future enhancement
|
|
33
30
|
/**
|
|
34
|
-
*
|
|
31
|
+
* Identifier names that imply the surrounding string is a credential.
|
|
32
|
+
* Used to require a *credential-typed* context for ambiguous regex matches
|
|
33
|
+
* (generic 32+-char alphanumeric, common passwords) — without this gate the
|
|
34
|
+
* rule fires on TS union-type literals, error class names, and test prompts
|
|
35
|
+
* (verified on vercel/ai: 807 ours-only findings, top samples are
|
|
36
|
+
* `'experimental_onToolExecutionStart'`, `'AI_ToolCallNotFoundForApprovalError'`,
|
|
37
|
+
* `'test'`).
|
|
38
|
+
*/
|
|
39
|
+
const CREDENTIAL_VARIABLE_NAMES = new Set([
|
|
40
|
+
'apikey', 'api_key', 'apiKey',
|
|
41
|
+
'secret', 'secretkey', 'secret_key', 'secretKey', 'clientsecret', 'client_secret', 'clientSecret',
|
|
42
|
+
'token', 'authtoken', 'auth_token', 'authToken', 'accesstoken', 'access_token', 'accessToken',
|
|
43
|
+
'refreshtoken', 'refresh_token', 'refreshToken', 'idtoken', 'id_token', 'idToken',
|
|
44
|
+
'password', 'passwd', 'pass', 'pwd',
|
|
45
|
+
'privatekey', 'private_key', 'privateKey',
|
|
46
|
+
'credentials', 'creds',
|
|
47
|
+
'authorization', 'auth',
|
|
48
|
+
'connectionstring', 'connection_string', 'connectionString', 'connectionuri', 'connectionURI',
|
|
49
|
+
'dburl', 'db_url', 'dbUrl', 'databaseurl', 'database_url', 'databaseUrl',
|
|
50
|
+
]);
|
|
51
|
+
/**
|
|
52
|
+
* Check if a string literal looks like a hardcoded credential.
|
|
53
|
+
* Returns `confidence: 'ambiguous'` when only a permissive pattern matched —
|
|
54
|
+
* the caller MUST verify the surrounding identifier is credential-named.
|
|
35
55
|
*/
|
|
36
56
|
function looksLikeCredential(value, options, ignorePatterns) {
|
|
57
|
+
const NONE = { isCredential: false, type: '', confidence: 'ambiguous' };
|
|
37
58
|
// Check ignore patterns first
|
|
38
|
-
if (ignorePatterns.some(pattern => pattern.test(value)))
|
|
39
|
-
return
|
|
40
|
-
|
|
41
|
-
// Check custom patterns first (highest priority)
|
|
59
|
+
if (ignorePatterns.some(pattern => pattern.test(value)))
|
|
60
|
+
return NONE;
|
|
61
|
+
// Custom patterns are user-defined → trust them, treat as structural
|
|
42
62
|
for (const customPattern of options.customPatterns) {
|
|
43
63
|
try {
|
|
44
64
|
const regex = new RegExp(customPattern.pattern);
|
|
45
65
|
if (regex.test(value)) {
|
|
46
|
-
return { isCredential: true, type: customPattern.type };
|
|
66
|
+
return { isCredential: true, type: customPattern.type, confidence: 'structural' };
|
|
47
67
|
}
|
|
48
68
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
49
69
|
}
|
|
50
70
|
catch (error) {
|
|
51
|
-
// Invalid regex pattern, skip it
|
|
52
71
|
continue;
|
|
53
72
|
}
|
|
54
73
|
}
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
74
|
+
// Common-password keyword match — AMBIGUOUS. Strings like "test", "admin",
|
|
75
|
+
// "guest" appear in test fixtures, error messages, and identifiers all the
|
|
76
|
+
// time. The caller must verify a credential-named context.
|
|
77
|
+
if (options.detectPasswords && CREDENTIAL_PATTERNS.commonPassword.test(value)) {
|
|
78
|
+
return { isCredential: true, type: 'Common password', confidence: 'ambiguous' };
|
|
60
79
|
}
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
80
|
+
// Structural: DB connection strings have unambiguous shape
|
|
81
|
+
// `protocol://user:pass@host` — no FP risk.
|
|
82
|
+
if (options.detectDatabaseStrings && CREDENTIAL_PATTERNS.databaseString.test(value)) {
|
|
83
|
+
return { isCredential: true, type: 'Database connection string', confidence: 'structural' };
|
|
66
84
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
85
|
+
if (value.length < options.minLength)
|
|
86
|
+
return NONE;
|
|
87
|
+
// Structural: JWT format (3 base64 parts dot-separated)
|
|
88
|
+
if (options.detectTokens && CREDENTIAL_PATTERNS.jwtToken.test(value)) {
|
|
89
|
+
return { isCredential: true, type: 'JWT token', confidence: 'structural' };
|
|
70
90
|
}
|
|
71
|
-
//
|
|
72
|
-
if (options.detectTokens) {
|
|
73
|
-
|
|
74
|
-
return { isCredential: true, type: 'JWT token' };
|
|
75
|
-
}
|
|
76
|
-
if (CREDENTIAL_PATTERNS.oauthToken.test(value)) {
|
|
77
|
-
return { isCredential: true, type: 'OAuth token' };
|
|
78
|
-
}
|
|
91
|
+
// Structural: OAuth tokens have provider prefixes (ghp_, gho_, ...)
|
|
92
|
+
if (options.detectTokens && CREDENTIAL_PATTERNS.oauthToken.test(value)) {
|
|
93
|
+
return { isCredential: true, type: 'OAuth token', confidence: 'structural' };
|
|
79
94
|
}
|
|
80
|
-
//
|
|
95
|
+
// Secret keys (base64/hex 32+) — context-required. Long base64 / hex
|
|
96
|
+
// strings appear in source maps, generated IDs, hash digests; without
|
|
97
|
+
// a credential-named context they're FPs.
|
|
81
98
|
if (value.length >= 32 && CREDENTIAL_PATTERNS.secretKey.test(value)) {
|
|
82
|
-
return { isCredential: true, type: 'Secret key' };
|
|
99
|
+
return { isCredential: true, type: 'Secret key', confidence: 'ambiguous' };
|
|
83
100
|
}
|
|
84
|
-
//
|
|
85
|
-
if (options.detectApiKeys) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
// Structural: AWS access key has a fixed prefix
|
|
102
|
+
if (options.detectApiKeys && CREDENTIAL_PATTERNS.awsAccessKey.test(value)) {
|
|
103
|
+
return { isCredential: true, type: 'AWS access key', confidence: 'structural' };
|
|
104
|
+
}
|
|
105
|
+
// Structural: Stripe-style keys (sk_live_, sk_test_, pk_live_, pk_test_,
|
|
106
|
+
// rk_live_, rk_test_) are unambiguous — the prefix is registered to
|
|
107
|
+
// Stripe and never appears in unrelated contexts. Type label is kept
|
|
108
|
+
// as the generic 'API key' to match existing test expectations and
|
|
109
|
+
// ensure the suggestion templates remain stable for callers.
|
|
110
|
+
if (options.detectApiKeys &&
|
|
111
|
+
/^(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9_-]{16,}$/.test(value)) {
|
|
112
|
+
return { isCredential: true, type: 'API key', confidence: 'structural' };
|
|
113
|
+
}
|
|
114
|
+
// Generic 32+-char alphanumeric — AMBIGUOUS. This is the FP source on
|
|
115
|
+
// vercel/ai's TS union types and error class names. Caller must verify
|
|
116
|
+
// a credential-named context.
|
|
117
|
+
if (options.detectApiKeys && /^[A-Za-z0-9_-]{32,}$/.test(value)) {
|
|
118
|
+
return { isCredential: true, type: 'API key', confidence: 'ambiguous' };
|
|
93
119
|
}
|
|
94
|
-
return
|
|
120
|
+
return NONE;
|
|
95
121
|
}
|
|
96
122
|
// Note: isCredentialVariableName is reserved for future use when we want to
|
|
97
123
|
// check variable names in addition to values
|
|
@@ -101,7 +127,11 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
101
127
|
meta: {
|
|
102
128
|
type: 'problem',
|
|
103
129
|
docs: {
|
|
130
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-hardcoded-credentials.md',
|
|
104
131
|
description: 'Detects hardcoded passwords, API keys, tokens, and other sensitive credentials',
|
|
132
|
+
cwe: 'CWE-798',
|
|
133
|
+
cvss: 9.5,
|
|
134
|
+
confidence: 'medium',
|
|
105
135
|
},
|
|
106
136
|
fixable: 'code',
|
|
107
137
|
hasSuggestions: true,
|
|
@@ -245,7 +275,7 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
245
275
|
create(context) {
|
|
246
276
|
const options = context.options[0] || {};
|
|
247
277
|
const { ignorePatterns = [], allowInTests = false, minLength = 8, detectApiKeys = true, detectPasswords = true, detectTokens = true, detectDatabaseStrings = true, customPatterns = [], } = options || {};
|
|
248
|
-
const filename = context.filename
|
|
278
|
+
const filename = context.filename;
|
|
249
279
|
const isTestFile = allowInTests && (filename.includes('.test.') ||
|
|
250
280
|
filename.includes('.spec.') ||
|
|
251
281
|
filename.includes('__tests__') ||
|
|
@@ -260,6 +290,225 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
260
290
|
detectDatabaseStrings,
|
|
261
291
|
customPatterns,
|
|
262
292
|
};
|
|
293
|
+
/**
|
|
294
|
+
* Variable / property names that hold UI labels and HTML attribute
|
|
295
|
+
* values, not secrets. When a literal containing the word "password"
|
|
296
|
+
* (or other credential-like text) lives in one of these contexts, it's
|
|
297
|
+
* a label or form-field metadata, not a hardcoded credential.
|
|
298
|
+
*/
|
|
299
|
+
const LABEL_CONTEXT_NAMES = new Set([
|
|
300
|
+
// HTML form attributes
|
|
301
|
+
'type', 'name', 'id', 'placeholder', 'label', 'title', 'role',
|
|
302
|
+
'autocomplete', 'autoFocus', 'autocapitalize', 'inputmode',
|
|
303
|
+
// ARIA
|
|
304
|
+
'aria-label', 'aria-labelledby', 'aria-describedby',
|
|
305
|
+
// Common semantic UI fields
|
|
306
|
+
'fieldName', 'fieldType', 'fieldLabel', 'inputType', 'inputName',
|
|
307
|
+
'displayName', 'columnName', 'paramName',
|
|
308
|
+
// i18n keys / translation lookup. NOTE: bare `'key'` is intentionally
|
|
309
|
+
// omitted — `const key = '...'` is the canonical name for actual API
|
|
310
|
+
// keys (e.g. AWS, Stripe), so exempting it would mask real secrets.
|
|
311
|
+
// The specific i18n names below cover translation lookups without
|
|
312
|
+
// that false-negative.
|
|
313
|
+
'i18nKey', 'translationKey', 'messageKey',
|
|
314
|
+
]);
|
|
315
|
+
/**
|
|
316
|
+
* Returns true if the literal is being used as a UI label or HTML
|
|
317
|
+
* attribute value rather than as a secret. Examples:
|
|
318
|
+
* const label = 'password'; // variable named `label`
|
|
319
|
+
* input.type = 'password'; // assigning to .type
|
|
320
|
+
* input.name = 'userPassword'; // assigning to .name
|
|
321
|
+
* <input type="password" /> // JSX attribute
|
|
322
|
+
* { type: 'password', name: 'pw' } // object literal property
|
|
323
|
+
* setAttribute('placeholder', 'Enter password') // setAttribute call
|
|
324
|
+
*/
|
|
325
|
+
function isLabelContext(node, parent) {
|
|
326
|
+
if (!parent)
|
|
327
|
+
return false;
|
|
328
|
+
// Object-literal entries: walk up through the ObjectExpression →
|
|
329
|
+
// its Property → its enclosing VariableDeclarator. Closes the
|
|
330
|
+
// regression where `const labels = { password: 'Enter password' }`
|
|
331
|
+
// was firing because the property key 'password' is in
|
|
332
|
+
// CREDENTIAL_VARIABLE_NAMES even though the surrounding `labels`
|
|
333
|
+
// var indicates the whole object is UI text.
|
|
334
|
+
if (parent.type === 'ObjectExpression') {
|
|
335
|
+
const grand = parent.parent;
|
|
336
|
+
if (grand)
|
|
337
|
+
return isLabelContext(node, grand);
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
// Array elements: walk up so `const labels = ['Enter password']`
|
|
341
|
+
// is treated as label context if `labels` is label-named.
|
|
342
|
+
if (parent.type === 'ArrayExpression') {
|
|
343
|
+
const grand = parent.parent;
|
|
344
|
+
if (grand)
|
|
345
|
+
return isLabelContext(node, grand);
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
// const label = 'password' / let label = 'password' / `labels` /
|
|
349
|
+
// any var ending in `label`/`name`/`placeholder`.
|
|
350
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
351
|
+
const n = parent.id.name.toLowerCase();
|
|
352
|
+
if (LABEL_CONTEXT_NAMES.has(n) || n === 'labels' || n.endsWith('label') || n.endsWith('labels') || n.endsWith('name') || n.endsWith('placeholder'))
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
// input.type = 'password' / input.name = 'userPassword'
|
|
356
|
+
if (parent.type === 'AssignmentExpression' && parent.right === node) {
|
|
357
|
+
const left = parent.left;
|
|
358
|
+
if (left.type === 'MemberExpression' && left.property.type === 'Identifier') {
|
|
359
|
+
if (LABEL_CONTEXT_NAMES.has(left.property.name))
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// { type: 'password', name: 'foo' } — direct property key is label-typed
|
|
364
|
+
if (parent.type === 'Property' && parent.value === node) {
|
|
365
|
+
const key = parent.key;
|
|
366
|
+
if (key.type === 'Identifier' && LABEL_CONTEXT_NAMES.has(key.name))
|
|
367
|
+
return true;
|
|
368
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
369
|
+
if (LABEL_CONTEXT_NAMES.has(key.value))
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
// Otherwise, walk up: the property may not have a label-typed
|
|
373
|
+
// key but the enclosing variable might be `labels` /
|
|
374
|
+
// `i18nStrings` / `messages` / etc., signalling that the
|
|
375
|
+
// whole object is UI text. Closes the FP regression where
|
|
376
|
+
// `const labels = { password: 'Enter password' }` fired
|
|
377
|
+
// because `password` is in CREDENTIAL_VARIABLE_NAMES.
|
|
378
|
+
const obj = parent.parent;
|
|
379
|
+
if (obj?.type === 'ObjectExpression') {
|
|
380
|
+
const grand = obj.parent;
|
|
381
|
+
if (grand)
|
|
382
|
+
return isLabelContext(node, grand);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// setAttribute('type', 'password') — second arg is the value
|
|
386
|
+
if (parent.type === 'CallExpression' &&
|
|
387
|
+
parent.callee.type === 'MemberExpression' &&
|
|
388
|
+
parent.callee.property.type === 'Identifier') {
|
|
389
|
+
const callee = parent.callee;
|
|
390
|
+
const methodName = callee.property.name;
|
|
391
|
+
if (methodName === 'setAttribute' || methodName === 'getAttribute') {
|
|
392
|
+
const args = parent.arguments;
|
|
393
|
+
if (args[1] === node)
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
// JSX: <input type="password" />
|
|
398
|
+
if (parent.type === 'JSXAttribute')
|
|
399
|
+
return true;
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Map a credential-context to its category so the detection-disable
|
|
404
|
+
* options can gate the context-positive code path. `password` covers
|
|
405
|
+
* password-like names, `token` covers JWT / OAuth / session tokens,
|
|
406
|
+
* `database` covers DB connection strings, `apikey` covers API key /
|
|
407
|
+
* secret-key style names. Anything else falls back to `other`, which
|
|
408
|
+
* the options never gate (e.g. `credentials`, `auth`).
|
|
409
|
+
*/
|
|
410
|
+
function inferCredentialTypeFromContext(parent) {
|
|
411
|
+
const name = (() => {
|
|
412
|
+
if (!parent)
|
|
413
|
+
return '';
|
|
414
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
415
|
+
return parent.id.name.toLowerCase();
|
|
416
|
+
}
|
|
417
|
+
if (parent.type === 'Property') {
|
|
418
|
+
const key = parent.key;
|
|
419
|
+
if (key.type === 'Identifier')
|
|
420
|
+
return key.name.toLowerCase();
|
|
421
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
422
|
+
return key.value.toLowerCase();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (parent.type === 'AssignmentExpression') {
|
|
426
|
+
const left = parent.left;
|
|
427
|
+
if (left.type === 'MemberExpression' && left.property.type === 'Identifier') {
|
|
428
|
+
return left.property.name.toLowerCase();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return '';
|
|
432
|
+
})();
|
|
433
|
+
if (/(?:^|[_-])(password|passwd|pass|pwd)$/.test(name))
|
|
434
|
+
return 'password';
|
|
435
|
+
if (/(?:^|[_-])(token|authtoken|auth_token|accesstoken|access_token|refreshtoken|refresh_token|idtoken|id_token)$/.test(name))
|
|
436
|
+
return 'token';
|
|
437
|
+
if (/(?:^|[_-])(dburl|db_url|databaseurl|database_url|connectionstring|connection_string|connectionuri)$/.test(name))
|
|
438
|
+
return 'database';
|
|
439
|
+
if (/(?:^|[_-])(apikey|api_key|secretkey|secret_key|privatekey|private_key|secret|key|clientsecret|client_secret)$/.test(name))
|
|
440
|
+
return 'apikey';
|
|
441
|
+
return 'other';
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns true when the string is in a context that names credentials —
|
|
445
|
+
* assigned to a credential-typed variable, property, parameter, or used
|
|
446
|
+
* as the second argument to common credential APIs. Required for
|
|
447
|
+
* ambiguous matches (generic 32+-char alphanumeric, common-password
|
|
448
|
+
* keywords) which would otherwise fire on identifier-shaped literals
|
|
449
|
+
* (TS union types, error class names, test prompts).
|
|
450
|
+
*/
|
|
451
|
+
function isCredentialContext(node, parent) {
|
|
452
|
+
if (!parent)
|
|
453
|
+
return false;
|
|
454
|
+
const matches = (name) => {
|
|
455
|
+
const lower = name.toLowerCase();
|
|
456
|
+
// Try the literal name AND its singular form (drop trailing 's')
|
|
457
|
+
// so collections like `tokens`, `apiKeys`, `secrets`, `passwords`
|
|
458
|
+
// are recognised. Closes the audit FN where
|
|
459
|
+
// `const tokens = ['Bearer sk_live_...']` bypassed credential
|
|
460
|
+
// detection because `tokens` (plural) wasn't in the allowlist.
|
|
461
|
+
const singular = lower.endsWith('s') ? lower.slice(0, -1) : lower;
|
|
462
|
+
if (CREDENTIAL_VARIABLE_NAMES.has(lower) || CREDENTIAL_VARIABLE_NAMES.has(singular))
|
|
463
|
+
return true;
|
|
464
|
+
return lower.endsWith('apikey') || lower.endsWith('apikeys') ||
|
|
465
|
+
lower.endsWith('secret') || lower.endsWith('secrets') ||
|
|
466
|
+
lower.endsWith('token') || lower.endsWith('tokens') ||
|
|
467
|
+
lower.endsWith('password') || lower.endsWith('passwords') ||
|
|
468
|
+
lower.endsWith('passwd') || lower.endsWith('credential') ||
|
|
469
|
+
lower.endsWith('credentials');
|
|
470
|
+
};
|
|
471
|
+
// Array elements: walk up through the ArrayExpression to its
|
|
472
|
+
// enclosing variable / property. Closes the audit FN where
|
|
473
|
+
// `const tokens = ['Bearer sk_live_...']` was bypassing the
|
|
474
|
+
// credential-name check because the literal's immediate parent is
|
|
475
|
+
// the ArrayExpression rather than the VariableDeclarator.
|
|
476
|
+
if (parent.type === 'ArrayExpression') {
|
|
477
|
+
const grand = parent.parent;
|
|
478
|
+
if (grand)
|
|
479
|
+
return isCredentialContext(node, grand);
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
// const apiKey = '...' / let secret = '...'
|
|
483
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
484
|
+
if (matches(parent.id.name))
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
// obj.apiKey = '...' / this.password = '...'
|
|
488
|
+
if (parent.type === 'AssignmentExpression' &&
|
|
489
|
+
parent.right === node) {
|
|
490
|
+
const left = parent.left;
|
|
491
|
+
if (left.type === 'MemberExpression' && left.property.type === 'Identifier') {
|
|
492
|
+
if (matches(left.property.name))
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
if (left.type === 'Identifier') {
|
|
496
|
+
if (matches(left.name))
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
// { apiKey: '...', secret: '...' }
|
|
501
|
+
if (parent.type === 'Property' && parent.value === node) {
|
|
502
|
+
const key = parent.key;
|
|
503
|
+
if (key.type === 'Identifier' && matches(key.name))
|
|
504
|
+
return true;
|
|
505
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
506
|
+
if (matches(key.value))
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
263
512
|
/**
|
|
264
513
|
* Check a string literal node
|
|
265
514
|
*/
|
|
@@ -272,9 +521,48 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
272
521
|
if (isTestFile) {
|
|
273
522
|
return;
|
|
274
523
|
}
|
|
524
|
+
// Skip if used as a UI label / HTML attribute value (form-field name,
|
|
525
|
+
// type tag, ARIA label, i18n key) — these aren't credentials.
|
|
526
|
+
if (isLabelContext(node, parent)) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
275
529
|
// Check if it looks like a credential
|
|
276
|
-
const { isCredential, type } = looksLikeCredential(value, detectionOptions, compiledIgnorePatterns);
|
|
277
|
-
|
|
530
|
+
const { isCredential, type, confidence } = looksLikeCredential(value, detectionOptions, compiledIgnorePatterns);
|
|
531
|
+
// Ambiguous matches require a credential-named context to fire.
|
|
532
|
+
// Without this gate the rule reports type literals, error class
|
|
533
|
+
// names, and test prompts as credentials (vercel/ai had 807 such
|
|
534
|
+
// FPs before the gate landed).
|
|
535
|
+
let finalIsCredential = isCredential;
|
|
536
|
+
let finalType = type;
|
|
537
|
+
if (isCredential && confidence === 'ambiguous' && !isCredentialContext(node, parent)) {
|
|
538
|
+
finalIsCredential = false;
|
|
539
|
+
}
|
|
540
|
+
// Context-positive: any non-trivial string assigned to a
|
|
541
|
+
// credential-named variable/property is a credential, even when no
|
|
542
|
+
// regex pattern matches. Catches passwords like 'SuperSecret123!'
|
|
543
|
+
// that don't fit any structural pattern but are clearly secrets
|
|
544
|
+
// by virtue of where they're stored.
|
|
545
|
+
//
|
|
546
|
+
// The detection-disable options (detectApiKeys / detectPasswords /
|
|
547
|
+
// detectTokens / detectDatabaseStrings) MUST gate this path too —
|
|
548
|
+
// otherwise `{ detectPasswords: false }` silently fires on
|
|
549
|
+
// `const password = "..."` because the var-name match alone
|
|
550
|
+
// bypasses the option. Map the var name back to its category and
|
|
551
|
+
// honour the option.
|
|
552
|
+
if (!finalIsCredential && value.length >= detectionOptions.minLength &&
|
|
553
|
+
isCredentialContext(node, parent)) {
|
|
554
|
+
const ctxType = inferCredentialTypeFromContext(parent);
|
|
555
|
+
const optionAllows = (ctxType === 'password' && detectionOptions.detectPasswords) ||
|
|
556
|
+
(ctxType === 'token' && detectionOptions.detectTokens) ||
|
|
557
|
+
(ctxType === 'database' && detectionOptions.detectDatabaseStrings) ||
|
|
558
|
+
(ctxType === 'apikey' && detectionOptions.detectApiKeys) ||
|
|
559
|
+
ctxType === 'other'; // "other" credential names always honoured
|
|
560
|
+
if (optionAllows) {
|
|
561
|
+
finalIsCredential = true;
|
|
562
|
+
finalType = type || 'Credential value';
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (!finalIsCredential) {
|
|
278
566
|
return;
|
|
279
567
|
}
|
|
280
568
|
// Generate environment variable name suggestion
|
|
@@ -297,7 +585,7 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
297
585
|
node,
|
|
298
586
|
messageId: 'useEnvironmentVariable',
|
|
299
587
|
data: {
|
|
300
|
-
credentialType:
|
|
588
|
+
credentialType: finalType,
|
|
301
589
|
envVarName,
|
|
302
590
|
},
|
|
303
591
|
suggest: [
|
|
@@ -327,8 +615,12 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
327
615
|
// Only check if there are no interpolations (static template literal)
|
|
328
616
|
if (node.expressions.length === 0) {
|
|
329
617
|
const fullText = node.quasis.map((q) => q.value.raw).join('');
|
|
330
|
-
const { isCredential, type } = looksLikeCredential(fullText, detectionOptions, compiledIgnorePatterns);
|
|
331
|
-
|
|
618
|
+
const { isCredential, type, confidence } = looksLikeCredential(fullText, detectionOptions, compiledIgnorePatterns);
|
|
619
|
+
// For template literals we don't have a Literal node to pass to
|
|
620
|
+
// `isCredentialContext`. Skip ambiguous matches entirely — if the
|
|
621
|
+
// user really has a credential in a template literal they should
|
|
622
|
+
// use a regular string anyway.
|
|
623
|
+
if (isCredential && !isTestFile && confidence === 'structural') {
|
|
332
624
|
context.report({
|
|
333
625
|
node,
|
|
334
626
|
messageId: 'useEnvironmentVariable',
|
|
@@ -359,8 +651,8 @@ exports.noHardcodedCredentials = (0, eslint_devkit_2.createRule)({
|
|
|
359
651
|
// For template literals with interpolations, check each quasi part
|
|
360
652
|
for (const quasi of node.quasis) {
|
|
361
653
|
if (quasi.value.raw) {
|
|
362
|
-
const { isCredential, type } = looksLikeCredential(quasi.value.raw, detectionOptions, compiledIgnorePatterns);
|
|
363
|
-
if (isCredential && !isTestFile) {
|
|
654
|
+
const { isCredential, type, confidence } = looksLikeCredential(quasi.value.raw, detectionOptions, compiledIgnorePatterns);
|
|
655
|
+
if (isCredential && !isTestFile && confidence === 'structural') {
|
|
364
656
|
// Note: Template literals with interpolations are complex to fix automatically
|
|
365
657
|
// So we report the error without suggestions
|
|
366
658
|
context.report({
|
|
@@ -15,7 +15,10 @@ exports.noHardcodedSessionTokens = (0, eslint_devkit_1.createRule)({
|
|
|
15
15
|
meta: {
|
|
16
16
|
type: 'problem',
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-hardcoded-session-tokens.md',
|
|
18
19
|
description: 'Detect hardcoded session/JWT tokens',
|
|
20
|
+
cwe: 'CWE-798',
|
|
21
|
+
cvss: 9.5,
|
|
19
22
|
},
|
|
20
23
|
messages: {
|
|
21
24
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -14,7 +14,9 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
14
14
|
meta: {
|
|
15
15
|
type: 'problem',
|
|
16
16
|
docs: {
|
|
17
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-improper-sanitization.md',
|
|
17
18
|
description: 'Detects improper sanitization of user input',
|
|
19
|
+
cwe: 'CWE-116',
|
|
18
20
|
},
|
|
19
21
|
fixable: 'code',
|
|
20
22
|
hasSuggestions: true,
|
|
@@ -200,8 +202,8 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
200
202
|
create(context) {
|
|
201
203
|
const options = context.options[0] || {};
|
|
202
204
|
const { safeSanitizers = ['DOMPurify.sanitize', 'he.encode', 'encodeURIComponent', 'encodeURI', 'escape'], dangerousChars = ['<', '>', '"', "'", '&', '`', '$', '{', '}', '|', ';', '(', ')'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
203
|
-
const sourceCode = context.sourceCode
|
|
204
|
-
const filename = context.filename
|
|
205
|
+
const sourceCode = context.sourceCode;
|
|
206
|
+
const filename = context.filename;
|
|
205
207
|
// Create safety checker for false positive detection
|
|
206
208
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
207
209
|
trustedSanitizers,
|
|
@@ -362,11 +364,37 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
362
364
|
if (typeof node.value !== 'string') {
|
|
363
365
|
return;
|
|
364
366
|
}
|
|
367
|
+
// SAFE: literal assigned directly to innerHTML/outerHTML with no
|
|
368
|
+
// concatenation or interpolation AND containing no dangerous
|
|
369
|
+
// markup. Static developer-authored HTML normally has no taint
|
|
370
|
+
// source (`el.innerHTML = '<span class="x"></span>'` is the
|
|
371
|
+
// standard way to set static markup; flagging it would force
|
|
372
|
+
// every static UI template into the noise floor) — BUT a literal
|
|
373
|
+
// like `<script>alert(1)</script>` is unsafe even when
|
|
374
|
+
// developer-authored, because the markup itself is the vector.
|
|
375
|
+
// Only take the safe path when the literal is free of script
|
|
376
|
+
// tags, inline event handlers, and `javascript:` URIs.
|
|
377
|
+
const directParent = node.parent;
|
|
378
|
+
if (directParent?.type === 'AssignmentExpression' &&
|
|
379
|
+
directParent.right === node &&
|
|
380
|
+
directParent.left.type === 'MemberExpression') {
|
|
381
|
+
const left = directParent.left;
|
|
382
|
+
if (left.property.type === 'Identifier' &&
|
|
383
|
+
['innerHTML', 'outerHTML'].includes(left.property.name)) {
|
|
384
|
+
const literalValue = typeof node.value === 'string' ? node.value : '';
|
|
385
|
+
const hasDangerousMarkup = /<script[\s>]|<\/script>|\son\w+\s*=|javascript:/i.test(literalValue);
|
|
386
|
+
if (!hasDangerousMarkup) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
365
391
|
const text = node.value;
|
|
366
392
|
// Check if this string is used in a dangerous context
|
|
367
393
|
let current = node;
|
|
368
394
|
let isInDangerousContext = false;
|
|
369
|
-
|
|
395
|
+
// Every path that sets `isInDangerousContext = true` is followed by `break`,
|
|
396
|
+
// so the negation in the condition is dead (CodeQL: `js/useless-conditional`).
|
|
397
|
+
while (current) {
|
|
370
398
|
if (current.type === 'AssignmentExpression') {
|
|
371
399
|
const left = current.left;
|
|
372
400
|
if (left.type === 'MemberExpression' &&
|
|
@@ -14,7 +14,9 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
14
14
|
meta: {
|
|
15
15
|
type: 'problem',
|
|
16
16
|
docs: {
|
|
17
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-improper-type-validation.md',
|
|
17
18
|
description: 'Detects improper type validation in user input handling',
|
|
19
|
+
cwe: 'CWE-1287',
|
|
18
20
|
},
|
|
19
21
|
fixable: 'code',
|
|
20
22
|
hasSuggestions: true,
|
|
@@ -185,8 +187,8 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
185
187
|
create(context) {
|
|
186
188
|
const options = context.options[0] || {};
|
|
187
189
|
const { userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'], allowInstanceofSameRealm = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
188
|
-
const sourceCode = context.sourceCode
|
|
189
|
-
const filename = context.filename
|
|
190
|
+
const sourceCode = context.sourceCode;
|
|
191
|
+
const filename = context.filename;
|
|
190
192
|
// Create safety checker for false positive detection
|
|
191
193
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
192
194
|
trustedSanitizers,
|
|
@@ -274,6 +276,7 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
274
276
|
/**
|
|
275
277
|
* Check for unreliable constructor checks
|
|
276
278
|
*/
|
|
279
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
277
280
|
const isUnreliableConstructorCheck = (node) => {
|
|
278
281
|
return node.property.type === 'Identifier' &&
|
|
279
282
|
node.property.name === 'name' &&
|
|
@@ -365,7 +368,7 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
365
368
|
let current = node;
|
|
366
369
|
let involvesUserInput = false;
|
|
367
370
|
// Walk up to find if this is used with user input
|
|
368
|
-
while (current
|
|
371
|
+
while (current?.parent) {
|
|
369
372
|
current = current.parent;
|
|
370
373
|
if (current.type === 'VariableDeclarator' &&
|
|
371
374
|
current.init === node) {
|
|
@@ -15,7 +15,10 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
15
15
|
deprecated: true,
|
|
16
16
|
replacedBy: ['@see eslint-plugin-crypto/no-timing-unsafe-compare'],
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-insecure-comparison.md',
|
|
18
19
|
description: 'Detects insecure comparison operators (==, !=) that can lead to type coercion vulnerabilities',
|
|
20
|
+
cwe: 'CWE-697',
|
|
21
|
+
cvss: 7.5,
|
|
19
22
|
},
|
|
20
23
|
fixable: 'code',
|
|
21
24
|
hasSuggestions: true,
|
|
@@ -75,12 +78,40 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
75
78
|
],
|
|
76
79
|
create(context, [options = {}]) {
|
|
77
80
|
const { allowInTests = false, ignorePatterns = [], } = options;
|
|
78
|
-
const filename = context.
|
|
81
|
+
const filename = context.filename;
|
|
79
82
|
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
80
|
-
const sourceCode = context.sourceCode
|
|
83
|
+
const sourceCode = context.sourceCode;
|
|
84
|
+
// Codemods and AST-walker tools legitimately compare AST identifiers
|
|
85
|
+
// (`node.key === 'foo'`, `node.name === 'bar'`) — those keys aren't
|
|
86
|
+
// secrets, they're AST property names that happen to share the word
|
|
87
|
+
// "key". Detect codemod context once per file.
|
|
88
|
+
const AST_TOOL_PACKAGES = [
|
|
89
|
+
'@babel/types', '@babel/traverse', '@babel/generator', '@babel/parser',
|
|
90
|
+
'recast', 'jscodeshift', 'estree-walker', 'unist-util-visit',
|
|
91
|
+
'@typescript-eslint/utils', '@typescript-eslint/typescript-estree',
|
|
92
|
+
'typescript', 'ts-morph', 'eslint',
|
|
93
|
+
];
|
|
94
|
+
const isCodemodFile = (() => {
|
|
95
|
+
if (/\/codemod[s]?\//i.test(filename))
|
|
96
|
+
return true;
|
|
97
|
+
if (/codemod\.[mc]?[jt]sx?$/i.test(filename))
|
|
98
|
+
return true;
|
|
99
|
+
// Look for AST-tool imports at the top of the file
|
|
100
|
+
const program = sourceCode.ast;
|
|
101
|
+
for (const stmt of program.body) {
|
|
102
|
+
if (stmt.type === 'ImportDeclaration') {
|
|
103
|
+
const source = stmt.source.value;
|
|
104
|
+
if (typeof source === 'string' && AST_TOOL_PACKAGES.some((p) => source === p || source.startsWith(p + '/'))) {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
})();
|
|
81
111
|
/**
|
|
82
112
|
* Check if a string matches any ignore pattern
|
|
83
113
|
*/
|
|
114
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
84
115
|
function matchesIgnorePattern(text, patterns) {
|
|
85
116
|
return patterns.some(pattern => {
|
|
86
117
|
try {
|
|
@@ -100,6 +131,11 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
100
131
|
if (isTestFile) {
|
|
101
132
|
return;
|
|
102
133
|
}
|
|
134
|
+
// Skip codemod / AST-walker contexts — `node.key === '...'` style
|
|
135
|
+
// comparisons there are AST identifier checks, not secret comparisons.
|
|
136
|
+
if (isCodemodFile) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
103
139
|
const secretKeywords = ['secret', 'token', 'password', 'apikey', 'api_key', 'signature', 'auth', 'key', 'hash', 'digest', 'mac'];
|
|
104
140
|
const isSecurityContext = (() => {
|
|
105
141
|
let current = node;
|