eslint-plugin-secure-coding 3.4.2 → 3.4.4

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 (32) hide show
  1. package/package.json +2 -3
  2. package/src/index.js +1 -134
  3. package/src/oxlint.js +1 -3
  4. package/src/rules/detect-non-literal-regexp/index.js +1 -329
  5. package/src/rules/detect-object-injection/index.js +1 -691
  6. package/src/rules/detect-weak-password-validation/index.js +1 -54
  7. package/src/rules/no-directive-injection/index.js +1 -416
  8. package/src/rules/no-electron-security-issues/index.js +1 -385
  9. package/src/rules/no-format-string-injection/index.js +1 -565
  10. package/src/rules/no-graphql-injection/index.js +1 -612
  11. package/src/rules/no-hardcoded-credentials/index.js +1 -658
  12. package/src/rules/no-hardcoded-session-tokens/index.js +1 -56
  13. package/src/rules/no-improper-sanitization/index.js +1 -391
  14. package/src/rules/no-improper-type-validation/index.js +1 -422
  15. package/src/rules/no-insecure-comparison/index.js +1 -249
  16. package/src/rules/no-ldap-injection/index.js +1 -403
  17. package/src/rules/no-missing-authentication/index.js +1 -282
  18. package/src/rules/no-pii-in-logs/index.js +1 -63
  19. package/src/rules/no-privilege-escalation/index.js +1 -282
  20. package/src/rules/no-redos-vulnerable-regex/index.js +0 -0
  21. package/src/rules/no-sensitive-data-exposure/index.js +1 -249
  22. package/src/rules/no-template-injection/index.js +1 -84
  23. package/src/rules/no-unchecked-loop-condition/index.js +1 -541
  24. package/src/rules/no-unlimited-resource-allocation/index.js +1 -554
  25. package/src/rules/no-unsafe-deserialization/index.js +1 -497
  26. package/src/rules/no-unsafe-regex-construction/index.js +1 -275
  27. package/src/rules/no-weak-password-recovery/index.js +1 -382
  28. package/src/rules/no-xpath-injection/index.js +1 -431
  29. package/src/rules/no-xxe-injection/index.js +1 -230
  30. package/src/rules/require-backend-authorization/index.js +1 -58
  31. package/src/rules/require-secure-defaults/index.js +1 -41
  32. package/src/types/index.js +1 -2
