eslint-plugin-secure-coding 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/AGENTS.md +1 -1
  2. package/CHANGELOG.md +1 -1
  3. package/README.md +90 -422
  4. package/package.json +6 -5
  5. package/src/index.d.ts +5 -14
  6. package/src/index.js +36 -265
  7. package/src/rules/detect-non-literal-regexp/index.d.ts +20 -1
  8. package/src/rules/detect-non-literal-regexp/index.js +5 -0
  9. package/src/rules/detect-object-injection/index.d.ts +25 -1
  10. package/src/rules/detect-object-injection/index.js +5 -0
  11. package/src/rules/detect-weak-password-validation/index.d.ts +8 -2
  12. package/src/rules/detect-weak-password-validation/index.js +6 -1
  13. package/src/rules/no-directive-injection/index.d.ts +27 -1
  14. package/src/rules/no-directive-injection/index.js +5 -0
  15. package/src/rules/no-electron-security-issues/index.d.ts +27 -1
  16. package/src/rules/no-electron-security-issues/index.js +5 -0
  17. package/src/rules/no-format-string-injection/index.d.ts +28 -1
  18. package/src/rules/no-format-string-injection/index.js +5 -0
  19. package/src/rules/no-graphql-injection/index.d.ts +29 -1
  20. package/src/rules/no-graphql-injection/index.js +5 -0
  21. package/src/rules/no-hardcoded-credentials/index.d.ts +19 -1
  22. package/src/rules/no-hardcoded-credentials/index.js +5 -0
  23. package/src/rules/no-hardcoded-session-tokens/index.d.ts +8 -2
  24. package/src/rules/no-hardcoded-session-tokens/index.js +6 -1
  25. package/src/rules/no-improper-sanitization/index.d.ts +27 -1
  26. package/src/rules/no-improper-sanitization/index.js +5 -0
  27. package/src/rules/no-improper-type-validation/index.d.ts +27 -1
  28. package/src/rules/no-improper-type-validation/index.js +5 -0
  29. package/src/rules/no-insecure-comparison/index.d.ts +20 -1
  30. package/src/rules/no-insecure-comparison/index.js +5 -0
  31. package/src/rules/no-ldap-injection/index.d.ts +30 -1
  32. package/src/rules/no-ldap-injection/index.js +5 -0
  33. package/src/rules/no-missing-authentication/index.d.ts +20 -1
  34. package/src/rules/no-missing-authentication/index.js +5 -1
  35. package/src/rules/no-pii-in-logs/index.d.ts +8 -4
  36. package/src/rules/no-pii-in-logs/index.js +15 -12
  37. package/src/rules/no-privilege-escalation/index.d.ts +20 -1
  38. package/src/rules/no-privilege-escalation/index.js +5 -0
  39. package/src/rules/no-redos-vulnerable-regex/index.d.ts +22 -1
  40. package/src/rules/no-redos-vulnerable-regex/index.js +5 -0
  41. package/src/rules/no-sensitive-data-exposure/index.d.ts +20 -1
  42. package/src/rules/no-sensitive-data-exposure/index.js +5 -0
  43. package/src/rules/no-unchecked-loop-condition/index.d.ts +27 -1
  44. package/src/rules/no-unchecked-loop-condition/index.js +5 -0
  45. package/src/rules/no-unlimited-resource-allocation/index.d.ts +27 -1
  46. package/src/rules/no-unlimited-resource-allocation/index.js +5 -0
  47. package/src/rules/no-unsafe-deserialization/index.d.ts +31 -1
  48. package/src/rules/no-unsafe-deserialization/index.js +5 -0
  49. package/src/rules/no-unsafe-regex-construction/index.d.ts +22 -1
  50. package/src/rules/no-unsafe-regex-construction/index.js +5 -0
  51. package/src/rules/no-weak-password-recovery/index.d.ts +27 -1
  52. package/src/rules/no-weak-password-recovery/index.js +5 -0
  53. package/src/rules/no-xpath-injection/index.d.ts +30 -1
  54. package/src/rules/no-xpath-injection/index.js +5 -0
  55. package/src/rules/no-xxe-injection/index.d.ts +30 -1
  56. package/src/rules/no-xxe-injection/index.js +5 -0
  57. package/src/rules/require-backend-authorization/index.d.ts +8 -2
  58. package/src/rules/require-backend-authorization/index.js +6 -1
  59. package/src/rules/require-secure-defaults/index.d.ts +8 -4
  60. package/src/rules/require-secure-defaults/index.js +7 -6
  61. package/src/types/index.d.ts +10 -52
  62. package/src/types/index.js +3 -12
  63. package/src/rules/detect-child-process/index.d.ts +0 -11
  64. package/src/rules/detect-child-process/index.js +0 -529
  65. package/src/rules/detect-eval-with-expression/index.d.ts +0 -9
  66. package/src/rules/detect-eval-with-expression/index.js +0 -392
  67. package/src/rules/detect-mixed-content/index.d.ts +0 -8
  68. package/src/rules/detect-mixed-content/index.js +0 -44
  69. package/src/rules/detect-non-literal-fs-filename/index.d.ts +0 -7
  70. package/src/rules/detect-non-literal-fs-filename/index.js +0 -454
  71. package/src/rules/detect-suspicious-dependencies/index.d.ts +0 -8
  72. package/src/rules/detect-suspicious-dependencies/index.js +0 -71
  73. package/src/rules/no-allow-arbitrary-loads/index.d.ts +0 -8
  74. package/src/rules/no-allow-arbitrary-loads/index.js +0 -47
  75. package/src/rules/no-arbitrary-file-access/index.d.ts +0 -13
  76. package/src/rules/no-arbitrary-file-access/index.js +0 -195
  77. package/src/rules/no-buffer-overread/index.d.ts +0 -29
  78. package/src/rules/no-buffer-overread/index.js +0 -606
  79. package/src/rules/no-clickjacking/index.d.ts +0 -10
  80. package/src/rules/no-clickjacking/index.js +0 -396
  81. package/src/rules/no-client-side-auth-logic/index.d.ts +0 -6
  82. package/src/rules/no-client-side-auth-logic/index.js +0 -69
  83. package/src/rules/no-credentials-in-query-params/index.d.ts +0 -8
  84. package/src/rules/no-credentials-in-query-params/index.js +0 -57
  85. package/src/rules/no-data-in-temp-storage/index.d.ts +0 -6
  86. package/src/rules/no-data-in-temp-storage/index.js +0 -64
  87. package/src/rules/no-debug-code-in-production/index.d.ts +0 -8
  88. package/src/rules/no-debug-code-in-production/index.js +0 -51
  89. package/src/rules/no-disabled-certificate-validation/index.d.ts +0 -6
  90. package/src/rules/no-disabled-certificate-validation/index.js +0 -61
  91. package/src/rules/no-dynamic-dependency-loading/index.d.ts +0 -8
  92. package/src/rules/no-dynamic-dependency-loading/index.js +0 -51
  93. package/src/rules/no-exposed-debug-endpoints/index.d.ts +0 -6
  94. package/src/rules/no-exposed-debug-endpoints/index.js +0 -62
  95. package/src/rules/no-exposed-sensitive-data/index.d.ts +0 -11
  96. package/src/rules/no-exposed-sensitive-data/index.js +0 -340
  97. package/src/rules/no-http-urls/index.d.ts +0 -12
  98. package/src/rules/no-http-urls/index.js +0 -114
  99. package/src/rules/no-insecure-redirects/index.d.ts +0 -7
  100. package/src/rules/no-insecure-redirects/index.js +0 -216
  101. package/src/rules/no-insecure-websocket/index.d.ts +0 -6
  102. package/src/rules/no-insecure-websocket/index.js +0 -61
  103. package/src/rules/no-missing-cors-check/index.d.ts +0 -9
  104. package/src/rules/no-missing-cors-check/index.js +0 -399
  105. package/src/rules/no-missing-csrf-protection/index.d.ts +0 -11
  106. package/src/rules/no-missing-csrf-protection/index.js +0 -180
  107. package/src/rules/no-missing-security-headers/index.d.ts +0 -7
  108. package/src/rules/no-missing-security-headers/index.js +0 -218
  109. package/src/rules/no-password-in-url/index.d.ts +0 -8
  110. package/src/rules/no-password-in-url/index.js +0 -54
  111. package/src/rules/no-permissive-cors/index.d.ts +0 -8
  112. package/src/rules/no-permissive-cors/index.js +0 -65
  113. package/src/rules/no-sensitive-data-in-analytics/index.d.ts +0 -8
  114. package/src/rules/no-sensitive-data-in-analytics/index.js +0 -62
  115. package/src/rules/no-sensitive-data-in-cache/index.d.ts +0 -8
  116. package/src/rules/no-sensitive-data-in-cache/index.js +0 -52
  117. package/src/rules/no-toctou-vulnerability/index.d.ts +0 -7
  118. package/src/rules/no-toctou-vulnerability/index.js +0 -208
  119. package/src/rules/no-tracking-without-consent/index.d.ts +0 -6
  120. package/src/rules/no-tracking-without-consent/index.js +0 -67
  121. package/src/rules/no-unencrypted-transmission/index.d.ts +0 -11
  122. package/src/rules/no-unencrypted-transmission/index.js +0 -236
  123. package/src/rules/no-unescaped-url-parameter/index.d.ts +0 -9
  124. package/src/rules/no-unescaped-url-parameter/index.js +0 -355
  125. package/src/rules/no-unsafe-dynamic-require/index.d.ts +0 -5
  126. package/src/rules/no-unsafe-dynamic-require/index.js +0 -106
  127. package/src/rules/no-unvalidated-deeplinks/index.d.ts +0 -6
  128. package/src/rules/no-unvalidated-deeplinks/index.js +0 -62
  129. package/src/rules/no-unvalidated-user-input/index.d.ts +0 -9
  130. package/src/rules/no-unvalidated-user-input/index.js +0 -420
  131. package/src/rules/no-verbose-error-messages/index.d.ts +0 -8
  132. package/src/rules/no-verbose-error-messages/index.js +0 -68
  133. package/src/rules/no-zip-slip/index.d.ts +0 -9
  134. package/src/rules/no-zip-slip/index.js +0 -445
  135. package/src/rules/require-code-minification/index.d.ts +0 -8
  136. package/src/rules/require-code-minification/index.js +0 -47
  137. package/src/rules/require-csp-headers/index.d.ts +0 -6
  138. package/src/rules/require-csp-headers/index.js +0 -64
  139. package/src/rules/require-data-minimization/index.d.ts +0 -8
  140. package/src/rules/require-data-minimization/index.js +0 -53
  141. package/src/rules/require-dependency-integrity/index.d.ts +0 -6
  142. package/src/rules/require-dependency-integrity/index.js +0 -64
  143. package/src/rules/require-https-only/index.d.ts +0 -8
  144. package/src/rules/require-https-only/index.js +0 -62
  145. package/src/rules/require-mime-type-validation/index.d.ts +0 -6
  146. package/src/rules/require-mime-type-validation/index.js +0 -66
  147. package/src/rules/require-network-timeout/index.d.ts +0 -8
  148. package/src/rules/require-network-timeout/index.js +0 -50
  149. package/src/rules/require-package-lock/index.d.ts +0 -8
  150. package/src/rules/require-package-lock/index.js +0 -63
  151. package/src/rules/require-secure-credential-storage/index.d.ts +0 -8
  152. package/src/rules/require-secure-credential-storage/index.js +0 -50
  153. package/src/rules/require-secure-deletion/index.d.ts +0 -8
  154. package/src/rules/require-secure-deletion/index.js +0 -44
  155. package/src/rules/require-storage-encryption/index.d.ts +0 -8
  156. package/src/rules/require-storage-encryption/index.js +0 -50
  157. package/src/rules/require-url-validation/index.d.ts +0 -6
  158. package/src/rules/require-url-validation/index.js +0 -72
