eslint-plugin-secure-coding 3.3.4 → 3.4.1

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 (65) hide show
  1. package/README.md +4 -4
  2. package/package.json +30 -37
  3. package/src/index.d.ts +17 -23
  4. package/src/index.js +2 -113
  5. package/src/oxlint.d.ts +17 -20
  6. package/src/oxlint.js +0 -18
  7. package/src/rules/detect-non-literal-regexp/index.js +5 -79
  8. package/src/rules/detect-object-injection/index.js +2 -221
  9. package/src/rules/detect-weak-password-validation/index.js +0 -12
  10. package/src/rules/no-directive-injection/index.js +0 -33
  11. package/src/rules/no-electron-security-issues/index.js +0 -41
  12. package/src/rules/no-format-string-injection/index.js +0 -82
  13. package/src/rules/no-graphql-injection/index.js +86 -151
  14. package/src/rules/no-hardcoded-credentials/index.js +142 -184
  15. package/src/rules/no-hardcoded-session-tokens/index.js +0 -11
  16. package/src/rules/no-improper-sanitization/index.js +0 -44
  17. package/src/rules/no-improper-type-validation/index.js +1 -51
  18. package/src/rules/no-insecure-comparison/index.js +39 -35
  19. package/src/rules/no-ldap-injection/index.js +8 -61
  20. package/src/rules/no-missing-authentication/index.js +2 -56
  21. package/src/rules/no-pii-in-logs/index.js +0 -13
  22. package/src/rules/no-privilege-escalation/index.js +2 -49
  23. package/src/rules/no-redos-vulnerable-regex/index.js +3 -49
  24. package/src/rules/no-sensitive-data-exposure/index.js +5 -29
  25. package/src/rules/no-template-injection/index.js +0 -6
  26. package/src/rules/no-unchecked-loop-condition/index.js +3 -99
  27. package/src/rules/no-unlimited-resource-allocation/index.js +2 -75
  28. package/src/rules/no-unsafe-deserialization/index.js +55 -66
  29. package/src/rules/no-unsafe-regex-construction/index.js +22 -62
  30. package/src/rules/no-weak-password-recovery/index.js +0 -43
  31. package/src/rules/no-xpath-injection/index.js +13 -75
  32. package/src/rules/no-xxe-injection/index.js +1 -47
  33. package/src/rules/require-backend-authorization/index.js +0 -10
  34. package/src/rules/require-secure-defaults/index.js +0 -10
  35. package/src/types/index.js +0 -5
  36. package/AGENTS.md +0 -131
  37. package/CHANGELOG.md +0 -311
  38. package/src/rules/detect-non-literal-regexp/index.d.ts +0 -28
  39. package/src/rules/detect-object-injection/index.d.ts +0 -35
  40. package/src/rules/detect-weak-password-validation/index.d.ts +0 -12
  41. package/src/rules/no-directive-injection/index.d.ts +0 -38
  42. package/src/rules/no-electron-security-issues/index.d.ts +0 -36
  43. package/src/rules/no-format-string-injection/index.d.ts +0 -44
  44. package/src/rules/no-graphql-injection/index.d.ts +0 -47
  45. package/src/rules/no-hardcoded-credentials/index.d.ts +0 -44
  46. package/src/rules/no-hardcoded-session-tokens/index.d.ts +0 -12
  47. package/src/rules/no-improper-sanitization/index.d.ts +0 -38
  48. package/src/rules/no-improper-type-validation/index.d.ts +0 -36
  49. package/src/rules/no-insecure-comparison/index.d.ts +0 -26
  50. package/src/rules/no-ldap-injection/index.d.ts +0 -53
  51. package/src/rules/no-missing-authentication/index.d.ts +0 -32
  52. package/src/rules/no-pii-in-logs/index.d.ts +0 -12
  53. package/src/rules/no-privilege-escalation/index.d.ts +0 -32
  54. package/src/rules/no-redos-vulnerable-regex/index.d.ts +0 -28
  55. package/src/rules/no-sensitive-data-exposure/index.d.ts +0 -30
  56. package/src/rules/no-template-injection/index.d.ts +0 -23
  57. package/src/rules/no-unchecked-loop-condition/index.d.ts +0 -38
  58. package/src/rules/no-unlimited-resource-allocation/index.d.ts +0 -38
  59. package/src/rules/no-unsafe-deserialization/index.d.ts +0 -40
  60. package/src/rules/no-unsafe-regex-construction/index.d.ts +0 -30
  61. package/src/rules/no-weak-password-recovery/index.d.ts +0 -38
  62. package/src/rules/no-xpath-injection/index.d.ts +0 -39
  63. package/src/rules/no-xxe-injection/index.d.ts +0 -36
  64. package/src/rules/require-backend-authorization/index.d.ts +0 -12
  65. package/src/rules/require-secure-defaults/index.d.ts +0 -12
