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.
Files changed (35) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/README.md +128 -65
  3. package/package.json +23 -4
  4. package/src/index.js +70 -12
  5. package/src/oxlint.d.ts +20 -0
  6. package/src/oxlint.js +21 -0
  7. package/src/rules/detect-non-literal-regexp/index.js +9 -1
  8. package/src/rules/detect-object-injection/index.js +344 -6
  9. package/src/rules/detect-weak-password-validation/index.js +4 -1
  10. package/src/rules/no-directive-injection/index.js +7 -4
  11. package/src/rules/no-electron-security-issues/index.js +6 -2
  12. package/src/rules/no-format-string-injection/index.js +32 -23
  13. package/src/rules/no-graphql-injection/index.js +8 -3
  14. package/src/rules/no-hardcoded-credentials/index.js +366 -52
  15. package/src/rules/no-hardcoded-session-tokens/index.js +3 -0
  16. package/src/rules/no-improper-sanitization/index.js +31 -4
  17. package/src/rules/no-improper-type-validation/index.js +6 -4
  18. package/src/rules/no-insecure-comparison/index.js +39 -3
  19. package/src/rules/no-ldap-injection/index.js +6 -3
  20. package/src/rules/no-missing-authentication/index.js +5 -2
  21. package/src/rules/no-pii-in-logs/index.js +4 -1
  22. package/src/rules/no-privilege-escalation/index.js +5 -2
  23. package/src/rules/no-redos-vulnerable-regex/index.js +79 -0
  24. package/src/rules/no-sensitive-data-exposure/index.js +4 -1
  25. package/src/rules/no-template-injection/index.d.ts +23 -0
  26. package/src/rules/no-template-injection/index.js +90 -0
  27. package/src/rules/no-unchecked-loop-condition/index.js +5 -3
  28. package/src/rules/no-unlimited-resource-allocation/index.js +46 -30
  29. package/src/rules/no-unsafe-deserialization/index.js +22 -5
  30. package/src/rules/no-unsafe-regex-construction/index.js +10 -3
  31. package/src/rules/no-weak-password-recovery/index.js +5 -3
  32. package/src/rules/no-xpath-injection/index.js +6 -3
  33. package/src/rules/no-xxe-injection/index.js +48 -46
  34. package/src/rules/require-backend-authorization/index.js +6 -3
  35. package/src/rules/require-secure-defaults/index.js +3 -0
@@ -14,7 +14,9 @@ exports.noUnsafeDeserialization = (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-unsafe-deserialization.md',
17
18
  description: 'Detects unsafe deserialization of untrusted data',
19
+ cwe: 'CWE-502',
18
20
  },
19
21
  fixable: 'code',
20
22
  hasSuggestions: true,
