eslint-plugin-node-security 4.8.0 → 4.9.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.
- package/README.md +1 -1
- package/package.json +3 -3
- package/src/index.js +1 -95
- package/src/oxlint.js +1 -3
- package/src/rules/detect-child-process/index.js +1 -625
- package/src/rules/detect-eval-with-expression/index.js +1 -364
- package/src/rules/detect-non-literal-fs-filename/index.js +1 -516
- package/src/rules/detect-suspicious-dependencies/index.js +1 -69
- package/src/rules/lock-file/index.js +1 -92
- package/src/rules/no-arbitrary-file-access/index.js +1 -152
- package/src/rules/no-buffer-overread/index.js +1 -543
- package/src/rules/no-cryptojs/index.js +1 -99
- package/src/rules/no-cryptojs-weak-random/index.js +1 -103
- package/src/rules/no-data-in-temp-storage/index.js +1 -85
- package/src/rules/no-deprecated-buffer/index.js +1 -84
- package/src/rules/no-deprecated-cipher-method/index.js +1 -112
- package/src/rules/no-dynamic-algorithm-selection/index.js +1 -72
- package/src/rules/no-dynamic-command-string/index.js +1 -201
- package/src/rules/no-dynamic-dependency-loading/index.js +1 -45
- package/src/rules/no-dynamic-require/index.js +1 -96
- package/src/rules/no-ecb-mode/index.js +1 -108
- package/src/rules/no-insecure-key-derivation/index.js +1 -109
- package/src/rules/no-insecure-rsa-padding/index.js +1 -104
- package/src/rules/no-math-random-crypto/index.js +1 -192
- package/src/rules/no-self-signed-certs/index.js +1 -110
- package/src/rules/no-sha1-hash/index.js +1 -121
- package/src/rules/no-shell-injection/index.js +1 -68
- package/src/rules/no-ssrf/index.js +1 -221
- package/src/rules/no-static-iv/index.js +1 -129
- package/src/rules/no-timing-unsafe-compare/index.js +1 -106
- package/src/rules/no-toctou-vulnerability/index.js +1 -195
- package/src/rules/no-unsafe-buffer-alloc/index.js +1 -87
- package/src/rules/no-unsafe-dynamic-require/index.js +1 -93
- package/src/rules/no-weak-cipher-algorithm/index.js +1 -174
- package/src/rules/no-weak-hash-algorithm/index.js +1 -199
- package/src/rules/no-zip-slip/index.js +1 -410
- package/src/rules/prefer-native-crypto/index.js +1 -119
- package/src/rules/require-dependency-integrity/index.js +1 -62
- package/src/rules/require-secure-credential-storage/index.js +1 -45
- package/src/rules/require-secure-deletion/index.js +1 -82
- package/src/rules/require-storage-encryption/index.js +1 -45
- package/src/types/index.js +1 -2
|
@@ -1,516 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.detectNonLiteralFsFilename = exports.determineRiskLevel = exports.isFsModule = exports.generateRefactoringSteps = void 0;
|
|
4
|
-
exports.fsMethodName = fsMethodName;
|
|
5
|
-
exports.isFsRequire = isFsRequire;
|
|
6
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
7
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
8
|
-
const FS_OPERATIONS = [
|
|
9
|
-
{
|
|
10
|
-
method: 'readFile',
|
|
11
|
-
dangerous: true,
|
|
12
|
-
vulnerability: 'file-access',
|
|
13
|
-
safePattern: 'path.resolve(SAFE_DIR, path.basename(userInput))',
|
|
14
|
-
example: {
|
|
15
|
-
bad: 'fs.readFile(userPath, callback)',
|
|
16
|
-
good: 'const safePath = path.join(SAFE_UPLOADS_DIR, path.basename(userPath)); fs.readFile(safePath, callback)'
|
|
17
|
-
},
|
|
18
|
-
effort: '10-15 minutes'
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
method: 'writeFile',
|
|
22
|
-
dangerous: true,
|
|
23
|
-
vulnerability: 'file-access',
|
|
24
|
-
safePattern: 'path.resolve(SAFE_DIR, path.basename(userInput))',
|
|
25
|
-
example: {
|
|
26
|
-
bad: 'fs.writeFile(userPath, data, callback)',
|
|
27
|
-
good: 'const safePath = path.join(SAFE_WRITES_DIR, path.basename(userPath)); fs.writeFile(safePath, data, callback)'
|
|
28
|
-
},
|
|
29
|
-
effort: '10-15 minutes'
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
method: 'stat',
|
|
33
|
-
dangerous: true,
|
|
34
|
-
vulnerability: 'path-traversal',
|
|
35
|
-
safePattern: 'path.resolve(baseDir, userInput) with validation',
|
|
36
|
-
example: {
|
|
37
|
-
bad: 'fs.stat(userPath, callback)',
|
|
38
|
-
good: 'const resolvedPath = path.resolve(SAFE_DIR, userPath);\nif (!resolvedPath.startsWith(SAFE_DIR)) return;\nfs.stat(resolvedPath, callback)'
|
|
39
|
-
},
|
|
40
|
-
effort: '15-20 minutes'
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
method: 'readdir',
|
|
44
|
-
dangerous: true,
|
|
45
|
-
vulnerability: 'directory-traversal',
|
|
46
|
-
safePattern: 'Validate directory is within allowed paths',
|
|
47
|
-
example: {
|
|
48
|
-
bad: 'fs.readdir(userDir, callback)',
|
|
49
|
-
good: 'const resolvedDir = path.resolve(ALLOWED_DIRS, userDir);\nif (!resolvedDir.startsWith(ALLOWED_DIRS)) return;\nfs.readdir(resolvedDir, callback)'
|
|
50
|
-
},
|
|
51
|
-
effort: '15-20 minutes'
|
|
52
|
-
}
|
|
53
|
-
];
|
|
54
|
-
const hasTraversalPatterns = (pathStr) => {
|
|
55
|
-
return /\.\.[/\\]/.test(pathStr) || /^\.\.[/\\]/.test(pathStr);
|
|
56
|
-
};
|
|
57
|
-
const generateRefactoringSteps = (operation) => {
|
|
58
|
-
switch (operation.method) {
|
|
59
|
-
case 'readFile':
|
|
60
|
-
case 'writeFile':
|
|
61
|
-
return [
|
|
62
|
-
' 1. Define a SAFE_DIR constant for allowed operations',
|
|
63
|
-
' 2. Use path.basename() to strip directory components',
|
|
64
|
-
' 3. Combine with SAFE_DIR: path.join(SAFE_DIR, path.basename(userPath))',
|
|
65
|
-
' 4. Optionally validate file extensions',
|
|
66
|
-
' 5. Add error handling for invalid paths'
|
|
67
|
-
].join('\n');
|
|
68
|
-
case 'stat':
|
|
69
|
-
return [
|
|
70
|
-
' 1. Use path.resolve() to normalize the path',
|
|
71
|
-
' 2. Check if resolved path starts with allowed base directory',
|
|
72
|
-
' 3. Reject requests that escape the allowed directory',
|
|
73
|
-
' 4. Use path.relative() for additional validation',
|
|
74
|
-
' 5. Log security events for monitoring'
|
|
75
|
-
].join('\n');
|
|
76
|
-
case 'readdir':
|
|
77
|
-
return [
|
|
78
|
-
' 1. Resolve the directory path: path.resolve(ALLOWED_DIRS, userDir)',
|
|
79
|
-
' 2. Validate resolved path starts with ALLOWED_DIRS',
|
|
80
|
-
' 3. Check directory exists and is readable',
|
|
81
|
-
' 4. Consider whitelisting allowed directories',
|
|
82
|
-
' 5. Add rate limiting to prevent enumeration attacks'
|
|
83
|
-
].join('\n');
|
|
84
|
-
default:
|
|
85
|
-
return [
|
|
86
|
-
' 1. Identify the specific file operation needed',
|
|
87
|
-
' 2. Define safe base directories for operations',
|
|
88
|
-
' 3. Use path.resolve() and validate containment',
|
|
89
|
-
' 4. Sanitize user input (basename, extension validation)',
|
|
90
|
-
' 5. Add comprehensive error handling'
|
|
91
|
-
].join('\n');
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
exports.generateRefactoringSteps = generateRefactoringSteps;
|
|
95
|
-
const FS_MODULES = new Set(['fs', 'node:fs', 'fs/promises', 'node:fs/promises']);
|
|
96
|
-
const isFsModule = (source) => typeof source === 'string' && FS_MODULES.has(source);
|
|
97
|
-
exports.isFsModule = isFsModule;
|
|
98
|
-
function fsMethodName(callee, namespaces, named) {
|
|
99
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
100
|
-
return named.get(callee.name);
|
|
101
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
102
|
-
callee.computed ||
|
|
103
|
-
callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
104
|
-
return undefined;
|
|
105
|
-
}
|
|
106
|
-
const object = callee.object;
|
|
107
|
-
if (object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && namespaces.has(object.name)) {
|
|
108
|
-
return callee.property.name;
|
|
109
|
-
}
|
|
110
|
-
if (object.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
111
|
-
!object.computed &&
|
|
112
|
-
object.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
113
|
-
namespaces.has(object.object.name) &&
|
|
114
|
-
object.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
115
|
-
object.property.name === 'promises') {
|
|
116
|
-
return callee.property.name;
|
|
117
|
-
}
|
|
118
|
-
return undefined;
|
|
119
|
-
}
|
|
120
|
-
function isFsRequire(node) {
|
|
121
|
-
return (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
122
|
-
node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
123
|
-
node.callee.name === 'require' &&
|
|
124
|
-
node.arguments.length > 0 &&
|
|
125
|
-
node.arguments[0].type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
126
|
-
(0, exports.isFsModule)(node.arguments[0].value));
|
|
127
|
-
}
|
|
128
|
-
const determineRiskLevel = (operation, pathStr) => {
|
|
129
|
-
if (hasTraversalPatterns(pathStr)) {
|
|
130
|
-
return 'CRITICAL';
|
|
131
|
-
}
|
|
132
|
-
if (operation.dangerous) {
|
|
133
|
-
return 'HIGH';
|
|
134
|
-
}
|
|
135
|
-
return 'MEDIUM';
|
|
136
|
-
};
|
|
137
|
-
exports.determineRiskLevel = determineRiskLevel;
|
|
138
|
-
exports.detectNonLiteralFsFilename = (0, eslint_devkit_2.createRule)({
|
|
139
|
-
name: 'detect-non-literal-fs-filename',
|
|
140
|
-
meta: {
|
|
141
|
-
type: 'problem',
|
|
142
|
-
docs: {
|
|
143
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-node-security/docs/rules/detect-non-literal-fs-filename.md',
|
|
144
|
-
description: 'Detects variable in filename argument of fs calls, which might allow an attacker to access anything on your system',
|
|
145
|
-
cwe: 'CWE-22',
|
|
146
|
-
confidence: 'medium',
|
|
147
|
-
},
|
|
148
|
-
messages: {
|
|
149
|
-
fsPathTraversal: (0, eslint_devkit_1.formatLLMMessage)({
|
|
150
|
-
icon: '🔑',
|
|
151
|
-
issueName: 'Path traversal',
|
|
152
|
-
cwe: 'CWE-22',
|
|
153
|
-
description: 'Path traversal vulnerability',
|
|
154
|
-
severity: '{{riskLevel}}',
|
|
155
|
-
fix: '{{safePattern}}',
|
|
156
|
-
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
157
|
-
}),
|
|
158
|
-
usePathResolve: (0, eslint_devkit_1.formatLLMMessage)({
|
|
159
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
160
|
-
issueName: 'Use path.resolve',
|
|
161
|
-
description: 'Use path.resolve() to normalize paths',
|
|
162
|
-
severity: 'LOW',
|
|
163
|
-
fix: 'path.resolve(SAFE_DIR, userInput)',
|
|
164
|
-
documentationLink: 'https://nodejs.org/api/path.html#pathresolvepaths',
|
|
165
|
-
}),
|
|
166
|
-
validatePath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
167
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
168
|
-
issueName: 'Validate Path',
|
|
169
|
-
description: 'Validate resolved path starts with allowed base',
|
|
170
|
-
severity: 'LOW',
|
|
171
|
-
fix: 'if (!resolved.startsWith(SAFE_DIR)) throw new Error()',
|
|
172
|
-
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
173
|
-
}),
|
|
174
|
-
useBasename: (0, eslint_devkit_1.formatLLMMessage)({
|
|
175
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
176
|
-
issueName: 'Use path.basename',
|
|
177
|
-
description: 'Use path.basename() to strip directory components',
|
|
178
|
-
severity: 'LOW',
|
|
179
|
-
fix: 'path.basename(userInput)',
|
|
180
|
-
documentationLink: 'https://nodejs.org/api/path.html#pathbasenamepath-suffix',
|
|
181
|
-
}),
|
|
182
|
-
createSafeDir: (0, eslint_devkit_1.formatLLMMessage)({
|
|
183
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
184
|
-
issueName: 'Define Safe Directory',
|
|
185
|
-
description: 'Define SAFE_DIR constant',
|
|
186
|
-
severity: 'LOW',
|
|
187
|
-
fix: 'const SAFE_DIR = path.resolve(__dirname, "uploads")',
|
|
188
|
-
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
189
|
-
}),
|
|
190
|
-
whitelistExtensions: (0, eslint_devkit_1.formatLLMMessage)({
|
|
191
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
192
|
-
issueName: 'Whitelist Extensions',
|
|
193
|
-
description: 'Whitelist allowed file extensions',
|
|
194
|
-
severity: 'LOW',
|
|
195
|
-
fix: 'const ALLOWED_EXT = [".txt", ".pdf"]; if (!ALLOWED_EXT.includes(ext)) throw',
|
|
196
|
-
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
197
|
-
})
|
|
198
|
-
},
|
|
199
|
-
schema: [
|
|
200
|
-
{
|
|
201
|
-
type: 'object',
|
|
202
|
-
properties: {
|
|
203
|
-
allowLiterals: {
|
|
204
|
-
type: 'boolean',
|
|
205
|
-
default: false,
|
|
206
|
-
description: 'Allow literal string paths'
|
|
207
|
-
},
|
|
208
|
-
additionalMethods: {
|
|
209
|
-
type: 'array',
|
|
210
|
-
items: { type: 'string' },
|
|
211
|
-
default: [],
|
|
212
|
-
description: 'Additional fs methods to check'
|
|
213
|
-
},
|
|
214
|
-
allowedExtensions: {
|
|
215
|
-
type: 'array',
|
|
216
|
-
items: { type: 'string' },
|
|
217
|
-
default: [],
|
|
218
|
-
description: 'Allowed file extensions (e.g., [".txt", ".json"])'
|
|
219
|
-
}
|
|
220
|
-
},
|
|
221
|
-
additionalProperties: false,
|
|
222
|
-
},
|
|
223
|
-
],
|
|
224
|
-
},
|
|
225
|
-
defaultOptions: [
|
|
226
|
-
{
|
|
227
|
-
allowLiterals: false,
|
|
228
|
-
additionalMethods: []
|
|
229
|
-
},
|
|
230
|
-
],
|
|
231
|
-
create(context) {
|
|
232
|
-
const options = context.options[0] || {};
|
|
233
|
-
const { allowLiterals = false, additionalMethods = [] } = options;
|
|
234
|
-
const dangerousMethods = new Set([
|
|
235
|
-
'readFile', 'readFileSync',
|
|
236
|
-
'writeFile', 'writeFileSync',
|
|
237
|
-
'appendFile', 'appendFileSync',
|
|
238
|
-
'stat', 'statSync',
|
|
239
|
-
'lstat', 'lstatSync',
|
|
240
|
-
'readdir', 'readdirSync',
|
|
241
|
-
'unlink', 'unlinkSync',
|
|
242
|
-
'mkdir', 'mkdirSync',
|
|
243
|
-
'rmdir', 'rmdirSync',
|
|
244
|
-
'access', 'accessSync',
|
|
245
|
-
'createReadStream', 'createWriteStream',
|
|
246
|
-
...additionalMethods
|
|
247
|
-
]);
|
|
248
|
-
const isLiteralString = (node) => {
|
|
249
|
-
return node.type === 'Literal' && typeof node.value === 'string';
|
|
250
|
-
};
|
|
251
|
-
const extractPathArgument = (node, method) => {
|
|
252
|
-
const operation = FS_OPERATIONS.find(op => op.method === method) || null;
|
|
253
|
-
const pathNode = node.arguments.length > 0 ? node.arguments[0] : null;
|
|
254
|
-
const sourceCode = context.sourceCode;
|
|
255
|
-
const path = pathNode ? sourceCode.getText(pathNode) : '';
|
|
256
|
-
return { path, pathNode, operation };
|
|
257
|
-
};
|
|
258
|
-
const isDangerousPath = (pathNode, pathStr) => {
|
|
259
|
-
if (allowLiterals && pathNode && isLiteralString(pathNode)) {
|
|
260
|
-
return false;
|
|
261
|
-
}
|
|
262
|
-
if (pathNode && isLiteralString(pathNode) && hasTraversalPatterns(pathStr)) {
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
if (pathNode && isSafePathConstruction(pathNode)) {
|
|
266
|
-
return false;
|
|
267
|
-
}
|
|
268
|
-
if (pathNode && hasPathValidation(pathNode)) {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
if (pathNode && pathNode.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
272
|
-
const callee = pathNode.callee;
|
|
273
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
274
|
-
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
275
|
-
callee.object.name === 'path' &&
|
|
276
|
-
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
277
|
-
['join', 'resolve'].includes(callee.property.name)) {
|
|
278
|
-
const dynamicArgs = pathNode.arguments.filter((arg) => arg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && arg.name !== '__dirname');
|
|
279
|
-
if (dynamicArgs.length > 0 && dynamicArgs.every((arg) => hasPathValidation(arg))) {
|
|
280
|
-
return false;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
return !pathNode || !isLiteralString(pathNode);
|
|
285
|
-
};
|
|
286
|
-
const isSafePathConstruction = (pathNode) => {
|
|
287
|
-
if (pathNode.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
288
|
-
return false;
|
|
289
|
-
}
|
|
290
|
-
const callee = pathNode.callee;
|
|
291
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
292
|
-
callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
293
|
-
callee.object.name !== 'path' ||
|
|
294
|
-
callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
295
|
-
return false;
|
|
296
|
-
}
|
|
297
|
-
const method = callee.property.name;
|
|
298
|
-
if (!['join', 'resolve'].includes(method)) {
|
|
299
|
-
return false;
|
|
300
|
-
}
|
|
301
|
-
const args = pathNode.arguments;
|
|
302
|
-
if (args.length === 0) {
|
|
303
|
-
return false;
|
|
304
|
-
}
|
|
305
|
-
const firstArg = args[0];
|
|
306
|
-
const isFirstArgSafe = (firstArg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && firstArg.name === '__dirname') ||
|
|
307
|
-
(firstArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof firstArg.value === 'string');
|
|
308
|
-
if (!isFirstArgSafe) {
|
|
309
|
-
return false;
|
|
310
|
-
}
|
|
311
|
-
for (let i = 1; i < args.length; i++) {
|
|
312
|
-
const arg = args[i];
|
|
313
|
-
if (arg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal || typeof arg.value !== 'string') {
|
|
314
|
-
return false;
|
|
315
|
-
}
|
|
316
|
-
if (hasTraversalPatterns(String(arg.value))) {
|
|
317
|
-
return false;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
return true;
|
|
321
|
-
};
|
|
322
|
-
const hasPathValidation = (pathNode) => {
|
|
323
|
-
if (pathNode.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
324
|
-
return false;
|
|
325
|
-
}
|
|
326
|
-
const varName = pathNode.name;
|
|
327
|
-
const isValidationCall = (testNode) => {
|
|
328
|
-
if (testNode.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression &&
|
|
329
|
-
testNode.operator === '!' &&
|
|
330
|
-
testNode.argument.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
331
|
-
testNode = testNode.argument;
|
|
332
|
-
}
|
|
333
|
-
if (testNode.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
if (testNode.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
337
|
-
testNode.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
338
|
-
testNode.callee.object.name === varName &&
|
|
339
|
-
testNode.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
340
|
-
(testNode.callee.property.name === 'startsWith' ||
|
|
341
|
-
testNode.callee.property.name === 'includes')) {
|
|
342
|
-
return true;
|
|
343
|
-
}
|
|
344
|
-
if (testNode.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
345
|
-
testNode.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
346
|
-
testNode.callee.property.name === 'includes') {
|
|
347
|
-
for (const arg of testNode.arguments) {
|
|
348
|
-
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && arg.name === varName) {
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
if (testNode.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
354
|
-
testNode.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
355
|
-
testNode.callee.property.name === 'test') {
|
|
356
|
-
for (const arg of testNode.arguments) {
|
|
357
|
-
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && arg.name === varName) {
|
|
358
|
-
return true;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return false;
|
|
363
|
-
};
|
|
364
|
-
const hasEarlyExit = (consequent) => {
|
|
365
|
-
if (consequent.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
|
|
366
|
-
return consequent.body.some(stmt => stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement ||
|
|
367
|
-
stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement);
|
|
368
|
-
}
|
|
369
|
-
return consequent.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement ||
|
|
370
|
-
consequent.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement;
|
|
371
|
-
};
|
|
372
|
-
let current = pathNode.parent;
|
|
373
|
-
let foundFunctionBody = false;
|
|
374
|
-
while (current && !foundFunctionBody) {
|
|
375
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement) {
|
|
376
|
-
if (isValidationCall(current.test)) {
|
|
377
|
-
return true;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement && current.parent && (current.parent.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration ||
|
|
381
|
-
current.parent.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
|
|
382
|
-
current.parent.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression)) {
|
|
383
|
-
foundFunctionBody = true;
|
|
384
|
-
const blockBody = current.body;
|
|
385
|
-
const nodeIndex = blockBody.findIndex((stmt) => {
|
|
386
|
-
let check = pathNode;
|
|
387
|
-
while (check) {
|
|
388
|
-
if (check === stmt)
|
|
389
|
-
return true;
|
|
390
|
-
check = check.parent;
|
|
391
|
-
}
|
|
392
|
-
return false;
|
|
393
|
-
});
|
|
394
|
-
for (let i = 0; i < nodeIndex; i++) {
|
|
395
|
-
const stmt = blockBody[i];
|
|
396
|
-
if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement &&
|
|
397
|
-
isValidationCall(stmt.test) &&
|
|
398
|
-
hasEarlyExit(stmt.consequent)) {
|
|
399
|
-
return true;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
current = current.parent;
|
|
404
|
-
}
|
|
405
|
-
return false;
|
|
406
|
-
};
|
|
407
|
-
const fsNamespaces = new Set(['fs']);
|
|
408
|
-
const fsNamedMethods = new Map();
|
|
409
|
-
const pendingCalls = [];
|
|
410
|
-
function bindFsName(local, imported) {
|
|
411
|
-
if (imported === 'promises')
|
|
412
|
-
fsNamespaces.add(local);
|
|
413
|
-
else
|
|
414
|
-
fsNamedMethods.set(local, imported);
|
|
415
|
-
}
|
|
416
|
-
const checkFsCall = (node) => {
|
|
417
|
-
const methodName = fsMethodName(node.callee, fsNamespaces, fsNamedMethods);
|
|
418
|
-
if (methodName === undefined) {
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
if (!dangerousMethods.has(methodName)) {
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
const { path, pathNode, operation } = extractPathArgument(node, methodName);
|
|
425
|
-
const method = methodName;
|
|
426
|
-
if (!isDangerousPath(pathNode, path)) {
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
429
|
-
const riskLevel = (0, exports.determineRiskLevel)(operation || FS_OPERATIONS[0], path);
|
|
430
|
-
const steps = operation ? (0, exports.generateRefactoringSteps)(operation) : 'Review file system access patterns';
|
|
431
|
-
const safePattern = operation?.safePattern || 'Use path.resolve() with validation';
|
|
432
|
-
context.report({
|
|
433
|
-
node,
|
|
434
|
-
messageId: 'fsPathTraversal',
|
|
435
|
-
data: {
|
|
436
|
-
method,
|
|
437
|
-
path,
|
|
438
|
-
riskLevel,
|
|
439
|
-
vulnerability: operation?.vulnerability || 'path traversal',
|
|
440
|
-
safePattern,
|
|
441
|
-
steps,
|
|
442
|
-
effort: operation?.effort || '15-20 minutes'
|
|
443
|
-
},
|
|
444
|
-
suggest: [
|
|
445
|
-
{
|
|
446
|
-
messageId: 'usePathResolve',
|
|
447
|
-
fix: () => null
|
|
448
|
-
},
|
|
449
|
-
{
|
|
450
|
-
messageId: 'validatePath',
|
|
451
|
-
fix: () => null
|
|
452
|
-
},
|
|
453
|
-
{
|
|
454
|
-
messageId: 'useBasename',
|
|
455
|
-
fix: () => null
|
|
456
|
-
},
|
|
457
|
-
{
|
|
458
|
-
messageId: 'createSafeDir',
|
|
459
|
-
fix: () => null
|
|
460
|
-
},
|
|
461
|
-
{
|
|
462
|
-
messageId: 'whitelistExtensions',
|
|
463
|
-
fix: () => null
|
|
464
|
-
}
|
|
465
|
-
]
|
|
466
|
-
});
|
|
467
|
-
};
|
|
468
|
-
return {
|
|
469
|
-
ImportDeclaration(node) {
|
|
470
|
-
if (!(0, exports.isFsModule)(node.source.value))
|
|
471
|
-
return;
|
|
472
|
-
for (const spec of node.specifiers) {
|
|
473
|
-
if (spec.type === eslint_devkit_1.AST_NODE_TYPES.ImportSpecifier) {
|
|
474
|
-
const imported = spec.imported.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
|
|
475
|
-
? spec.imported.name
|
|
476
|
-
: spec.imported.value;
|
|
477
|
-
bindFsName(spec.local.name, imported);
|
|
478
|
-
continue;
|
|
479
|
-
}
|
|
480
|
-
fsNamespaces.add(spec.local.name);
|
|
481
|
-
}
|
|
482
|
-
},
|
|
483
|
-
VariableDeclarator(node) {
|
|
484
|
-
if (node.init === null || !isFsRequire(node.init))
|
|
485
|
-
return;
|
|
486
|
-
if (node.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
487
|
-
fsNamespaces.add(node.id.name);
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectPattern)
|
|
491
|
-
return;
|
|
492
|
-
for (const prop of node.id.properties) {
|
|
493
|
-
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property || prop.computed)
|
|
494
|
-
continue;
|
|
495
|
-
if (prop.value.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
496
|
-
continue;
|
|
497
|
-
const key = prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
|
|
498
|
-
? prop.key.name
|
|
499
|
-
: prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof prop.key.value === 'string'
|
|
500
|
-
? prop.key.value
|
|
501
|
-
: undefined;
|
|
502
|
-
if (key === undefined)
|
|
503
|
-
continue;
|
|
504
|
-
bindFsName(prop.value.name, key);
|
|
505
|
-
}
|
|
506
|
-
},
|
|
507
|
-
CallExpression(node) {
|
|
508
|
-
pendingCalls.push(node);
|
|
509
|
-
},
|
|
510
|
-
'Program:exit'() {
|
|
511
|
-
for (const call of pendingCalls)
|
|
512
|
-
checkFsCall(call);
|
|
513
|
-
},
|
|
514
|
-
};
|
|
515
|
-
},
|
|
516
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.detectNonLiteralFsFilename=exports.determineRiskLevel=exports.isFsModule=exports.generateRefactoringSteps=void 0;exports.fsMethodName=fsMethodName;exports.isFsRequire=isFsRequire;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const FS_OPERATIONS=[{method:"readFile",dangerous:true,vulnerability:"file-access",safePattern:"path.resolve(SAFE_DIR, path.basename(userInput))",example:{bad:"fs.readFile(userPath, callback)",good:"const safePath = path.join(SAFE_UPLOADS_DIR, path.basename(userPath)); fs.readFile(safePath, callback)"},effort:"10-15 minutes"},{method:"writeFile",dangerous:true,vulnerability:"file-access",safePattern:"path.resolve(SAFE_DIR, path.basename(userInput))",example:{bad:"fs.writeFile(userPath, data, callback)",good:"const safePath = path.join(SAFE_WRITES_DIR, path.basename(userPath)); fs.writeFile(safePath, data, callback)"},effort:"10-15 minutes"},{method:"stat",dangerous:true,vulnerability:"path-traversal",safePattern:"path.resolve(baseDir, userInput) with validation",example:{bad:"fs.stat(userPath, callback)",good:"const resolvedPath = path.resolve(SAFE_DIR, userPath);\nif (!resolvedPath.startsWith(SAFE_DIR)) return;\nfs.stat(resolvedPath, callback)"},effort:"15-20 minutes"},{method:"readdir",dangerous:true,vulnerability:"directory-traversal",safePattern:"Validate directory is within allowed paths",example:{bad:"fs.readdir(userDir, callback)",good:"const resolvedDir = path.resolve(ALLOWED_DIRS, userDir);\nif (!resolvedDir.startsWith(ALLOWED_DIRS)) return;\nfs.readdir(resolvedDir, callback)"},effort:"15-20 minutes"}];const hasTraversalPatterns=pathStr=>{return/\.\.[/\\]/.test(pathStr)||/^\.\.[/\\]/.test(pathStr)};const generateRefactoringSteps=operation=>{switch(operation.method){case"readFile":case"writeFile":return[" 1. Define a SAFE_DIR constant for allowed operations"," 2. Use path.basename() to strip directory components"," 3. Combine with SAFE_DIR: path.join(SAFE_DIR, path.basename(userPath))"," 4. Optionally validate file extensions"," 5. Add error handling for invalid paths"].join("\n");case"stat":return[" 1. Use path.resolve() to normalize the path"," 2. Check if resolved path starts with allowed base directory"," 3. Reject requests that escape the allowed directory"," 4. Use path.relative() for additional validation"," 5. Log security events for monitoring"].join("\n");case"readdir":return[" 1. Resolve the directory path: path.resolve(ALLOWED_DIRS, userDir)"," 2. Validate resolved path starts with ALLOWED_DIRS"," 3. Check directory exists and is readable"," 4. Consider whitelisting allowed directories"," 5. Add rate limiting to prevent enumeration attacks"].join("\n");default:return[" 1. Identify the specific file operation needed"," 2. Define safe base directories for operations"," 3. Use path.resolve() and validate containment"," 4. Sanitize user input (basename, extension validation)"," 5. Add comprehensive error handling"].join("\n")}};exports.generateRefactoringSteps=generateRefactoringSteps;const FS_MODULES=new Set(["fs","node:fs","fs/promises","node:fs/promises"]);const isFsModule=source=>typeof source==="string"&&FS_MODULES.has(source);exports.isFsModule=isFsModule;function fsMethodName(callee,namespaces,named){if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier)return named.get(callee.name);if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression||callee.computed||callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return void 0}const object=callee.object;if(object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&namespaces.has(object.name)){return callee.property.name}if(object.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&!object.computed&&object.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&namespaces.has(object.object.name)&&object.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&object.property.name==="promises"){return callee.property.name}return void 0}function isFsRequire(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&node.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.name==="require"&&node.arguments.length>0&&node.arguments[0].type===eslint_devkit_1.AST_NODE_TYPES.Literal&&(0,exports.isFsModule)(node.arguments[0].value)}const determineRiskLevel=(operation,pathStr)=>{if(hasTraversalPatterns(pathStr)){return"CRITICAL"}if(operation.dangerous){return"HIGH"}return"MEDIUM"};exports.determineRiskLevel=determineRiskLevel;exports.detectNonLiteralFsFilename=(0,eslint_devkit_2.createRule)({name:"detect-non-literal-fs-filename",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-node-security/docs/rules/detect-non-literal-fs-filename.md",description:"Detects variable in filename argument of fs calls, which might allow an attacker to access anything on your system",cwe:"CWE-22",confidence:"medium"},hasSuggestions:true,messages:{fsPathTraversal:(0,eslint_devkit_1.formatLLMMessage)({icon:"\u{1F511}",issueName:"Path traversal",cwe:"CWE-22",description:"Path traversal vulnerability",severity:"{{riskLevel}}",fix:"{{safePattern}}",documentationLink:"https://owasp.org/www-community/attacks/Path_Traversal"}),usePathResolve:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use path.resolve",description:"Use path.resolve() to normalize paths",severity:"LOW",fix:"path.resolve(SAFE_DIR, userInput)",documentationLink:"https://nodejs.org/api/path.html#pathresolvepaths"}),validatePath:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Validate Path",description:"Validate resolved path starts with allowed base",severity:"LOW",fix:"if (!resolved.startsWith(SAFE_DIR)) throw new Error()",documentationLink:"https://owasp.org/www-community/attacks/Path_Traversal"}),useBasename:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use path.basename",description:"Use path.basename() to strip directory components",severity:"LOW",fix:"path.basename(userInput)",documentationLink:"https://nodejs.org/api/path.html#pathbasenamepath-suffix"}),createSafeDir:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Define Safe Directory",description:"Define SAFE_DIR constant",severity:"LOW",fix:'const SAFE_DIR = path.resolve(__dirname, "uploads")',documentationLink:"https://owasp.org/www-community/attacks/Path_Traversal"}),whitelistExtensions:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Whitelist Extensions",description:"Whitelist allowed file extensions",severity:"LOW",fix:'const ALLOWED_EXT = [".txt", ".pdf"]; if (!ALLOWED_EXT.includes(ext)) throw',documentationLink:"https://owasp.org/www-community/attacks/Path_Traversal"})},schema:[{type:"object",properties:{allowLiterals:{type:"boolean",default:false,description:"Allow literal string paths"},additionalMethods:{type:"array",items:{type:"string"},default:[],description:"Additional fs methods to check"},allowedExtensions:{type:"array",items:{type:"string"},default:[],description:'Allowed file extensions (e.g., [".txt", ".json"])'}},additionalProperties:false}]},defaultOptions:[{allowLiterals:false,additionalMethods:[]}],create(context){const options=context.options[0]||{};const{allowLiterals=false,additionalMethods=[]}=options;const dangerousMethods=new Set(["readFile","readFileSync","writeFile","writeFileSync","appendFile","appendFileSync","stat","statSync","lstat","lstatSync","readdir","readdirSync","unlink","unlinkSync","mkdir","mkdirSync","rmdir","rmdirSync","access","accessSync","createReadStream","createWriteStream",...additionalMethods]);const isLiteralString=node=>{return node.type==="Literal"&&typeof node.value==="string"};const extractPathArgument=(node,method)=>{const operation=FS_OPERATIONS.find(op=>op.method===method)||null;const pathNode=node.arguments.length>0?node.arguments[0]:null;const sourceCode=context.sourceCode;const path=pathNode?sourceCode.getText(pathNode):"";return{path,pathNode,operation}};const isDangerousPath=(pathNode,pathStr)=>{if(allowLiterals&&pathNode&&isLiteralString(pathNode)){return false}if(pathNode&&isLiteralString(pathNode)&&hasTraversalPatterns(pathStr)){return true}if(pathNode&&isSafePathConstruction(pathNode)){return false}if(pathNode&&hasPathValidation(pathNode)){return false}if(pathNode&&pathNode.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){const callee=pathNode.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.object.name==="path"&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&["join","resolve"].includes(callee.property.name)){const dynamicArgs=pathNode.arguments.filter(arg=>arg.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&arg.name!=="__dirname");if(dynamicArgs.length>0&&dynamicArgs.every(arg=>hasPathValidation(arg))){return false}}}return!pathNode||!isLiteralString(pathNode)};const isSafePathConstruction=pathNode=>{if(pathNode.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression){return false}const callee=pathNode.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression||callee.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier||callee.object.name!=="path"||callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return false}const method=callee.property.name;if(!["join","resolve"].includes(method)){return false}const args=pathNode.arguments;if(args.length===0){return false}const firstArg=args[0];const isFirstArgSafe=firstArg.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&firstArg.name==="__dirname"||firstArg.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof firstArg.value==="string";if(!isFirstArgSafe){return false}for(let i=1;i<args.length;i++){const arg=args[i];if(arg.type!==eslint_devkit_1.AST_NODE_TYPES.Literal||typeof arg.value!=="string"){return false}if(hasTraversalPatterns(String(arg.value))){return false}}return true};const hasPathValidation=pathNode=>{if(pathNode.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return false}const varName=pathNode.name;const isValidationCall=testNode=>{if(testNode.type===eslint_devkit_1.AST_NODE_TYPES.UnaryExpression&&testNode.operator==="!"&&testNode.argument.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){testNode=testNode.argument}if(testNode.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression){return false}if(testNode.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&testNode.callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&testNode.callee.object.name===varName&&testNode.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&(testNode.callee.property.name==="startsWith"||testNode.callee.property.name==="includes")){return true}if(testNode.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&testNode.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&testNode.callee.property.name==="includes"){for(const arg of testNode.arguments){if(arg.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&arg.name===varName){return true}}}if(testNode.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&testNode.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&testNode.callee.property.name==="test"){for(const arg of testNode.arguments){if(arg.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&arg.name===varName){return true}}}return false};const hasEarlyExit=consequent=>{if(consequent.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){return consequent.body.some(stmt=>stmt.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement||stmt.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)}return consequent.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement||consequent.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement};let current=pathNode.parent;let foundFunctionBody=false;while(current&&!foundFunctionBody){if(current.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement){if(isValidationCall(current.test)){return true}}if(current.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement&¤t.parent&&(current.parent.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration||current.parent.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||current.parent.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression)){foundFunctionBody=true;const blockBody=current.body;const nodeIndex=blockBody.findIndex(stmt=>{let check=pathNode;while(check){if(check===stmt)return true;check=check.parent}return false});for(let i=0;i<nodeIndex;i++){const stmt=blockBody[i];if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement&&isValidationCall(stmt.test)&&hasEarlyExit(stmt.consequent)){return true}}}current=current.parent}return false};const fsNamespaces=new Set(["fs"]);const fsNamedMethods=new Map;const pendingCalls=[];function bindFsName(local,imported){if(imported==="promises")fsNamespaces.add(local);else fsNamedMethods.set(local,imported)}const checkFsCall=node=>{const methodName=fsMethodName(node.callee,fsNamespaces,fsNamedMethods);if(methodName===void 0){return}if(!dangerousMethods.has(methodName)){return}const{path,pathNode,operation}=extractPathArgument(node,methodName);const method=methodName;if(!isDangerousPath(pathNode,path)){return}const riskLevel=(0,exports.determineRiskLevel)(operation||FS_OPERATIONS[0],path);const steps=operation?(0,exports.generateRefactoringSteps)(operation):"Review file system access patterns";const safePattern=operation?.safePattern||"Use path.resolve() with validation";context.report({node,messageId:"fsPathTraversal",data:{method,path,riskLevel,vulnerability:operation?.vulnerability||"path traversal",safePattern,steps,effort:operation?.effort||"15-20 minutes"},suggest:[{messageId:"usePathResolve",fix:()=>null},{messageId:"validatePath",fix:()=>null},{messageId:"useBasename",fix:()=>null},{messageId:"createSafeDir",fix:()=>null},{messageId:"whitelistExtensions",fix:()=>null}]})};return{ImportDeclaration(node){if(!(0,exports.isFsModule)(node.source.value))return;for(const spec of node.specifiers){if(spec.type===eslint_devkit_1.AST_NODE_TYPES.ImportSpecifier){const imported=spec.imported.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?spec.imported.name:spec.imported.value;bindFsName(spec.local.name,imported);continue}fsNamespaces.add(spec.local.name)}},VariableDeclarator(node){if(node.init===null||!isFsRequire(node.init))return;if(node.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){fsNamespaces.add(node.id.name);return}if(node.id.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectPattern)return;for(const prop of node.id.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.Property||prop.computed)continue;if(prop.value.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)continue;const key=prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?prop.key.name:prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"?prop.key.value:void 0;if(key===void 0)continue;bindFsName(prop.value.name,key)}},CallExpression(node){pendingCalls.push(node)},"Program:exit"(){for(const call of pendingCalls)checkFsCall(call)}}}});
|
|
@@ -1,69 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.detectSuspiciousDependencies = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
exports.detectSuspiciousDependencies = (0, eslint_devkit_1.createRule)({
|
|
6
|
-
name: 'detect-suspicious-dependencies',
|
|
7
|
-
meta: {
|
|
8
|
-
type: 'problem',
|
|
9
|
-
docs: {
|
|
10
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-node-security/docs/rules/detect-suspicious-dependencies.md',
|
|
11
|
-
description: 'Detect typosquatting in package names',
|
|
12
|
-
cwe: 'CWE-506',
|
|
13
|
-
cvss: 7.5,
|
|
14
|
-
},
|
|
15
|
-
messages: {
|
|
16
|
-
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
17
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
18
|
-
issueName: 'Suspicious Dependency',
|
|
19
|
-
cwe: 'CWE-506',
|
|
20
|
-
description: 'Suspicious package name detected - possible typosquatting',
|
|
21
|
-
severity: 'HIGH',
|
|
22
|
-
fix: 'Verify package authenticity on npm registry',
|
|
23
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/506.html',
|
|
24
|
-
})
|
|
25
|
-
},
|
|
26
|
-
schema: [],
|
|
27
|
-
},
|
|
28
|
-
defaultOptions: [],
|
|
29
|
-
create(context) {
|
|
30
|
-
const popularPackages = ['react', 'lodash', 'express', 'axios', 'webpack'];
|
|
31
|
-
function levenshtein(a, b) {
|
|
32
|
-
const matrix = [];
|
|
33
|
-
for (let i = 0; i <= b.length; i++) {
|
|
34
|
-
matrix[i] = [i];
|
|
35
|
-
}
|
|
36
|
-
for (let j = 0; j <= a.length; j++) {
|
|
37
|
-
matrix[0][j] = j;
|
|
38
|
-
}
|
|
39
|
-
for (let i = 1; i <= b.length; i++) {
|
|
40
|
-
for (let j = 1; j <= a.length; j++) {
|
|
41
|
-
if (b.charAt(i - 1) === a.charAt(j - 1)) {
|
|
42
|
-
matrix[i][j] = matrix[i - 1][j - 1];
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return matrix[b.length][a.length];
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
ImportDeclaration(node) {
|
|
53
|
-
const source = node.source.value;
|
|
54
|
-
if (typeof source === 'string' && !source.startsWith('.') && !source.startsWith('@')) {
|
|
55
|
-
for (const popular of popularPackages) {
|
|
56
|
-
const distance = levenshtein(source, popular);
|
|
57
|
-
if (distance > 0 && distance <= 2) {
|
|
58
|
-
context.report({
|
|
59
|
-
node,
|
|
60
|
-
messageId: 'violationDetected',
|
|
61
|
-
data: { name: source, similar: popular },
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
},
|
|
69
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.detectSuspiciousDependencies=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");exports.detectSuspiciousDependencies=(0,eslint_devkit_1.createRule)({name:"detect-suspicious-dependencies",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-node-security/docs/rules/detect-suspicious-dependencies.md",description:"Detect typosquatting in package names",cwe:"CWE-506",cvss:7.5},messages:{violationDetected:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Suspicious Dependency",cwe:"CWE-506",description:"Suspicious package name detected - possible typosquatting",severity:"HIGH",fix:"Verify package authenticity on npm registry",documentationLink:"https://cwe.mitre.org/data/definitions/506.html"})},schema:[]},defaultOptions:[],create(context){const popularPackages=["react","lodash","express","axios","webpack"];function levenshtein(a,b){const matrix=[];for(let i=0;i<=b.length;i++){matrix[i]=[i]}for(let j=0;j<=a.length;j++){matrix[0][j]=j}for(let i=1;i<=b.length;i++){for(let j=1;j<=a.length;j++){if(b.charAt(i-1)===a.charAt(j-1)){matrix[i][j]=matrix[i-1][j-1]}else{matrix[i][j]=Math.min(matrix[i-1][j-1]+1,matrix[i][j-1]+1,matrix[i-1][j]+1)}}}return matrix[b.length][a.length]}return{ImportDeclaration(node){const source=node.source.value;if(typeof source==="string"&&!source.startsWith(".")&&!source.startsWith("@")){for(const popular of popularPackages){const distance=levenshtein(source,popular);if(distance>0&&distance<=2){context.report({node,messageId:"violationDetected",data:{name:source,similar:popular}})}}}}}}});
|