eslint-plugin-secure-coding 3.4.0 → 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.
- package/package.json +5 -18
- package/src/index.d.ts +17 -23
- package/src/index.js +2 -131
- package/src/oxlint.d.ts +17 -20
- package/src/oxlint.js +0 -18
- package/src/rules/detect-non-literal-regexp/index.js +5 -79
- package/src/rules/detect-object-injection/index.js +2 -221
- package/src/rules/detect-weak-password-validation/index.js +0 -12
- package/src/rules/no-directive-injection/index.js +0 -33
- package/src/rules/no-electron-security-issues/index.js +0 -41
- package/src/rules/no-format-string-injection/index.js +0 -82
- package/src/rules/no-graphql-injection/index.js +8 -133
- package/src/rules/no-hardcoded-credentials/index.js +3 -297
- package/src/rules/no-hardcoded-session-tokens/index.js +0 -11
- package/src/rules/no-improper-sanitization/index.js +0 -44
- package/src/rules/no-improper-type-validation/index.js +1 -51
- package/src/rules/no-insecure-comparison/index.js +2 -43
- package/src/rules/no-ldap-injection/index.js +8 -61
- package/src/rules/no-missing-authentication/index.js +2 -56
- package/src/rules/no-pii-in-logs/index.js +0 -13
- package/src/rules/no-privilege-escalation/index.js +2 -49
- package/src/rules/no-redos-vulnerable-regex/index.js +3 -49
- package/src/rules/no-sensitive-data-exposure/index.js +5 -29
- package/src/rules/no-template-injection/index.js +0 -6
- package/src/rules/no-unchecked-loop-condition/index.js +3 -99
- package/src/rules/no-unlimited-resource-allocation/index.js +2 -75
- package/src/rules/no-unsafe-deserialization/index.js +1 -111
- package/src/rules/no-unsafe-regex-construction/index.js +22 -62
- package/src/rules/no-weak-password-recovery/index.js +0 -43
- package/src/rules/no-xpath-injection/index.js +13 -75
- package/src/rules/no-xxe-injection/index.js +1 -47
- package/src/rules/require-backend-authorization/index.js +0 -10
- package/src/rules/require-secure-defaults/index.js +0 -10
- package/src/types/index.js +0 -5
- package/AGENTS.md +0 -131
- package/CHANGELOG.md +0 -379
- package/src/rules/detect-non-literal-regexp/index.d.ts +0 -28
- package/src/rules/detect-object-injection/index.d.ts +0 -35
- package/src/rules/detect-weak-password-validation/index.d.ts +0 -12
- package/src/rules/no-directive-injection/index.d.ts +0 -38
- package/src/rules/no-electron-security-issues/index.d.ts +0 -36
- package/src/rules/no-format-string-injection/index.d.ts +0 -44
- package/src/rules/no-graphql-injection/index.d.ts +0 -47
- package/src/rules/no-hardcoded-credentials/index.d.ts +0 -95
- package/src/rules/no-hardcoded-session-tokens/index.d.ts +0 -12
- package/src/rules/no-improper-sanitization/index.d.ts +0 -38
- package/src/rules/no-improper-type-validation/index.d.ts +0 -36
- package/src/rules/no-insecure-comparison/index.d.ts +0 -26
- package/src/rules/no-ldap-injection/index.d.ts +0 -53
- package/src/rules/no-missing-authentication/index.d.ts +0 -32
- package/src/rules/no-pii-in-logs/index.d.ts +0 -12
- package/src/rules/no-privilege-escalation/index.d.ts +0 -32
- package/src/rules/no-redos-vulnerable-regex/index.d.ts +0 -28
- package/src/rules/no-sensitive-data-exposure/index.d.ts +0 -30
- package/src/rules/no-template-injection/index.d.ts +0 -23
- package/src/rules/no-unchecked-loop-condition/index.d.ts +0 -38
- package/src/rules/no-unlimited-resource-allocation/index.d.ts +0 -38
- package/src/rules/no-unsafe-deserialization/index.d.ts +0 -40
- package/src/rules/no-unsafe-regex-construction/index.d.ts +0 -30
- package/src/rules/no-weak-password-recovery/index.d.ts +0 -38
- package/src/rules/no-xpath-injection/index.d.ts +0 -39
- package/src/rules/no-xxe-injection/index.d.ts +0 -36
- package/src/rules/require-backend-authorization/index.d.ts +0 -12
- 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.noUnlimitedResourceAllocation = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -148,7 +143,7 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
148
143
|
maxResourceSize: {
|
|
149
144
|
type: 'number',
|
|
150
145
|
minimum: 1024,
|
|
151
|
-
default: 1048576,
|
|
146
|
+
default: 1048576,
|
|
152
147
|
},
|
|
153
148
|
userInputVariables: {
|
|
154
149
|
type: 'array',
|
|
@@ -188,7 +183,7 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
188
183
|
},
|
|
189
184
|
defaultOptions: [
|
|
190
185
|
{
|
|
191
|
-
maxResourceSize: 1048576,
|
|
186
|
+
maxResourceSize: 1048576,
|
|
192
187
|
userInputVariables: ['req', 'request', 'body', 'query', 'params', 'input', 'data'],
|
|
193
188
|
safeResourceFunctions: ['validateSize', 'checkLimits', 'limitResource', 'safeAlloc'],
|
|
194
189
|
requireResourceValidation: true,
|
|
@@ -202,41 +197,26 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
202
197
|
const { maxResourceSize = 1048576, userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data'], safeResourceFunctions = ['validateSize', 'checkLimits', 'limitResource', 'safeAlloc'], requireResourceValidation = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
203
198
|
const sourceCode = context.sourceCode;
|
|
204
199
|
const filename = context.filename;
|
|
205
|
-
// Create safety checker for false positive detection
|
|
206
200
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
207
201
|
trustedSanitizers,
|
|
208
202
|
trustedAnnotations,
|
|
209
203
|
trustedOrmPatterns: [],
|
|
210
204
|
strictMode,
|
|
211
205
|
});
|
|
212
|
-
/**
|
|
213
|
-
* Check if an expression contains user input - using shared utility
|
|
214
|
-
*/
|
|
215
206
|
const isUserInput = (expression) => (0, eslint_devkit_3.isUserInputExpression)(expression, sourceCode, userInputVariables);
|
|
216
|
-
/**
|
|
217
|
-
* Check if resource allocation has size validation
|
|
218
|
-
*/
|
|
219
|
-
// All call sites invoke this only after already confirming `args.length > 0`,
|
|
220
|
-
// so the "no arguments" case is unreachable and intentionally not handled here.
|
|
221
207
|
const hasSizeValidation = (node) => {
|
|
222
208
|
const args = node.arguments;
|
|
223
|
-
// Check if size argument is a validated expression
|
|
224
209
|
const sizeArg = args[0];
|
|
225
210
|
const sizeText = sourceCode.getText(sizeArg);
|
|
226
|
-
// Look for validation patterns
|
|
227
211
|
return sizeText.includes('Math.min(') ||
|
|
228
212
|
sizeText.includes('Math.max(') ||
|
|
229
213
|
sizeText.includes('Math.clamp(') ||
|
|
230
214
|
safeResourceFunctions.some(func => sizeText.includes(func));
|
|
231
215
|
};
|
|
232
|
-
/**
|
|
233
|
-
* Estimate resource size from static analysis
|
|
234
|
-
*/
|
|
235
216
|
const estimateResourceSize = (sizeExpression) => {
|
|
236
217
|
if (sizeExpression.type === 'Literal' && typeof sizeExpression.value === 'number') {
|
|
237
218
|
return sizeExpression.value;
|
|
238
219
|
}
|
|
239
|
-
// Handle binary expressions like 1024 * 1024 * 100
|
|
240
220
|
if (sizeExpression.type === 'BinaryExpression') {
|
|
241
221
|
const left = estimateResourceSize(sizeExpression.left);
|
|
242
222
|
const right = estimateResourceSize(sizeExpression.right);
|
|
@@ -258,11 +238,9 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
258
238
|
return null;
|
|
259
239
|
};
|
|
260
240
|
return {
|
|
261
|
-
// Check Buffer allocation
|
|
262
241
|
CallExpression(node) {
|
|
263
242
|
const callee = node.callee;
|
|
264
243
|
const calleeText = sourceCode.getText(callee);
|
|
265
|
-
// Check for Buffer.alloc(), Buffer.allocUnsafe() or new Buffer()
|
|
266
244
|
const isBufferAlloc = callee.type === 'MemberExpression' &&
|
|
267
245
|
callee.object.type === 'Identifier' &&
|
|
268
246
|
callee.object.name === 'Buffer' &&
|
|
@@ -275,10 +253,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
275
253
|
const args = node.arguments;
|
|
276
254
|
if (args.length > 0) {
|
|
277
255
|
const sizeArg = args[0];
|
|
278
|
-
// Check if size comes from user input (but skip if validated).
|
|
279
|
-
// When `requireResourceValidation` is disabled, callers have opted
|
|
280
|
-
// out of the "must call a validator" requirement entirely, so this
|
|
281
|
-
// branch is skipped regardless of whether validation is present.
|
|
282
256
|
if (requireResourceValidation &&
|
|
283
257
|
sizeArg.type !== 'SpreadElement' &&
|
|
284
258
|
isUserInput(sizeArg) &&
|
|
@@ -296,7 +270,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
296
270
|
});
|
|
297
271
|
return;
|
|
298
272
|
}
|
|
299
|
-
// Check if size exceeds limits
|
|
300
273
|
const estimatedSize = sizeArg.type === 'SpreadElement' ? null : estimateResourceSize(sizeArg);
|
|
301
274
|
if (estimatedSize && estimatedSize > maxResourceSize) {
|
|
302
275
|
if (safetyChecker.isSafe(node, context)) {
|
|
@@ -312,29 +285,18 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
312
285
|
});
|
|
313
286
|
return;
|
|
314
287
|
}
|
|
315
|
-
// NOTE: a `missingResourceLimits` check previously lived here for
|
|
316
|
-
// "non-literal, user-input sizes with no validation" — but that
|
|
317
|
-
// condition is a strict subset of the userControlledResourceSize
|
|
318
|
-
// check above (same `isUserInput(sizeArg) && !hasSizeValidation(node)`
|
|
319
|
-
// predicate), which already reports and returns first. It was
|
|
320
|
-
// therefore unreachable dead code and has been removed
|
|
321
|
-
// (behavior-neutral: no observable path changes).
|
|
322
288
|
}
|
|
323
289
|
}
|
|
324
|
-
// Check for multer configuration without limits
|
|
325
290
|
if (callee.type === 'Identifier' && callee.name === 'multer') {
|
|
326
291
|
const args = node.arguments;
|
|
327
292
|
if (args.length > 0 && args[0].type === 'ObjectExpression') {
|
|
328
293
|
const props = args[0].properties;
|
|
329
|
-
// Check for valid limits definition
|
|
330
294
|
const hasValidLimits = props.some((prop) => {
|
|
331
295
|
if (prop.type !== 'Property' || prop.key.type !== 'Identifier') {
|
|
332
296
|
return false;
|
|
333
297
|
}
|
|
334
|
-
// Direct fileSize (not standard but maybe used?)
|
|
335
298
|
if (prop.key.name === 'fileSize')
|
|
336
299
|
return true;
|
|
337
|
-
// Limits object
|
|
338
300
|
if (prop.key.name === 'limits' && prop.value.type === 'ObjectExpression') {
|
|
339
301
|
return prop.value.properties.some((limitProp) => limitProp.type === 'Property' &&
|
|
340
302
|
limitProp.key.type === 'Identifier' &&
|
|
@@ -358,7 +320,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
358
320
|
}
|
|
359
321
|
return;
|
|
360
322
|
}
|
|
361
|
-
// Check for fs operations
|
|
362
323
|
if (callee.type === 'MemberExpression' &&
|
|
363
324
|
callee.object.type === 'Identifier' &&
|
|
364
325
|
callee.object.name === 'fs' &&
|
|
@@ -366,23 +327,18 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
366
327
|
['readFile', 'writeFile', 'readFileSync', 'writeFileSync'].includes(callee.property.name)) {
|
|
367
328
|
const args = node.arguments;
|
|
368
329
|
if (args.length > 0) {
|
|
369
|
-
// Check if file path comes from user input (potential for large files)
|
|
370
330
|
const pathArg = args[0];
|
|
371
|
-
// SAFE: Static path construction with path.join(__dirname, ...literals)
|
|
372
|
-
// This is a common pattern that doesn't involve user input
|
|
373
331
|
if (pathArg.type === 'CallExpression' &&
|
|
374
332
|
pathArg.callee.type === 'MemberExpression' &&
|
|
375
333
|
pathArg.callee.object.type === 'Identifier' &&
|
|
376
334
|
pathArg.callee.object.name === 'path' &&
|
|
377
335
|
pathArg.callee.property.type === 'Identifier' &&
|
|
378
336
|
(pathArg.callee.property.name === 'join' || pathArg.callee.property.name === 'resolve')) {
|
|
379
|
-
// Check if first arg is __dirname and all subsequent args are literals
|
|
380
337
|
const pathArgs = pathArg.arguments;
|
|
381
338
|
if (pathArgs.length > 0 &&
|
|
382
339
|
pathArgs[0].type === 'Identifier' &&
|
|
383
340
|
pathArgs[0].name === '__dirname' &&
|
|
384
341
|
pathArgs.slice(1).every(arg => arg.type === 'Literal')) {
|
|
385
|
-
// Safe: path.join(__dirname, 'static', 'path')
|
|
386
342
|
return;
|
|
387
343
|
}
|
|
388
344
|
}
|
|
@@ -401,7 +357,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
401
357
|
}
|
|
402
358
|
}
|
|
403
359
|
}
|
|
404
|
-
// Check for Array constructor with user input
|
|
405
360
|
if (callee.type === 'Identifier' && callee.name === 'Array') {
|
|
406
361
|
const args = node.arguments;
|
|
407
362
|
if (args.length === 1) {
|
|
@@ -421,10 +376,7 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
421
376
|
}
|
|
422
377
|
}
|
|
423
378
|
}
|
|
424
|
-
// Check for complex resource exhaustion patterns
|
|
425
|
-
// ZIP bomb detection - unlimited decompression
|
|
426
379
|
if (calleeText.includes('unzipper') || calleeText.includes('Extract')) {
|
|
427
|
-
// Check for unlimited ZIP extraction
|
|
428
380
|
context.report({
|
|
429
381
|
node,
|
|
430
382
|
messageId: 'unlimitedFileOperations',
|
|
@@ -434,7 +386,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
434
386
|
},
|
|
435
387
|
});
|
|
436
388
|
}
|
|
437
|
-
// XML expansion attack detection
|
|
438
389
|
if (calleeText.includes('xml2js') || calleeText.includes('parseString')) {
|
|
439
390
|
context.report({
|
|
440
391
|
node,
|
|
@@ -445,9 +396,7 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
445
396
|
},
|
|
446
397
|
});
|
|
447
398
|
}
|
|
448
|
-
// Check for cache with unlimited growth
|
|
449
399
|
if (calleeText.includes('set') && sourceCode.getText(node).includes('Buffer.alloc')) {
|
|
450
|
-
// Detect cache patterns that allocate buffers without limits
|
|
451
400
|
const args = node.arguments;
|
|
452
401
|
if (args.length >= 2) {
|
|
453
402
|
const valueArg = args[1];
|
|
@@ -464,13 +413,11 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
464
413
|
}
|
|
465
414
|
}
|
|
466
415
|
}
|
|
467
|
-
// Check for recursive data structure processing
|
|
468
416
|
if (calleeText.includes('map') || calleeText.includes('forEach')) {
|
|
469
417
|
const args = node.arguments;
|
|
470
418
|
if (args.length > 0) {
|
|
471
419
|
const callbackArg = args[0];
|
|
472
420
|
const callbackText = sourceCode.getText(callbackArg);
|
|
473
|
-
// Detect patterns that create arrays from nested object properties
|
|
474
421
|
if (callbackText.includes('Object.keys') && callbackText.includes('map')) {
|
|
475
422
|
context.report({
|
|
476
423
|
node,
|
|
@@ -483,37 +430,30 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
483
430
|
}
|
|
484
431
|
}
|
|
485
432
|
}
|
|
486
|
-
// Check for resource allocation inside loops
|
|
487
433
|
if ((0, eslint_devkit_3.isInsideLoop)(node)) {
|
|
488
|
-
// Check if this allocates resources
|
|
489
434
|
if (calleeText.includes('alloc') ||
|
|
490
435
|
calleeText.includes('Array') ||
|
|
491
436
|
calleeText.includes('Buffer') ||
|
|
492
437
|
calleeText.includes('readFile') ||
|
|
493
438
|
calleeText.includes('writeFile')) {
|
|
494
|
-
// SAFE: Array.isArray / Array.from / Array.of are not allocation hazards.
|
|
495
439
|
if (calleeText === 'Array.isArray' ||
|
|
496
440
|
calleeText === 'Array.from' ||
|
|
497
441
|
calleeText === 'Array.of') {
|
|
498
|
-
// Array.from / Array.of are fine; allocation comes from their args, not size
|
|
499
442
|
}
|
|
500
443
|
else {
|
|
501
444
|
if (safetyChecker.isSafe(node, context)) {
|
|
502
445
|
return;
|
|
503
446
|
}
|
|
504
|
-
// SAFE: first arg is a numeric literal — size is statically bounded.
|
|
505
447
|
const firstArg = node.arguments[0];
|
|
506
448
|
if (firstArg?.type === 'Literal' && typeof firstArg.value === 'number') {
|
|
507
449
|
return;
|
|
508
450
|
}
|
|
509
|
-
// Skip if this is an assignment to an array element (pre-allocated pattern)
|
|
510
451
|
const parent = node.parent;
|
|
511
452
|
if (parent && parent.type === 'AssignmentExpression' &&
|
|
512
453
|
parent.left.type === 'MemberExpression' &&
|
|
513
454
|
parent.left.object.type === 'Identifier') {
|
|
514
455
|
return;
|
|
515
456
|
}
|
|
516
|
-
// Report resourceAllocationInLoop - this can be in addition to user input errors
|
|
517
457
|
context.report({
|
|
518
458
|
node,
|
|
519
459
|
messageId: 'resourceAllocationInLoop',
|
|
@@ -526,17 +466,12 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
526
466
|
}
|
|
527
467
|
}
|
|
528
468
|
},
|
|
529
|
-
// Check new expressions for resource allocation
|
|
530
469
|
NewExpression(node) {
|
|
531
470
|
const callee = node.callee;
|
|
532
|
-
// Check for new Buffer() with user input
|
|
533
471
|
if (callee.type === 'Identifier' && callee.name === 'Buffer') {
|
|
534
472
|
const args = node.arguments;
|
|
535
473
|
if (args.length > 0) {
|
|
536
474
|
const sizeArg = args[0];
|
|
537
|
-
// Check if size comes from user input (but skip if validated).
|
|
538
|
-
// Disabling `requireResourceValidation` opts out of this check
|
|
539
|
-
// entirely, mirroring the CallExpression Buffer.alloc() branch.
|
|
540
475
|
if (requireResourceValidation &&
|
|
541
476
|
sizeArg.type !== 'SpreadElement' &&
|
|
542
477
|
isUserInput(sizeArg) &&
|
|
@@ -554,7 +489,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
554
489
|
});
|
|
555
490
|
return;
|
|
556
491
|
}
|
|
557
|
-
// Check if size exceeds limits
|
|
558
492
|
const estimatedSize = sizeArg.type === 'SpreadElement' ? null : estimateResourceSize(sizeArg);
|
|
559
493
|
if (estimatedSize && estimatedSize > maxResourceSize) {
|
|
560
494
|
if (safetyChecker.isSafe(node, context)) {
|
|
@@ -570,13 +504,8 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
570
504
|
});
|
|
571
505
|
return;
|
|
572
506
|
}
|
|
573
|
-
// NOTE: same unreachable `missingResourceLimits` pattern as the
|
|
574
|
-
// CallExpression Buffer.alloc handler above — removed for the
|
|
575
|
-
// identical reason (subset of the userControlledResourceSize
|
|
576
|
-
// check, which already reports and returns first).
|
|
577
507
|
}
|
|
578
508
|
}
|
|
579
|
-
// Check for new Array() with user input
|
|
580
509
|
if (callee.type === 'Identifier' && callee.name === 'Array') {
|
|
581
510
|
const args = node.arguments;
|
|
582
511
|
if (args.length === 1) {
|
|
@@ -596,7 +525,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
596
525
|
}
|
|
597
526
|
}
|
|
598
527
|
}
|
|
599
|
-
// Check for resource allocation inside loops
|
|
600
528
|
if ((0, eslint_devkit_3.isInsideLoop)(node)) {
|
|
601
529
|
const newCalleeText = sourceCode.getText(callee);
|
|
602
530
|
if (newCalleeText.includes('Buffer') ||
|
|
@@ -606,7 +534,6 @@ exports.noUnlimitedResourceAllocation = (0, eslint_devkit_1.createRule)({
|
|
|
606
534
|
if (safetyChecker.isSafe(node, context)) {
|
|
607
535
|
return;
|
|
608
536
|
}
|
|
609
|
-
// SAFE: first arg is a numeric literal — size is statically bounded.
|
|
610
537
|
const firstArg = node.arguments[0];
|
|
611
538
|
if (firstArg?.type === 'Literal' && typeof firstArg.value === 'number') {
|
|
612
539
|
return;
|
|
@@ -1,25 +1,10 @@
|
|
|
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.noUnsafeDeserialization = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
5
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
6
|
const eslint_devkit_3 = require("@interlace/eslint-devkit");
|
|
12
|
-
/**
|
|
13
|
-
* Timer functions that double as an implied-`eval` sink — but only when the
|
|
14
|
-
* first argument is a string. `setTimeout(fn, 0)` is a scheduler, not a
|
|
15
|
-
* deserializer.
|
|
16
|
-
*/
|
|
17
7
|
const TIMER_FUNCTIONS = new Set(['setTimeout', 'setInterval']);
|
|
18
|
-
/**
|
|
19
|
-
* True when the expression can only evaluate to a string, i.e. the argument
|
|
20
|
-
* would actually be compiled by `setTimeout` / `setInterval`. A function
|
|
21
|
-
* reference, an arrow function, or anything else is never implied-eval.
|
|
22
|
-
*/
|
|
23
8
|
function isStringValued(arg) {
|
|
24
9
|
if (!arg)
|
|
25
10
|
return false;
|
|
@@ -194,69 +179,40 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
194
179
|
const { dangerousFunctions = ['eval', 'Function', 'setTimeout', 'setInterval', 'unserialize', 'deserialize', 'parseUnsafe'], validationFunctions = ['validateInput', 'sanitizeData', 'checkSchema', 'validateSchema'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
195
180
|
const sourceCode = context.sourceCode;
|
|
196
181
|
const filename = context.filename;
|
|
197
|
-
// Create safety checker for false positive detection
|
|
198
182
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
199
183
|
trustedSanitizers,
|
|
200
184
|
trustedAnnotations,
|
|
201
185
|
trustedOrmPatterns: [],
|
|
202
186
|
strictMode,
|
|
203
187
|
});
|
|
204
|
-
// Track variables that have been validated/sanitized
|
|
205
188
|
const validatedVariables = new Set();
|
|
206
|
-
// Track variables that contain untrusted data
|
|
207
189
|
const untrustedVariables = new Set();
|
|
208
|
-
// Variables read from a literal-string file path (e.g. fs.readFileSync('config.json')).
|
|
209
|
-
// These are still "untrusted" for eval/Function but safe for JSON.parse and other
|
|
210
|
-
// schema-validating parsers because the file content is statically bundled.
|
|
211
190
|
const literalPathFileVars = new Set();
|
|
212
|
-
/**
|
|
213
|
-
* Check if this is a dangerous deserialization function
|
|
214
|
-
*/
|
|
215
191
|
const isDangerousDeserialization = (node) => {
|
|
216
192
|
const callee = node.callee;
|
|
217
|
-
// Check for dangerous function calls
|
|
218
193
|
if (callee.type === 'Identifier' && dangerousFunctions.includes(callee.name)) {
|
|
219
|
-
// `setTimeout` / `setInterval` are only a code-execution sink in their
|
|
220
|
-
// implied-eval form — when the FIRST argument is a *string* that the
|
|
221
|
-
// engine compiles. `setTimeout(callback, 1000)` is the ordinary timer
|
|
222
|
-
// form and is not deserialization of anything.
|
|
223
|
-
//
|
|
224
|
-
// Without this gate the rule reported
|
|
225
|
-
// `await new Promise(resolve => setTimeout(resolve, 1000))` at CVSS
|
|
226
|
-
// 9.8 CRITICAL, because `resolve` is an enclosing arrow-function
|
|
227
|
-
// parameter and every parameter is treated as untrusted input.
|
|
228
194
|
if (TIMER_FUNCTIONS.has(callee.name)) {
|
|
229
195
|
return isStringValued(node.arguments[0]);
|
|
230
196
|
}
|
|
231
197
|
return true;
|
|
232
198
|
}
|
|
233
|
-
// Check for member expressions like yaml.load, serialize.unserialize
|
|
234
199
|
if (callee.type === 'MemberExpression') {
|
|
235
200
|
const memberName = callee.property.type === 'Identifier' ? callee.property.name : '';
|
|
236
201
|
const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
|
|
237
|
-
// `super.deserialize(context)` / `this.deserialize(context)` is a class
|
|
238
|
-
// *implementing* a (de)serialization protocol and chaining to its own
|
|
239
|
-
// base implementation — webpack's serialization layer does this in
|
|
240
|
-
// every Dependency subclass. The argument is the framework's own
|
|
241
|
-
// context object, not attacker-controlled data.
|
|
242
202
|
if (callee.object.type === 'Super' || callee.object.type === 'ThisExpression') {
|
|
243
203
|
return false;
|
|
244
204
|
}
|
|
245
|
-
// Same implied-eval reasoning as above for `window.setTimeout(...)`.
|
|
246
205
|
if (TIMER_FUNCTIONS.has(memberName)) {
|
|
247
206
|
return isStringValued(node.arguments[0]);
|
|
248
207
|
}
|
|
249
|
-
// Check dangerous methods
|
|
250
208
|
if (dangerousFunctions.includes(memberName)) {
|
|
251
209
|
return true;
|
|
252
210
|
}
|
|
253
|
-
// Check dangerous libraries
|
|
254
211
|
if (['yaml', 'js-yaml', 'node-serialize', 'serialize-javascript'].includes(objectName.toLowerCase()) &&
|
|
255
212
|
['load', 'parse', 'unserialize', 'deserialize'].includes(memberName)) {
|
|
256
213
|
return true;
|
|
257
214
|
}
|
|
258
215
|
}
|
|
259
|
-
// Check for require() calls with dangerous libraries
|
|
260
216
|
if (callee.type === 'Identifier' && callee.name === 'require') {
|
|
261
217
|
const args = node.arguments;
|
|
262
218
|
if (args.length > 0 && args[0].type === 'Literal' && typeof args[0].value === 'string') {
|
|
@@ -268,21 +224,13 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
268
224
|
}
|
|
269
225
|
return false;
|
|
270
226
|
};
|
|
271
|
-
/**
|
|
272
|
-
* Check if input comes from untrusted source
|
|
273
|
-
*/
|
|
274
227
|
const isUntrustedInput = (inputNode) => {
|
|
275
|
-
// Concatenations and template literals carry their operands' trust:
|
|
276
|
-
// `setTimeout("alert(" + userCode + ")", 100)` is implied eval on
|
|
277
|
-
// untrusted input even though the argument node itself is a
|
|
278
|
-
// BinaryExpression rather than an Identifier.
|
|
279
228
|
if (inputNode.type === 'BinaryExpression' && inputNode.operator === '+') {
|
|
280
229
|
return isUntrustedInput(inputNode.left) || isUntrustedInput(inputNode.right);
|
|
281
230
|
}
|
|
282
231
|
if (inputNode.type === 'TemplateLiteral') {
|
|
283
232
|
return inputNode.expressions.some((e) => isUntrustedInput(e));
|
|
284
233
|
}
|
|
285
|
-
// Check for MemberExpression patterns like req.body, req.query, etc.
|
|
286
234
|
if (inputNode.type === 'MemberExpression') {
|
|
287
235
|
if (inputNode.object.type === 'Identifier' && inputNode.object.name === 'req') {
|
|
288
236
|
return true;
|
|
@@ -294,14 +242,9 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
294
242
|
}
|
|
295
243
|
}
|
|
296
244
|
if (inputNode.type === 'Identifier') {
|
|
297
|
-
// Check if this variable has been marked as untrusted
|
|
298
245
|
if (untrustedVariables.has(inputNode.name)) {
|
|
299
246
|
return true;
|
|
300
247
|
}
|
|
301
|
-
// Only consider variables untrusted if they actually come from req.* patterns
|
|
302
|
-
// Don't flag generic variable names like 'input', 'data', etc.
|
|
303
|
-
// unless they have been explicitly marked as untrusted
|
|
304
|
-
// Check if it comes from function parameters (these are potentially untrusted)
|
|
305
248
|
let current = inputNode;
|
|
306
249
|
while (current) {
|
|
307
250
|
if (current.type === 'FunctionDeclaration' ||
|
|
@@ -314,7 +257,7 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
314
257
|
}
|
|
315
258
|
return false;
|
|
316
259
|
})) {
|
|
317
|
-
return true;
|
|
260
|
+
return true;
|
|
318
261
|
}
|
|
319
262
|
}
|
|
320
263
|
current = current.parent;
|
|
@@ -322,9 +265,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
322
265
|
}
|
|
323
266
|
return false;
|
|
324
267
|
};
|
|
325
|
-
/**
|
|
326
|
-
* Check if input has been validated
|
|
327
|
-
*/
|
|
328
268
|
const isInputValidated = (inputNode) => {
|
|
329
269
|
let current = inputNode;
|
|
330
270
|
while (current) {
|
|
@@ -337,16 +277,11 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
337
277
|
}
|
|
338
278
|
return false;
|
|
339
279
|
};
|
|
340
|
-
/**
|
|
341
|
-
* Check if this is a safe deserialization library
|
|
342
|
-
*/
|
|
343
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
344
280
|
const isSafeLibrary = (node) => {
|
|
345
281
|
const callee = node.callee;
|
|
346
282
|
if (callee.type === 'MemberExpression') {
|
|
347
283
|
const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
|
|
348
284
|
const memberName = callee.property.type === 'Identifier' ? callee.property.name : '';
|
|
349
|
-
// Check for safe patterns
|
|
350
285
|
if (objectName === 'JSON' && memberName === 'parse') {
|
|
351
286
|
return true;
|
|
352
287
|
}
|
|
@@ -356,23 +291,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
356
291
|
}
|
|
357
292
|
return false;
|
|
358
293
|
};
|
|
359
|
-
/**
|
|
360
|
-
* True when the call sits inside a function that is itself named
|
|
361
|
-
* `deserialize` / `unserialize` / `fromJSON` — i.e. the file is
|
|
362
|
-
* *implementing* a (de)serialization protocol and delegating to the next
|
|
363
|
-
* layer of it, which is what webpack's whole `lib/serialization` tree and
|
|
364
|
-
* every `static deserialize(context)` factory does:
|
|
365
|
-
*
|
|
366
|
-
* static deserialize(context) {
|
|
367
|
-
* const obj = new CssModule({ ... });
|
|
368
|
-
* obj.deserialize(context); // ← not attacker-controlled
|
|
369
|
-
* return obj;
|
|
370
|
-
* }
|
|
371
|
-
*
|
|
372
|
-
* A rule that cannot see the protocol cannot judge it, and 33 of 35 corpus
|
|
373
|
-
* findings were exactly this shape.
|
|
374
|
-
*/
|
|
375
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
376
294
|
const isInsideDeserializerImplementation = (node) => {
|
|
377
295
|
const DESERIALIZER_NAMES = new Set(['deserialize', 'unserialize', 'fromJSON', 'fromBuffer']);
|
|
378
296
|
let current = node.parent;
|
|
@@ -399,7 +317,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
399
317
|
return false;
|
|
400
318
|
};
|
|
401
319
|
const checkCallExpression = (node) => {
|
|
402
|
-
// 1. Check Function Constructor (NewExpression or CallExpression)
|
|
403
320
|
if ((node.type === 'NewExpression' || node.type === 'CallExpression') &&
|
|
404
321
|
node.callee.type === 'Identifier' &&
|
|
405
322
|
node.callee.name === 'Function') {
|
|
@@ -421,24 +338,18 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
421
338
|
return;
|
|
422
339
|
}
|
|
423
340
|
}
|
|
424
|
-
// 2. Check CallExpressions (eval, unserialize, yaml, etc.)
|
|
425
341
|
if (isDangerousDeserialization(node) && !isInsideDeserializerImplementation(node)) {
|
|
426
342
|
const args = node.arguments;
|
|
427
343
|
const hasUntrustedInput = args.some((arg) => isUntrustedInput(arg));
|
|
428
|
-
// Check if explicit validation is present
|
|
429
344
|
const isSafe = isSafeLibrary(node);
|
|
430
345
|
if (!isSafe && hasUntrustedInput) {
|
|
431
|
-
// Basic safety check
|
|
432
346
|
const safe = safetyChecker.isSafe(node, context);
|
|
433
347
|
if (!safe) {
|
|
434
|
-
// Determine message ID
|
|
435
348
|
let messageId = 'unsafeDeserialization';
|
|
436
|
-
// Check specifically for YAML
|
|
437
349
|
const calleeText = sourceCode.getText(node.callee);
|
|
438
350
|
if (calleeText.includes('yaml') || calleeText.includes('YAML')) {
|
|
439
351
|
messageId = 'unsafeYamlParsing';
|
|
440
352
|
}
|
|
441
|
-
// Check for generic dangerous functions
|
|
442
353
|
if (node.callee.type === 'Identifier' && ['eval', 'setTimeout', 'setInterval'].includes(node.callee.name)) {
|
|
443
354
|
messageId = 'dangerousEvalUsage';
|
|
444
355
|
}
|
|
@@ -462,23 +373,18 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
462
373
|
}
|
|
463
374
|
}
|
|
464
375
|
}
|
|
465
|
-
// Check for untrusted input in potentially safe functions
|
|
466
376
|
if (isSafeLibrary(node)) {
|
|
467
377
|
const args = node.arguments;
|
|
468
378
|
const hasUntrustedInput = args.some((arg) => {
|
|
469
|
-
// Check if it's validated
|
|
470
379
|
if (arg.type === 'Identifier' && validatedVariables.has(arg.name)) {
|
|
471
380
|
return false;
|
|
472
381
|
}
|
|
473
|
-
// SAFE for JSON.parse/schema parsers: if the input came from a
|
|
474
|
-
// literal-path file read (bundled config), it is not user-controlled.
|
|
475
382
|
if (arg.type === 'Identifier' && literalPathFileVars.has(arg.name)) {
|
|
476
383
|
return false;
|
|
477
384
|
}
|
|
478
385
|
return isUntrustedInput(arg) && !isInputValidated(arg);
|
|
479
386
|
});
|
|
480
387
|
if (hasUntrustedInput) {
|
|
481
|
-
// Even JSON.parse can be unsafe if used on complex objects that get eval'd later
|
|
482
388
|
if (safetyChecker.isSafe(node, context)) {
|
|
483
389
|
return;
|
|
484
390
|
}
|
|
@@ -500,15 +406,12 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
500
406
|
}
|
|
501
407
|
};
|
|
502
408
|
return {
|
|
503
|
-
// Track variable assignments from untrusted sources
|
|
504
409
|
VariableDeclaration(node) {
|
|
505
410
|
for (const declarator of node.declarations) {
|
|
506
411
|
if (declarator.id.type === 'Identifier' && declarator.init) {
|
|
507
|
-
// Check if the initializer comes from an untrusted source
|
|
508
412
|
if (isUntrustedInput(declarator.init)) {
|
|
509
413
|
untrustedVariables.add(declarator.id.name);
|
|
510
414
|
}
|
|
511
|
-
// Check if it's assigned from fs operations or other untrusted sources
|
|
512
415
|
if (declarator.init.type === 'CallExpression') {
|
|
513
416
|
const callee = declarator.init.callee;
|
|
514
417
|
if (callee.type === 'MemberExpression' &&
|
|
@@ -517,8 +420,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
517
420
|
callee.property.type === 'Identifier' &&
|
|
518
421
|
['readFile', 'readFileSync'].includes(callee.property.name)) {
|
|
519
422
|
untrustedVariables.add(declarator.id.name);
|
|
520
|
-
// Track whether the path is a literal — used downstream to skip
|
|
521
|
-
// safe deserializers (JSON.parse) on known-static files.
|
|
522
423
|
const pathArg = declarator.init.arguments[0];
|
|
523
424
|
if (pathArg?.type === 'Literal' && typeof pathArg.value === 'string') {
|
|
524
425
|
literalPathFileVars.add(declarator.id.name);
|
|
@@ -528,32 +429,27 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
528
429
|
}
|
|
529
430
|
}
|
|
530
431
|
},
|
|
531
|
-
// Track assignment expressions
|
|
532
432
|
AssignmentExpression(node) {
|
|
533
433
|
if (node.left.type === 'Identifier' && isUntrustedInput(node.right)) {
|
|
534
434
|
untrustedVariables.add(node.left.name);
|
|
535
435
|
}
|
|
536
436
|
},
|
|
537
|
-
// Check dangerous function calls
|
|
538
437
|
CallExpression(node) {
|
|
539
438
|
checkCallExpression(node);
|
|
540
439
|
},
|
|
541
440
|
NewExpression(node) {
|
|
542
441
|
checkCallExpression(node);
|
|
543
442
|
},
|
|
544
|
-
// Check for dangerous require/import patterns
|
|
545
443
|
VariableDeclarator(node) {
|
|
546
444
|
if (!node.init) {
|
|
547
445
|
return;
|
|
548
446
|
}
|
|
549
|
-
// Track variables assigned from validation functions
|
|
550
447
|
if (node.id.type === 'Identifier' &&
|
|
551
448
|
node.init.type === 'CallExpression' &&
|
|
552
449
|
node.init.callee.type === 'Identifier' &&
|
|
553
450
|
(validationFunctions.includes(node.init.callee.name) || trustedSanitizers.includes(node.init.callee.name))) {
|
|
554
451
|
validatedVariables.add(node.id.name);
|
|
555
452
|
}
|
|
556
|
-
// Check for require/import of dangerous libraries
|
|
557
453
|
if (node.init.type === 'CallExpression' &&
|
|
558
454
|
node.init.callee.type === 'Identifier' &&
|
|
559
455
|
node.init.callee.name === 'require') {
|
|
@@ -561,16 +457,11 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
561
457
|
if (requireArg?.type === 'Literal' && typeof requireArg.value === 'string') {
|
|
562
458
|
const moduleName = requireArg.value;
|
|
563
459
|
if (['node-serialize', 'serialize-javascript', 'js-yaml', 'yaml'].includes(moduleName)) {
|
|
564
|
-
// Check if this variable is used unsafely later
|
|
565
460
|
if (node.id.type === 'Identifier') {
|
|
566
|
-
// Look ahead to see if this library is used dangerously
|
|
567
|
-
// This is a simplified check - in practice, we'd need more sophisticated analysis
|
|
568
461
|
const variables = sourceCode.getDeclaredVariables(node);
|
|
569
462
|
for (const variable of variables) {
|
|
570
463
|
for (const reference of variable.references) {
|
|
571
464
|
const refNode = reference.identifier;
|
|
572
|
-
// Check if reference is part of a call to dangerous method
|
|
573
|
-
// e.g. serialize.unserialize()
|
|
574
465
|
if (refNode.parent && refNode.parent.type === 'MemberExpression' &&
|
|
575
466
|
refNode.parent.object === refNode) {
|
|
576
467
|
const memberExpr = refNode.parent;
|
|
@@ -578,7 +469,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
578
469
|
if (['unserialize', 'deserialize', 'load', 'parse'].includes(propertyName)) {
|
|
579
470
|
const callExpr = memberExpr.parent;
|
|
580
471
|
if (callExpr && callExpr.type === 'CallExpression' && callExpr.callee === memberExpr) {
|
|
581
|
-
// FALSE POSITIVE REDUCTION
|
|
582
472
|
if (safetyChecker.isSafe(callExpr, context)) {
|
|
583
473
|
continue;
|
|
584
474
|
}
|