@@ -167,8 +169,8 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
167
169
  create(context) {
168
170
  const options = context.options[0] || {};
169
171
  const { dangerousFunctions = ['eval', 'Function', 'setTimeout', 'setInterval', 'unserialize', 'deserialize', 'parseUnsafe'], validationFunctions = ['validateInput', 'sanitizeData', 'checkSchema', 'validateSchema'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
170
- const sourceCode = context.sourceCode || context.sourceCode;
171
- const filename = context.filename || context.getFilename();
172
+ const sourceCode = context.sourceCode;
173
+ const filename = context.filename;
172
174
  // Create safety checker for false positive detection
173
175
  const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
174
176
  trustedSanitizers,
@@ -180,6 +182,10 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
180
182
  const validatedVariables = new Set();
181
183
  // Track variables that contain untrusted data
182
184
  const untrustedVariables = new Set();
185
+ // Variables read from a literal-string file path (e.g. fs.readFileSync('config.json')).
186
+ // These are still "untrusted" for eval/Function but safe for JSON.parse and other
187
+ // schema-validating parsers because the file content is statically bundled.
188
+ const literalPathFileVars = new Set();
183
189
  /**
184
190
  * Check if this is a dangerous deserialization function
185
191
  */
@@ -277,6 +283,7 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
277
283
  /**
278
284
  * Check if this is a safe deserialization library
279
285
  */
286
+ // oxlint-disable-next-line consistent-function-scoping
280
287
  const isSafeLibrary = (node) => {
281
288
  const callee = node.callee;
282
289
  if (callee.type === 'MemberExpression') {
@@ -309,7 +316,7 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
309
316
  node,
310
317
  messageId: 'dangerousFunctionConstructor',
311
318
  data: {
312
- filePath: context.getFilename(),
319
+ filePath: context.filename,
313
320
  line: String(node.loc?.start.line ?? 0),
314
321
  severity: 'HIGH',
315
322
  safeAlternative: 'Avoid dynamic function creation',
@@ -324,7 +331,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
324
331
  const hasUntrustedInput = args.some((arg) => isUntrustedInput(arg));
325
332
  // Check if explicit validation is present
326
333
  const isSafe = isSafeLibrary(node);
327
- const filename = context.getFilename();
328
334
  if (!isSafe && hasUntrustedInput) {
329
335
  // Basic safety check
330
336
  const safe = safetyChecker.isSafe(node, context);
@@ -368,6 +374,11 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
368
374
  if (arg.type === 'Identifier' && validatedVariables.has(arg.name)) {
369
375
  return false;
370
376
  }
377
+ // SAFE for JSON.parse/schema parsers: if the input came from a
378
+ // literal-path file read (bundled config), it is not user-controlled.
379
+ if (arg.type === 'Identifier' && literalPathFileVars.has(arg.name)) {
380
+ return false;
381
+ }
371
382
  return isUntrustedInput(arg) && !isInputValidated(arg);
372
383
  });
373
384
  if (hasUntrustedInput) {
@@ -381,7 +392,7 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
381
392
  node,
382
393
  messageId: 'untrustedDeserializationInput',
383
394
  data: {
384
- filePath: context.getFilename(),
395
+ filePath: context.filename,
385
396
  line: String(node.loc?.start.line ?? 0),
386
397
  },
387
398
  suggest: [
@@ -412,6 +423,12 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
412
423
  callee.property.type === 'Identifier' &&
413
424
  ['readFile', 'readFileSync'].includes(callee.property.name)) {
414
425
  untrustedVariables.add(declarator.id.name);
426
+ // Track whether the path is a literal — used downstream to skip
427
+ // safe deserializers (JSON.parse) on known-static files.
428
+ const pathArg = declarator.init.arguments[0];
429
+ if (pathArg?.type === 'Literal' && typeof pathArg.value === 'string') {
430
+ literalPathFileVars.add(declarator.id.name);
431
+ }
415
432
  }
416
433
  }
417
434
  }
@@ -48,11 +48,14 @@ function isEscaped(node, trustedFunctions, sourceCode) {
48
48
  return true;
49
49
  }
50
50
  }
51
- // Also check if it's wrapped in a trusted function call (for complex cases)
51
+ // Also check if it's wrapped in a trusted function call (for complex cases).
52
+ // `current` is only assigned from `node` (truthy on entry) or `parent` after
53
+ // an explicit `if (!parent) break;`, so it's never null at the loop check
54
+ // (CodeQL: `js/useless-conditional` on the `current &&` test).
52
55
  let current = node;
53
56
  let depth = 0;
54
57
  const maxDepth = 5; // Prevent infinite loops
55
- while (current && depth < maxDepth) {
58
+ while (depth < maxDepth) {
56
59
  const parent = sourceCode.getNodeByRangeIndex?.(current.range[0] - 1) ||
57
60
  current.parent;
58
61
  if (!parent)
@@ -105,7 +108,10 @@ exports.noUnsafeRegexConstruction = (0, eslint_devkit_2.createRule)({
105
108
  meta: {
106
109
  type: 'problem',
107
110
  docs: {
111
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-unsafe-regex-construction.md',
108
112
  description: 'Detects unsafe regex construction patterns (user input without escaping, dynamic flags)',
113
+ cwe: 'CWE-400',
114
+ cvss: 7.5,
109
115
  },
110
116
  hasSuggestions: true,
111
117
  messages: {
@@ -123,6 +129,7 @@ exports.noUnsafeRegexConstruction = (0, eslint_devkit_2.createRule)({
123
129
  issueName: 'Escape User Input',
124
130
  description: 'Escape user input for regex',
125
131
  severity: 'LOW',
132
+ // oxlint-disable-next-line no-template-curly-in-string
126
133
  fix: 'input.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&")',
127
134
  documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping',
128
135
  }),
@@ -186,7 +193,7 @@ exports.noUnsafeRegexConstruction = (0, eslint_devkit_2.createRule)({
186
193
  ],
187
194
  create(context, [options = {}]) {
188
195
  const { allowLiterals = true, maxPatternLength = 100, trustedEscapingFunctions = ['escapeRegex', 'escape', 'sanitize'], } = options || {};
189
- const sourceCode = context.sourceCode || context.sourceCode;
196
+ const sourceCode = context.sourceCode;
190
197
  /**
191
198
  * Check RegExp constructor calls
192
199
  */
@@ -14,9 +14,10 @@ exports.noWeakPasswordRecovery = (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-weak-password-recovery.md',
17
18
  description: 'Detects weak password recovery mechanisms',
19
+ cwe: 'CWE-640',
18
20
  },
19
- fixable: 'code',
20
21
  hasSuggestions: true,
21
22
  messages: {
22
23
  weakPasswordRecovery: (0, eslint_devkit_2.formatLLMMessage)({
@@ -217,8 +218,8 @@ exports.noWeakPasswordRecovery = (0, eslint_devkit_1.createRule)({
217
218
  create(context) {
218
219
  const options = context.options[0] || {};
219
220
  const { secureTokenFunctions = ['crypto.randomBytes', 'crypto.randomUUID', 'randomBytes', 'generateSecureToken'], trustedSanitizers = [], trustedAnnotations = ['secure-recovery', 'rate-limited'], strictMode = false, } = options;
220
- const sourceCode = context.sourceCode || context.sourceCode;
221
- const filename = context.filename || context.getFilename();
221
+ const sourceCode = context.sourceCode;
222
+ const filename = context.filename;
222
223
  // Create safety checker for false positive detection
223
224
  const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
224
225
  trustedSanitizers,
@@ -233,6 +234,7 @@ exports.noWeakPasswordRecovery = (0, eslint_devkit_1.createRule)({
233
234
  * 1. Have password/forgot in the name
234
235
  * 2. AND have reset/recovery/forgot in the name
235
236
  */
237
+ // oxlint-disable-next-line consistent-function-scoping
236
238
  const isRecoveryRelated = (text) => {
237
239
  const lowerText = text.toLowerCase();
238
240
  // Require BOTH a password keyword AND a recovery action keyword
@@ -14,9 +14,10 @@ exports.noXpathInjection = (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-xpath-injection.md',
17
18
  description: 'Detects XPath injection vulnerabilities',
19
+ cwe: 'CWE-643',
18
20
  },
19
- fixable: 'code',
20
21
  hasSuggestions: true,
21
22
  messages: {
22
23
  xpathInjection: (0, eslint_devkit_2.formatLLMMessage)({
@@ -158,8 +159,8 @@ exports.noXpathInjection = (0, eslint_devkit_1.createRule)({
158
159
  create(context) {
159
160
  const options = context.options[0] || {};
160
161
  const { xpathFunctions = ['evaluate', 'selectSingleNode', 'selectNodes', 'xpath', 'select'], safeXpathConstructors = ['buildXPath', 'createXPath', 'safeXPath', 'xpathBuilder'], xpathValidationFunctions = ['validateXPath', 'escapeXPath', 'sanitizeXPath', 'cleanXPath'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
161
- const sourceCode = context.sourceCode || context.sourceCode;
162
- const filename = context.filename || context.getFilename();
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,
@@ -189,6 +190,7 @@ exports.noXpathInjection = (0, eslint_devkit_1.createRule)({
189
190
  /**
190
191
  * Check if XPath expression contains dangerous patterns
191
192
  */
193
+ // oxlint-disable-next-line consistent-function-scoping
192
194
  const containsDangerousXpath = (xpathText) => {
193
195
  // Dangerous XPath patterns that allow traversal or injection
194
196
  const dangerousPatterns = [
@@ -205,6 +207,7 @@ exports.noXpathInjection = (0, eslint_devkit_1.createRule)({
205
207
  /**
206
208
  * Check if string contains XPath interpolation
207
209
  */
210
+ // oxlint-disable-next-line consistent-function-scoping
208
211
  const containsXpathInterpolation = (text) => {
209
212
  return /\$\{[^}]+\}/.test(text) || /'[^']*\+[^+]*'/.test(text) || /"[^"]*\+[^+]*"/.test(text);
210
213
  };
@@ -8,6 +8,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.noXxeInjection = void 0;
9
9
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
10
  const eslint_devkit_2 = require("@interlace/eslint-devkit");
11
+ /**
12
+ * Check if this is an XML parsing operation
13
+ */
14
+ const isXmlParsingCall = (node) => {
15
+ const callee = node.callee;
16
+ // Check for XML library method calls
17
+ if (callee.type === 'MemberExpression' &&
18
+ callee.property.type === 'Identifier' &&
19
+ ['parse', 'parseFromString', 'parseString', 'parseXmlString', 'parseXML'].includes(callee.property.name)) {
20
+ return true;
21
+ }
22
+ // Check for constructor calls
23
+ if (callee.type === 'Identifier' &&
24
+ ['DOMParser', 'XMLHttpRequest', 'ActiveXObject'].includes(callee.name)) {
25
+ return true;
26
+ }
27
+ return false;
28
+ };
29
+ /**
30
+ * Check if parser options enable dangerous features
31
+ */
32
+ const hasDangerousParserOptions = (optionsNode) => {
33
+ if (optionsNode.type !== 'ObjectExpression') {
34
+ return false;
35
+ }
36
+ // Check for dangerous options
37
+ for (const prop of optionsNode.properties) {
38
+ if (prop.type === 'Property' &&
39
+ prop.key.type === 'Identifier' &&
40
+ ['resolveExternals', 'expandEntityReferences', 'noent'].includes(prop.key.name)) {
41
+ // Check if the value enables dangerous features
42
+ if (prop.value.type === 'Literal' && prop.value.value === true) {
43
+ return true;
44
+ }
45
+ }
46
+ }
47
+ return false;
48
+ };
49
+ /**
50
+ * Check if XML contains dangerous entity declarations
51
+ */
52
+ const containsDangerousEntities = (xmlText) => {
53
+ return /<!ENTITY/i.test(xmlText) &&
54
+ /SYSTEM\s+["']/i.test(xmlText);
55
+ };
11
56
  exports.noXxeInjection = (0, eslint_devkit_1.createRule)({
12
57
  name: 'no-xxe-injection',
13
58
  meta: {
@@ -15,6 +60,8 @@ exports.noXxeInjection = (0, eslint_devkit_1.createRule)({
15
60
  docs: {
16
61
  description: 'Detect XML External Entity (XXE) injection vulnerabilities',
17
62
  url: 'https://cwe.mitre.org/data/definitions/611.html',
63
+ cwe: 'CWE-611',
64
+ cvss: 9.5,
18
65
  },
19
66
  messages: {
20
67
  xxeInjection: (0, eslint_devkit_2.formatLLMMessage)({
@@ -79,25 +126,7 @@ exports.noXxeInjection = (0, eslint_devkit_1.createRule)({
79
126
  ],
80
127
  create(context, [options]) {
81
128
  const { safeParserOptions = ['noent', 'resolveExternals', 'expandEntityReferences', 'entityResolver'], xmlValidationFunctions = ['validateXml', 'sanitizeXml', 'cleanXml', 'parseXmlSafe'], } = options || {};
82
- const filename = context.filename || context.getFilename();
83
- /**
84
- * Check if this is an XML parsing operation
85
- */
86
- const isXmlParsingCall = (node) => {
87
- const callee = node.callee;
88
- // Check for XML library method calls
89
- if (callee.type === 'MemberExpression' &&
90
- callee.property.type === 'Identifier' &&
91
- ['parse', 'parseFromString', 'parseString', 'parseXmlString', 'parseXML'].includes(callee.property.name)) {
92
- return true;
93
- }
94
- // Check for constructor calls
95
- if (callee.type === 'Identifier' &&
96
- ['DOMParser', 'XMLHttpRequest', 'ActiveXObject'].includes(callee.name)) {
97
- return true;
98
- }
99
- return false;
100
- };
129
+ const filename = context.filename;
101
130
  /**
102
131
  * Check if parser options are secure
103
132
  */
@@ -121,26 +150,6 @@ exports.noXxeInjection = (0, eslint_devkit_1.createRule)({
121
150
  }
122
151
  return false;
123
152
  };
124
- /**
125
- * Check if parser options enable dangerous features
126
- */
127
- const hasDangerousParserOptions = (optionsNode) => {
128
- if (optionsNode.type !== 'ObjectExpression') {
129
- return false;
130
- }
131
- // Check for dangerous options
132
- for (const prop of optionsNode.properties) {
133
- if (prop.type === 'Property' &&
134
- prop.key.type === 'Identifier' &&
135
- ['resolveExternals', 'expandEntityReferences', 'noent'].includes(prop.key.name)) {
136
- // Check if the value enables dangerous features
137
- if (prop.value.type === 'Literal' && prop.value.value === true) {
138
- return true;
139
- }
140
- }
141
- }
142
- return false;
143
- };
144
153
  /**
145
154
  * Check if input has been validated
146
155
  */
@@ -157,13 +166,6 @@ exports.noXxeInjection = (0, eslint_devkit_1.createRule)({
157
166
  }
158
167
  return false;
159
168
  };
160
- /**
161
- * Check if XML contains dangerous entity declarations
162
- */
163
- const containsDangerousEntities = (xmlText) => {
164
- return /<!ENTITY/i.test(xmlText) &&
165
- /SYSTEM\s+["']/i.test(xmlText);
166
- };
167
169
  /**
168
170
  * Check if input source is untrusted
169
171
  */
@@ -15,7 +15,10 @@ exports.requireBackendAuthorization = (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/require-backend-authorization.md',
18
19
  description: 'Require server-side authorization checks',
20
+ cwe: 'CWE-602',
21
+ cvss: 9.5,
19
22
  },
20
23
  messages: {
21
24
  violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
@@ -35,7 +38,7 @@ exports.requireBackendAuthorization = (0, eslint_devkit_1.createRule)({
35
38
  function report(node) {
36
39
  context.report({ node, messageId: 'violationDetected' });
37
40
  }
38
- const authProperties = ['role', 'isAdmin', 'isAuthenticated', 'permissions', 'admin'];
41
+ const authProperties = new Set(['role', 'isAdmin', 'isAuthenticated', 'permissions', 'admin']);
39
42
  return {
40
43
  IfStatement(node) {
41
44
  // Detect role-based access in client-side if statements
@@ -43,7 +46,7 @@ exports.requireBackendAuthorization = (0, eslint_devkit_1.createRule)({
43
46
  const checkMember = (expr) => {
44
47
  if (expr.type === 'MemberExpression' &&
45
48
  expr.property.type === 'Identifier' &&
46
- authProperties.includes(expr.property.name)) {
49
+ authProperties.has(expr.property.name)) {
47
50
  return true;
48
51
  }
49
52
  return false;
@@ -56,7 +59,7 @@ exports.requireBackendAuthorization = (0, eslint_devkit_1.createRule)({
56
59
  // Check for user.role or user.isAdmin access
57
60
  if (node.test.type === 'MemberExpression' &&
58
61
  node.test.property.type === 'Identifier' &&
59
- authProperties.includes(node.test.property.name)) {
62
+ authProperties.has(node.test.property.name)) {
60
63
  report(node);
61
64
  }
62
65
  },
@@ -17,7 +17,10 @@ exports.requireSecureDefaults = (0, eslint_devkit_1.createRule)({
17
17
  meta: {
18
18
  type: 'problem',
19
19
  docs: {
20
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/require-secure-defaults.md',
20
21
  description: 'Ensure secure default configurations',
22
+ cwe: 'CWE-1188',
23
+ cvss: 5,
21
24
  },
22
25
  messages: {
23
26
  violationDetected: (0, eslint_devkit_1.formatLLMMessage)({