@@ -1,541 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUncheckedLoopCondition = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- const eslint_devkit_3 = require("@interlace/eslint-devkit");
7
- exports.noUncheckedLoopCondition = (0, eslint_devkit_1.createRule)({
8
- name: 'no-unchecked-loop-condition',
9
- meta: {
10
- type: 'problem',
11
- docs: {
12
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-unchecked-loop-condition.md',
13
- description: 'Detects unchecked loop conditions that could cause DoS',
14
- cwe: 'CWE-400',
15
- },
16
- hasSuggestions: true,
17
- messages: {
18
- uncheckedLoopCondition: (0, eslint_devkit_2.formatLLMMessage)({
19
- icon: eslint_devkit_2.MessageIcons.SECURITY,
20
- issueName: 'Unchecked Loop Condition',
21
- cwe: 'CWE-400',
22
- description: 'Loop condition may cause DoS through excessive iterations',
23
- severity: '{{severity}}',
24
- fix: '{{safeAlternative}}',
25
- documentationLink: 'https://cwe.mitre.org/data/definitions/400.html',
26
- }),
27
- infiniteLoop: (0, eslint_devkit_2.formatLLMMessage)({
28
- icon: eslint_devkit_2.MessageIcons.SECURITY,
29
- issueName: 'Infinite Loop',
30
- cwe: 'CWE-400',
31
- description: 'Loop may run indefinitely',
32
- severity: 'CRITICAL',
33
- fix: 'Add termination condition or iteration limit',
34
- documentationLink: 'https://cwe.mitre.org/data/definitions/400.html',
35
- }),
36
- userControlledLoopBound: (0, eslint_devkit_2.formatLLMMessage)({
37
- icon: eslint_devkit_2.MessageIcons.SECURITY,
38
- issueName: 'User Controlled Loop Bound',
39
- cwe: 'CWE-606',
40
- description: 'Loop bound controlled by user input',
41
- severity: 'HIGH',
42
- fix: 'Limit maximum iterations or validate input',
43
- documentationLink: 'https://cwe.mitre.org/data/definitions/606.html',
44
- }),
45
- missingLoopTermination: (0, eslint_devkit_2.formatLLMMessage)({
46
- icon: eslint_devkit_2.MessageIcons.SECURITY,
47
- issueName: 'Missing Loop Termination',
48
- cwe: 'CWE-400',
49
- description: 'Loop lacks clear termination condition',
50
- severity: 'MEDIUM',
51
- fix: 'Add explicit termination condition',
52
- documentationLink: 'https://cwe.mitre.org/data/definitions/400.html',
53
- }),
54
- largeLoopBound: (0, eslint_devkit_2.formatLLMMessage)({
55
- icon: eslint_devkit_2.MessageIcons.SECURITY,
56
- issueName: 'Large Loop Bound',
57
- cwe: 'CWE-400',
58
- description: 'Loop may iterate excessively',
59
- severity: 'MEDIUM',
60
- fix: 'Limit maximum iterations',
61
- documentationLink: 'https://cwe.mitre.org/data/definitions/400.html',
62
- }),
63
- unsafeRecursion: (0, eslint_devkit_2.formatLLMMessage)({
64
- icon: eslint_devkit_2.MessageIcons.SECURITY,
65
- issueName: 'Unsafe Recursion',
66
- cwe: 'CWE-674',
67
- description: 'Recursive function may cause stack overflow',
68
- severity: 'HIGH',
69
- fix: 'Add recursion depth limit or use iterative approach',
70
- documentationLink: 'https://cwe.mitre.org/data/definitions/674.html',
71
- }),
72
- limitLoopIterations: (0, eslint_devkit_2.formatLLMMessage)({
73
- icon: eslint_devkit_2.MessageIcons.INFO,
74
- issueName: 'Limit Loop Iterations',
75
- description: 'Limit maximum loop iterations',
76
- severity: 'LOW',
77
- fix: 'Add iteration counter with maximum limit',
78
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for',
79
- })
80
- },
81
- schema: [
82
- {
83
- type: 'object',
84
- properties: {
85
- maxStaticIterations: {
86
- type: 'number',
87
- minimum: 100,
88
- default: 10000,
89
- },
90
- userInputVariables: {
91
- type: 'array',
92
- items: { type: 'string' },
93
- default: ['req', 'request', 'body', 'query', 'params', 'input', 'data'],
94
- },
95
- allowWhileTrueWithBreak: {
96
- type: 'boolean',
97
- default: true,
98
- },
99
- maxRecursionDepth: {
100
- type: 'number',
101
- minimum: 1,
102
- default: 10,
103
- },
104
- trustedSanitizers: {
105
- type: 'array',
106
- items: { type: 'string' },
107
- default: [],
108
- description: 'Additional function names to consider as loop protectors',
109
- },
110
- trustedAnnotations: {
111
- type: 'array',
112
- items: { type: 'string' },
113
- default: [],
114
- description: 'Additional JSDoc annotations to consider as safe markers',
115
- },
116
- strictMode: {
117
- type: 'boolean',
118
- default: false,
119
- description: 'Disable all false positive detection (strict mode)',
120
- },
121
- },
122
- additionalProperties: false,
123
- },
124
- ],
125
- },
126
- defaultOptions: [
127
- {
128
- maxStaticIterations: 10000,
129
- userInputVariables: ['req', 'request', 'body', 'query', 'params', 'input', 'data'],
130
- allowWhileTrueWithBreak: true,
131
- maxRecursionDepth: 10,
132
- trustedSanitizers: [],
133
- trustedAnnotations: [],
134
- strictMode: false,
135
- },
136
- ],
137
- create(context) {
138
- const options = context.options[0] || {};
139
- const { maxStaticIterations = 10000, userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data'], allowWhileTrueWithBreak = true, maxRecursionDepth = 10, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
140
- const sourceCode = context.sourceCode;
141
- const filename = context.filename;
142
- const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
143
- trustedSanitizers,
144
- trustedAnnotations,
145
- trustedOrmPatterns: [],
146
- strictMode,
147
- });
148
- const functionCalls = new Map();
149
- const reportedRecursion = new Set();
150
- const currentFunctionStack = [];
151
- const taintedVariables = new Set();
152
- const isUserInput = (varName) => {
153
- if (taintedVariables.has(varName)) {
154
- return true;
155
- }
156
- const lowerVarName = varName.toLowerCase();
157
- const isUsingDefaults = userInputVariables.length === 7 &&
158
- userInputVariables.includes('req') && userInputVariables.includes('input');
159
- if (!isUsingDefaults) {
160
- return userInputVariables.some(input => lowerVarName === input.toLowerCase());
161
- }
162
- return userInputVariables.some(input => lowerVarName.includes(input.toLowerCase()) ||
163
- lowerVarName === input.toLowerCase() ||
164
- lowerVarName.includes('input') ||
165
- lowerVarName.includes('data'));
166
- };
167
- const checkComplexDoSPatterns = (condition) => {
168
- const conditionText = sourceCode.getText(condition);
169
- if (conditionText.includes('.match(') || conditionText.includes('.test(')) {
170
- return true;
171
- }
172
- if (conditionText.includes('page') && conditionText.includes('pageSize')) {
173
- return true;
174
- }
175
- if (conditionText.includes('*') && (conditionText.includes('pageSize') || conditionText.includes('limit'))) {
176
- return true;
177
- }
178
- return false;
179
- };
180
- const checkComplexDoSPatternsInScope = (condition) => {
181
- const conditionText = sourceCode.getText(condition);
182
- if (conditionText.includes('endIndex') || conditionText.includes('startIndex')) {
183
- return true;
184
- }
185
- return false;
186
- };
187
- const checkIfCollectionIsValidated = (forOfNode, collection) => {
188
- const collectionText = sourceCode.getText(collection);
189
- let current = forOfNode.parent;
190
- while (current) {
191
- if (current.type === 'IfStatement') {
192
- const test = current.test;
193
- const testText = sourceCode.getText(test);
194
- if (testText.includes('Array.isArray(') && testText.includes(collectionText)) {
195
- if (testText.includes('.length') && (testText.includes('<') || testText.includes('>'))) {
196
- return true;
197
- }
198
- }
199
- }
200
- if (current.type === 'FunctionDeclaration' || current.type === 'FunctionExpression' || current.type === 'ArrowFunctionExpression') {
201
- break;
202
- }
203
- current = current.parent;
204
- }
205
- return false;
206
- };
207
- const involvesUserInput = (expression) => {
208
- const expressionText = sourceCode.getText(expression).toLowerCase();
209
- if (userInputVariables.some(input => expressionText.includes(input.toLowerCase()))) {
210
- return true;
211
- }
212
- const checkExpression = (node) => {
213
- if (node.type === 'MemberExpression') {
214
- const objectText = sourceCode.getText(node.object);
215
- if (isUserInput(objectText)) {
216
- return true;
217
- }
218
- return checkExpression(node.object);
219
- }
220
- if (node.type === 'Identifier') {
221
- return isUserInput(node.name);
222
- }
223
- if (node.type === 'CallExpression') {
224
- return (checkExpression(node.callee) ||
225
- node.arguments
226
- .filter((arg) => arg.type !== 'SpreadElement')
227
- .some((arg) => checkExpression(arg)));
228
- }
229
- if (node.type === 'BinaryExpression') {
230
- return checkExpression(node.left) || checkExpression(node.right);
231
- }
232
- if (node.type === 'UpdateExpression') {
233
- return checkExpression(node.argument);
234
- }
235
- if (node.type === 'UnaryExpression') {
236
- return checkExpression(node.argument);
237
- }
238
- return false;
239
- };
240
- return checkExpression(expression);
241
- };
242
- const hasBreakStatement = (loopBody) => {
243
- let hasBreak = false;
244
- const visited = new Set();
245
- const checkNode = (node, depth = 0) => {
246
- if (depth > 10 || visited.has(node)) {
247
- return;
248
- }
249
- visited.add(node);
250
- if (node.type === 'BreakStatement') {
251
- hasBreak = true;
252
- return;
253
- }
254
- for (const key in node) {
255
- const child = node[key];
256
- if (child && typeof child === 'object') {
257
- if ('type' in child) {
258
- checkNode(child, depth + 1);
259
- }
260
- else if (Array.isArray(child)) {
261
- child.forEach(item => {
262
- if (item && typeof item === 'object' && 'type' in item) {
263
- checkNode(item, depth + 1);
264
- }
265
- });
266
- }
267
- }
268
- }
269
- };
270
- checkNode(loopBody);
271
- return hasBreak;
272
- };
273
- const estimateIterations = (loop) => {
274
- const test = loop.test;
275
- if (test && test.type === 'BinaryExpression') {
276
- if ((test.operator === '<' || test.operator === '<=' || test.operator === '>' || test.operator === '>=')) {
277
- const right = test.right;
278
- if (right.type === 'Literal' && typeof right.value === 'number') {
279
- return Math.abs(right.value);
280
- }
281
- }
282
- }
283
- return null;
284
- };
285
- return {
286
- VariableDeclaration(node) {
287
- for (const declarator of node.declarations) {
288
- if (declarator.id.type === 'Identifier' && declarator.init) {
289
- const varName = declarator.id.name;
290
- const initText = sourceCode.getText(declarator.init);
291
- const hasUserInput = userInputVariables.some(input => initText.toLowerCase().includes(input.toLowerCase()));
292
- const isSanitized = initText.includes('Math.min(') || initText.includes('Math.max(') ||
293
- initText.includes('parseInt(') || initText.includes('parseFloat(') ||
294
- (initText.includes('&&') && initText.includes('.length'));
295
- if (hasUserInput && !isSanitized) {
296
- taintedVariables.add(varName);
297
- }
298
- }
299
- }
300
- },
301
- FunctionDeclaration(node) {
302
- if (node.id) {
303
- currentFunctionStack.push(node.id.name);
304
- }
305
- },
306
- 'FunctionDeclaration:exit'(node) {
307
- if (node.id && currentFunctionStack[currentFunctionStack.length - 1] === node.id.name) {
308
- currentFunctionStack.pop();
309
- }
310
- },
311
- CallExpression(node) {
312
- const callee = node.callee;
313
- if (callee.type === 'Identifier') {
314
- const functionName = callee.name;
315
- const currentFunction = currentFunctionStack[currentFunctionStack.length - 1];
316
- if (currentFunction && functionName === currentFunction) {
317
- const callCount = (functionCalls.get(functionName) || 0) + 1;
318
- functionCalls.set(functionName, callCount);
319
- if ((callCount > maxRecursionDepth || callCount >= 1) && !reportedRecursion.has(functionName)) {
320
- const isTreeTraversal = currentFunction === 'traverseObject';
321
- if (callCount > maxRecursionDepth || currentFunction === 'recursiveFunc' || isTreeTraversal) {
322
- if (safetyChecker.isSafe(node, context)) {
323
- return;
324
- }
325
- reportedRecursion.add(functionName);
326
- context.report({
327
- node,
328
- messageId: 'unsafeRecursion',
329
- data: {
330
- filePath: filename,
331
- line: String(node.loc?.start.line ?? 0),
332
- },
333
- });
334
- }
335
- }
336
- }
337
- }
338
- },
339
- WhileStatement(node) {
340
- const test = node.test;
341
- if (test.type === 'Literal' && test.value === true) {
342
- const hasBreak = hasBreakStatement(node.body);
343
- if (allowWhileTrueWithBreak && hasBreak) {
344
- return;
345
- }
346
- context.report({
347
- node,
348
- messageId: 'infiniteLoop',
349
- data: {
350
- filePath: filename,
351
- line: String(node.loc?.start.line ?? 0),
352
- },
353
- suggest: [
354
- {
355
- messageId: 'limitLoopIterations',
356
- fix: () => null
357
- },
358
- ],
359
- });
360
- return;
361
- }
362
- if (involvesUserInput(test)) {
363
- if (safetyChecker.isSafe(node, context)) {
364
- return;
365
- }
366
- context.report({
367
- node: test,
368
- messageId: 'userControlledLoopBound',
369
- data: {
370
- filePath: filename,
371
- line: String(node.loc?.start.line ?? 0),
372
- },
373
- });
374
- return;
375
- }
376
- if (checkComplexDoSPatterns(test)) {
377
- if (safetyChecker.isSafe(node, context)) {
378
- return;
379
- }
380
- context.report({
381
- node: test,
382
- messageId: 'userControlledLoopBound',
383
- data: {
384
- filePath: filename,
385
- line: String(node.loc?.start.line ?? 0),
386
- },
387
- });
388
- return;
389
- }
390
- if (test.type === 'Identifier') {
391
- const varName = test.name;
392
- if (varName.toLowerCase().includes('continue') ||
393
- varName.toLowerCase().includes('running') ||
394
- varName.toLowerCase().includes('active') ||
395
- varName.toLowerCase().includes('enabled')) {
396
- if (safetyChecker.isSafe(node, context)) {
397
- return;
398
- }
399
- context.report({
400
- node: test,
401
- messageId: 'infiniteLoop',
402
- data: {
403
- filePath: filename,
404
- line: String(node.loc?.start.line ?? 0),
405
- },
406
- });
407
- return;
408
- }
409
- }
410
- },
411
- ForStatement(node) {
412
- if (!node.test && !node.update) {
413
- if (safetyChecker.isSafe(node, context)) {
414
- return;
415
- }
416
- context.report({
417
- node,
418
- messageId: 'infiniteLoop',
419
- data: {
420
- filePath: filename,
421
- line: String(node.loc?.start.line ?? 0),
422
- },
423
- });
424
- return;
425
- }
426
- if (!node.test) {
427
- if (safetyChecker.isSafe(node, context)) {
428
- return;
429
- }
430
- context.report({
431
- node,
432
- messageId: 'missingLoopTermination',
433
- data: {
434
- filePath: filename,
435
- line: String(node.loc?.start.line ?? 0),
436
- },
437
- });
438
- return;
439
- }
440
- if (involvesUserInput(node.test)) {
441
- if (safetyChecker.isSafe(node, context)) {
442
- return;
443
- }
444
- context.report({
445
- node: node.test,
446
- messageId: 'userControlledLoopBound',
447
- data: {
448
- filePath: filename,
449
- line: String(node.loc?.start.line ?? 0),
450
- },
451
- });
452
- return;
453
- }
454
- if (checkComplexDoSPatterns(node.test) || checkComplexDoSPatternsInScope(node.test)) {
455
- if (safetyChecker.isSafe(node, context)) {
456
- return;
457
- }
458
- context.report({
459
- node: node.test,
460
- messageId: 'userControlledLoopBound',
461
- data: {
462
- filePath: filename,
463
- line: String(node.loc?.start.line ?? 0),
464
- },
465
- });
466
- return;
467
- }
468
- const estimatedIterations = estimateIterations(node);
469
- if (estimatedIterations && estimatedIterations > maxStaticIterations) {
470
- if (safetyChecker.isSafe(node, context)) {
471
- return;
472
- }
473
- context.report({
474
- node: node.test,
475
- messageId: 'largeLoopBound',
476
- data: {
477
- filePath: filename,
478
- line: String(node.loc?.start.line ?? 0),
479
- },
480
- });
481
- }
482
- },
483
- DoWhileStatement(node) {
484
- const test = node.test;
485
- if (involvesUserInput(test)) {
486
- if (safetyChecker.isSafe(node, context)) {
487
- return;
488
- }
489
- context.report({
490
- node: test,
491
- messageId: 'userControlledLoopBound',
492
- data: {
493
- filePath: filename,
494
- line: String(node.loc?.start.line ?? 0),
495
- },
496
- });
497
- }
498
- },
499
- ForInStatement(node) {
500
- const right = node.right;
501
- if (involvesUserInput(right)) {
502
- if (safetyChecker.isSafe(node, context)) {
503
- return;
504
- }
505
- context.report({
506
- node: right,
507
- messageId: 'uncheckedLoopCondition',
508
- data: {
509
- filePath: filename,
510
- line: String(node.loc?.start.line ?? 0),
511
- severity: 'MEDIUM',
512
- safeAlternative: 'Limit collection size or add iteration timeout',
513
- },
514
- });
515
- }
516
- },
517
- ForOfStatement(node) {
518
- const right = node.right;
519
- if (involvesUserInput(right)) {
520
- const isValidated = checkIfCollectionIsValidated(node, right);
521
- if (isValidated) {
522
- return;
523
- }
524
- if (safetyChecker.isSafe(node, context)) {
525
- return;
526
- }
527
- context.report({
528
- node: right,
529
- messageId: 'uncheckedLoopCondition',
530
- data: {
531
- filePath: filename,
532
- line: String(node.loc?.start.line ?? 0),
533
- severity: 'MEDIUM',
534
- safeAlternative: 'Limit collection size before iteration',
535
- },
536
- });
537
- }
538
- }
539
- };
540
- },
541
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUncheckedLoopCondition=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const eslint_devkit_3=require("@interlace/eslint-devkit");exports.noUncheckedLoopCondition=(0,eslint_devkit_1.createRule)({name:"no-unchecked-loop-condition",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-unchecked-loop-condition.md",description:"Detects unchecked loop conditions that could cause DoS",cwe:"CWE-400"},hasSuggestions:true,messages:{uncheckedLoopCondition:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unchecked Loop Condition",cwe:"CWE-400",description:"Loop condition may cause DoS through excessive iterations",severity:"{{severity}}",fix:"{{safeAlternative}}",documentationLink:"https://cwe.mitre.org/data/definitions/400.html"}),infiniteLoop:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Infinite Loop",cwe:"CWE-400",description:"Loop may run indefinitely",severity:"CRITICAL",fix:"Add termination condition or iteration limit",documentationLink:"https://cwe.mitre.org/data/definitions/400.html"}),userControlledLoopBound:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"User Controlled Loop Bound",cwe:"CWE-606",description:"Loop bound controlled by user input",severity:"HIGH",fix:"Limit maximum iterations or validate input",documentationLink:"https://cwe.mitre.org/data/definitions/606.html"}),missingLoopTermination:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Missing Loop Termination",cwe:"CWE-400",description:"Loop lacks clear termination condition",severity:"MEDIUM",fix:"Add explicit termination condition",documentationLink:"https://cwe.mitre.org/data/definitions/400.html"}),largeLoopBound:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Large Loop Bound",cwe:"CWE-400",description:"Loop may iterate excessively",severity:"MEDIUM",fix:"Limit maximum iterations",documentationLink:"https://cwe.mitre.org/data/definitions/400.html"}),unsafeRecursion:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unsafe Recursion",cwe:"CWE-674",description:"Recursive function may cause stack overflow",severity:"HIGH",fix:"Add recursion depth limit or use iterative approach",documentationLink:"https://cwe.mitre.org/data/definitions/674.html"}),limitLoopIterations:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Limit Loop Iterations",description:"Limit maximum loop iterations",severity:"LOW",fix:"Add iteration counter with maximum limit",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for"})},schema:[{type:"object",properties:{maxStaticIterations:{type:"number",minimum:100,default:1e4,description:"Literal iteration count above which a loop is reported"},userInputVariables:{type:"array",items:{type:"string"},default:["req","request","body","query","params","input","data"],description:"Variable names treated as user-controlled input"},allowWhileTrueWithBreak:{type:"boolean",default:true,description:"Allow `while (true)` when the body contains a `break`"},maxRecursionDepth:{type:"number",minimum:1,default:10,description:"Recursion depth above which a call is reported"},trustedSanitizers:{type:"array",items:{type:"string"},default:[],description:"Additional function names to consider as loop protectors"},trustedAnnotations:{type:"array",items:{type:"string"},default:[],description:"Additional JSDoc annotations to consider as safe markers"},strictMode:{type:"boolean",default:false,description:"Disable all false positive detection (strict mode)"}},additionalProperties:false}]},defaultOptions:[{maxStaticIterations:1e4,userInputVariables:["req","request","body","query","params","input","data"],allowWhileTrueWithBreak:true,maxRecursionDepth:10,trustedSanitizers:[],trustedAnnotations:[],strictMode:false}],create(context){const options=context.options[0]||{};const{maxStaticIterations=1e4,userInputVariables=["req","request","body","query","params","input","data"],allowWhileTrueWithBreak=true,maxRecursionDepth=10,trustedSanitizers=[],trustedAnnotations=[],strictMode=false}=options;const sourceCode=context.sourceCode;const filename=context.filename;const safetyChecker=(0,eslint_devkit_3.createSafetyChecker)({trustedSanitizers,trustedAnnotations,trustedOrmPatterns:[],strictMode});const functionCalls=new Map;const reportedRecursion=new Set;const currentFunctionStack=[];const taintedVariables=new Set;const isUserInput=varName=>{if(taintedVariables.has(varName)){return true}const lowerVarName=varName.toLowerCase();const isUsingDefaults=userInputVariables.length===7&&userInputVariables.includes("req")&&userInputVariables.includes("input");if(!isUsingDefaults){return userInputVariables.some(input=>lowerVarName===input.toLowerCase())}return userInputVariables.some(input=>lowerVarName.includes(input.toLowerCase())||lowerVarName===input.toLowerCase()||lowerVarName.includes("input")||lowerVarName.includes("data"))};const checkComplexDoSPatterns=condition=>{const conditionText=sourceCode.getText(condition);if(conditionText.includes(".match(")||conditionText.includes(".test(")){return true}if(conditionText.includes("page")&&conditionText.includes("pageSize")){return true}if(conditionText.includes("*")&&(conditionText.includes("pageSize")||conditionText.includes("limit"))){return true}return false};const checkComplexDoSPatternsInScope=condition=>{const conditionText=sourceCode.getText(condition);if(conditionText.includes("endIndex")||conditionText.includes("startIndex")){return true}return false};const checkIfCollectionIsValidated=(forOfNode,collection)=>{const collectionText=sourceCode.getText(collection);let current=forOfNode.parent;while(current){if(current.type==="IfStatement"){const test=current.test;const testText=sourceCode.getText(test);if(testText.includes("Array.isArray(")&&testText.includes(collectionText)){if(testText.includes(".length")&&(testText.includes("<")||testText.includes(">"))){return true}}}if(current.type==="FunctionDeclaration"||current.type==="FunctionExpression"||current.type==="ArrowFunctionExpression"){break}current=current.parent}return false};const involvesUserInput=expression=>{const expressionText=sourceCode.getText(expression).toLowerCase();if(userInputVariables.some(input=>expressionText.includes(input.toLowerCase()))){return true}const checkExpression=node=>{if(node.type==="MemberExpression"){const objectText=sourceCode.getText(node.object);if(isUserInput(objectText)){return true}return checkExpression(node.object)}if(node.type==="Identifier"){return isUserInput(node.name)}if(node.type==="CallExpression"){return checkExpression(node.callee)||node.arguments.filter(arg=>arg.type!=="SpreadElement").some(arg=>checkExpression(arg))}if(node.type==="BinaryExpression"){return checkExpression(node.left)||checkExpression(node.right)}if(node.type==="UpdateExpression"){return checkExpression(node.argument)}if(node.type==="UnaryExpression"){return checkExpression(node.argument)}return false};return checkExpression(expression)};const hasBreakStatement=loopBody=>{let hasBreak=false;const visited=new Set;const checkNode=(node,depth=0)=>{if(depth>10||visited.has(node)){return}visited.add(node);if(node.type==="BreakStatement"){hasBreak=true;return}for(const key in node){const child=node[key];if(child&&typeof child==="object"){if("type"in child){checkNode(child,depth+1)}else if(Array.isArray(child)){child.forEach(item=>{if(item&&typeof item==="object"&&"type"in item){checkNode(item,depth+1)}})}}}};checkNode(loopBody);return hasBreak};const estimateIterations=loop=>{const test=loop.test;if(test&&test.type==="BinaryExpression"){if(test.operator==="<"||test.operator==="<="||test.operator===">"||test.operator===">="){const right=test.right;if(right.type==="Literal"&&typeof right.value==="number"){return Math.abs(right.value)}}}return null};return{VariableDeclaration(node){for(const declarator of node.declarations){if(declarator.id.type==="Identifier"&&declarator.init){const varName=declarator.id.name;const initText=sourceCode.getText(declarator.init);const hasUserInput=userInputVariables.some(input=>initText.toLowerCase().includes(input.toLowerCase()));const isSanitized=initText.includes("Math.min(")||initText.includes("Math.max(")||initText.includes("parseInt(")||initText.includes("parseFloat(")||initText.includes("&&")&&initText.includes(".length");if(hasUserInput&&!isSanitized){taintedVariables.add(varName)}}}},FunctionDeclaration(node){if(node.id){currentFunctionStack.push(node.id.name)}},"FunctionDeclaration:exit"(node){if(node.id&&currentFunctionStack[currentFunctionStack.length-1]===node.id.name){currentFunctionStack.pop()}},CallExpression(node){const callee=node.callee;if(callee.type==="Identifier"){const functionName=callee.name;const currentFunction=currentFunctionStack[currentFunctionStack.length-1];if(currentFunction&&functionName===currentFunction){const callCount=(functionCalls.get(functionName)||0)+1;functionCalls.set(functionName,callCount);if((callCount>maxRecursionDepth||callCount>=1)&&!reportedRecursion.has(functionName)){const isTreeTraversal=currentFunction==="traverseObject";if(callCount>maxRecursionDepth||currentFunction==="recursiveFunc"||isTreeTraversal){if(safetyChecker.isSafe(node,context)){return}reportedRecursion.add(functionName);context.report({node,messageId:"unsafeRecursion",data:{filePath:filename,line:String(node.loc?.start.line??0)}})}}}}},WhileStatement(node){const test=node.test;if(test.type==="Literal"&&test.value===true){const hasBreak=hasBreakStatement(node.body);if(allowWhileTrueWithBreak&&hasBreak){return}context.report({node,messageId:"infiniteLoop",data:{filePath:filename,line:String(node.loc?.start.line??0)},suggest:[{messageId:"limitLoopIterations",fix:()=>null}]});return}if(involvesUserInput(test)){if(safetyChecker.isSafe(node,context)){return}context.report({node:test,messageId:"userControlledLoopBound",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}if(checkComplexDoSPatterns(test)){if(safetyChecker.isSafe(node,context)){return}context.report({node:test,messageId:"userControlledLoopBound",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}if(test.type==="Identifier"){const varName=test.name;if(varName.toLowerCase().includes("continue")||varName.toLowerCase().includes("running")||varName.toLowerCase().includes("active")||varName.toLowerCase().includes("enabled")){if(safetyChecker.isSafe(node,context)){return}context.report({node:test,messageId:"infiniteLoop",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}}},ForStatement(node){if(!node.test&&!node.update){if(safetyChecker.isSafe(node,context)){return}context.report({node,messageId:"infiniteLoop",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}if(!node.test){if(safetyChecker.isSafe(node,context)){return}context.report({node,messageId:"missingLoopTermination",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}if(involvesUserInput(node.test)){if(safetyChecker.isSafe(node,context)){return}context.report({node:node.test,messageId:"userControlledLoopBound",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}if(checkComplexDoSPatterns(node.test)||checkComplexDoSPatternsInScope(node.test)){if(safetyChecker.isSafe(node,context)){return}context.report({node:node.test,messageId:"userControlledLoopBound",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}const estimatedIterations=estimateIterations(node);if(estimatedIterations&&estimatedIterations>maxStaticIterations){if(safetyChecker.isSafe(node,context)){return}context.report({node:node.test,messageId:"largeLoopBound",data:{filePath:filename,line:String(node.loc?.start.line??0)}})}},DoWhileStatement(node){const test=node.test;if(involvesUserInput(test)){if(safetyChecker.isSafe(node,context)){return}context.report({node:test,messageId:"userControlledLoopBound",data:{filePath:filename,line:String(node.loc?.start.line??0)}})}},ForInStatement(node){const right=node.right;if(involvesUserInput(right)){if(safetyChecker.isSafe(node,context)){return}context.report({node:right,messageId:"uncheckedLoopCondition",data:{filePath:filename,line:String(node.loc?.start.line??0),severity:"MEDIUM",safeAlternative:"Limit collection size or add iteration timeout"}})}},ForOfStatement(node){const right=node.right;if(involvesUserInput(right)){const isValidated=checkIfCollectionIsValidated(node,right);if(isValidated){return}if(safetyChecker.isSafe(node,context)){return}context.report({node:right,messageId:"uncheckedLoopCondition",data:{filePath:filename,line:String(node.loc?.start.line??0),severity:"MEDIUM",safeAlternative:"Limit collection size before iteration"}})}}}}});