eslint-plugin-secure-coding 3.4.3 → 3.5.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 (33) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -3
  3. package/src/index.js +1 -107
  4. package/src/oxlint.js +1 -3
  5. package/src/rules/detect-non-literal-regexp/index.js +1 -329
  6. package/src/rules/detect-object-injection/index.js +1 -691
  7. package/src/rules/detect-weak-password-validation/index.js +1 -54
  8. package/src/rules/no-directive-injection/index.js +1 -416
  9. package/src/rules/no-electron-security-issues/index.js +1 -385
  10. package/src/rules/no-format-string-injection/index.js +1 -565
  11. package/src/rules/no-graphql-injection/index.js +1 -612
  12. package/src/rules/no-hardcoded-credentials/index.js +1 -658
  13. package/src/rules/no-hardcoded-session-tokens/index.js +1 -56
  14. package/src/rules/no-improper-sanitization/index.js +1 -391
  15. package/src/rules/no-improper-type-validation/index.js +1 -422
  16. package/src/rules/no-insecure-comparison/index.js +1 -249
  17. package/src/rules/no-ldap-injection/index.js +1 -403
  18. package/src/rules/no-missing-authentication/index.js +1 -282
  19. package/src/rules/no-pii-in-logs/index.js +1 -63
  20. package/src/rules/no-privilege-escalation/index.js +1 -282
  21. package/src/rules/no-redos-vulnerable-regex/index.js +0 -0
  22. package/src/rules/no-sensitive-data-exposure/index.js +1 -249
  23. package/src/rules/no-template-injection/index.js +1 -84
  24. package/src/rules/no-unchecked-loop-condition/index.js +1 -541
  25. package/src/rules/no-unlimited-resource-allocation/index.js +1 -555
  26. package/src/rules/no-unsafe-deserialization/index.js +1 -497
  27. package/src/rules/no-unsafe-regex-construction/index.js +1 -275
  28. package/src/rules/no-weak-password-recovery/index.js +1 -382
  29. package/src/rules/no-xpath-injection/index.js +1 -431
  30. package/src/rules/no-xxe-injection/index.js +1 -230
  31. package/src/rules/require-backend-authorization/index.js +1 -58
  32. package/src/rules/require-secure-defaults/index.js +1 -41
  33. package/src/types/index.js +1 -2