@@ -1,9 +1,4 @@
1
1
  "use strict";
2
- /**
3
- * Copyright (c) 2025 Ofri Peretz
4
- * Licensed under the MIT License. Use of this source code is governed by the
5
- * MIT license that can be found in the LICENSE file.
6
- */
7
2
  Object.defineProperty(exports, "__esModule", { value: true });
8
3
  exports.noUncheckedLoopCondition = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -144,96 +139,64 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
144
139
  const { maxStaticIterations = 10000, userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data'], allowWhileTrueWithBreak = true, maxRecursionDepth = 10, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
145
140
  const sourceCode = context.sourceCode;
146
141
  const filename = context.filename;
147
- // Create safety checker for false positive detection
148
142
  const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
149
143
  trustedSanitizers,
150
144
  trustedAnnotations,
151
145
  trustedOrmPatterns: [],
152
146
  strictMode,
153
147
  });
154
- // Track function calls to detect recursion
155
148
  const functionCalls = new Map();
156
149
  const reportedRecursion = new Set();
157
150
  const currentFunctionStack = [];
158
- // Track tainted variables (assigned from user input)
159
151
  const taintedVariables = new Set();
160
- /**
161
- * Check if a variable contains user input
162
- */
163
152
  const isUserInput = (varName) => {
164
- // Check if it's a tainted variable (assigned from user input)
165
153
  if (taintedVariables.has(varName)) {
166
154
  return true;
167
155
  }
168
- // Check if it's a known user input variable
169
156
  const lowerVarName = varName.toLowerCase();
170
- // If custom userInputVariables are specified (not the defaults), only use those
171
- const isUsingDefaults = userInputVariables.length === 7 && // default length
157
+ const isUsingDefaults = userInputVariables.length === 7 &&
172
158
  userInputVariables.includes('req') && userInputVariables.includes('input');
173
159
  if (!isUsingDefaults) {
174
- // Custom userInputVariables specified, only check those
175
160
  return userInputVariables.some(input => lowerVarName === input.toLowerCase());
176
161
  }
177
- // Using defaults, use broader matching
178
162
  return userInputVariables.some(input => lowerVarName.includes(input.toLowerCase()) ||
179
163
  lowerVarName === input.toLowerCase() ||
180
- // Handle cases like "userInput", "customInput", etc.
181
164
  lowerVarName.includes('input') ||
182
165
  lowerVarName.includes('data'));
183
166
  };
184
- /**
185
- * Check for complex DoS patterns in loop conditions
186
- */
187
167
  const checkComplexDoSPatterns = (condition) => {
188
168
  const conditionText = sourceCode.getText(condition);
189
- // Check for regex match patterns that could cause ReDoS
190
169
  if (conditionText.includes('.match(') || conditionText.includes('.test(')) {
191
170
  return true;
192
171
  }
193
- // Check for pagination patterns that could cause DoS
194
172
  if (conditionText.includes('page') && conditionText.includes('pageSize')) {
195
173
  return true;
196
174
  }
197
- // Check for complex arithmetic that could lead to overflow
198
175
  if (conditionText.includes('*') && (conditionText.includes('pageSize') || conditionText.includes('limit'))) {
199
176
  return true;
200
177
  }
201
178
  return false;
202
179
  };
203
- /**
204
- * Check for complex DoS patterns in variables used in loop conditions
205
- */
206
180
  const checkComplexDoSPatternsInScope = (condition) => {
207
181
  const conditionText = sourceCode.getText(condition);
208
- // Simple heuristic: check for variable names that suggest pagination
209
182
  if (conditionText.includes('endIndex') || conditionText.includes('startIndex')) {
210
183
  return true;
211
184
  }
212
185
  return false;
213
186
  };
