eslint-plugin-secure-coding 1.0.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/AGENTS.md +196 -0
- package/CHANGELOG.md +105 -0
- package/LICENSE +23 -0
- package/README.md +377 -0
- package/package.json +80 -0
- package/src/index.d.ts +32 -0
- package/src/index.js +345 -0
- package/src/index.js.map +1 -0
- package/src/rules/security/database-injection.d.ts +13 -0
- package/src/rules/security/database-injection.js +407 -0
- package/src/rules/security/database-injection.js.map +1 -0
- package/src/rules/security/detect-child-process.d.ts +11 -0
- package/src/rules/security/detect-child-process.js +460 -0
- package/src/rules/security/detect-child-process.js.map +1 -0
- package/src/rules/security/detect-eval-with-expression.d.ts +9 -0
- package/src/rules/security/detect-eval-with-expression.js +393 -0
- package/src/rules/security/detect-eval-with-expression.js.map +1 -0
- package/src/rules/security/detect-non-literal-fs-filename.d.ts +7 -0
- package/src/rules/security/detect-non-literal-fs-filename.js +322 -0
- package/src/rules/security/detect-non-literal-fs-filename.js.map +1 -0
- package/src/rules/security/detect-non-literal-regexp.d.ts +9 -0
- package/src/rules/security/detect-non-literal-regexp.js +387 -0
- package/src/rules/security/detect-non-literal-regexp.js.map +1 -0
- package/src/rules/security/detect-object-injection.d.ts +11 -0
- package/src/rules/security/detect-object-injection.js +411 -0
- package/src/rules/security/detect-object-injection.js.map +1 -0
- package/src/rules/security/no-buffer-overread.d.ts +14 -0
- package/src/rules/security/no-buffer-overread.js +519 -0
- package/src/rules/security/no-buffer-overread.js.map +1 -0
- package/src/rules/security/no-clickjacking.d.ts +10 -0
- package/src/rules/security/no-clickjacking.js +381 -0
- package/src/rules/security/no-clickjacking.js.map +1 -0
- package/src/rules/security/no-directive-injection.d.ts +12 -0
- package/src/rules/security/no-directive-injection.js +446 -0
- package/src/rules/security/no-directive-injection.js.map +1 -0
- package/src/rules/security/no-document-cookie.d.ts +5 -0
- package/src/rules/security/no-document-cookie.js +90 -0
- package/src/rules/security/no-document-cookie.js.map +1 -0
- package/src/rules/security/no-electron-security-issues.d.ts +10 -0
- package/src/rules/security/no-electron-security-issues.js +421 -0
- package/src/rules/security/no-electron-security-issues.js.map +1 -0
- package/src/rules/security/no-exposed-sensitive-data.d.ts +11 -0
- package/src/rules/security/no-exposed-sensitive-data.js +341 -0
- package/src/rules/security/no-exposed-sensitive-data.js.map +1 -0
- package/src/rules/security/no-format-string-injection.d.ts +17 -0
- package/src/rules/security/no-format-string-injection.js +653 -0
- package/src/rules/security/no-format-string-injection.js.map +1 -0
- package/src/rules/security/no-graphql-injection.d.ts +12 -0
- package/src/rules/security/no-graphql-injection.js +410 -0
- package/src/rules/security/no-graphql-injection.js.map +1 -0
- package/src/rules/security/no-hardcoded-credentials.d.ts +26 -0
- package/src/rules/security/no-hardcoded-credentials.js +377 -0
- package/src/rules/security/no-hardcoded-credentials.js.map +1 -0
- package/src/rules/security/no-improper-sanitization.d.ts +12 -0
- package/src/rules/security/no-improper-sanitization.js +408 -0
- package/src/rules/security/no-improper-sanitization.js.map +1 -0
- package/src/rules/security/no-improper-type-validation.d.ts +10 -0
- package/src/rules/security/no-improper-type-validation.js +420 -0
- package/src/rules/security/no-improper-type-validation.js.map +1 -0
- package/src/rules/security/no-insecure-comparison.d.ts +7 -0
- package/src/rules/security/no-insecure-comparison.js +125 -0
- package/src/rules/security/no-insecure-comparison.js.map +1 -0
- package/src/rules/security/no-insecure-cookie-settings.d.ts +9 -0
- package/src/rules/security/no-insecure-cookie-settings.js +305 -0
- package/src/rules/security/no-insecure-cookie-settings.js.map +1 -0
- package/src/rules/security/no-insecure-jwt.d.ts +10 -0
- package/src/rules/security/no-insecure-jwt.js +338 -0
- package/src/rules/security/no-insecure-jwt.js.map +1 -0
- package/src/rules/security/no-insecure-redirects.d.ts +7 -0
- package/src/rules/security/no-insecure-redirects.js +215 -0
- package/src/rules/security/no-insecure-redirects.js.map +1 -0
- package/src/rules/security/no-insufficient-postmessage-validation.d.ts +14 -0
- package/src/rules/security/no-insufficient-postmessage-validation.js +390 -0
- package/src/rules/security/no-insufficient-postmessage-validation.js.map +1 -0
- package/src/rules/security/no-insufficient-random.d.ts +9 -0
- package/src/rules/security/no-insufficient-random.js +207 -0
- package/src/rules/security/no-insufficient-random.js.map +1 -0
- package/src/rules/security/no-ldap-injection.d.ts +10 -0
- package/src/rules/security/no-ldap-injection.js +449 -0
- package/src/rules/security/no-ldap-injection.js.map +1 -0
- package/src/rules/security/no-missing-authentication.d.ts +13 -0
- package/src/rules/security/no-missing-authentication.js +322 -0
- package/src/rules/security/no-missing-authentication.js.map +1 -0
- package/src/rules/security/no-missing-cors-check.d.ts +9 -0
- package/src/rules/security/no-missing-cors-check.js +449 -0
- package/src/rules/security/no-missing-cors-check.js.map +1 -0
- package/src/rules/security/no-missing-csrf-protection.d.ts +11 -0
- package/src/rules/security/no-missing-csrf-protection.js +183 -0
- package/src/rules/security/no-missing-csrf-protection.js.map +1 -0
- package/src/rules/security/no-missing-security-headers.d.ts +7 -0
- package/src/rules/security/no-missing-security-headers.js +217 -0
- package/src/rules/security/no-missing-security-headers.js.map +1 -0
- package/src/rules/security/no-privilege-escalation.d.ts +13 -0
- package/src/rules/security/no-privilege-escalation.js +321 -0
- package/src/rules/security/no-privilege-escalation.js.map +1 -0
- package/src/rules/security/no-redos-vulnerable-regex.d.ts +7 -0
- package/src/rules/security/no-redos-vulnerable-regex.js +307 -0
- package/src/rules/security/no-redos-vulnerable-regex.js.map +1 -0
- package/src/rules/security/no-sensitive-data-exposure.d.ts +11 -0
- package/src/rules/security/no-sensitive-data-exposure.js +251 -0
- package/src/rules/security/no-sensitive-data-exposure.js.map +1 -0
- package/src/rules/security/no-sql-injection.d.ts +10 -0
- package/src/rules/security/no-sql-injection.js +332 -0
- package/src/rules/security/no-sql-injection.js.map +1 -0
- package/src/rules/security/no-timing-attack.d.ts +10 -0
- package/src/rules/security/no-timing-attack.js +358 -0
- package/src/rules/security/no-timing-attack.js.map +1 -0
- package/src/rules/security/no-toctou-vulnerability.d.ts +7 -0
- package/src/rules/security/no-toctou-vulnerability.js +165 -0
- package/src/rules/security/no-toctou-vulnerability.js.map +1 -0
- package/src/rules/security/no-unchecked-loop-condition.d.ts +12 -0
- package/src/rules/security/no-unchecked-loop-condition.js +635 -0
- package/src/rules/security/no-unchecked-loop-condition.js.map +1 -0
- package/src/rules/security/no-unencrypted-transmission.d.ts +11 -0
- package/src/rules/security/no-unencrypted-transmission.js +237 -0
- package/src/rules/security/no-unencrypted-transmission.js.map +1 -0
- package/src/rules/security/no-unescaped-url-parameter.d.ts +9 -0
- package/src/rules/security/no-unescaped-url-parameter.js +266 -0
- package/src/rules/security/no-unescaped-url-parameter.js.map +1 -0
- package/src/rules/security/no-unlimited-resource-allocation.d.ts +12 -0
- package/src/rules/security/no-unlimited-resource-allocation.js +659 -0
- package/src/rules/security/no-unlimited-resource-allocation.js.map +1 -0
- package/src/rules/security/no-unsafe-deserialization.d.ts +10 -0
- package/src/rules/security/no-unsafe-deserialization.js +501 -0
- package/src/rules/security/no-unsafe-deserialization.js.map +1 -0
- package/src/rules/security/no-unsafe-dynamic-require.d.ts +5 -0
- package/src/rules/security/no-unsafe-dynamic-require.js +107 -0
- package/src/rules/security/no-unsafe-dynamic-require.js.map +1 -0
- package/src/rules/security/no-unsafe-regex-construction.d.ts +9 -0
- package/src/rules/security/no-unsafe-regex-construction.js +292 -0
- package/src/rules/security/no-unsafe-regex-construction.js.map +1 -0
- package/src/rules/security/no-unsanitized-html.d.ts +9 -0
- package/src/rules/security/no-unsanitized-html.js +347 -0
- package/src/rules/security/no-unsanitized-html.js.map +1 -0
- package/src/rules/security/no-unvalidated-user-input.d.ts +9 -0
- package/src/rules/security/no-unvalidated-user-input.js +418 -0
- package/src/rules/security/no-unvalidated-user-input.js.map +1 -0
- package/src/rules/security/no-weak-crypto.d.ts +11 -0
- package/src/rules/security/no-weak-crypto.js +350 -0
- package/src/rules/security/no-weak-crypto.js.map +1 -0
- package/src/rules/security/no-weak-password-recovery.d.ts +12 -0
- package/src/rules/security/no-weak-password-recovery.js +401 -0
- package/src/rules/security/no-weak-password-recovery.js.map +1 -0
- package/src/rules/security/no-xpath-injection.d.ts +10 -0
- package/src/rules/security/no-xpath-injection.js +487 -0
- package/src/rules/security/no-xpath-injection.js.map +1 -0
- package/src/rules/security/no-xxe-injection.d.ts +7 -0
- package/src/rules/security/no-xxe-injection.js +270 -0
- package/src/rules/security/no-xxe-injection.js.map +1 -0
- package/src/rules/security/no-zip-slip.d.ts +9 -0
- package/src/rules/security/no-zip-slip.js +446 -0
- package/src/rules/security/no-zip-slip.js.map +1 -0
- package/src/types/index.d.ts +131 -0
- package/src/types/index.js +18 -0
- package/src/types/index.js.map +1 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.detectNonLiteralFsFilename = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
+
const FS_OPERATIONS = [
|
|
7
|
+
{
|
|
8
|
+
method: 'readFile',
|
|
9
|
+
dangerous: true,
|
|
10
|
+
vulnerability: 'file-access',
|
|
11
|
+
safePattern: 'path.resolve(SAFE_DIR, path.basename(userInput))',
|
|
12
|
+
example: {
|
|
13
|
+
bad: 'fs.readFile(userPath, callback)',
|
|
14
|
+
good: 'const safePath = path.join(SAFE_UPLOADS_DIR, path.basename(userPath)); fs.readFile(safePath, callback)'
|
|
15
|
+
},
|
|
16
|
+
effort: '10-15 minutes'
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
method: 'writeFile',
|
|
20
|
+
dangerous: true,
|
|
21
|
+
vulnerability: 'file-access',
|
|
22
|
+
safePattern: 'path.resolve(SAFE_DIR, path.basename(userInput))',
|
|
23
|
+
example: {
|
|
24
|
+
bad: 'fs.writeFile(userPath, data, callback)',
|
|
25
|
+
good: 'const safePath = path.join(SAFE_WRITES_DIR, path.basename(userPath)); fs.writeFile(safePath, data, callback)'
|
|
26
|
+
},
|
|
27
|
+
effort: '10-15 minutes'
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
method: 'stat',
|
|
31
|
+
dangerous: true,
|
|
32
|
+
vulnerability: 'path-traversal',
|
|
33
|
+
safePattern: 'path.resolve(baseDir, userInput) with validation',
|
|
34
|
+
example: {
|
|
35
|
+
bad: 'fs.stat(userPath, callback)',
|
|
36
|
+
good: 'const resolvedPath = path.resolve(SAFE_DIR, userPath);\nif (!resolvedPath.startsWith(SAFE_DIR)) return;\nfs.stat(resolvedPath, callback)'
|
|
37
|
+
},
|
|
38
|
+
effort: '15-20 minutes'
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
method: 'readdir',
|
|
42
|
+
dangerous: true,
|
|
43
|
+
vulnerability: 'directory-traversal',
|
|
44
|
+
safePattern: 'Validate directory is within allowed paths',
|
|
45
|
+
example: {
|
|
46
|
+
bad: 'fs.readdir(userDir, callback)',
|
|
47
|
+
good: 'const resolvedDir = path.resolve(ALLOWED_DIRS, userDir);\nif (!resolvedDir.startsWith(ALLOWED_DIRS)) return;\nfs.readdir(resolvedDir, callback)'
|
|
48
|
+
},
|
|
49
|
+
effort: '15-20 minutes'
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
exports.detectNonLiteralFsFilename = (0, eslint_devkit_2.createRule)({
|
|
53
|
+
name: 'detect-non-literal-fs-filename',
|
|
54
|
+
meta: {
|
|
55
|
+
type: 'problem',
|
|
56
|
+
docs: {
|
|
57
|
+
description: 'Detects variable in filename argument of fs calls, which might allow an attacker to access anything on your system',
|
|
58
|
+
},
|
|
59
|
+
messages: {
|
|
60
|
+
// 🎯 Token optimization: 39% reduction (49→30 tokens) - template variables still work
|
|
61
|
+
fsPathTraversal: (0, eslint_devkit_1.formatLLMMessage)({
|
|
62
|
+
icon: '🔑',
|
|
63
|
+
issueName: 'Path traversal',
|
|
64
|
+
cwe: 'CWE-22',
|
|
65
|
+
description: 'Path traversal vulnerability',
|
|
66
|
+
severity: '{{riskLevel}}',
|
|
67
|
+
fix: '{{safePattern}}',
|
|
68
|
+
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
69
|
+
}),
|
|
70
|
+
usePathResolve: (0, eslint_devkit_1.formatLLMMessage)({
|
|
71
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
72
|
+
issueName: 'Use path.resolve',
|
|
73
|
+
description: 'Use path.resolve() to normalize paths',
|
|
74
|
+
severity: 'LOW',
|
|
75
|
+
fix: 'path.resolve(SAFE_DIR, userInput)',
|
|
76
|
+
documentationLink: 'https://nodejs.org/api/path.html#pathresolvepaths',
|
|
77
|
+
}),
|
|
78
|
+
validatePath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
79
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
80
|
+
issueName: 'Validate Path',
|
|
81
|
+
description: 'Validate resolved path starts with allowed base',
|
|
82
|
+
severity: 'LOW',
|
|
83
|
+
fix: 'if (!resolved.startsWith(SAFE_DIR)) throw new Error()',
|
|
84
|
+
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
85
|
+
}),
|
|
86
|
+
useBasename: (0, eslint_devkit_1.formatLLMMessage)({
|
|
87
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
88
|
+
issueName: 'Use path.basename',
|
|
89
|
+
description: 'Use path.basename() to strip directory components',
|
|
90
|
+
severity: 'LOW',
|
|
91
|
+
fix: 'path.basename(userInput)',
|
|
92
|
+
documentationLink: 'https://nodejs.org/api/path.html#pathbasenamepath-suffix',
|
|
93
|
+
}),
|
|
94
|
+
createSafeDir: (0, eslint_devkit_1.formatLLMMessage)({
|
|
95
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
96
|
+
issueName: 'Define Safe Directory',
|
|
97
|
+
description: 'Define SAFE_DIR constant',
|
|
98
|
+
severity: 'LOW',
|
|
99
|
+
fix: 'const SAFE_DIR = path.resolve(__dirname, "uploads")',
|
|
100
|
+
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
101
|
+
}),
|
|
102
|
+
whitelistExtensions: (0, eslint_devkit_1.formatLLMMessage)({
|
|
103
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
104
|
+
issueName: 'Whitelist Extensions',
|
|
105
|
+
description: 'Whitelist allowed file extensions',
|
|
106
|
+
severity: 'LOW',
|
|
107
|
+
fix: 'const ALLOWED_EXT = [".txt", ".pdf"]; if (!ALLOWED_EXT.includes(ext)) throw',
|
|
108
|
+
documentationLink: 'https://owasp.org/www-community/attacks/Path_Traversal',
|
|
109
|
+
})
|
|
110
|
+
},
|
|
111
|
+
schema: [
|
|
112
|
+
{
|
|
113
|
+
type: 'object',
|
|
114
|
+
properties: {
|
|
115
|
+
allowLiterals: {
|
|
116
|
+
type: 'boolean',
|
|
117
|
+
default: false,
|
|
118
|
+
description: 'Allow literal string paths'
|
|
119
|
+
},
|
|
120
|
+
additionalMethods: {
|
|
121
|
+
type: 'array',
|
|
122
|
+
items: { type: 'string' },
|
|
123
|
+
default: [],
|
|
124
|
+
description: 'Additional fs methods to check'
|
|
125
|
+
},
|
|
126
|
+
allowedExtensions: {
|
|
127
|
+
type: 'array',
|
|
128
|
+
items: { type: 'string' },
|
|
129
|
+
default: [],
|
|
130
|
+
description: 'Allowed file extensions (e.g., [".txt", ".json"])'
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
additionalProperties: false,
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
},
|
|
137
|
+
defaultOptions: [
|
|
138
|
+
{
|
|
139
|
+
allowLiterals: false,
|
|
140
|
+
additionalMethods: []
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
create(context) {
|
|
144
|
+
const options = context.options[0] || {};
|
|
145
|
+
const { allowLiterals = false, additionalMethods = [] } = options || {};
|
|
146
|
+
/**
|
|
147
|
+
* File system methods that can be dangerous with user input
|
|
148
|
+
*/
|
|
149
|
+
const dangerousMethods = [
|
|
150
|
+
'readFile', 'readFileSync',
|
|
151
|
+
'writeFile', 'writeFileSync',
|
|
152
|
+
'appendFile', 'appendFileSync',
|
|
153
|
+
'stat', 'statSync',
|
|
154
|
+
'lstat', 'lstatSync',
|
|
155
|
+
'readdir', 'readdirSync',
|
|
156
|
+
'unlink', 'unlinkSync',
|
|
157
|
+
'mkdir', 'mkdirSync',
|
|
158
|
+
'rmdir', 'rmdirSync',
|
|
159
|
+
'access', 'accessSync',
|
|
160
|
+
'createReadStream', 'createWriteStream',
|
|
161
|
+
...additionalMethods
|
|
162
|
+
];
|
|
163
|
+
/**
|
|
164
|
+
* Check if a node is a literal string (safe)
|
|
165
|
+
*/
|
|
166
|
+
const isLiteralString = (node) => {
|
|
167
|
+
return node.type === 'Literal' && typeof node.value === 'string';
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Check if path has dangerous patterns like ../ or ..\
|
|
171
|
+
*/
|
|
172
|
+
const hasTraversalPatterns = (pathStr) => {
|
|
173
|
+
return /\.\.[/\\]/.test(pathStr) || /^\.\.[/\\]/.test(pathStr);
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Extract path argument from fs call
|
|
177
|
+
*/
|
|
178
|
+
const extractPathArgument = (node) => {
|
|
179
|
+
const method = node.callee.type === 'MemberExpression' &&
|
|
180
|
+
node.callee.property.type === 'Identifier'
|
|
181
|
+
? node.callee.property.name
|
|
182
|
+
: 'unknown';
|
|
183
|
+
const operation = FS_OPERATIONS.find(op => op.method === method) || null;
|
|
184
|
+
// First argument is usually the path
|
|
185
|
+
const pathNode = node.arguments.length > 0 ? node.arguments[0] : null;
|
|
186
|
+
const sourceCode = context.sourceCode || context.sourceCode;
|
|
187
|
+
const path = pathNode ? sourceCode.getText(pathNode) : '';
|
|
188
|
+
return { path, pathNode, method, operation };
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* Determine if the path argument is potentially dangerous
|
|
192
|
+
*/
|
|
193
|
+
const isDangerousPath = (pathNode, pathStr) => {
|
|
194
|
+
// Allow literals if configured
|
|
195
|
+
if (allowLiterals && pathNode && isLiteralString(pathNode)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
// Check for obvious traversal patterns in literals
|
|
199
|
+
if (pathNode && isLiteralString(pathNode) && hasTraversalPatterns(pathStr)) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
// Any non-literal is dangerous
|
|
203
|
+
return !pathNode || !isLiteralString(pathNode);
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Generate refactoring steps based on the operation
|
|
207
|
+
*/
|
|
208
|
+
const generateRefactoringSteps = (operation) => {
|
|
209
|
+
switch (operation.method) {
|
|
210
|
+
case 'readFile':
|
|
211
|
+
case 'writeFile':
|
|
212
|
+
return [
|
|
213
|
+
' 1. Define a SAFE_DIR constant for allowed operations',
|
|
214
|
+
' 2. Use path.basename() to strip directory components',
|
|
215
|
+
' 3. Combine with SAFE_DIR: path.join(SAFE_DIR, path.basename(userPath))',
|
|
216
|
+
' 4. Optionally validate file extensions',
|
|
217
|
+
' 5. Add error handling for invalid paths'
|
|
218
|
+
].join('\n');
|
|
219
|
+
case 'stat':
|
|
220
|
+
return [
|
|
221
|
+
' 1. Use path.resolve() to normalize the path',
|
|
222
|
+
' 2. Check if resolved path starts with allowed base directory',
|
|
223
|
+
' 3. Reject requests that escape the allowed directory',
|
|
224
|
+
' 4. Use path.relative() for additional validation',
|
|
225
|
+
' 5. Log security events for monitoring'
|
|
226
|
+
].join('\n');
|
|
227
|
+
case 'readdir':
|
|
228
|
+
return [
|
|
229
|
+
' 1. Resolve the directory path: path.resolve(ALLOWED_DIRS, userDir)',
|
|
230
|
+
' 2. Validate resolved path starts with ALLOWED_DIRS',
|
|
231
|
+
' 3. Check directory exists and is readable',
|
|
232
|
+
' 4. Consider whitelisting allowed directories',
|
|
233
|
+
' 5. Add rate limiting to prevent enumeration attacks'
|
|
234
|
+
].join('\n');
|
|
235
|
+
default:
|
|
236
|
+
return [
|
|
237
|
+
' 1. Identify the specific file operation needed',
|
|
238
|
+
' 2. Define safe base directories for operations',
|
|
239
|
+
' 3. Use path.resolve() and validate containment',
|
|
240
|
+
' 4. Sanitize user input (basename, extension validation)',
|
|
241
|
+
' 5. Add comprehensive error handling'
|
|
242
|
+
].join('\n');
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Determine risk level based on the operation and path
|
|
247
|
+
*/
|
|
248
|
+
const determineRiskLevel = (operation, pathStr) => {
|
|
249
|
+
if (hasTraversalPatterns(pathStr)) {
|
|
250
|
+
return 'CRITICAL';
|
|
251
|
+
}
|
|
252
|
+
if (operation.dangerous) {
|
|
253
|
+
return 'HIGH';
|
|
254
|
+
}
|
|
255
|
+
return 'MEDIUM';
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* Check fs method calls for path traversal vulnerabilities
|
|
259
|
+
*/
|
|
260
|
+
const checkFsCall = (node) => {
|
|
261
|
+
// Check if it's an fs method call
|
|
262
|
+
if (node.callee.type !== 'MemberExpression' ||
|
|
263
|
+
node.callee.object.type !== 'Identifier' ||
|
|
264
|
+
node.callee.object.name !== 'fs' ||
|
|
265
|
+
node.callee.property.type !== 'Identifier') {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const methodName = node.callee.property.name;
|
|
269
|
+
// Skip if not a dangerous method
|
|
270
|
+
if (!dangerousMethods.includes(methodName)) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const { path, pathNode, method, operation } = extractPathArgument(node);
|
|
274
|
+
// Check if the path argument is dangerous
|
|
275
|
+
if (!isDangerousPath(pathNode, path)) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const riskLevel = determineRiskLevel(operation || FS_OPERATIONS[0], path);
|
|
279
|
+
const steps = operation ? generateRefactoringSteps(operation) : 'Review file system access patterns';
|
|
280
|
+
const safePattern = operation?.safePattern || 'Use path.resolve() with validation';
|
|
281
|
+
context.report({
|
|
282
|
+
node,
|
|
283
|
+
messageId: 'fsPathTraversal',
|
|
284
|
+
data: {
|
|
285
|
+
method,
|
|
286
|
+
path,
|
|
287
|
+
riskLevel,
|
|
288
|
+
vulnerability: operation?.vulnerability || 'path traversal',
|
|
289
|
+
safePattern,
|
|
290
|
+
steps,
|
|
291
|
+
effort: operation?.effort || '15-20 minutes'
|
|
292
|
+
},
|
|
293
|
+
suggest: [
|
|
294
|
+
{
|
|
295
|
+
messageId: 'usePathResolve',
|
|
296
|
+
fix: () => null
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
messageId: 'validatePath',
|
|
300
|
+
fix: () => null
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
messageId: 'useBasename',
|
|
304
|
+
fix: () => null
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
messageId: 'createSafeDir',
|
|
308
|
+
fix: () => null
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
messageId: 'whitelistExtensions',
|
|
312
|
+
fix: () => null
|
|
313
|
+
}
|
|
314
|
+
]
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
return {
|
|
318
|
+
CallExpression: checkFsCall
|
|
319
|
+
};
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
//# sourceMappingURL=detect-non-literal-fs-filename.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect-non-literal-fs-filename.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-secure-coding/src/rules/security/detect-non-literal-fs-filename.ts"],"names":[],"mappings":";;;AASA,4DAA0E;AAC1E,4DAAsD;AAgCtD,MAAM,aAAa,GAAkB;IACnC;QACE,MAAM,EAAE,UAAU;QAClB,SAAS,EAAE,IAAI;QACf,aAAa,EAAE,aAAa;QAC5B,WAAW,EAAE,kDAAkD;QAC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,IAAI,gBAAgB;oBAC3D,WAAW;oBACX,KAAK;oBACL,MAAM,EAAE,SAAS,EAAE,MAAM,IAAI,eAAe;iBAC7C;gBACD,OAAO,EAAE;oBACP;wBACE,SAAS,EAAE,gBAAgB;wBAC3B,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qBAChB;oBACD;wBACE,SAAS,EAAE,cAAc;wBACzB,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qBAChB;oBACD;wBACE,SAAS,EAAE,aAAa;wBACxB,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qBAChB;oBACD;wBACE,SAAS,EAAE,eAAe;wBAC1B,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qBAChB;oBACD;wBACE,SAAS,EAAE,qBAAqB;wBAChC,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qBAChB;iBACF;aACF,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,OAAO;YACL,cAAc,EAAE,WAAW;SAC5B,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
/** Allow literal string regex patterns. Default: false (stricter) */
|
|
3
|
+
allowLiterals?: boolean;
|
|
4
|
+
/** Additional RegExp creation patterns to check */
|
|
5
|
+
additionalPatterns?: string[];
|
|
6
|
+
/** Maximum allowed pattern length for dynamic regex */
|
|
7
|
+
maxPatternLength?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const detectNonLiteralRegexp: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
|