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
|
@@ -65,7 +65,10 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
65
65
|
meta: {
|
|
66
66
|
type: 'problem',
|
|
67
67
|
docs: {
|
|
68
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/detect-object-injection.md',
|
|
68
69
|
description: 'Detects variable[key] as a left- or right-hand assignment operand',
|
|
70
|
+
cwe: 'CWE-915',
|
|
71
|
+
confidence: 'low',
|
|
69
72
|
},
|
|
70
73
|
messages: {
|
|
71
74
|
// ๐ฏ Token optimization: 37% reduction (54โ34 tokens) - removes verbose current/fix/doc labels
|
|
@@ -188,6 +191,35 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
188
191
|
const { allowLiterals = false, dangerousProperties = ['__proto__', 'prototype', 'constructor'], } = options || {};
|
|
189
192
|
// Track MemberExpressions that are part of AssignmentExpressions to avoid double-reporting
|
|
190
193
|
const handledMemberExpressions = new WeakSet();
|
|
194
|
+
// โโ AST-walker / codemod context detection (closes the audit FP
|
|
195
|
+
// surfaced by `npm run ilb:stress-test`). When the file imports any
|
|
196
|
+
// AST library (`@babel/types`, `recast`, `jscodeshift`, `eslint`,
|
|
197
|
+
// `estree-walker`, `unist-util-visit`), `node[name]`-style access is
|
|
198
|
+
// tree traversal, not user-input indexing. The same helper landed
|
|
199
|
+
// for `no-insecure-comparison` in audit iter-2; this is the port to
|
|
200
|
+
// `detect-object-injection`. See benchmarks/AUDIT_PATTERNS.md ยง2.1.
|
|
201
|
+
const sourceCode = context.sourceCode;
|
|
202
|
+
const isInCodemodContext = (() => {
|
|
203
|
+
const filename = context.filename;
|
|
204
|
+
if (/\/codemod[s]?\//i.test(filename))
|
|
205
|
+
return true;
|
|
206
|
+
if (/codemod\.[mc]?[jt]sx?$/i.test(filename))
|
|
207
|
+
return true;
|
|
208
|
+
const imports = sourceCode.ast.body.filter((n) => n.type === eslint_devkit_1.AST_NODE_TYPES.ImportDeclaration);
|
|
209
|
+
return imports.some((i) => {
|
|
210
|
+
const src = String(i.source.value);
|
|
211
|
+
return (src === '@babel/types' ||
|
|
212
|
+
src === '@babel/traverse' ||
|
|
213
|
+
src === 'recast' ||
|
|
214
|
+
src === 'jscodeshift' ||
|
|
215
|
+
src === 'eslint' ||
|
|
216
|
+
src === 'estree-walker' ||
|
|
217
|
+
src === 'ast-types' ||
|
|
218
|
+
src === 'esrap' ||
|
|
219
|
+
src === 'unist-util-visit' ||
|
|
220
|
+
src.startsWith('@typescript-eslint/'));
|
|
221
|
+
});
|
|
222
|
+
})();
|
|
191
223
|
// Check if TypeScript parser services are available for type-aware checking
|
|
192
224
|
const hasTypeInfo = (0, eslint_devkit_2.hasParserServices)(context);
|
|
193
225
|
const parserServices = hasTypeInfo ? (0, eslint_devkit_2.getParserServices)(context) : null;
|
|
@@ -465,7 +497,6 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
465
497
|
* Extract property access information
|
|
466
498
|
*/
|
|
467
499
|
const extractPropertyAccess = (node) => {
|
|
468
|
-
const sourceCode = context.sourceCode || context.sourceCode;
|
|
469
500
|
let object;
|
|
470
501
|
let property;
|
|
471
502
|
let propertyNode;
|
|
@@ -509,6 +540,11 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
509
540
|
return false;
|
|
510
541
|
}
|
|
511
542
|
const { propertyNode } = extractPropertyAccess(node);
|
|
543
|
+
// SAFE: numeric keys can't pollute Object prototypes (typed-array
|
|
544
|
+
// / numeric-array assignment is structurally safe).
|
|
545
|
+
if (isNumericKey(propertyNode)) {
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
512
548
|
// Skip if the key has been validated (e.g., includes() or hasOwnProperty check)
|
|
513
549
|
if (hasPrecedingValidation(propertyNode, node)) {
|
|
514
550
|
return false;
|
|
@@ -525,6 +561,15 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
525
561
|
return false;
|
|
526
562
|
}
|
|
527
563
|
const { propertyNode } = extractPropertyAccess(node);
|
|
564
|
+
// Numeric keys cannot pollute Object prototypes โ typed-array and
|
|
565
|
+
// numeric-array access (`arr[0]`, `arr[i]` where i is a for-loop
|
|
566
|
+
// counter) is structurally safe. This eliminates the bulk of false
|
|
567
|
+
// positives on numeric/buffer-heavy codebases (Three.js, webpack,
|
|
568
|
+
// image/audio/geometry libraries) without weakening detection of
|
|
569
|
+
// string-key prototype pollution.
|
|
570
|
+
if (isNumericKey(propertyNode)) {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
528
573
|
// Skip if the key has been validated (e.g., includes() or hasOwnProperty check)
|
|
529
574
|
if (hasPrecedingValidation(propertyNode, node)) {
|
|
530
575
|
return false;
|
|
@@ -532,9 +577,76 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
532
577
|
// Check for dangerous property access
|
|
533
578
|
return isDangerousPropertyAccess(propertyNode);
|
|
534
579
|
};
|
|
580
|
+
/**
|
|
581
|
+
* Returns true if the property expression is provably a numeric key
|
|
582
|
+
* (and therefore cannot trigger prototype pollution).
|
|
583
|
+
*
|
|
584
|
+
* Detected as numeric:
|
|
585
|
+
* - Numeric literal: arr[0], arr[42]
|
|
586
|
+
* - Unary plus on number: arr[+x]
|
|
587
|
+
* - Number(...) coercion: arr[Number(x)]
|
|
588
|
+
* - parseInt/parseFloat: arr[parseInt(x)]
|
|
589
|
+
* - Bitwise on identifier: arr[x | 0], arr[x >>> 0]
|
|
590
|
+
* - Identifier whose declaration is the init of a `for` statement
|
|
591
|
+
* (the standard `for (let i = 0; i < n; i++)` counter pattern)
|
|
592
|
+
*/
|
|
593
|
+
const isNumericKey = (node) => {
|
|
594
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof node.value === 'number') {
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression && node.operator === '+') {
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
|
|
601
|
+
const op = node.operator;
|
|
602
|
+
if (op === '|' || op === '&' || op === '^' || op === '<<' || op === '>>' || op === '>>>' || op === '*' || op === '/' || op === '%' || op === '-') {
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
607
|
+
const callee = node.callee;
|
|
608
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
609
|
+
const name = callee.name;
|
|
610
|
+
if (name === 'Number' || name === 'parseInt' || name === 'parseFloat')
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
615
|
+
return isLoopCounterIdentifier(node);
|
|
616
|
+
}
|
|
617
|
+
return false;
|
|
618
|
+
};
|
|
619
|
+
/**
|
|
620
|
+
* Returns true if the identifier is the loop variable of an enclosing
|
|
621
|
+
* `for` statement, e.g. `for (let i = 0; i < n; i++) arr[i]`. The loop
|
|
622
|
+
* counter is by construction numeric, so the access is safe.
|
|
623
|
+
*/
|
|
624
|
+
const isLoopCounterIdentifier = (node) => {
|
|
625
|
+
const scope = context.sourceCode.getScope(node);
|
|
626
|
+
const variable = scope.references.find((r) => r.identifier === node)?.resolved;
|
|
627
|
+
if (!variable || variable.defs.length === 0)
|
|
628
|
+
return false;
|
|
629
|
+
const def = variable.defs[0];
|
|
630
|
+
// Look for `for (let i = <numeric init>; ...; ...)` shape.
|
|
631
|
+
const parent = def.node?.parent;
|
|
632
|
+
const grand = parent?.parent;
|
|
633
|
+
if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration &&
|
|
634
|
+
grand?.type === eslint_devkit_1.AST_NODE_TYPES.ForStatement &&
|
|
635
|
+
grand.init === parent) {
|
|
636
|
+
const init = def.node.init;
|
|
637
|
+
if (!init)
|
|
638
|
+
return false;
|
|
639
|
+
// Initializer must itself be numeric.
|
|
640
|
+
if (init.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof init.value === 'number') {
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return false;
|
|
645
|
+
};
|
|
535
646
|
/**
|
|
536
647
|
* Determine risk level based on the pattern and context
|
|
537
648
|
*/
|
|
649
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
538
650
|
const determineRiskLevel = (pattern, isAssignment) => {
|
|
539
651
|
if (pattern?.riskLevel === 'critical' || (pattern && isAssignment)) {
|
|
540
652
|
return 'CRITICAL';
|
|
@@ -620,9 +732,63 @@ exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
|
620
732
|
}
|
|
621
733
|
});
|
|
622
734
|
};
|
|
735
|
+
/**
|
|
736
|
+
* Object.assign(target, untrustedSource) and `{ ...untrustedSource }`
|
|
737
|
+
* spread into an object are functionally equivalent to `obj[k] = v`
|
|
738
|
+
* for prototype-pollution purposes โ they copy every enumerable
|
|
739
|
+
* property of `source` onto `target`, including any `__proto__` /
|
|
740
|
+
* `constructor` / `prototype` keys the source carries. The hand-
|
|
741
|
+
* curated stress test surfaced this as an FN; closing it requires a
|
|
742
|
+
* separate visitor since Object.assign is a CallExpression and
|
|
743
|
+
* spread is a SpreadElement, not a MemberExpression. See
|
|
744
|
+
* benchmarks/AUDIT_PATTERNS.md ยง3.4 ("equivalent merger patterns").
|
|
745
|
+
*/
|
|
746
|
+
const checkObjectAssignSpread = (node) => {
|
|
747
|
+
if (isInCodemodContext)
|
|
748
|
+
return;
|
|
749
|
+
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
750
|
+
return;
|
|
751
|
+
const callee = node.callee;
|
|
752
|
+
const objectIsObject = callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
753
|
+
callee.object.name === 'Object';
|
|
754
|
+
const propIsAssign = !callee.computed &&
|
|
755
|
+
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
756
|
+
callee.property.name === 'assign';
|
|
757
|
+
if (!objectIsObject || !propIsAssign)
|
|
758
|
+
return;
|
|
759
|
+
// Object.assign({}, โฆ) โ first arg is fresh literal, no taint risk.
|
|
760
|
+
if (node.arguments[0]?.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
|
|
761
|
+
return;
|
|
762
|
+
// Sources are arguments[1...]. Any non-literal source is an
|
|
763
|
+
// assumed taint source. Literals are safe (they're inline data).
|
|
764
|
+
const sources = node.arguments.slice(1);
|
|
765
|
+
const anyTaintedSource = sources.some((s) => s.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression &&
|
|
766
|
+
s.type !== eslint_devkit_1.AST_NODE_TYPES.Literal);
|
|
767
|
+
if (!anyTaintedSource)
|
|
768
|
+
return;
|
|
769
|
+
context.report({
|
|
770
|
+
node,
|
|
771
|
+
messageId: 'objectInjection',
|
|
772
|
+
data: {
|
|
773
|
+
pattern: 'Object.assign(target, untrustedSource)',
|
|
774
|
+
riskLevel: 'HIGH',
|
|
775
|
+
vulnerability: 'object injection via Object.assign spread',
|
|
776
|
+
safeAlternative: 'Validate or whitelist keys before merging: `for (const k of Object.keys(src)) if (!ALLOWED.has(k)) continue;`',
|
|
777
|
+
},
|
|
778
|
+
});
|
|
779
|
+
};
|
|
623
780
|
return {
|
|
624
|
-
AssignmentExpression:
|
|
625
|
-
|
|
781
|
+
AssignmentExpression: (node) => {
|
|
782
|
+
if (isInCodemodContext)
|
|
783
|
+
return;
|
|
784
|
+
return checkAssignmentExpression(node);
|
|
785
|
+
},
|
|
786
|
+
MemberExpression: (node) => {
|
|
787
|
+
if (isInCodemodContext)
|
|
788
|
+
return;
|
|
789
|
+
return checkMemberExpression(node);
|
|
790
|
+
},
|
|
791
|
+
CallExpression: checkObjectAssignSpread,
|
|
626
792
|
};
|
|
627
793
|
},
|
|
628
794
|
});
|
|
@@ -15,7 +15,10 @@ exports.detectWeakPasswordValidation = (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/detect-weak-password-validation.md',
|
|
18
19
|
description: 'Identify weak password requirements',
|
|
20
|
+
cwe: 'CWE-521',
|
|
21
|
+
cvss: 9.5,
|
|
19
22
|
},
|
|
20
23
|
messages: {
|
|
21
24
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -14,7 +14,9 @@ exports.noDirectiveInjection = (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-directive-injection.md',
|
|
17
18
|
description: 'Detects directive injection vulnerabilities in templates',
|
|
19
|
+
cwe: 'CWE-96',
|
|
18
20
|
},
|
|
19
21
|
fixable: 'code',
|
|
20
22
|
hasSuggestions: true,
|
|
@@ -199,8 +201,8 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
|
|
|
199
201
|
create(context) {
|
|
200
202
|
const options = context.options[0] || {};
|
|
201
203
|
const { userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
202
|
-
const sourceCode = context.sourceCode
|
|
203
|
-
const filename = context.filename
|
|
204
|
+
const sourceCode = context.sourceCode;
|
|
205
|
+
const filename = context.filename;
|
|
204
206
|
// Create safety checker for false positive detection
|
|
205
207
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
206
208
|
trustedSanitizers,
|
|
@@ -410,7 +412,9 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
|
|
|
410
412
|
// Check if template literal is used dangerously
|
|
411
413
|
let current = node;
|
|
412
414
|
let isInDangerousContext = false;
|
|
413
|
-
|
|
415
|
+
// Every assignment of `isInDangerousContext = true` is followed by `break`,
|
|
416
|
+
// so the negation is dead (CodeQL: `js/useless-conditional`).
|
|
417
|
+
while (current) {
|
|
414
418
|
if (current.type === 'JSXExpressionContainer') {
|
|
415
419
|
// Check if we are inside dangerouslySetInnerHTML attribute
|
|
416
420
|
if (current.parent?.type === 'JSXAttribute' &&
|
|
@@ -14,7 +14,9 @@ exports.noElectronSecurityIssues = (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-electron-security-issues.md',
|
|
17
18
|
description: 'Detects Electron security vulnerabilities and insecure configurations',
|
|
19
|
+
cwe: 'CWE-16',
|
|
18
20
|
},
|
|
19
21
|
fixable: 'code',
|
|
20
22
|
hasSuggestions: true,
|
|
@@ -203,7 +205,7 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
|
|
|
203
205
|
create(context) {
|
|
204
206
|
const options = context.options[0] || {};
|
|
205
207
|
const { allowedIpcChannels = [], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
206
|
-
const filename = context.filename
|
|
208
|
+
const filename = context.filename;
|
|
207
209
|
// Create safety checker for false positive detection
|
|
208
210
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
209
211
|
trustedSanitizers,
|
|
@@ -214,6 +216,7 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
|
|
|
214
216
|
/**
|
|
215
217
|
* Check if this is an Electron BrowserWindow creation
|
|
216
218
|
*/
|
|
219
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
217
220
|
const isBrowserWindowCreation = (node) => {
|
|
218
221
|
return node.callee.type === 'Identifier' &&
|
|
219
222
|
node.callee.name === 'BrowserWindow';
|
|
@@ -262,6 +265,7 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
|
|
|
262
265
|
/**
|
|
263
266
|
* Check if this is an IPC call
|
|
264
267
|
*/
|
|
268
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
265
269
|
const isIpcCall = (node) => {
|
|
266
270
|
const callee = node.callee;
|
|
267
271
|
if (callee.type === 'MemberExpression' &&
|
|
@@ -315,6 +319,7 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
|
|
|
315
319
|
/**
|
|
316
320
|
* Check for Node.js API usage
|
|
317
321
|
*/
|
|
322
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
318
323
|
const isNodeApiCall = (node) => {
|
|
319
324
|
const callee = node.callee;
|
|
320
325
|
// Check for require('fs'), require('child_process'), etc.
|
|
@@ -13,7 +13,9 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
docs: {
|
|
16
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-format-string-injection.md',
|
|
16
17
|
description: 'Detects format string injection vulnerabilities',
|
|
18
|
+
cwe: 'CWE-134',
|
|
17
19
|
},
|
|
18
20
|
fixable: 'code',
|
|
19
21
|
hasSuggestions: true,
|
|
@@ -138,26 +140,26 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
138
140
|
trustedAnnotations: [],
|
|
139
141
|
strictMode: false,
|
|
140
142
|
};
|
|
141
|
-
const options = { ...defaultOptions, ...
|
|
143
|
+
const options = { ...defaultOptions, ...context.options[0] };
|
|
142
144
|
const { formatSpecifiers, userInputVariables, trustedSanitizers, } = options;
|
|
143
|
-
const filename = context.filename
|
|
145
|
+
const filename = context.filename;
|
|
144
146
|
// Create safety checker for false positive detection (simplified implementation)
|
|
145
147
|
const safetyChecker = {
|
|
146
|
-
isSafe: (
|
|
148
|
+
isSafe: (safeNode, ruleCtx) => {
|
|
147
149
|
// Check for JSDoc @safe-format annotation
|
|
148
|
-
const comments =
|
|
150
|
+
const comments = ruleCtx.sourceCode.getCommentsBefore(safeNode);
|
|
149
151
|
for (const comment of comments) {
|
|
150
152
|
if (comment.type === 'Block' && comment.value.includes('@safe-format')) {
|
|
151
153
|
return true;
|
|
152
154
|
}
|
|
153
155
|
}
|
|
154
156
|
// Check if the node is a validated/sanitized variable
|
|
155
|
-
if (
|
|
157
|
+
if (safeNode.type === 'Identifier' && validatedVariables.has(safeNode.name)) {
|
|
156
158
|
return true;
|
|
157
159
|
}
|
|
158
160
|
// For CallExpression nodes, check if first argument is safe
|
|
159
|
-
if (
|
|
160
|
-
const firstArg =
|
|
161
|
+
if (safeNode.type === 'CallExpression' && safeNode.arguments.length > 0) {
|
|
162
|
+
const firstArg = safeNode.arguments[0];
|
|
161
163
|
if (firstArg.type === 'Identifier' && validatedVariables.has(firstArg.name)) {
|
|
162
164
|
return true;
|
|
163
165
|
}
|
|
@@ -209,6 +211,7 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
209
211
|
/**
|
|
210
212
|
* Get the full name of a member expression (e.g., req.query.format)
|
|
211
213
|
*/
|
|
214
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
212
215
|
const getMemberExpressionName = (node) => {
|
|
213
216
|
if (node.object.type === 'Identifier') {
|
|
214
217
|
if (node.property.type === 'Identifier') {
|
|
@@ -232,6 +235,7 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
232
235
|
/**
|
|
233
236
|
* Check if a call expression uses format functions
|
|
234
237
|
*/
|
|
238
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
235
239
|
const isConsoleMethod = (node) => {
|
|
236
240
|
const callee = node.callee;
|
|
237
241
|
return callee.type === 'MemberExpression' &&
|
|
@@ -240,6 +244,7 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
240
244
|
callee.property.type === 'Identifier' &&
|
|
241
245
|
['log', 'error', 'warn', 'info', 'debug'].includes(callee.property.name);
|
|
242
246
|
};
|
|
247
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
243
248
|
const isFormatFunctionCall = (node) => {
|
|
244
249
|
const callee = node.callee;
|
|
245
250
|
// Check for util.format
|
|
@@ -405,9 +410,9 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
405
410
|
}
|
|
406
411
|
// Check for format specifiers in subsequent arguments (could indicate user input in format position)
|
|
407
412
|
// Only check if the format string itself is not validated/safe
|
|
408
|
-
const
|
|
409
|
-
const isFormatSafe = isInputValidated(
|
|
410
|
-
(
|
|
413
|
+
const fmtArg = args[0];
|
|
414
|
+
const isFormatSafe = isInputValidated(fmtArg) ||
|
|
415
|
+
(fmtArg.type === 'Identifier' && validatedVariables.has(fmtArg.name));
|
|
411
416
|
if (!isFormatSafe) {
|
|
412
417
|
let hasUserInputInArgs = false;
|
|
413
418
|
let hasSpecifiersInFormat = false;
|
|
@@ -484,8 +489,10 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
484
489
|
// Check if this literal is constructed from user input (e.g., concatenation)
|
|
485
490
|
let current = node;
|
|
486
491
|
let isFromUserInput = false;
|
|
487
|
-
// Walk up to find if this literal is part of a concatenation or template with user input
|
|
488
|
-
|
|
492
|
+
// Walk up to find if this literal is part of a concatenation or template with user input.
|
|
493
|
+
// Every path that sets `isFromUserInput = true` is immediately followed by `break`,
|
|
494
|
+
// so the negation in the loop condition is dead (CodeQL: `js/useless-conditional`).
|
|
495
|
+
while (current) {
|
|
489
496
|
if (current.type === 'BinaryExpression' &&
|
|
490
497
|
current.operator === '+' &&
|
|
491
498
|
(current.left === node || current.right === node)) {
|
|
@@ -515,8 +522,10 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
515
522
|
// Check if this string is used in a context where it could be dangerous
|
|
516
523
|
current = node;
|
|
517
524
|
let isInDangerousContext = false;
|
|
518
|
-
// Walk up to find if this is passed to a format function
|
|
519
|
-
|
|
525
|
+
// Walk up to find if this is passed to a format function. Every path that sets
|
|
526
|
+
// `isInDangerousContext = true` is followed by `break`, so the negation in the
|
|
527
|
+
// condition is dead (CodeQL: `js/useless-conditional`).
|
|
528
|
+
while (current) {
|
|
520
529
|
if (current.type === 'CallExpression' && isFormatFunctionCall(current)) {
|
|
521
530
|
// Check if this is the first argument (format string position)
|
|
522
531
|
const args = current.arguments;
|
|
@@ -546,17 +555,17 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
546
555
|
// Check template literals for format string injection
|
|
547
556
|
TemplateLiteral: function (node) {
|
|
548
557
|
// Check if template literal is used as format string
|
|
549
|
-
let
|
|
558
|
+
let templateCurrent = node;
|
|
550
559
|
let isFormatString = false;
|
|
551
|
-
while (
|
|
552
|
-
if (
|
|
553
|
-
const args =
|
|
560
|
+
while (templateCurrent) {
|
|
561
|
+
if (templateCurrent.type === 'CallExpression' && isFormatFunctionCall(templateCurrent)) {
|
|
562
|
+
const args = templateCurrent.arguments;
|
|
554
563
|
if (args.length > 0 && args[0] === node) {
|
|
555
564
|
isFormatString = true;
|
|
556
565
|
break;
|
|
557
566
|
}
|
|
558
567
|
}
|
|
559
|
-
|
|
568
|
+
templateCurrent = templateCurrent.parent;
|
|
560
569
|
}
|
|
561
570
|
if (isFormatString) {
|
|
562
571
|
// Template literal used as format string - let CallExpression visitor handle this
|
|
@@ -567,14 +576,14 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
|
|
|
567
576
|
const hasUserInput = node.expressions.some((expr) => isUserInputNode(expr));
|
|
568
577
|
if (hasUserInput) {
|
|
569
578
|
// Check if this template is assigned to a variable that could be used as format string
|
|
570
|
-
let
|
|
579
|
+
let assignCurrent = node;
|
|
571
580
|
let isAssignedToVariable = false;
|
|
572
|
-
while (
|
|
573
|
-
if (
|
|
581
|
+
while (assignCurrent) {
|
|
582
|
+
if (assignCurrent.type === 'VariableDeclarator') {
|
|
574
583
|
isAssignedToVariable = true;
|
|
575
584
|
break;
|
|
576
585
|
}
|
|
577
|
-
|
|
586
|
+
assignCurrent = assignCurrent.parent;
|
|
578
587
|
}
|
|
579
588
|
if (isAssignedToVariable) {
|
|
580
589
|
/* c8 ignore start -- safetyChecker requires JSDoc annotations not testable via RuleTester */
|
|
@@ -14,7 +14,9 @@ exports.noGraphqlInjection = (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-graphql-injection.md',
|
|
17
18
|
description: 'Detects GraphQL injection vulnerabilities and DoS attacks',
|
|
19
|
+
cwe: 'CWE-89',
|
|
18
20
|
},
|
|
19
21
|
fixable: 'code',
|
|
20
22
|
hasSuggestions: true,
|
|
@@ -179,8 +181,8 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
179
181
|
create(context) {
|
|
180
182
|
const options = context.options[0] || {};
|
|
181
183
|
const { allowIntrospection = false, maxQueryDepth = 10, trustedGraphqlLibraries = ['graphql', 'apollo-server', 'graphql-tools', 'graphql-tag'], validationFunctions = ['validate', 'sanitize', 'isValid', 'assertValid'], safeTemplateLiteralCallers = [], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
182
|
-
const sourceCode = context.sourceCode
|
|
183
|
-
const filename = context.filename
|
|
184
|
+
const sourceCode = context.sourceCode;
|
|
185
|
+
const filename = context.filename;
|
|
184
186
|
// Create safety checker for false positive detection
|
|
185
187
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
186
188
|
trustedSanitizers,
|
|
@@ -267,6 +269,7 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
267
269
|
return false;
|
|
268
270
|
};
|
|
269
271
|
// โโโ AST-based GraphQL detection helpers โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
272
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
270
273
|
const isWordChar = (ch) => {
|
|
271
274
|
const code = ch.charCodeAt(0);
|
|
272
275
|
return (code >= 65 && code <= 90) || // A-Z
|
|
@@ -274,6 +277,7 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
274
277
|
(code >= 48 && code <= 57) || // 0-9
|
|
275
278
|
code === 95; // _
|
|
276
279
|
};
|
|
280
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
277
281
|
const isWhitespace = (ch) => ch === ' ' || ch === '\n' || ch === '\t' || ch === '\r';
|
|
278
282
|
/** Operation keywords that must be followed by optional name + { or ( */
|
|
279
283
|
const GRAPHQL_OP_KEYWORDS = ['query', 'mutation', 'subscription'];
|
|
@@ -389,6 +393,7 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
389
393
|
* Check if a TemplateLiteral contains introspection patterns.
|
|
390
394
|
* AST-based: scans quasis directly.
|
|
391
395
|
*/
|
|
396
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
392
397
|
const templateHasIntrospection = (node) => {
|
|
393
398
|
return node.quasis.some(q => {
|
|
394
399
|
const text = (q.value.cooked ?? q.value.raw).toLowerCase();
|
|
@@ -398,6 +403,7 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
398
403
|
/**
|
|
399
404
|
* Calculate query depth from template quasis (brace depth scan).
|
|
400
405
|
*/
|
|
406
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
401
407
|
const templateQueryDepth = (node) => {
|
|
402
408
|
let depth = 0;
|
|
403
409
|
let braceCount = 0;
|