214
- /**
215
- * Check if a collection is validated before iteration
216
- */
217
187
  const checkIfCollectionIsValidated = (forOfNode, collection) => {
218
188
  const collectionText = sourceCode.getText(collection);
219
- // Traverse up the AST to find if statements that validate this collection
220
189
  let current = forOfNode.parent;
221
190
  while (current) {
222
191
  if (current.type === 'IfStatement') {
223
192
  const test = current.test;
224
193
  const testText = sourceCode.getText(test);
225
- // Check for Array.isArray validation
226
194
  if (testText.includes('Array.isArray(') && testText.includes(collectionText)) {
227
- // Also check for length validation. Only `<`/`>` need checking:
228
- // any text containing `<=` or `>=` already contains `<`/`>` as a
229
- // substring, so those two extra disjuncts could never be the
230
- // deciding factor (dead code, removed rather than tested around).
231
195
  if (testText.includes('.length') && (testText.includes('<') || testText.includes('>'))) {
232
196
  return true;
233
197
  }
234
198
  }
235
199
  }
236
- // Stop at function boundaries
237
200
  if (current.type === 'FunctionDeclaration' || current.type === 'FunctionExpression' || current.type === 'ArrowFunctionExpression') {
238
201
  break;
239
202
  }
@@ -241,60 +204,45 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
241
204
  }
242
205
  return false;
243
206
  };
244
- /**
245
- * Check if an expression involves user input
246
- */
247
207
  const involvesUserInput = (expression) => {
248
208
  const expressionText = sourceCode.getText(expression).toLowerCase();
249
- // Check for user input variables in expression text (case-insensitive)
250
209
  if (userInputVariables.some(input => expressionText.includes(input.toLowerCase()))) {
251
210
  return true;
252
211
  }
253
- // Recursively check all parts of the expression
254
212
  const checkExpression = (node) => {
255
213
  if (node.type === 'MemberExpression') {
256
- // Check object part (e.g., req, request, body, query, params)
257
214
  const objectText = sourceCode.getText(node.object);
258
215
  if (isUserInput(objectText)) {
259
216
  return true;
260
217
  }
261
- // Recursively check nested member expressions
262
218
  return checkExpression(node.object);
263
219
  }
264
220
  if (node.type === 'Identifier') {
265
221
  return isUserInput(node.name);
266
222
  }
267
223
  if (node.type === 'CallExpression') {
268
- // Check for Math.min, parseInt, etc. with user input
269
224
  return (checkExpression(node.callee) ||
270
225
  node.arguments
271
226
  .filter((arg) => arg.type !== 'SpreadElement')
272
227
  .some((arg) => checkExpression(arg)));
273
228
  }
274
229
  if (node.type === 'BinaryExpression') {
275
- // Check both sides of binary expressions
276
230
  return checkExpression(node.left) || checkExpression(node.right);
277
231
  }
278
232
  if (node.type === 'UpdateExpression') {
279
- // Check update expressions like i++, ++i
280
233
  return checkExpression(node.argument);
281
234
  }
282
235
  if (node.type === 'UnaryExpression') {
283
- // Check unary expressions like -x, +x, !x
284
236
  return checkExpression(node.argument);
285
237
  }
286
238
  return false;
287
239
  };
288
240
  return checkExpression(expression);
289
241
  };