@@ -1,454 +0,0 @@
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 === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
180
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.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
- // SAFE: path.join(__dirname, 'literal', 'path') with all literal args
203
- if (pathNode && isSafePathConstruction(pathNode)) {
204
- return false;
205
- }
206
- // SAFE: Path variable inside validated if-block with startsWith check
207
- if (pathNode && hasPathValidation(pathNode)) {
208
- return false;
209
- }
210
- // Any non-literal is dangerous
211
- return !pathNode || !isLiteralString(pathNode);
212
- };
213
- /**
214
- * Check if path is constructed safely using path.join/__dirname with literal args
215
- *
216
- * Safe patterns:
217
- * - path.join(__dirname, 'data', 'file.json')
218
- * - path.resolve(__dirname, 'uploads')
219
- */
220
- const isSafePathConstruction = (pathNode) => {
221
- if (pathNode.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
222
- return false;
223
- }
224
- const callee = pathNode.callee;
225
- if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
226
- callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
227
- callee.object.name !== 'path' ||
228
- callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
229
- return false;
230
- }
231
- const method = callee.property.name;
232
- if (!['join', 'resolve'].includes(method)) {
233
- return false;
234
- }
235
- const args = pathNode.arguments;
236
- if (args.length === 0) {
237
- return false;
238
- }
239
- // First arg should be __dirname or a literal
240
- const firstArg = args[0];
241
- const isFirstArgSafe = (firstArg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && firstArg.name === '__dirname') ||
242
- (firstArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof firstArg.value === 'string');
243
- if (!isFirstArgSafe) {
244
- return false;
245
- }
246
- // All remaining args should be literals
247
- for (let i = 1; i < args.length; i++) {
248
- const arg = args[i];
249
- if (arg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal || typeof arg.value !== 'string') {
250
- return false;
251
- }
252
- // Also check for traversal patterns in literals
253
- if (hasTraversalPatterns(String(arg.value))) {
254
- return false;
255
- }
256
- }
257
- return true;
258
- };
259
- /**
260
- * Check if the path variable has been validated with startsWith()
261
- *
262
- * Safe patterns:
263
- * 1. Inside if-block: if (safePath.startsWith(SAFE_DIR)) { fs.readFileSync(safePath); }
264
- * 2. After guard clause: if (!safePath.startsWith(SAFE_DIR)) { throw }; fs.readFileSync(safePath);
265
- */
266
- const hasPathValidation = (pathNode) => {
267
- if (pathNode.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
268
- return false;
269
- }
270
- const varName = pathNode.name;
271
- // AST-based validation detection (faster than getText + regex)
272
- const isStartsWithOrIncludesCall = (testNode) => {
273
- // Handle negation: !path.startsWith(...)
274
- if (testNode.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression &&
275
- testNode.operator === '!' &&
276
- testNode.argument.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
277
- testNode = testNode.argument;
278
- }
279
- // Pattern: varName.startsWith(...) or varName.includes(...)
280
- if (testNode.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
281
- testNode.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
282
- testNode.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
283
- testNode.callee.object.name === varName &&
284
- testNode.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
285
- (testNode.callee.property.name === 'startsWith' ||
286
- testNode.callee.property.name === 'includes')) {
287
- return true;
288
- }
289
- return false;
290
- };
291
- const hasEarlyExit = (consequent) => {
292
- if (consequent.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
293
- return consequent.body.some(stmt => stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement ||
294
- stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement);
295
- }
296
- return consequent.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement ||
297
- consequent.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement;
298
- };
299
- // Walk up to find enclosing IfStatement or BlockStatement
300
- let current = pathNode.parent;
301
- let foundFunctionBody = false;
302
- while (current && !foundFunctionBody) {
303
- // Check 1: Inside an if-block with validation
304
- if (current.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement) {
305
- if (isStartsWithOrIncludesCall(current.test)) {
306
- return true;
307
- }
308
- }
309
- // Check 2: In a function body, look for preceding sibling if-statements with guard clause
310
- if (current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement && current.parent && (current.parent.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration ||
311
- current.parent.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
312
- current.parent.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression)) {
313
- foundFunctionBody = true;
314
- const blockBody = current.body;
315
- const nodeIndex = blockBody.findIndex((stmt) => {
316
- let check = pathNode;
317
- while (check) {
318
- if (check === stmt)
319
- return true;
320
- check = check.parent;
321
- }
322
- return false;
323
- });
324
- // Look at preceding statements for validation patterns with early exit
325
- for (let i = 0; i < nodeIndex; i++) {
326
- const stmt = blockBody[i];
327
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement &&
328
- isStartsWithOrIncludesCall(stmt.test) &&
329
- hasEarlyExit(stmt.consequent)) {
330
- return true;
331
- }
332
- }
333
- }
334
- current = current.parent;
335
- }
336
- return false;
337
- };
338
- /**
339
- * Generate refactoring steps based on the operation
340
- */
341
- const generateRefactoringSteps = (operation) => {
342
- switch (operation.method) {
343
- case 'readFile':
344
- case 'writeFile':
345
- return [
346
- ' 1. Define a SAFE_DIR constant for allowed operations',
347
- ' 2. Use path.basename() to strip directory components',
348
- ' 3. Combine with SAFE_DIR: path.join(SAFE_DIR, path.basename(userPath))',
349
- ' 4. Optionally validate file extensions',
350
- ' 5. Add error handling for invalid paths'
351
- ].join('\n');
352
- case 'stat':
353
- return [
354
- ' 1. Use path.resolve() to normalize the path',
355
- ' 2. Check if resolved path starts with allowed base directory',
356
- ' 3. Reject requests that escape the allowed directory',
357
- ' 4. Use path.relative() for additional validation',
358
- ' 5. Log security events for monitoring'
359
- ].join('\n');
360
- case 'readdir':
361
- return [
362
- ' 1. Resolve the directory path: path.resolve(ALLOWED_DIRS, userDir)',
363
- ' 2. Validate resolved path starts with ALLOWED_DIRS',
364
- ' 3. Check directory exists and is readable',
365
- ' 4. Consider whitelisting allowed directories',
366
- ' 5. Add rate limiting to prevent enumeration attacks'
367
- ].join('\n');
368
- default:
369
- return [
370
- ' 1. Identify the specific file operation needed',
371
- ' 2. Define safe base directories for operations',
372
- ' 3. Use path.resolve() and validate containment',
373
- ' 4. Sanitize user input (basename, extension validation)',
374
- ' 5. Add comprehensive error handling'
375
- ].join('\n');
376
- }
377
- };
378
- /**
379
- * Determine risk level based on the operation and path
380
- */
381
- const determineRiskLevel = (operation, pathStr) => {
382
- if (hasTraversalPatterns(pathStr)) {
383
- return 'CRITICAL';
384
- }
385
- if (operation.dangerous) {
386
- return 'HIGH';
387
- }
388
- return 'MEDIUM';
389
- };
390
- /**
391
- * Check fs method calls for path traversal vulnerabilities
392
- */
393
- const checkFsCall = (node) => {
394
- // Check if it's an fs method call
395
- if (node.callee.type !== 'MemberExpression' ||
396
- node.callee.object.type !== 'Identifier' ||
397
- node.callee.object.name !== 'fs' ||
398
- node.callee.property.type !== 'Identifier') {
399
- return;
400
- }
401
- const methodName = node.callee.property.name;
402
- // Skip if not a dangerous method
403
- if (!dangerousMethods.includes(methodName)) {
404
- return;
405
- }
406
- const { path, pathNode, method, operation } = extractPathArgument(node);
407
- // Check if the path argument is dangerous
408
- if (!isDangerousPath(pathNode, path)) {
409
- return;
410
- }
411
- const riskLevel = determineRiskLevel(operation || FS_OPERATIONS[0], path);
412
- const steps = operation ? generateRefactoringSteps(operation) : 'Review file system access patterns';
413
- const safePattern = operation?.safePattern || 'Use path.resolve() with validation';
414
- context.report({
415
- node,
416
- messageId: 'fsPathTraversal',
417
- data: {
418
- method,
419
- path,
420
- riskLevel,
421
- vulnerability: operation?.vulnerability || 'path traversal',
422
- safePattern,
423
- steps,
424
- effort: operation?.effort || '15-20 minutes'
425
- },
426
- suggest: [
427
- {
428
- messageId: 'usePathResolve',
429
- fix: () => null
430
- },
431
- {
432
- messageId: 'validatePath',
433
- fix: () => null
434
- },
435
- {
436
- messageId: 'useBasename',
437
- fix: () => null
438
- },
439
- {
440
- messageId: 'createSafeDir',
441
- fix: () => null
442
- },
443
- {
444
- messageId: 'whitelistExtensions',
445
- fix: () => null
446
- }
447
- ]
448
- });
449
- };
450
- return {
451
- CallExpression: checkFsCall
452
- };
453
- },
454
- });
@@ -1,8 +0,0 @@
1
- /**
2
- * @fileoverview Detect potential typosquatting in dependencies
3
- * @see https://owasp.org/www-project-mobile-top-10/
4
- * @see https://cwe.mitre.org/data/definitions/506.html
5
- */
6
- export interface Options {
7
- }
8
- export declare const detectSuspiciousDependencies: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
@@ -1,71 +0,0 @@
1
- "use strict";
2
- /**
3
- * @fileoverview Detect potential typosquatting in dependencies
4
- * @see https://owasp.org/www-project-mobile-top-10/
5
- * @see https://cwe.mitre.org/data/definitions/506.html
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.detectSuspiciousDependencies = void 0;
9
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- exports.detectSuspiciousDependencies = (0, eslint_devkit_1.createRule)({
11
- name: 'detect-suspicious-dependencies',
12
- meta: {
13
- type: 'problem',
14
- docs: {
15
- description: 'Detect typosquatting in package names',
16
- },
17
- messages: {
18
- violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
19
- icon: eslint_devkit_1.MessageIcons.SECURITY,
20
- issueName: 'Suspicious Dependency',
21
- cwe: 'CWE-506',
22
- description: 'Suspicious package name detected - possible typosquatting',
23
- severity: 'HIGH',
24
- fix: 'Verify package authenticity on npm registry',
25
- documentationLink: 'https://cwe.mitre.org/data/definitions/506.html',
26
- })
27
- },
28
- schema: [],
29
- },
30
- defaultOptions: [],
31
- create(context) {
32
- const popularPackages = ['react', 'lodash', 'express', 'axios', 'webpack'];
33
- function levenshtein(a, b) {
34
- const matrix = [];
35
- for (let i = 0; i <= b.length; i++) {
36
- matrix[i] = [i];
37
- }
38
- for (let j = 0; j <= a.length; j++) {
39
- matrix[0][j] = j;
40
- }
41
- for (let i = 1; i <= b.length; i++) {
42
- for (let j = 1; j <= a.length; j++) {
43
- if (b.charAt(i - 1) === a.charAt(j - 1)) {
44
- matrix[i][j] = matrix[i - 1][j - 1];
45
- }
46
- else {
47
- matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1);
48
- }
49
- }
50
- }
51
- return matrix[b.length][a.length];
52
- }
53
- return {
54
- ImportDeclaration(node) {
55
- const source = node.source.value;
56
- if (typeof source === 'string' && !source.startsWith('.') && !source.startsWith('@')) {
57
- for (const popular of popularPackages) {
58
- const distance = levenshtein(source, popular);
59
- if (distance > 0 && distance <= 2) {
60
- context.report({
61
- node,
62
- messageId: 'violationDetected',
63
- data: { name: source, similar: popular },
64
- });
65
- }
66
- }
67
- }
68
- },
69
- };
70
- },
71
- });
@@ -1,8 +0,0 @@
1
- /**
2
- * @fileoverview Prevent configuration allowing insecure loads
3
- * @see https://owasp.org/www-project-mobile-top-10/
4
- * @see https://cwe.mitre.org/data/definitions/749.html
5
- */
6
- export interface Options {
7
- }
8
- export declare const noAllowArbitraryLoads: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
@@ -1,47 +0,0 @@
1
- "use strict";
2
- /**
3
- * @fileoverview Prevent configuration allowing insecure loads
4
- * @see https://owasp.org/www-project-mobile-top-10/
5
- * @see https://cwe.mitre.org/data/definitions/749.html
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.noAllowArbitraryLoads = void 0;
9
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- exports.noAllowArbitraryLoads = (0, eslint_devkit_1.createRule)({
11
- name: 'no-allow-arbitrary-loads',
12
- meta: {
13
- type: 'problem',
14
- docs: {
15
- description: 'Prevent configuration allowing insecure loads',
16
- category: 'Security',
17
- recommended: true,
18
- owaspMobile: ['M5'],
19
- cweIds: ["CWE-749"],
20
- },
21
- messages: {
22
- violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
23
- icon: eslint_devkit_1.MessageIcons.SECURITY,
24
- issueName: 'violation Detected',
25
- cwe: 'CWE-295',
26
- description: 'Prevent configuration allowing insecure loads detected - allowArbitraryLoads: true',
27
- severity: 'HIGH',
28
- fix: 'Review and apply secure practices',
29
- documentationLink: 'https://cwe.mitre.org/data/definitions/295.html',
30
- })
31
- },
32
- schema: [],
33
- },
34
- defaultOptions: [],
35
- create(context) {
36
- return {
37
- Property(node) {
38
- if (node.key.type === 'Identifier' &&
39
- node.key.name === 'allowArbitraryLoads' &&
40
- node.value.type === 'Literal' &&
41
- node.value.value === true) {
42
- context.report({ node, messageId: 'violationDetected' });
43
- }
44
- },
45
- };
46
- },
47
- });
@@ -1,13 +0,0 @@
1
- /**
2
- * @fileoverview Prevent file access from user input
3
- *
4
- * False Positive Reduction:
5
- * This rule detects safe patterns including:
6
- * - path.basename() sanitization
7
- * - path.join() with validated base directories
8
- * - startsWith() validation guards
9
- * - Early-return throw patterns
10
- */
11
- export interface Options {
12
- }
13
- export declare const noArbitraryFileAccess: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;