@@ -1,555 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUnlimitedResourceAllocation = 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.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
8
- name: 'no-unlimited-resource-allocation',
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-unlimited-resource-allocation.md',
13
- description: 'Detects unlimited resource allocation that could cause DoS',
14
- cwe: 'CWE-770',
15
- },
16
- hasSuggestions: true,
17
- messages: {
18
- unlimitedResourceAllocation: (0, eslint_devkit_2.formatLLMMessage)({
19
- icon: eslint_devkit_2.MessageIcons.SECURITY,
20
- issueName: 'Unlimited Resource Allocation',
21
- cwe: 'CWE-770',
22
- description: 'Resource allocation without limits',
23
- severity: '{{severity}}',
24
- fix: '{{safeAlternative}}',
25
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
26
- }),
27
- unlimitedBufferAllocation: (0, eslint_devkit_2.formatLLMMessage)({
28
- icon: eslint_devkit_2.MessageIcons.SECURITY,
29
- issueName: 'Unlimited Buffer Allocation',
30
- cwe: 'CWE-770',
31
- description: 'Buffer allocated without size limits',
32
- severity: 'HIGH',
33
- fix: 'Set maximum buffer size limits',
34
- documentationLink: 'https://nodejs.org/api/buffer.html',
35
- }),
36
- unlimitedFileOperations: (0, eslint_devkit_2.formatLLMMessage)({
37
- icon: eslint_devkit_2.MessageIcons.SECURITY,
38
- issueName: 'Unlimited File Operations',
39
- cwe: 'CWE-770',
40
- description: 'File operations without size limits',
41
- severity: 'MEDIUM',
42
- fix: 'Validate file size before operations',
43
- documentationLink: 'https://nodejs.org/api/fs.html',
44
- }),
45
- unlimitedNetworkConnections: (0, eslint_devkit_2.formatLLMMessage)({
46
- icon: eslint_devkit_2.MessageIcons.SECURITY,
47
- issueName: 'Unlimited Network Connections',
48
- cwe: 'CWE-770',
49
- description: 'Network connections without limits',
50
- severity: 'MEDIUM',
51
- fix: 'Limit concurrent connections',
52
- documentationLink: 'https://nodejs.org/api/http.html',
53
- }),
54
- unlimitedMemoryAllocation: (0, eslint_devkit_2.formatLLMMessage)({
55
- icon: eslint_devkit_2.MessageIcons.SECURITY,
56
- issueName: 'Unlimited Memory Allocation',
57
- cwe: 'CWE-770',
58
- description: 'Memory allocated without limits',
59
- severity: 'MEDIUM',
60
- fix: 'Set memory allocation limits',
61
- documentationLink: 'https://nodejs.org/api/buffer.html',
62
- }),
63
- userControlledResourceSize: (0, eslint_devkit_2.formatLLMMessage)({
64
- icon: eslint_devkit_2.MessageIcons.SECURITY,
65
- issueName: 'User Controlled Resource Size',
66
- cwe: 'CWE-770',
67
- description: 'Resource size controlled by user input',
68
- severity: 'HIGH',
69
- fix: 'Validate and limit user-controlled resource sizes',
70
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
71
- }),
72
- missingResourceLimits: (0, eslint_devkit_2.formatLLMMessage)({
73
- icon: eslint_devkit_2.MessageIcons.SECURITY,
74
- issueName: 'Missing Resource Limits',
75
- cwe: 'CWE-770',
76
- description: 'Resource allocation lacks proper limits',
77
- severity: 'MEDIUM',
78
- fix: 'Implement resource size validation',
79
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
80
- }),
81
- resourceAllocationInLoop: (0, eslint_devkit_2.formatLLMMessage)({
82
- icon: eslint_devkit_2.MessageIcons.SECURITY,
83
- issueName: 'Resource Allocation in Loop',
84
- cwe: 'CWE-770',
85
- description: 'Resource allocation inside loop without limits',
86
- severity: 'HIGH',
87
- fix: 'Move resource allocation outside loop or add iteration limits',
88
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
89
- }),
90
- implementResourceLimits: (0, eslint_devkit_2.formatLLMMessage)({
91
- icon: eslint_devkit_2.MessageIcons.INFO,
92
- issueName: 'Implement Resource Limits',
93
- description: 'Add limits to resource allocation',
94
- severity: 'LOW',
95
- fix: 'const limitedSize = Math.min(userSize, MAX_SIZE);',
96
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
97
- }),
98
- validateResourceSize: (0, eslint_devkit_2.formatLLMMessage)({
99
- icon: eslint_devkit_2.MessageIcons.INFO,
100
- issueName: 'Validate Resource Size',
101
- description: 'Validate resource size before allocation',
102
- severity: 'LOW',
103
- fix: 'if (size > MAX_SIZE) throw new Error("Size too large");',
104
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
105
- }),
106
- useResourcePools: (0, eslint_devkit_2.formatLLMMessage)({
107
- icon: eslint_devkit_2.MessageIcons.INFO,
108
- issueName: 'Use Resource Pools',
109
- description: 'Use resource pools for better control',
110
- severity: 'LOW',
111
- fix: 'Implement connection pooling and resource reuse',
112
- documentationLink: 'https://en.wikipedia.org/wiki/Object_pool_pattern',
113
- }),
114
- strategyResourceManagement: (0, eslint_devkit_2.formatLLMMessage)({
115
- icon: eslint_devkit_2.MessageIcons.STRATEGY,
116
- issueName: 'Resource Management Strategy',
117
- description: 'Implement comprehensive resource management',
118
- severity: 'LOW',
119
- fix: 'Use resource pools, limits, and cleanup mechanisms',
120
- documentationLink: 'https://cwe.mitre.org/data/definitions/770.html',
121
- }),
122
- strategyRateLimiting: (0, eslint_devkit_2.formatLLMMessage)({
123
- icon: eslint_devkit_2.MessageIcons.STRATEGY,
124
- issueName: 'Rate Limiting Strategy',
125
- description: 'Implement rate limiting for resource allocation',
126
- severity: 'LOW',
127
- fix: 'Use rate limiters to prevent resource exhaustion',
128
- documentationLink: 'https://en.wikipedia.org/wiki/Rate_limiting',
129
- }),
130
- strategyResourceCleanup: (0, eslint_devkit_2.formatLLMMessage)({
131
- icon: eslint_devkit_2.MessageIcons.STRATEGY,
132
- issueName: 'Resource Cleanup Strategy',
133
- description: 'Ensure proper resource cleanup',
134
- severity: 'LOW',
135
- fix: 'Implement try-finally blocks and resource disposal',
136
- documentationLink: 'https://en.wikipedia.org/wiki/Resource_management_(computing)',
137
- })
138
- },
139
- schema: [
140
- {
141
- type: 'object',
142
- properties: {
143
- maxResourceSize: {
144
- type: 'number',
145
- minimum: 1024,
146
- default: 1048576,
147
- description: 'Allocation size in bytes above which a call is reported',
148
- },
149
- userInputVariables: {
150
- type: 'array',
151
- items: { type: 'string' },
152
- default: ['req', 'request', 'body', 'query', 'params', 'input', 'data'], description: 'Variable names treated as user-controlled input'
153
- },
154
- safeResourceFunctions: {
155
- type: 'array',
156
- items: { type: 'string' },
157
- default: ['validateSize', 'checkLimits', 'limitResource', 'safeAlloc'], description: 'Function names that bound an allocation'
158
- },
159
- requireResourceValidation: {
160
- type: 'boolean',
161
- default: true, description: 'Require an explicit size check before allocating'
162
- },
163
- trustedSanitizers: {
164
- type: 'array',
165
- items: { type: 'string' },
166
- default: [],
167
- description: 'Additional function names to consider as resource validators',
168
- },
169
- trustedAnnotations: {
170
- type: 'array',
171
- items: { type: 'string' },
172
- default: [],
173
- description: 'Additional JSDoc annotations to consider as safe markers',
174
- },
175
- strictMode: {
176
- type: 'boolean',
177
- default: false,
178
- description: 'Disable all false positive detection (strict mode)',
179
- },
180
- },
181
- additionalProperties: false,
182
- },
183
- ],
184
- },
185
- defaultOptions: [
186
- {
187
- maxResourceSize: 1048576,
188
- userInputVariables: ['req', 'request', 'body', 'query', 'params', 'input', 'data'],
189
- safeResourceFunctions: ['validateSize', 'checkLimits', 'limitResource', 'safeAlloc'],
190
- requireResourceValidation: true,
191
- trustedSanitizers: [],
192
- trustedAnnotations: [],
193
- strictMode: false,
194
- },
195
- ],
196
- create(context) {
197
- const options = context.options[0] || {};
198
- const { maxResourceSize = 1048576, userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data'], safeResourceFunctions = ['validateSize', 'checkLimits', 'limitResource', 'safeAlloc'], requireResourceValidation = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
199
- const sourceCode = context.sourceCode;
200
- const filename = context.filename;
201
- const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
202
- trustedSanitizers,
203
- trustedAnnotations,
204
- trustedOrmPatterns: [],
205
- strictMode,
206
- });
207
- const isUserInput = (expression) => (0, eslint_devkit_3.isUserInputExpression)(expression, sourceCode, userInputVariables);
208
- const hasSizeValidation = (node) => {
209
- const args = node.arguments;
210
- const sizeArg = args[0];
211
- const sizeText = sourceCode.getText(sizeArg);
212
- return sizeText.includes('Math.min(') ||
213
- sizeText.includes('Math.max(') ||
214
- sizeText.includes('Math.clamp(') ||
215
- safeResourceFunctions.some(func => sizeText.includes(func));
216
- };
217
- const estimateResourceSize = (sizeExpression) => {
218
- if (sizeExpression.type === 'Literal' && typeof sizeExpression.value === 'number') {
219
- return sizeExpression.value;
220
- }
221
- if (sizeExpression.type === 'BinaryExpression') {
222
- const left = estimateResourceSize(sizeExpression.left);
223
- const right = estimateResourceSize(sizeExpression.right);
224
- if (left !== null && right !== null) {
225
- switch (sizeExpression.operator) {
226
- case '*':
227
- return left * right;
228
- case '+':
229
- return left + right;
230
- case '-':
231
- return left - right;
232
- case '/':
233
- return right !== 0 ? left / right : null;
234
- default:
235
- return null;
236
- }
237
- }
238
- }
239
- return null;
240
- };
241
- return {
242
- CallExpression(node) {
243
- const callee = node.callee;
244
- const calleeText = sourceCode.getText(callee);
245
- const isBufferAlloc = callee.type === 'MemberExpression' &&
246
- callee.object.type === 'Identifier' &&
247
- callee.object.name === 'Buffer' &&
248
- callee.property.type === 'Identifier' &&
249
- (callee.property.name === 'alloc' || callee.property.name === 'allocUnsafe');
250
- const isNewBuffer = callee.type === 'NewExpression' &&
251
- callee.callee.type === 'Identifier' &&
252
- callee.callee.name === 'Buffer';
253
- if (isBufferAlloc || isNewBuffer) {
254
- const args = node.arguments;
255
- if (args.length > 0) {
256
- const sizeArg = args[0];
257
- if (requireResourceValidation &&
258
- sizeArg.type !== 'SpreadElement' &&
259
- isUserInput(sizeArg) &&
260
- !hasSizeValidation(node)) {
261
- if (safetyChecker.isSafe(node, context)) {
262
- return;
263
- }
264
- context.report({
265
- node: sizeArg,
266
- messageId: 'userControlledResourceSize',
267
- data: {
268
- filePath: filename,
269
- line: String(node.loc.start.line),
270
- },
271
- });
272
- return;
273
- }
274
- const estimatedSize = sizeArg.type === 'SpreadElement' ? null : estimateResourceSize(sizeArg);
275
- if (estimatedSize && estimatedSize > maxResourceSize) {
276
- if (safetyChecker.isSafe(node, context)) {
277
- return;
278
- }
279
- context.report({
280
- node: sizeArg,
281
- messageId: 'unlimitedBufferAllocation',
282
- data: {
283
- filePath: filename,
284
- line: String(node.loc.start.line),
285
- },
286
- });
287
- return;
288
- }
289
- }
290
- }
291
- if (callee.type === 'Identifier' && callee.name === 'multer') {
292
- const args = node.arguments;
293
- if (args.length > 0 && args[0].type === 'ObjectExpression') {
294
- const props = args[0].properties;
295
- const hasValidLimits = props.some((prop) => {
296
- if (prop.type !== 'Property' || prop.key.type !== 'Identifier') {
297
- return false;
298
- }
299
- if (prop.key.name === 'fileSize')
300
- return true;
301
- if (prop.key.name === 'limits' && prop.value.type === 'ObjectExpression') {
302
- return prop.value.properties.some((limitProp) => limitProp.type === 'Property' &&
303
- limitProp.key.type === 'Identifier' &&
304
- limitProp.key.name === 'fileSize');
305
- }
306
- return false;
307
- });
308
- if (!hasValidLimits) {
309
- if (safetyChecker.isSafe(node, context)) {
310
- return;
311
- }
312
- context.report({
313
- node,
314
- messageId: 'unlimitedFileOperations',
315
- data: {
316
- filePath: filename,
317
- line: String(node.loc.start.line),
318
- },
319
- });
320
- }
321
- }
322
- return;
323
- }
324
- if (callee.type === 'MemberExpression' &&
325
- callee.object.type === 'Identifier' &&
326
- callee.object.name === 'fs' &&
327
- callee.property.type === 'Identifier' &&
328
- ['readFile', 'writeFile', 'readFileSync', 'writeFileSync'].includes(callee.property.name)) {
329
- const args = node.arguments;
330
- if (args.length > 0) {
331
- const pathArg = args[0];
332
- if (pathArg.type === 'CallExpression' &&
333
- pathArg.callee.type === 'MemberExpression' &&
334
- pathArg.callee.object.type === 'Identifier' &&
335
- pathArg.callee.object.name === 'path' &&
336
- pathArg.callee.property.type === 'Identifier' &&
337
- (pathArg.callee.property.name === 'join' || pathArg.callee.property.name === 'resolve')) {
338
- const pathArgs = pathArg.arguments;
339
- if (pathArgs.length > 0 &&
340
- pathArgs[0].type === 'Identifier' &&
341
- pathArgs[0].name === '__dirname' &&
342
- pathArgs.slice(1).every(arg => arg.type === 'Literal')) {
343
- return;
344
- }
345
- }
346
- if (pathArg.type !== 'SpreadElement' && isUserInput(pathArg)) {
347
- if (safetyChecker.isSafe(node, context)) {
348
- return;
349
- }
350
- context.report({
351
- node: pathArg,
352
- messageId: 'unlimitedFileOperations',
353
- data: {
354
- filePath: filename,
355
- line: String(node.loc.start.line),
356
- },
357
- });
358
- }
359
- }
360
- }
361
- if (callee.type === 'Identifier' && callee.name === 'Array') {
362
- const args = node.arguments;
363
- if (args.length === 1) {
364
- const sizeArg = args[0];
365
- if (sizeArg.type !== 'SpreadElement' && isUserInput(sizeArg)) {
366
- if (safetyChecker.isSafe(node, context)) {
367
- return;
368
- }
369
- context.report({
370
- node: sizeArg,
371
- messageId: 'unlimitedMemoryAllocation',
372
- data: {
373
- filePath: filename,
374
- line: String(node.loc.start.line),
375
- },
376
- });
377
- }
378
- }
379
- }
380
- if (calleeText.includes('unzipper') || calleeText.includes('Extract')) {
381
- context.report({
382
- node,
383
- messageId: 'unlimitedFileOperations',
384
- data: {
385
- filePath: filename,
386
- line: String(node.loc.start.line),
387
- },
388
- });
389
- }
390
- if (calleeText.includes('xml2js') || calleeText.includes('parseString')) {
391
- context.report({
392
- node,
393
- messageId: 'unlimitedMemoryAllocation',
394
- data: {
395
- filePath: filename,
396
- line: String(node.loc.start.line),
397
- },
398
- });
399
- }
400
- if (calleeText.includes('set') && sourceCode.getText(node).includes('Buffer.alloc')) {
401
- const args = node.arguments;
402
- if (args.length >= 2) {
403
- const valueArg = args[1];
404
- const valueText = sourceCode.getText(valueArg);
405
- if (valueText.includes('Buffer.alloc') && valueText.includes('length')) {
406
- context.report({
407
- node,
408
- messageId: 'unlimitedMemoryAllocation',
409
- data: {
410
- filePath: filename,
411
- line: String(node.loc.start.line),
412
- },
413
- });
414
- }
415
- }
416
- }
417
- if (calleeText.includes('map') || calleeText.includes('forEach')) {
418
- const args = node.arguments;
419
- if (args.length > 0) {
420
- const callbackArg = args[0];
421
- const callbackText = sourceCode.getText(callbackArg);
422
- if (callbackText.includes('Object.keys') && callbackText.includes('map')) {
423
- context.report({
424
- node,
425
- messageId: 'unlimitedMemoryAllocation',
426
- data: {
427
- filePath: filename,
428
- line: String(node.loc.start.line),
429
- },
430
- });
431
- }
432
- }
433
- }
434
- if ((0, eslint_devkit_3.isInsideLoop)(node)) {
435
- if (calleeText.includes('alloc') ||
436
- calleeText.includes('Array') ||
437
- calleeText.includes('Buffer') ||
438
- calleeText.includes('readFile') ||
439
- calleeText.includes('writeFile')) {
440
- if (calleeText === 'Array.isArray' ||
441
- calleeText === 'Array.from' ||
442
- calleeText === 'Array.of') {
443
- }
444
- else {
445
- if (safetyChecker.isSafe(node, context)) {
446
- return;
447
- }
448
- const firstArg = node.arguments[0];
449
- if (firstArg?.type === 'Literal' && typeof firstArg.value === 'number') {
450
- return;
451
- }
452
- const parent = node.parent;
453
- if (parent && parent.type === 'AssignmentExpression' &&
454
- parent.left.type === 'MemberExpression' &&
455
- parent.left.object.type === 'Identifier') {
456
- return;
457
- }
458
- context.report({
459
- node,
460
- messageId: 'resourceAllocationInLoop',
461
- data: {
462
- filePath: filename,
463
- line: String(node.loc.start.line),
464
- },
465
- });
466
- }
467
- }
468
- }
469
- },
470
- NewExpression(node) {
471
- const callee = node.callee;
472
- if (callee.type === 'Identifier' && callee.name === 'Buffer') {
473
- const args = node.arguments;
474
- if (args.length > 0) {
475
- const sizeArg = args[0];
476
- if (requireResourceValidation &&
477
- sizeArg.type !== 'SpreadElement' &&
478
- isUserInput(sizeArg) &&
479
- !hasSizeValidation(node)) {
480
- if (safetyChecker.isSafe(node, context)) {
481
- return;
482
- }
483
- context.report({
484
- node: sizeArg,
485
- messageId: 'userControlledResourceSize',
486
- data: {
487
- filePath: filename,
488
- line: String(node.loc.start.line),
489
- },
490
- });
491
- return;
492
- }
493
- const estimatedSize = sizeArg.type === 'SpreadElement' ? null : estimateResourceSize(sizeArg);
494
- if (estimatedSize && estimatedSize > maxResourceSize) {
495
- if (safetyChecker.isSafe(node, context)) {
496
- return;
497
- }
498
- context.report({
499
- node: sizeArg,
500
- messageId: 'unlimitedBufferAllocation',
501
- data: {
502
- filePath: filename,
503
- line: String(node.loc.start.line),
504
- },
505
- });
506
- return;
507
- }
508
- }
509
- }
510
- if (callee.type === 'Identifier' && callee.name === 'Array') {
511
- const args = node.arguments;
512
- if (args.length === 1) {
513
- const sizeArg = args[0];
514
- if (sizeArg.type !== 'SpreadElement' && isUserInput(sizeArg)) {
515
- if (safetyChecker.isSafe(node, context)) {
516
- return;
517
- }
518
- context.report({
519
- node: sizeArg,
520
- messageId: 'unlimitedMemoryAllocation',
521
- data: {
522
- filePath: filename,
523
- line: String(node.loc.start.line),
524
- },
525
- });
526
- }
527
- }
528
- }
529
- if ((0, eslint_devkit_3.isInsideLoop)(node)) {
530
- const newCalleeText = sourceCode.getText(callee);
531
- if (newCalleeText.includes('Buffer') ||
532
- newCalleeText.includes('Array') ||
533
- newCalleeText.includes('Map') ||
534
- newCalleeText.includes('Set')) {
535
- if (safetyChecker.isSafe(node, context)) {
536
- return;
537
- }
538
- const firstArg = node.arguments[0];
539
- if (firstArg?.type === 'Literal' && typeof firstArg.value === 'number') {
540
- return;
541
- }
542
- context.report({
543
- node,
544
- messageId: 'resourceAllocationInLoop',
545
- data: {
546
- filePath: filename,
547
- line: String(node.loc.start.line),
548
- },
549
- });
550
- }
551
- }
552
- }
553
- };
554
- },
555
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnlimitedResourceAllocation=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.noUnlimitedResourceAllocation=(0,eslint_devkit_1.createRule)({name:"no-unlimited-resource-allocation",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-unlimited-resource-allocation.md",description:"Detects unlimited resource allocation that could cause DoS",cwe:"CWE-770"},messages:{unlimitedResourceAllocation:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unlimited Resource Allocation",cwe:"CWE-770",description:"Resource allocation without limits",severity:"{{severity}}",fix:"{{safeAlternative}}",documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),unlimitedBufferAllocation:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unlimited Buffer Allocation",cwe:"CWE-770",description:"Buffer allocated without size limits",severity:"HIGH",fix:"Set maximum buffer size limits",documentationLink:"https://nodejs.org/api/buffer.html"}),unlimitedFileOperations:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unlimited File Operations",cwe:"CWE-770",description:"File operations without size limits",severity:"MEDIUM",fix:"Validate file size before operations",documentationLink:"https://nodejs.org/api/fs.html"}),unlimitedNetworkConnections:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unlimited Network Connections",cwe:"CWE-770",description:"Network connections without limits",severity:"MEDIUM",fix:"Limit concurrent connections",documentationLink:"https://nodejs.org/api/http.html"}),unlimitedMemoryAllocation:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unlimited Memory Allocation",cwe:"CWE-770",description:"Memory allocated without limits",severity:"MEDIUM",fix:"Set memory allocation limits",documentationLink:"https://nodejs.org/api/buffer.html"}),userControlledResourceSize:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"User Controlled Resource Size",cwe:"CWE-770",description:"Resource size controlled by user input",severity:"HIGH",fix:"Validate and limit user-controlled resource sizes",documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),missingResourceLimits:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Missing Resource Limits",cwe:"CWE-770",description:"Resource allocation lacks proper limits",severity:"MEDIUM",fix:"Implement resource size validation",documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),resourceAllocationInLoop:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Resource Allocation in Loop",cwe:"CWE-770",description:"Resource allocation inside loop without limits",severity:"HIGH",fix:"Move resource allocation outside loop or add iteration limits",documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),implementResourceLimits:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Implement Resource Limits",description:"Add limits to resource allocation",severity:"LOW",fix:"const limitedSize = Math.min(userSize, MAX_SIZE);",documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),validateResourceSize:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Validate Resource Size",description:"Validate resource size before allocation",severity:"LOW",fix:'if (size > MAX_SIZE) throw new Error("Size too large");',documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),useResourcePools:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Use Resource Pools",description:"Use resource pools for better control",severity:"LOW",fix:"Implement connection pooling and resource reuse",documentationLink:"https://en.wikipedia.org/wiki/Object_pool_pattern"}),strategyResourceManagement:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Resource Management Strategy",description:"Implement comprehensive resource management",severity:"LOW",fix:"Use resource pools, limits, and cleanup mechanisms",documentationLink:"https://cwe.mitre.org/data/definitions/770.html"}),strategyRateLimiting:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Rate Limiting Strategy",description:"Implement rate limiting for resource allocation",severity:"LOW",fix:"Use rate limiters to prevent resource exhaustion",documentationLink:"https://en.wikipedia.org/wiki/Rate_limiting"}),strategyResourceCleanup:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Resource Cleanup Strategy",description:"Ensure proper resource cleanup",severity:"LOW",fix:"Implement try-finally blocks and resource disposal",documentationLink:"https://en.wikipedia.org/wiki/Resource_management_(computing)"})},schema:[{type:"object",properties:{maxResourceSize:{type:"number",minimum:1024,default:1048576,description:"Allocation size in bytes above which a call is reported"},userInputVariables:{type:"array",items:{type:"string"},default:["req","request","body","query","params","input","data"],description:"Variable names treated as user-controlled input"},safeResourceFunctions:{type:"array",items:{type:"string"},default:["validateSize","checkLimits","limitResource","safeAlloc"],description:"Function names that bound an allocation"},requireResourceValidation:{type:"boolean",default:true,description:"Require an explicit size check before allocating"},trustedSanitizers:{type:"array",items:{type:"string"},default:[],description:"Additional function names to consider as resource validators"},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:[{maxResourceSize:1048576,userInputVariables:["req","request","body","query","params","input","data"],safeResourceFunctions:["validateSize","checkLimits","limitResource","safeAlloc"],requireResourceValidation:true,trustedSanitizers:[],trustedAnnotations:[],strictMode:false}],create(context){const options=context.options[0]||{};const{maxResourceSize=1048576,userInputVariables=["req","request","body","query","params","input","data"],safeResourceFunctions=["validateSize","checkLimits","limitResource","safeAlloc"],requireResourceValidation=true,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 isUserInput=expression=>(0,eslint_devkit_3.isUserInputExpression)(expression,sourceCode,userInputVariables);const hasSizeValidation=node=>{const args=node.arguments;const sizeArg=args[0];const sizeText=sourceCode.getText(sizeArg);return sizeText.includes("Math.min(")||sizeText.includes("Math.max(")||sizeText.includes("Math.clamp(")||safeResourceFunctions.some(func=>sizeText.includes(func))};const estimateResourceSize=sizeExpression=>{if(sizeExpression.type==="Literal"&&typeof sizeExpression.value==="number"){return sizeExpression.value}if(sizeExpression.type==="BinaryExpression"){const left=estimateResourceSize(sizeExpression.left);const right=estimateResourceSize(sizeExpression.right);if(left!==null&&right!==null){switch(sizeExpression.operator){case"*":return left*right;case"+":return left+right;case"-":return left-right;case"/":return right!==0?left/right:null;default:return null}}}return null};return{CallExpression(node){const callee=node.callee;const calleeText=sourceCode.getText(callee);const isBufferAlloc=callee.type==="MemberExpression"&&callee.object.type==="Identifier"&&callee.object.name==="Buffer"&&callee.property.type==="Identifier"&&(callee.property.name==="alloc"||callee.property.name==="allocUnsafe");const isNewBuffer=callee.type==="NewExpression"&&callee.callee.type==="Identifier"&&callee.callee.name==="Buffer";if(isBufferAlloc||isNewBuffer){const args=node.arguments;if(args.length>0){const sizeArg=args[0];if(requireResourceValidation&&sizeArg.type!=="SpreadElement"&&isUserInput(sizeArg)&&!hasSizeValidation(node)){if(safetyChecker.isSafe(node,context)){return}context.report({node:sizeArg,messageId:"userControlledResourceSize",data:{filePath:filename,line:String(node.loc.start.line)}});return}const estimatedSize=sizeArg.type==="SpreadElement"?null:estimateResourceSize(sizeArg);if(estimatedSize&&estimatedSize>maxResourceSize){if(safetyChecker.isSafe(node,context)){return}context.report({node:sizeArg,messageId:"unlimitedBufferAllocation",data:{filePath:filename,line:String(node.loc.start.line)}});return}}}if(callee.type==="Identifier"&&callee.name==="multer"){const args=node.arguments;if(args.length>0&&args[0].type==="ObjectExpression"){const props=args[0].properties;const hasValidLimits=props.some(prop=>{if(prop.type!=="Property"||prop.key.type!=="Identifier"){return false}if(prop.key.name==="fileSize")return true;if(prop.key.name==="limits"&&prop.value.type==="ObjectExpression"){return prop.value.properties.some(limitProp=>limitProp.type==="Property"&&limitProp.key.type==="Identifier"&&limitProp.key.name==="fileSize")}return false});if(!hasValidLimits){if(safetyChecker.isSafe(node,context)){return}context.report({node,messageId:"unlimitedFileOperations",data:{filePath:filename,line:String(node.loc.start.line)}})}}return}if(callee.type==="MemberExpression"&&callee.object.type==="Identifier"&&callee.object.name==="fs"&&callee.property.type==="Identifier"&&["readFile","writeFile","readFileSync","writeFileSync"].includes(callee.property.name)){const args=node.arguments;if(args.length>0){const pathArg=args[0];if(pathArg.type==="CallExpression"&&pathArg.callee.type==="MemberExpression"&&pathArg.callee.object.type==="Identifier"&&pathArg.callee.object.name==="path"&&pathArg.callee.property.type==="Identifier"&&(pathArg.callee.property.name==="join"||pathArg.callee.property.name==="resolve")){const pathArgs=pathArg.arguments;if(pathArgs.length>0&&pathArgs[0].type==="Identifier"&&pathArgs[0].name==="__dirname"&&pathArgs.slice(1).every(arg=>arg.type==="Literal")){return}}if(pathArg.type!=="SpreadElement"&&isUserInput(pathArg)){if(safetyChecker.isSafe(node,context)){return}context.report({node:pathArg,messageId:"unlimitedFileOperations",data:{filePath:filename,line:String(node.loc.start.line)}})}}}if(callee.type==="Identifier"&&callee.name==="Array"){const args=node.arguments;if(args.length===1){const sizeArg=args[0];if(sizeArg.type!=="SpreadElement"&&isUserInput(sizeArg)){if(safetyChecker.isSafe(node,context)){return}context.report({node:sizeArg,messageId:"unlimitedMemoryAllocation",data:{filePath:filename,line:String(node.loc.start.line)}})}}}if(calleeText.includes("unzipper")||calleeText.includes("Extract")){context.report({node,messageId:"unlimitedFileOperations",data:{filePath:filename,line:String(node.loc.start.line)}})}if(calleeText.includes("xml2js")||calleeText.includes("parseString")){context.report({node,messageId:"unlimitedMemoryAllocation",data:{filePath:filename,line:String(node.loc.start.line)}})}if(calleeText.includes("set")&&sourceCode.getText(node).includes("Buffer.alloc")){const args=node.arguments;if(args.length>=2){const valueArg=args[1];const valueText=sourceCode.getText(valueArg);if(valueText.includes("Buffer.alloc")&&valueText.includes("length")){context.report({node,messageId:"unlimitedMemoryAllocation",data:{filePath:filename,line:String(node.loc.start.line)}})}}}if(calleeText.includes("map")||calleeText.includes("forEach")){const args=node.arguments;if(args.length>0){const callbackArg=args[0];const callbackText=sourceCode.getText(callbackArg);if(callbackText.includes("Object.keys")&&callbackText.includes("map")){context.report({node,messageId:"unlimitedMemoryAllocation",data:{filePath:filename,line:String(node.loc.start.line)}})}}}if((0,eslint_devkit_3.isInsideLoop)(node)){if(calleeText.includes("alloc")||calleeText.includes("Array")||calleeText.includes("Buffer")||calleeText.includes("readFile")||calleeText.includes("writeFile")){if(calleeText==="Array.isArray"||calleeText==="Array.from"||calleeText==="Array.of"){}else{if(safetyChecker.isSafe(node,context)){return}const firstArg=node.arguments[0];if(firstArg?.type==="Literal"&&typeof firstArg.value==="number"){return}const parent=node.parent;if(parent&&parent.type==="AssignmentExpression"&&parent.left.type==="MemberExpression"&&parent.left.object.type==="Identifier"){return}context.report({node,messageId:"resourceAllocationInLoop",data:{filePath:filename,line:String(node.loc.start.line)}})}}}},NewExpression(node){const callee=node.callee;if(callee.type==="Identifier"&&callee.name==="Buffer"){const args=node.arguments;if(args.length>0){const sizeArg=args[0];if(requireResourceValidation&&sizeArg.type!=="SpreadElement"&&isUserInput(sizeArg)&&!hasSizeValidation(node)){if(safetyChecker.isSafe(node,context)){return}context.report({node:sizeArg,messageId:"userControlledResourceSize",data:{filePath:filename,line:String(node.loc.start.line)}});return}const estimatedSize=sizeArg.type==="SpreadElement"?null:estimateResourceSize(sizeArg);if(estimatedSize&&estimatedSize>maxResourceSize){if(safetyChecker.isSafe(node,context)){return}context.report({node:sizeArg,messageId:"unlimitedBufferAllocation",data:{filePath:filename,line:String(node.loc.start.line)}});return}}}if(callee.type==="Identifier"&&callee.name==="Array"){const args=node.arguments;if(args.length===1){const sizeArg=args[0];if(sizeArg.type!=="SpreadElement"&&isUserInput(sizeArg)){if(safetyChecker.isSafe(node,context)){return}context.report({node:sizeArg,messageId:"unlimitedMemoryAllocation",data:{filePath:filename,line:String(node.loc.start.line)}})}}}if((0,eslint_devkit_3.isInsideLoop)(node)){const newCalleeText=sourceCode.getText(callee);if(newCalleeText.includes("Buffer")||newCalleeText.includes("Array")||newCalleeText.includes("Map")||newCalleeText.includes("Set")){if(safetyChecker.isSafe(node,context)){return}const firstArg=node.arguments[0];if(firstArg?.type==="Literal"&&typeof firstArg.value==="number"){return}context.report({node,messageId:"resourceAllocationInLoop",data:{filePath:filename,line:String(node.loc.start.line)}})}}}}}});