290
- /**
291
- * Check if a loop has a break statement
292
- */
293
242
  const hasBreakStatement = (loopBody) => {
294
243
  let hasBreak = false;
295
244
  const visited = new Set();
296
245
  const checkNode = (node, depth = 0) => {
297
- // Prevent infinite recursion
298
246
  if (depth > 10 || visited.has(node)) {
299
247
  return;
300
248
  }
@@ -303,7 +251,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
303
251
  hasBreak = true;
304
252
  return;
305
253
  }
306
- // Check child nodes
307
254
  for (const key in node) {
308
255
  const child = node[key];
309
256
  if (child && typeof child === 'object') {
@@ -323,22 +270,9 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
323
270
  checkNode(loopBody);
324
271
  return hasBreak;
325
272
  };
326
- /**
327
- * Estimate loop iterations from static analysis.
328
- *
329
- * Only ever called from the ForStatement visitor below with its own
330
- * `node`, so `loop` is always a ForStatement in practice; the parameter
331
- * type reflects that (a prior `TSESTree.ForStatement | WhileStatement |
332
- * DoWhileStatement` signature carried a redundant `loop.type ===
333
- * 'ForStatement'` guard around the whole body that could never be
334
- * false through any real call site).
335
- */
336
- // oxlint-disable-next-line consistent-function-scoping
337
273
  const estimateIterations = (loop) => {
338
- // Try to parse for loop bounds
339
274
  const test = loop.test;
340
275
  if (test && test.type === 'BinaryExpression') {
341
- // Look for patterns like i < limit or i <= limit
342
276
  if ((test.operator === '<' || test.operator === '<=' || test.operator === '>' || test.operator === '>=')) {
343
277
  const right = test.right;
344
278
  if (right.type === 'Literal' && typeof right.value === 'number') {
@@ -349,13 +283,11 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
349
283
  return null;
350
284
  };
351
285
  return {
352
- // Track variable declarations for tainting
353
286
  VariableDeclaration(node) {
354
287
  for (const declarator of node.declarations) {
355
288
  if (declarator.id.type === 'Identifier' && declarator.init) {
356
289
  const varName = declarator.id.name;
357
290
  const initText = sourceCode.getText(declarator.init);
358
- // Check if the initializer contains user input patterns, but not if it's sanitized
359
291
  const hasUserInput = userInputVariables.some(input => initText.toLowerCase().includes(input.toLowerCase()));
360
292
  const isSanitized = initText.includes('Math.min(') || initText.includes('Math.max(') ||
361
293
  initText.includes('parseInt(') || initText.includes('parseFloat(') ||
@@ -366,7 +298,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
366
298
  }
367
299
  }
368
300
  },
369
- // Track function declarations and calls for recursion detection
370
301
  FunctionDeclaration(node) {
371
302
  if (node.id) {
372
303
  currentFunctionStack.push(node.id.name);
@@ -377,20 +308,16 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
377
308
  currentFunctionStack.pop();
378
309
  }
379
310
  },
380
- // Track function calls
381
311
  CallExpression(node) {
382
312
  const callee = node.callee;
383
313
  if (callee.type === 'Identifier') {
384
314
  const functionName = callee.name;
385
315
  const currentFunction = currentFunctionStack[currentFunctionStack.length - 1];
386
- // Check for recursion
387
316
  if (currentFunction && functionName === currentFunction) {
388
317
  const callCount = (functionCalls.get(functionName) || 0) + 1;
389
318
  functionCalls.set(functionName, callCount);
390
319
  if ((callCount > maxRecursionDepth || callCount >= 1) && !reportedRecursion.has(functionName)) {
391
- // For functions that look like tree traversal (have 'obj' and 'path' params)
392
320
  const isTreeTraversal = currentFunction === 'traverseObject';
393
- // Flag excessive recursion or specific dangerous patterns
394
321
  if (callCount > maxRecursionDepth || currentFunction === 'recursiveFunc' || isTreeTraversal) {
395
322
  if (safetyChecker.isSafe(node, context)) {
396
323
  return;
@@ -409,18 +336,13 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
409
336
  }
410
337
  }
411
338
  },
412
- // Check while statements
413
339
  WhileStatement(node) {
414
340
  const test = node.test;
415
- // Check for while(true) or while(true) with potential infinite loop
416
341
  if (test.type === 'Literal' && test.value === true) {
417
- // Check if it has a break statement
418
342
  const hasBreak = hasBreakStatement(node.body);
419
343
  if (allowWhileTrueWithBreak && hasBreak) {
420
- // Allow while(true) with break if configured
421
344
  return;
422
345
  }
423
- // Report infinite loop for while(true) without break
424
346
  context.report({
425
347
  node,
426
348
  messageId: 'infiniteLoop',
@@ -431,13 +353,12 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
431
353
  suggest: [
432
354
  {
433
355
  messageId: 'limitLoopIterations',
434
- fix: () => null // Complex to auto-fix
356
+ fix: () => null
435
357
  },
436
358
  ],
437
359
  });
438
360
  return;
439
361
  }
440
- // Check for user-controlled loop conditions
441
362
  if (involvesUserInput(test)) {
442
363
  if (safetyChecker.isSafe(node, context)) {
443
364
  return;
@@ -452,7 +373,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
452
373
  });
453
374
  return;
454
375
  }
455
- // Check for complex DoS patterns (regex loops, pagination, etc.)
456
376
  if (checkComplexDoSPatterns(test)) {
457
377
  if (safetyChecker.isSafe(node, context)) {
458
378
  return;
@@ -467,15 +387,12 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
467
387
  });
468
388
  return;
469
389
  }
470
- // Check for state-dependent conditions (simple heuristic)
471
390
  if (test.type === 'Identifier') {
472
391
  const varName = test.name;
473
- // Simple check: if the variable name suggests it's a control flag
474
392
  if (varName.toLowerCase().includes('continue') ||
475
393
  varName.toLowerCase().includes('running') ||
476
394
  varName.toLowerCase().includes('active') ||
477
395
  varName.toLowerCase().includes('enabled')) {
478
- // This could be a state-dependent infinite loop
479
396
  if (safetyChecker.isSafe(node, context)) {
480
397
  return;
481
398
  }
@@ -491,9 +408,7 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
491
408
  }
492
409
  }
493
410
  },
494
- // Check for statements
495
411
  ForStatement(node) {
496
- // Check for for(;;) infinite loops
497
412
  if (!node.test && !node.update) {
498
413
  if (safetyChecker.isSafe(node, context)) {
499
414
  return;
@@ -508,7 +423,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
508
423
  });
509
424
  return;
510
425
  }
511
- // Check for missing test condition (for(;condition;))
512
426
  if (!node.test) {
513
427
  if (safetyChecker.isSafe(node, context)) {
514
428
  return;
@@ -523,7 +437,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
523
437
  });
524
438
  return;
525
439
  }
526
- // Check for user-controlled loop bounds
527
440
  if (involvesUserInput(node.test)) {
528
441
  if (safetyChecker.isSafe(node, context)) {
529
442
  return;
@@ -538,7 +451,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
538
451
  });
539
452
  return;
540
453
  }
541
- // Check for complex DoS patterns in for loops
542
454
  if (checkComplexDoSPatterns(node.test) || checkComplexDoSPatternsInScope(node.test)) {
543
455
  if (safetyChecker.isSafe(node, context)) {
544
456
  return;
@@ -553,7 +465,6 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
553
465
  });
554
466
  return;
555
467
  }
556
- // Check for potentially large iteration counts
557
468
  const estimatedIterations = estimateIterations(node);
558
469
  if (estimatedIterations && estimatedIterations > maxStaticIterations) {
559
470
  if (safetyChecker.isSafe(node, context)) {
@@ -569,10 +480,8 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
569
480
  });
570
481
  }
571
482
  },
572
- // Check do-while statements
573
483
  DoWhileStatement(node) {
574
484
  const test = node.test;
575
- // Check for user-controlled conditions
576
485
  if (involvesUserInput(test)) {
577
486
  if (safetyChecker.isSafe(node, context)) {
578
487
  return;
@@ -587,12 +496,9 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
587
496
  });
588
497
  }
589
498
  },
590
- // Check for-in and for-of statements
591
499
  ForInStatement(node) {
592
500
  const right = node.right;
593
- // Check if iterating over user-controlled collections
594
501
  if (involvesUserInput(right)) {
595
- // This could be problematic if the collection is very large
596
502
  if (safetyChecker.isSafe(node, context)) {
597
503
  return;
598
504
  }
@@ -610,12 +516,10 @@ exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
610
516
  },
611
517
  ForOfStatement(node) {
612
518
  const right = node.right;
613
- // Check if iterating over user-controlled collections
614
519
  if (involvesUserInput(right)) {
615
- // Check if the collection is validated in the same context
616
520
  const isValidated = checkIfCollectionIsValidated(node, right);
617
521
  if (isValidated) {
618
- return; // Collection is validated, safe to iterate
522
+ return;
619
523
  }
620
524
  if (safetyChecker.isSafe(node, context)) {
621
525
  return;