eslint-plugin-secure-coding 3.3.4 → 3.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/package.json +30 -37
- package/src/index.d.ts +17 -23
- package/src/index.js +2 -113
- 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 +86 -151
- package/src/rules/no-hardcoded-credentials/index.js +142 -184
- 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 +39 -35
- 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 +55 -66
- 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 -311
- 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 -44
- 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,14 +1,22 @@
|
|
|
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");
|
|
7
|
+
const TIMER_FUNCTIONS = new Set(['setTimeout', 'setInterval']);
|
|
8
|
+
function isStringValued(arg) {
|
|
9
|
+
if (!arg)
|
|
10
|
+
return false;
|
|
11
|
+
if (arg.type === 'Literal')
|
|
12
|
+
return typeof arg.value === 'string';
|
|
13
|
+
if (arg.type === 'TemplateLiteral')
|
|
14
|
+
return true;
|
|
15
|
+
if (arg.type === 'BinaryExpression' && arg.operator === '+') {
|
|
16
|
+
return isStringValued(arg.left) || isStringValued(arg.right);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
12
20
|
exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
13
21
|
name: 'no-unsafe-deserialization',
|
|
14
22
|
meta: {
|
|
@@ -171,45 +179,40 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
171
179
|
const { dangerousFunctions = ['eval', 'Function', 'setTimeout', 'setInterval', 'unserialize', 'deserialize', 'parseUnsafe'], validationFunctions = ['validateInput', 'sanitizeData', 'checkSchema', 'validateSchema'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
172
180
|
const sourceCode = context.sourceCode;
|
|
173
181
|
const filename = context.filename;
|
|
174
|
-
// Create safety checker for false positive detection
|
|
175
182
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
176
183
|
trustedSanitizers,
|
|
177
184
|
trustedAnnotations,
|
|
178
185
|
trustedOrmPatterns: [],
|
|
179
186
|
strictMode,
|
|
180
187
|
});
|
|
181
|
-
// Track variables that have been validated/sanitized
|
|
182
188
|
const validatedVariables = new Set();
|
|
183
|
-
// Track variables that contain untrusted data
|
|
184
189
|
const untrustedVariables = new Set();
|
|
185
|
-
// Variables read from a literal-string file path (e.g. fs.readFileSync('config.json')).
|
|
186
|
-
// These are still "untrusted" for eval/Function but safe for JSON.parse and other
|
|
187
|
-
// schema-validating parsers because the file content is statically bundled.
|
|
188
190
|
const literalPathFileVars = new Set();
|
|
189
|
-
/**
|
|
190
|
-
* Check if this is a dangerous deserialization function
|
|
191
|
-
*/
|
|
192
191
|
const isDangerousDeserialization = (node) => {
|
|
193
192
|
const callee = node.callee;
|
|
194
|
-
// Check for dangerous function calls
|
|
195
193
|
if (callee.type === 'Identifier' && dangerousFunctions.includes(callee.name)) {
|
|
194
|
+
if (TIMER_FUNCTIONS.has(callee.name)) {
|
|
195
|
+
return isStringValued(node.arguments[0]);
|
|
196
|
+
}
|
|
196
197
|
return true;
|
|
197
198
|
}
|
|
198
|
-
// Check for member expressions like yaml.load, serialize.unserialize
|
|
199
199
|
if (callee.type === 'MemberExpression') {
|
|
200
200
|
const memberName = callee.property.type === 'Identifier' ? callee.property.name : '';
|
|
201
201
|
const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
|
|
202
|
-
|
|
202
|
+
if (callee.object.type === 'Super' || callee.object.type === 'ThisExpression') {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
if (TIMER_FUNCTIONS.has(memberName)) {
|
|
206
|
+
return isStringValued(node.arguments[0]);
|
|
207
|
+
}
|
|
203
208
|
if (dangerousFunctions.includes(memberName)) {
|
|
204
209
|
return true;
|
|
205
210
|
}
|
|
206
|
-
// Check dangerous libraries
|
|
207
211
|
if (['yaml', 'js-yaml', 'node-serialize', 'serialize-javascript'].includes(objectName.toLowerCase()) &&
|
|
208
212
|
['load', 'parse', 'unserialize', 'deserialize'].includes(memberName)) {
|
|
209
213
|
return true;
|
|
210
214
|
}
|
|
211
215
|
}
|
|
212
|
-
// Check for require() calls with dangerous libraries
|
|
213
216
|
if (callee.type === 'Identifier' && callee.name === 'require') {
|
|
214
217
|
const args = node.arguments;
|
|
215
218
|
if (args.length > 0 && args[0].type === 'Literal' && typeof args[0].value === 'string') {
|
|
@@ -221,11 +224,13 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
221
224
|
}
|
|
222
225
|
return false;
|
|
223
226
|
};
|
|
224
|
-
/**
|
|
225
|
-
* Check if input comes from untrusted source
|
|
226
|
-
*/
|
|
227
227
|
const isUntrustedInput = (inputNode) => {
|
|
228
|
-
|
|
228
|
+
if (inputNode.type === 'BinaryExpression' && inputNode.operator === '+') {
|
|
229
|
+
return isUntrustedInput(inputNode.left) || isUntrustedInput(inputNode.right);
|
|
230
|
+
}
|
|
231
|
+
if (inputNode.type === 'TemplateLiteral') {
|
|
232
|
+
return inputNode.expressions.some((e) => isUntrustedInput(e));
|
|
233
|
+
}
|
|
229
234
|
if (inputNode.type === 'MemberExpression') {
|
|
230
235
|
if (inputNode.object.type === 'Identifier' && inputNode.object.name === 'req') {
|
|
231
236
|
return true;
|
|
@@ -237,14 +242,9 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
237
242
|
}
|
|
238
243
|
}
|
|
239
244
|
if (inputNode.type === 'Identifier') {
|
|
240
|
-
// Check if this variable has been marked as untrusted
|
|
241
245
|
if (untrustedVariables.has(inputNode.name)) {
|
|
242
246
|
return true;
|
|
243
247
|
}
|
|
244
|
-
// Only consider variables untrusted if they actually come from req.* patterns
|
|
245
|
-
// Don't flag generic variable names like 'input', 'data', etc.
|
|
246
|
-
// unless they have been explicitly marked as untrusted
|
|
247
|
-
// Check if it comes from function parameters (these are potentially untrusted)
|
|
248
248
|
let current = inputNode;
|
|
249
249
|
while (current) {
|
|
250
250
|
if (current.type === 'FunctionDeclaration' ||
|
|
@@ -257,7 +257,7 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
257
257
|
}
|
|
258
258
|
return false;
|
|
259
259
|
})) {
|
|
260
|
-
return true;
|
|
260
|
+
return true;
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
263
|
current = current.parent;
|
|
@@ -265,9 +265,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
265
265
|
}
|
|
266
266
|
return false;
|
|
267
267
|
};
|
|
268
|
-
/**
|
|
269
|
-
* Check if input has been validated
|
|
270
|
-
*/
|
|
271
268
|
const isInputValidated = (inputNode) => {
|
|
272
269
|
let current = inputNode;
|
|
273
270
|
while (current) {
|
|
@@ -280,16 +277,11 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
280
277
|
}
|
|
281
278
|
return false;
|
|
282
279
|
};
|
|
283
|
-
/**
|
|
284
|
-
* Check if this is a safe deserialization library
|
|
285
|
-
*/
|
|
286
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
287
280
|
const isSafeLibrary = (node) => {
|
|
288
281
|
const callee = node.callee;
|
|
289
282
|
if (callee.type === 'MemberExpression') {
|
|
290
283
|
const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
|
|
291
284
|
const memberName = callee.property.type === 'Identifier' ? callee.property.name : '';
|
|
292
|
-
// Check for safe patterns
|
|
293
285
|
if (objectName === 'JSON' && memberName === 'parse') {
|
|
294
286
|
return true;
|
|
295
287
|
}
|
|
@@ -299,8 +291,32 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
299
291
|
}
|
|
300
292
|
return false;
|
|
301
293
|
};
|
|
294
|
+
const isInsideDeserializerImplementation = (node) => {
|
|
295
|
+
const DESERIALIZER_NAMES = new Set(['deserialize', 'unserialize', 'fromJSON', 'fromBuffer']);
|
|
296
|
+
let current = node.parent;
|
|
297
|
+
while (current) {
|
|
298
|
+
if (current.type === 'FunctionDeclaration' ||
|
|
299
|
+
current.type === 'FunctionExpression' ||
|
|
300
|
+
current.type === 'ArrowFunctionExpression') {
|
|
301
|
+
const fn = current;
|
|
302
|
+
if ('id' in fn && fn.id?.name && DESERIALIZER_NAMES.has(fn.id.name))
|
|
303
|
+
return true;
|
|
304
|
+
const owner = fn.parent;
|
|
305
|
+
if (owner?.type === 'MethodDefinition' && owner.key.type === 'Identifier' &&
|
|
306
|
+
DESERIALIZER_NAMES.has(owner.key.name))
|
|
307
|
+
return true;
|
|
308
|
+
if (owner?.type === 'Property' && owner.key.type === 'Identifier' &&
|
|
309
|
+
DESERIALIZER_NAMES.has(owner.key.name))
|
|
310
|
+
return true;
|
|
311
|
+
if (owner?.type === 'VariableDeclarator' && owner.id.type === 'Identifier' &&
|
|
312
|
+
DESERIALIZER_NAMES.has(owner.id.name))
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
current = current.parent;
|
|
316
|
+
}
|
|
317
|
+
return false;
|
|
318
|
+
};
|
|
302
319
|
const checkCallExpression = (node) => {
|
|
303
|
-
// 1. Check Function Constructor (NewExpression or CallExpression)
|
|
304
320
|
if ((node.type === 'NewExpression' || node.type === 'CallExpression') &&
|
|
305
321
|
node.callee.type === 'Identifier' &&
|
|
306
322
|
node.callee.name === 'Function') {
|
|
@@ -322,24 +338,18 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
322
338
|
return;
|
|
323
339
|
}
|
|
324
340
|
}
|
|
325
|
-
|
|
326
|
-
if (isDangerousDeserialization(node)) {
|
|
341
|
+
if (isDangerousDeserialization(node) && !isInsideDeserializerImplementation(node)) {
|
|
327
342
|
const args = node.arguments;
|
|
328
343
|
const hasUntrustedInput = args.some((arg) => isUntrustedInput(arg));
|
|
329
|
-
// Check if explicit validation is present
|
|
330
344
|
const isSafe = isSafeLibrary(node);
|
|
331
345
|
if (!isSafe && hasUntrustedInput) {
|
|
332
|
-
// Basic safety check
|
|
333
346
|
const safe = safetyChecker.isSafe(node, context);
|
|
334
347
|
if (!safe) {
|
|
335
|
-
// Determine message ID
|
|
336
348
|
let messageId = 'unsafeDeserialization';
|
|
337
|
-
// Check specifically for YAML
|
|
338
349
|
const calleeText = sourceCode.getText(node.callee);
|
|
339
350
|
if (calleeText.includes('yaml') || calleeText.includes('YAML')) {
|
|
340
351
|
messageId = 'unsafeYamlParsing';
|
|
341
352
|
}
|
|
342
|
-
// Check for generic dangerous functions
|
|
343
353
|
if (node.callee.type === 'Identifier' && ['eval', 'setTimeout', 'setInterval'].includes(node.callee.name)) {
|
|
344
354
|
messageId = 'dangerousEvalUsage';
|
|
345
355
|
}
|
|
@@ -363,23 +373,18 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
363
373
|
}
|
|
364
374
|
}
|
|
365
375
|
}
|
|
366
|
-
// Check for untrusted input in potentially safe functions
|
|
367
376
|
if (isSafeLibrary(node)) {
|
|
368
377
|
const args = node.arguments;
|
|
369
378
|
const hasUntrustedInput = args.some((arg) => {
|
|
370
|
-
// Check if it's validated
|
|
371
379
|
if (arg.type === 'Identifier' && validatedVariables.has(arg.name)) {
|
|
372
380
|
return false;
|
|
373
381
|
}
|
|
374
|
-
// SAFE for JSON.parse/schema parsers: if the input came from a
|
|
375
|
-
// literal-path file read (bundled config), it is not user-controlled.
|
|
376
382
|
if (arg.type === 'Identifier' && literalPathFileVars.has(arg.name)) {
|
|
377
383
|
return false;
|
|
378
384
|
}
|
|
379
385
|
return isUntrustedInput(arg) && !isInputValidated(arg);
|
|
380
386
|
});
|
|
381
387
|
if (hasUntrustedInput) {
|
|
382
|
-
// Even JSON.parse can be unsafe if used on complex objects that get eval'd later
|
|
383
388
|
if (safetyChecker.isSafe(node, context)) {
|
|
384
389
|
return;
|
|
385
390
|
}
|
|
@@ -401,15 +406,12 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
401
406
|
}
|
|
402
407
|
};
|
|
403
408
|
return {
|
|
404
|
-
// Track variable assignments from untrusted sources
|
|
405
409
|
VariableDeclaration(node) {
|
|
406
410
|
for (const declarator of node.declarations) {
|
|
407
411
|
if (declarator.id.type === 'Identifier' && declarator.init) {
|
|
408
|
-
// Check if the initializer comes from an untrusted source
|
|
409
412
|
if (isUntrustedInput(declarator.init)) {
|
|
410
413
|
untrustedVariables.add(declarator.id.name);
|
|
411
414
|
}
|
|
412
|
-
// Check if it's assigned from fs operations or other untrusted sources
|
|
413
415
|
if (declarator.init.type === 'CallExpression') {
|
|
414
416
|
const callee = declarator.init.callee;
|
|
415
417
|
if (callee.type === 'MemberExpression' &&
|
|
@@ -418,8 +420,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
418
420
|
callee.property.type === 'Identifier' &&
|
|
419
421
|
['readFile', 'readFileSync'].includes(callee.property.name)) {
|
|
420
422
|
untrustedVariables.add(declarator.id.name);
|
|
421
|
-
// Track whether the path is a literal — used downstream to skip
|
|
422
|
-
// safe deserializers (JSON.parse) on known-static files.
|
|
423
423
|
const pathArg = declarator.init.arguments[0];
|
|
424
424
|
if (pathArg?.type === 'Literal' && typeof pathArg.value === 'string') {
|
|
425
425
|
literalPathFileVars.add(declarator.id.name);
|
|
@@ -429,32 +429,27 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
429
429
|
}
|
|
430
430
|
}
|
|
431
431
|
},
|
|
432
|
-
// Track assignment expressions
|
|
433
432
|
AssignmentExpression(node) {
|
|
434
433
|
if (node.left.type === 'Identifier' && isUntrustedInput(node.right)) {
|
|
435
434
|
untrustedVariables.add(node.left.name);
|
|
436
435
|
}
|
|
437
436
|
},
|
|
438
|
-
// Check dangerous function calls
|
|
439
437
|
CallExpression(node) {
|
|
440
438
|
checkCallExpression(node);
|
|
441
439
|
},
|
|
442
440
|
NewExpression(node) {
|
|
443
441
|
checkCallExpression(node);
|
|
444
442
|
},
|
|
445
|
-
// Check for dangerous require/import patterns
|
|
446
443
|
VariableDeclarator(node) {
|
|
447
444
|
if (!node.init) {
|
|
448
445
|
return;
|
|
449
446
|
}
|
|
450
|
-
// Track variables assigned from validation functions
|
|
451
447
|
if (node.id.type === 'Identifier' &&
|
|
452
448
|
node.init.type === 'CallExpression' &&
|
|
453
449
|
node.init.callee.type === 'Identifier' &&
|
|
454
450
|
(validationFunctions.includes(node.init.callee.name) || trustedSanitizers.includes(node.init.callee.name))) {
|
|
455
451
|
validatedVariables.add(node.id.name);
|
|
456
452
|
}
|
|
457
|
-
// Check for require/import of dangerous libraries
|
|
458
453
|
if (node.init.type === 'CallExpression' &&
|
|
459
454
|
node.init.callee.type === 'Identifier' &&
|
|
460
455
|
node.init.callee.name === 'require') {
|
|
@@ -462,16 +457,11 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
462
457
|
if (requireArg?.type === 'Literal' && typeof requireArg.value === 'string') {
|
|
463
458
|
const moduleName = requireArg.value;
|
|
464
459
|
if (['node-serialize', 'serialize-javascript', 'js-yaml', 'yaml'].includes(moduleName)) {
|
|
465
|
-
// Check if this variable is used unsafely later
|
|
466
460
|
if (node.id.type === 'Identifier') {
|
|
467
|
-
// Look ahead to see if this library is used dangerously
|
|
468
|
-
// This is a simplified check - in practice, we'd need more sophisticated analysis
|
|
469
461
|
const variables = sourceCode.getDeclaredVariables(node);
|
|
470
462
|
for (const variable of variables) {
|
|
471
463
|
for (const reference of variable.references) {
|
|
472
464
|
const refNode = reference.identifier;
|
|
473
|
-
// Check if reference is part of a call to dangerous method
|
|
474
|
-
// e.g. serialize.unserialize()
|
|
475
465
|
if (refNode.parent && refNode.parent.type === 'MemberExpression' &&
|
|
476
466
|
refNode.parent.object === refNode) {
|
|
477
467
|
const memberExpr = refNode.parent;
|
|
@@ -479,7 +469,6 @@ exports.noUnsafeDeserialization = (0, eslint_devkit_1.createRule)({
|
|
|
479
469
|
if (['unserialize', 'deserialize', 'load', 'parse'].includes(propertyName)) {
|
|
480
470
|
const callExpr = memberExpr.parent;
|
|
481
471
|
if (callExpr && callExpr.type === 'CallExpression' && callExpr.callee === memberExpr) {
|
|
482
|
-
// FALSE POSITIVE REDUCTION
|
|
483
472
|
if (safetyChecker.isSafe(callExpr, context)) {
|
|
484
473
|
continue;
|
|
485
474
|
}
|