eslint-plugin-secure-coding 3.1.3 → 3.3.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 +102 -0
- package/README.md +128 -65
- package/package.json +23 -4
- package/src/index.js +70 -12
- 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 +344 -6
- package/src/rules/detect-weak-password-validation/index.js +4 -1
- package/src/rules/no-directive-injection/index.js +7 -4
- package/src/rules/no-electron-security-issues/index.js +6 -2
- package/src/rules/no-format-string-injection/index.js +32 -23
- package/src/rules/no-graphql-injection/index.js +8 -3
- package/src/rules/no-hardcoded-credentials/index.js +366 -52
- package/src/rules/no-hardcoded-session-tokens/index.js +3 -0
- package/src/rules/no-improper-sanitization/index.js +31 -4
- package/src/rules/no-improper-type-validation/index.js +6 -4
- package/src/rules/no-insecure-comparison/index.js +39 -3
- package/src/rules/no-ldap-injection/index.js +6 -3
- package/src/rules/no-missing-authentication/index.js +5 -2
- package/src/rules/no-pii-in-logs/index.js +4 -1
- 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 +4 -1
- package/src/rules/no-template-injection/index.d.ts +23 -0
- package/src/rules/no-template-injection/index.js +90 -0
- package/src/rules/no-unchecked-loop-condition/index.js +5 -3
- package/src/rules/no-unlimited-resource-allocation/index.js +46 -30
- package/src/rules/no-unsafe-deserialization/index.js +22 -5
- package/src/rules/no-unsafe-regex-construction/index.js +10 -3
- package/src/rules/no-weak-password-recovery/index.js +5 -3
- package/src/rules/no-xpath-injection/index.js +6 -3
- 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
|
@@ -13,9 +13,12 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
13
13
|
meta: {
|
|
14
14
|
type: 'problem',
|
|
15
15
|
deprecated: true,
|
|
16
|
-
replacedBy: ['
|
|
16
|
+
replacedBy: ['node-security/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;
|
|
@@ -14,9 +14,10 @@ exports.noLdapInjection = (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-ldap-injection.md',
|
|
17
18
|
description: 'Detects LDAP injection vulnerabilities',
|
|
19
|
+
cwe: 'CWE-90',
|
|
18
20
|
},
|
|
19
|
-
fixable: 'code',
|
|
20
21
|
hasSuggestions: true,
|
|
21
22
|
messages: {
|
|
22
23
|
ldapInjection: (0, eslint_devkit_2.formatLLMMessage)({
|
|
@@ -158,8 +159,8 @@ exports.noLdapInjection = (0, eslint_devkit_1.createRule)({
|
|
|
158
159
|
create(context) {
|
|
159
160
|
const options = context.options[0] || {};
|
|
160
161
|
const { ldapFunctions = ['search', 'bind', 'modify', 'add', 'delete', 'compare', 'searchAsync'], ldapEscapeFunctions = ['escape.filterValue', 'escape.dnValue', 'filterEscape', 'dnEscape'], ldapValidationFunctions = ['validateLdapInput', 'sanitizeLdapFilter', 'cleanLdapValue', 'checkLdapFilter'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
161
|
-
const sourceCode = context.sourceCode
|
|
162
|
-
const filename = context.filename
|
|
162
|
+
const sourceCode = context.sourceCode;
|
|
163
|
+
const filename = context.filename;
|
|
163
164
|
// Create safety checker for false positive detection
|
|
164
165
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
165
166
|
trustedSanitizers,
|
|
@@ -183,6 +184,7 @@ exports.noLdapInjection = (0, eslint_devkit_1.createRule)({
|
|
|
183
184
|
/**
|
|
184
185
|
* Check if LDAP filter contains dangerous patterns
|
|
185
186
|
*/
|
|
187
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
186
188
|
const containsDangerousLdapFilter = (filterText) => {
|
|
187
189
|
// Dangerous LDAP filter patterns
|
|
188
190
|
const dangerousPatterns = [
|
|
@@ -199,6 +201,7 @@ exports.noLdapInjection = (0, eslint_devkit_1.createRule)({
|
|
|
199
201
|
/**
|
|
200
202
|
* Check if string contains LDAP filter interpolation
|
|
201
203
|
*/
|
|
204
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
202
205
|
const containsLdapInterpolation = (text) => {
|
|
203
206
|
return /\$\{[^}]+\}/.test(text) || /'[^']*\+[^+]*'/.test(text) || /"[^"]*\+[^+]*"/.test(text);
|
|
204
207
|
};
|
|
@@ -108,7 +108,10 @@ exports.noMissingAuthentication = (0, eslint_devkit_2.createRule)({
|
|
|
108
108
|
meta: {
|
|
109
109
|
type: 'problem',
|
|
110
110
|
docs: {
|
|
111
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-missing-authentication.md',
|
|
111
112
|
description: 'Detects missing authentication checks in route handlers',
|
|
113
|
+
cwe: 'CWE-287',
|
|
114
|
+
cvss: 9.5,
|
|
112
115
|
},
|
|
113
116
|
hasSuggestions: true,
|
|
114
117
|
messages: {
|
|
@@ -178,10 +181,10 @@ exports.noMissingAuthentication = (0, eslint_devkit_2.createRule)({
|
|
|
178
181
|
],
|
|
179
182
|
create(context, [options = {}]) {
|
|
180
183
|
const { allowInTests = false, testFilePattern = '\\.(test|spec)\\.(ts|tsx|js|jsx)$', authMiddlewarePatterns = DEFAULT_AUTH_MIDDLEWARE_PATTERNS, routeHandlerPatterns = DEFAULT_ROUTE_HANDLER_PATTERNS, ignorePatterns = [], } = options;
|
|
181
|
-
const filename = context.
|
|
184
|
+
const filename = context.filename;
|
|
182
185
|
const testFileRegex = new RegExp(testFilePattern);
|
|
183
186
|
const isTestFile = allowInTests && testFileRegex.test(filename);
|
|
184
|
-
const sourceCode = context.sourceCode
|
|
187
|
+
const sourceCode = context.sourceCode;
|
|
185
188
|
/**
|
|
186
189
|
* Find variable declaration for an identifier
|
|
187
190
|
*/
|
|
@@ -15,9 +15,12 @@ const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
|
15
15
|
exports.noPiiInLogs = (0, eslint_devkit_1.createRule)({
|
|
16
16
|
name: 'no-pii-in-logs',
|
|
17
17
|
meta: {
|
|
18
|
-
type: '
|
|
18
|
+
type: 'suggestion',
|
|
19
19
|
docs: {
|
|
20
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-pii-in-logs.md',
|
|
20
21
|
description: 'Prevent PII (email, SSN, credit cards) in console logs',
|
|
22
|
+
cwe: 'CWE-359',
|
|
23
|
+
cvss: 7.5,
|
|
21
24
|
},
|
|
22
25
|
messages: {
|
|
23
26
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -108,7 +108,10 @@ exports.noPrivilegeEscalation = (0, eslint_devkit_2.createRule)({
|
|
|
108
108
|
meta: {
|
|
109
109
|
type: 'problem',
|
|
110
110
|
docs: {
|
|
111
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-privilege-escalation.md',
|
|
111
112
|
description: 'Detects potential privilege escalation vulnerabilities',
|
|
113
|
+
cwe: 'CWE-269',
|
|
114
|
+
cvss: 7.5,
|
|
112
115
|
},
|
|
113
116
|
hasSuggestions: true,
|
|
114
117
|
messages: {
|
|
@@ -178,10 +181,10 @@ exports.noPrivilegeEscalation = (0, eslint_devkit_2.createRule)({
|
|
|
178
181
|
],
|
|
179
182
|
create(context, [options = {}]) {
|
|
180
183
|
const { allowInTests = false, testFilePattern = '\\.(test|spec)\\.(ts|tsx|js|jsx)$', roleCheckPatterns = DEFAULT_ROLE_CHECK_PATTERNS, userInputPatterns: additionalUserInputPatterns = [], ignorePatterns = [], } = options;
|
|
181
|
-
const filename = context.
|
|
184
|
+
const filename = context.filename;
|
|
182
185
|
const testFileRegex = new RegExp(testFilePattern);
|
|
183
186
|
const isTestFile = allowInTests && testFileRegex.test(filename);
|
|
184
|
-
const sourceCode = context.sourceCode
|
|
187
|
+
const sourceCode = context.sourceCode;
|
|
185
188
|
// Combine default and additional user input patterns
|
|
186
189
|
const userInputPatterns = [
|
|
187
190
|
...DEFAULT_USER_INPUT_PATTERNS,
|
|
@@ -8,6 +8,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.noRedosVulnerableRegex = void 0;
|
|
9
9
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
10
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
const regexpp_1 = require("@eslint-community/regexpp");
|
|
12
|
+
const scslre_1 = require("scslre");
|
|
13
|
+
// Module-level parser; cheap to reuse.
|
|
14
|
+
const REGEXPP_PARSER = new regexpp_1.RegExpParser();
|
|
11
15
|
// Type guard for regex literal nodes
|
|
12
16
|
const isRegExpLiteral = (node) => {
|
|
13
17
|
return node.type === 'Literal' && Object.prototype.hasOwnProperty.call(node, 'regex');
|
|
@@ -127,7 +131,9 @@ exports.noRedosVulnerableRegex = (0, eslint_devkit_2.createRule)({
|
|
|
127
131
|
meta: {
|
|
128
132
|
type: 'problem',
|
|
129
133
|
docs: {
|
|
134
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-redos-vulnerable-regex.md',
|
|
130
135
|
description: 'Detects ReDoS-vulnerable regex patterns in literal regex patterns',
|
|
136
|
+
cwe: 'CWE-400',
|
|
131
137
|
},
|
|
132
138
|
hasSuggestions: true,
|
|
133
139
|
messages: {
|
|
@@ -201,6 +207,43 @@ exports.noRedosVulnerableRegex = (0, eslint_devkit_2.createRule)({
|
|
|
201
207
|
],
|
|
202
208
|
create(context, [options = {}]) {
|
|
203
209
|
const { allowCommonPatterns = false, maxPatternLength = 500 } = options || {};
|
|
210
|
+
/**
|
|
211
|
+
* NFA-based ReDoS detection via scslre — the same library used by
|
|
212
|
+
* eslint-plugin-regexp. Catches patterns that the heuristic regex
|
|
213
|
+
* pattern matching can't see (cross-quantifier "trade" issues, deep
|
|
214
|
+
* nested loops, etc.). Heuristic detection runs as a fallback for
|
|
215
|
+
* cases where parsing fails or the pattern is exotic.
|
|
216
|
+
*/
|
|
217
|
+
function checkWithScslre(node, pattern, flags) {
|
|
218
|
+
try {
|
|
219
|
+
const ast = REGEXPP_PARSER.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes('u'), unicodeSets: flags.includes('v') });
|
|
220
|
+
const result = (0, scslre_1.analyse)({ pattern: ast, flags: { ignoreCase: flags.includes('i'), unicode: flags.includes('u'), dotAll: flags.includes('s'), multiline: flags.includes('m') } }, { reportTypes: { Move: false } });
|
|
221
|
+
if (result.reports.length === 0)
|
|
222
|
+
return false;
|
|
223
|
+
for (const report of result.reports) {
|
|
224
|
+
const isExp = report.exponential;
|
|
225
|
+
context.report({
|
|
226
|
+
node,
|
|
227
|
+
messageId: 'redosVulnerable',
|
|
228
|
+
data: {
|
|
229
|
+
vulnerabilityName: report.type === 'Self'
|
|
230
|
+
? `Self-loop quantifier (${isExp ? 'exponential' : 'polynomial'} backtracking)`
|
|
231
|
+
: `Cross-quantifier trade (${isExp ? 'exponential' : 'polynomial'} backtracking)`,
|
|
232
|
+
description: report.type === 'Self'
|
|
233
|
+
? `A quantifier reaches itself via the parent loop. An attacker can craft input that triggers ${isExp ? 'exponential' : 'polynomial'} backtracking.`
|
|
234
|
+
: `Two quantifiers can exchange characters, enabling ${isExp ? 'exponential' : 'polynomial'} backtracking on crafted input.`,
|
|
235
|
+
severity: isExp ? 'CRITICAL' : 'HIGH',
|
|
236
|
+
fix: 'Atomic group, possessive quantifier, or rewrite to eliminate the ambiguity. The scslre auto-suggested fix may be available.',
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
// Fall through to heuristic check
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
204
247
|
/**
|
|
205
248
|
* Check literal regex patterns for ReDoS vulnerabilities
|
|
206
249
|
*/
|
|
@@ -209,10 +252,15 @@ exports.noRedosVulnerableRegex = (0, eslint_devkit_2.createRule)({
|
|
|
209
252
|
return;
|
|
210
253
|
}
|
|
211
254
|
const pattern = node.regex.pattern;
|
|
255
|
+
const flags = node.regex.flags || '';
|
|
212
256
|
// Skip if pattern is too long (performance)
|
|
213
257
|
if (pattern.length > maxPatternLength) {
|
|
214
258
|
return;
|
|
215
259
|
}
|
|
260
|
+
// NFA-based detection first — catches what heuristics miss.
|
|
261
|
+
if (checkWithScslre(node, pattern, flags)) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
216
264
|
const vulnerability = hasReDoSVulnerability(pattern);
|
|
217
265
|
if (!vulnerability) {
|
|
218
266
|
return;
|
|
@@ -265,6 +313,37 @@ exports.noRedosVulnerableRegex = (0, eslint_devkit_2.createRule)({
|
|
|
265
313
|
return;
|
|
266
314
|
}
|
|
267
315
|
const firstArg = node.arguments[0];
|
|
316
|
+
// Template literal with interpolation — runtime-built pattern.
|
|
317
|
+
// Closes the audit FN where `new RegExp(\`^(\${pattern}+)+$\`)` was
|
|
318
|
+
// bypassing detection. We can't fully analyse the resulting regex
|
|
319
|
+
// (the interpolated parts are user-controlled), but we CAN detect
|
|
320
|
+
// the nested-quantifier signature `+)+`/`+)*`/`*)+`/`*)*` in the
|
|
321
|
+
// static template text — that shape is catastrophic regardless of
|
|
322
|
+
// what the interpolation injects. See benchmarks/AUDIT_PATTERNS.md
|
|
323
|
+
// §3.5 ("Runtime-built patterns").
|
|
324
|
+
if (firstArg.type === 'TemplateLiteral' && firstArg.expressions.length > 0) {
|
|
325
|
+
const concatenated = firstArg.quasis
|
|
326
|
+
.map((q) => q.value.cooked ?? q.value.raw)
|
|
327
|
+
.join(''); // sentinel between static parts
|
|
328
|
+
// Look for the nested-quantifier signature on either side of an
|
|
329
|
+
// interpolation: `+)+`, `+)*`, `*)+`, `*)*`, `})+` etc.
|
|
330
|
+
const hasNestedQuantifier = /[+*}](?:\)\s*[+*]|\)\s*\{\s*\d+\s*,?\s*\d*\s*\}|\s*[+*])/.test(concatenated) ||
|
|
331
|
+
/[+*].*\)[+*]/.test(concatenated) ||
|
|
332
|
+
/\([+*]\)[+*]/.test(concatenated);
|
|
333
|
+
if (hasNestedQuantifier) {
|
|
334
|
+
context.report({
|
|
335
|
+
node,
|
|
336
|
+
messageId: 'redosVulnerable',
|
|
337
|
+
data: {
|
|
338
|
+
vulnerabilityName: 'Runtime-built nested quantifier',
|
|
339
|
+
description: 'Template-literal pattern interpolation places user input inside a nested-quantifier shape (`(...+)+` / `(...*)*`); this is catastrophic regardless of what the interpolation injects.',
|
|
340
|
+
severity: 'HIGH',
|
|
341
|
+
fix: 'Avoid runtime-built nested quantifiers. Build the pattern at module load time and validate; or constrain the interpolated section to a non-quantifier region.',
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
268
347
|
if (firstArg.type !== 'Literal' || typeof firstArg.value !== 'string') {
|
|
269
348
|
/* c8 ignore next */
|
|
270
349
|
return;
|
|
@@ -30,9 +30,12 @@ function containsSensitiveData(text, patterns) {
|
|
|
30
30
|
exports.noSensitiveDataExposure = (0, eslint_devkit_2.createRule)({
|
|
31
31
|
name: 'no-sensitive-data-exposure',
|
|
32
32
|
meta: {
|
|
33
|
-
type: '
|
|
33
|
+
type: 'suggestion',
|
|
34
34
|
docs: {
|
|
35
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-sensitive-data-exposure.md',
|
|
35
36
|
description: 'Detects PII/credentials in logs, responses, or error messages',
|
|
37
|
+
cwe: 'CWE-532',
|
|
38
|
+
cvss: 7.5,
|
|
36
39
|
},
|
|
37
40
|
hasSuggestions: true,
|
|
38
41
|
messages: {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* ESLint Rule: no-template-injection
|
|
7
|
+
* CWE-94: Improper Control of Generation of Code (Code Injection via Templates)
|
|
8
|
+
*
|
|
9
|
+
* Detects server-side template engine calls where the template argument is
|
|
10
|
+
* dynamic (not a string literal). An attacker who controls the template
|
|
11
|
+
* string can execute arbitrary server-side code.
|
|
12
|
+
*
|
|
13
|
+
* Detection: structural-api.
|
|
14
|
+
* Handlebars.compile(userInput) — fires (dynamic first arg)
|
|
15
|
+
* Handlebars.compile('<h1>{{t}}</h1>') — silent (string literal)
|
|
16
|
+
*
|
|
17
|
+
* Covered engines: Handlebars, EJS, Pug/Jade, Mustache, Nunjucks, Swig,
|
|
18
|
+
* Dust, doT, and their common aliases.
|
|
19
|
+
*/
|
|
20
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
21
|
+
export declare const noTemplateInjection: TSESLint.RuleModule<"templateInjection", [], unknown, TSESLint.RuleListener> & {
|
|
22
|
+
name: string;
|
|
23
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.noTemplateInjection = void 0;
|
|
8
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
9
|
+
const TEMPLATE_COMPILE_METHODS = new Set([
|
|
10
|
+
'compile', 'precompile', 'create', 'parse', 'template',
|
|
11
|
+
]);
|
|
12
|
+
const TEMPLATE_RENDER_METHODS = new Set([
|
|
13
|
+
'render', 'renderFile', 'renderString', 'renderToString', 'renderTemplate',
|
|
14
|
+
]);
|
|
15
|
+
const ALL_TEMPLATE_METHODS = new Set([
|
|
16
|
+
...TEMPLATE_COMPILE_METHODS,
|
|
17
|
+
...TEMPLATE_RENDER_METHODS,
|
|
18
|
+
]);
|
|
19
|
+
const TEMPLATE_ENGINE_OBJECTS = new Set([
|
|
20
|
+
'Handlebars', 'handlebars',
|
|
21
|
+
'ejs',
|
|
22
|
+
'pug', 'jade',
|
|
23
|
+
'mustache', 'Mustache',
|
|
24
|
+
'nunjucks',
|
|
25
|
+
'swig',
|
|
26
|
+
'dust', 'Dust',
|
|
27
|
+
'doT',
|
|
28
|
+
'consolidate',
|
|
29
|
+
]);
|
|
30
|
+
exports.noTemplateInjection = (0, eslint_devkit_1.createRule)({
|
|
31
|
+
name: 'no-template-injection',
|
|
32
|
+
meta: {
|
|
33
|
+
type: 'problem',
|
|
34
|
+
docs: {
|
|
35
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-template-injection.md',
|
|
36
|
+
description: 'Disallow dynamic strings as template arguments to server-side template engines (CWE-94)',
|
|
37
|
+
cwe: 'CWE-94',
|
|
38
|
+
cvss: 9.1,
|
|
39
|
+
},
|
|
40
|
+
messages: {
|
|
41
|
+
templateInjection: (0, eslint_devkit_1.formatLLMMessage)({
|
|
42
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
43
|
+
issueName: 'Template Injection (CWE-94)',
|
|
44
|
+
cwe: 'CWE-94',
|
|
45
|
+
description: '{{engine}}.{{method}}() receives a dynamic string. An attacker who controls the template can execute arbitrary server-side code.',
|
|
46
|
+
severity: 'CRITICAL',
|
|
47
|
+
fix: 'Pass only string literals as templates. If the template must vary, load it from a trusted file system path, never from user input.',
|
|
48
|
+
documentationLink: 'https://portswigger.net/web-security/server-side-template-injection',
|
|
49
|
+
}),
|
|
50
|
+
},
|
|
51
|
+
schema: [],
|
|
52
|
+
},
|
|
53
|
+
defaultOptions: [],
|
|
54
|
+
create(context) {
|
|
55
|
+
return {
|
|
56
|
+
CallExpression(node) {
|
|
57
|
+
const { callee, arguments: args } = node;
|
|
58
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
59
|
+
return;
|
|
60
|
+
const { object, property } = callee;
|
|
61
|
+
if (object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
62
|
+
return;
|
|
63
|
+
if (property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
64
|
+
return;
|
|
65
|
+
const engineName = object.name;
|
|
66
|
+
const methodName = property.name;
|
|
67
|
+
if (!TEMPLATE_ENGINE_OBJECTS.has(engineName))
|
|
68
|
+
return;
|
|
69
|
+
if (!ALL_TEMPLATE_METHODS.has(methodName))
|
|
70
|
+
return;
|
|
71
|
+
const firstArg = args[0];
|
|
72
|
+
if (!firstArg)
|
|
73
|
+
return;
|
|
74
|
+
// String literal → safe (static template, no injection surface)
|
|
75
|
+
if (firstArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
76
|
+
typeof firstArg.value === 'string')
|
|
77
|
+
return;
|
|
78
|
+
// Template literal with NO expressions → safe (equivalent to a string literal)
|
|
79
|
+
if (firstArg.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral &&
|
|
80
|
+
firstArg.expressions.length === 0)
|
|
81
|
+
return;
|
|
82
|
+
context.report({
|
|
83
|
+
node: firstArg,
|
|
84
|
+
messageId: 'templateInjection',
|
|
85
|
+
data: { engine: engineName, method: methodName },
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
});
|
|
@@ -14,9 +14,10 @@ exports.noUncheckedLoopCondition = (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-unchecked-loop-condition.md',
|
|
17
18
|
description: 'Detects unchecked loop conditions that could cause DoS',
|
|
19
|
+
cwe: 'CWE-400',
|
|
18
20
|
},
|
|
19
|
-
fixable: 'code',
|
|
20
21
|
hasSuggestions: true,
|
|
21
22
|
messages: {
|
|
22
23
|
uncheckedLoopCondition: (0, eslint_devkit_2.formatLLMMessage)({
|
|
@@ -141,8 +142,8 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
|
|
|
141
142
|
create(context) {
|
|
142
143
|
const options = context.options[0] || {};
|
|
143
144
|
const { maxStaticIterations = 10000, userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data'], allowWhileTrueWithBreak = true, maxRecursionDepth = 10, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
144
|
-
const sourceCode = context.sourceCode
|
|
145
|
-
const filename = context.filename
|
|
145
|
+
const sourceCode = context.sourceCode;
|
|
146
|
+
const filename = context.filename;
|
|
146
147
|
// Create safety checker for false positive detection
|
|
147
148
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
148
149
|
trustedSanitizers,
|
|
@@ -322,6 +323,7 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
|
|
|
322
323
|
/**
|
|
323
324
|
* Estimate loop iterations from static analysis
|
|
324
325
|
*/
|
|
326
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
325
327
|
const estimateIterations = (loop) => {
|
|
326
328
|
if (loop.type === 'ForStatement') {
|
|
327
329
|
// Try to parse for loop bounds
|
|
@@ -14,9 +14,10 @@ exports.noUnlimitedResourceAllocation = (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-unlimited-resource-allocation.md',
|
|
17
18
|
description: 'Detects unlimited resource allocation that could cause DoS',
|
|
19
|
+
cwe: 'CWE-770',
|
|
18
20
|
},
|
|
19
|
-
fixable: 'code',
|
|
20
21
|
hasSuggestions: true,
|
|
21
22
|
messages: {
|
|
22
23
|
unlimitedResourceAllocation: (0, eslint_devkit_2.formatLLMMessage)({
|
|
@@ -199,8 +200,8 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
199
200
|
create(context) {
|
|
200
201
|
const options = context.options[0] || {};
|
|
201
202
|
const { maxResourceSize = 1048576, userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data'], safeResourceFunctions = ['validateSize', 'checkLimits', 'limitResource', 'safeAlloc'], requireResourceValidation = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
202
|
-
const sourceCode = context.sourceCode
|
|
203
|
-
const filename = context.filename
|
|
203
|
+
const sourceCode = context.sourceCode;
|
|
204
|
+
const filename = context.filename;
|
|
204
205
|
// Create safety checker for false positive detection
|
|
205
206
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
206
207
|
trustedSanitizers,
|
|
@@ -502,35 +503,46 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
502
503
|
/* c8 ignore stop */
|
|
503
504
|
// Check for resource allocation inside loops
|
|
504
505
|
if ((0, eslint_devkit_3.isInsideLoop)(node)) {
|
|
505
|
-
const calleeText = sourceCode.getText(callee);
|
|
506
506
|
// Check if this allocates resources
|
|
507
507
|
if (calleeText.includes('alloc') ||
|
|
508
508
|
calleeText.includes('Array') ||
|
|
509
509
|
calleeText.includes('Buffer') ||
|
|
510
510
|
calleeText.includes('readFile') ||
|
|
511
511
|
calleeText.includes('writeFile')) {
|
|
512
|
-
|
|
513
|
-
if (
|
|
514
|
-
|
|
512
|
+
// SAFE: Array.isArray / Array.from / Array.of are not allocation hazards.
|
|
513
|
+
if (calleeText === 'Array.isArray' ||
|
|
514
|
+
calleeText === 'Array.from' ||
|
|
515
|
+
calleeText === 'Array.of') {
|
|
516
|
+
// Array.from / Array.of are fine; allocation comes from their args, not size
|
|
515
517
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
//
|
|
523
|
-
|
|
518
|
+
else {
|
|
519
|
+
/* c8 ignore start -- safetyChecker requires JSDoc annotations not testable via RuleTester */
|
|
520
|
+
if (safetyChecker.isSafe(node, context)) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
/* c8 ignore stop */
|
|
524
|
+
// SAFE: first arg is a numeric literal — size is statically bounded.
|
|
525
|
+
const firstArg = node.arguments[0];
|
|
526
|
+
if (firstArg?.type === 'Literal' && typeof firstArg.value === 'number') {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
// Skip if this is an assignment to an array element (pre-allocated pattern)
|
|
530
|
+
const parent = node.parent;
|
|
531
|
+
if (parent && parent.type === 'AssignmentExpression' &&
|
|
532
|
+
parent.left.type === 'MemberExpression' &&
|
|
533
|
+
parent.left.object.type === 'Identifier') {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
// Report resourceAllocationInLoop - this can be in addition to user input errors
|
|
537
|
+
context.report({
|
|
538
|
+
node,
|
|
539
|
+
messageId: 'resourceAllocationInLoop',
|
|
540
|
+
data: {
|
|
541
|
+
filePath: filename,
|
|
542
|
+
line: String(node.loc?.start.line ?? 0),
|
|
543
|
+
},
|
|
544
|
+
});
|
|
524
545
|
}
|
|
525
|
-
// Report resourceAllocationInLoop - this can be in addition to user input errors
|
|
526
|
-
context.report({
|
|
527
|
-
node,
|
|
528
|
-
messageId: 'resourceAllocationInLoop',
|
|
529
|
-
data: {
|
|
530
|
-
filePath: filename,
|
|
531
|
-
line: String(node.loc?.start.line ?? 0),
|
|
532
|
-
},
|
|
533
|
-
});
|
|
534
546
|
}
|
|
535
547
|
}
|
|
536
548
|
},
|
|
@@ -621,17 +633,21 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
621
633
|
}
|
|
622
634
|
// Check for resource allocation inside loops
|
|
623
635
|
if ((0, eslint_devkit_3.isInsideLoop)(node)) {
|
|
624
|
-
const
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
calleeText.includes('Set')) {
|
|
636
|
+
const newCalleeText = sourceCode.getText(callee);
|
|
637
|
+
if (newCalleeText.includes('Buffer') ||
|
|
638
|
+
newCalleeText.includes('Array') ||
|
|
639
|
+
newCalleeText.includes('Map') ||
|
|
640
|
+
newCalleeText.includes('Set')) {
|
|
630
641
|
/* c8 ignore start -- safetyChecker requires JSDoc annotations not testable via RuleTester */
|
|
631
642
|
if (safetyChecker.isSafe(node, context)) {
|
|
632
643
|
return;
|
|
633
644
|
}
|
|
634
645
|
/* c8 ignore stop */
|
|
646
|
+
// SAFE: first arg is a numeric literal — size is statically bounded.
|
|
647
|
+
const firstArg = node.arguments[0];
|
|
648
|
+
if (firstArg?.type === 'Literal' && typeof firstArg.value === 'number') {
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
635
651
|
context.report({
|
|
636
652
|
node,
|
|
637
653
|
messageId: 'resourceAllocationInLoop',
|