eslint-plugin-express-security 1.3.4 → 1.5.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 +46 -27
- package/package.json +27 -18
- package/src/index.d.ts +17 -40
- package/src/index.js +47 -62
- package/src/oxlint.d.ts +17 -20
- package/src/oxlint.js +0 -18
- package/src/rules/no-client-controlled-authorization/index.js +194 -0
- package/src/rules/no-cors-credentials-wildcard/index.js +0 -22
- package/src/rules/no-disabled-helmet-protections/index.js +125 -0
- package/src/rules/no-error-details-in-response/index.js +176 -0
- package/src/rules/no-exposed-debug-endpoints/index.js +27 -32
- package/src/rules/no-express-unsafe-regex-route/index.js +1 -32
- package/src/rules/no-graphql-introspection-production/index.js +0 -21
- package/src/rules/no-host-header-in-links/index.js +235 -0
- package/src/rules/no-idor-resource-access/index.js +126 -0
- package/src/rules/no-insecure-cookie-options/index.js +0 -12
- package/src/rules/no-missing-cors-check/index.js +4 -54
- package/src/rules/no-missing-csrf-protection/index.js +0 -25
- package/src/rules/no-missing-security-headers/index.js +50 -69
- package/src/rules/no-permissive-cors/index.js +0 -27
- package/src/rules/no-permissive-trust-proxy/index.js +106 -0
- package/src/rules/no-sensitive-data-in-query/index.js +182 -0
- package/src/rules/no-static-root-exposure/index.js +267 -0
- package/src/rules/no-unsafe-csp-directives/index.js +264 -0
- package/src/rules/no-user-controlled-redirect/index.js +0 -26
- package/src/rules/no-user-controlled-render-locals/index.js +268 -0
- package/src/rules/require-case-insensitive-path-guard/index.js +180 -0
- package/src/rules/require-csrf-protection/index.js +0 -42
- package/src/rules/require-express-body-parser-limits/index.js +0 -24
- package/src/rules/require-helmet/index.js +1 -25
- package/src/rules/require-query-type-guard/index.js +258 -0
- package/src/rules/require-rate-limiting/index.js +0 -13
- package/src/rules/require-route-authentication/index.js +225 -0
- package/src/rules/require-strict-transport-security/index.js +171 -0
- package/src/types/index.js +0 -5
- package/src/utils/index.js +81 -0
- package/AGENTS.md +0 -100
- package/CHANGELOG.md +0 -149
- package/src/rules/no-cors-credentials-wildcard/index.d.ts +0 -26
- package/src/rules/no-exposed-debug-endpoints/index.d.ts +0 -14
- package/src/rules/no-express-unsafe-regex-route/index.d.ts +0 -27
- package/src/rules/no-graphql-introspection-production/index.d.ts +0 -26
- package/src/rules/no-insecure-cookie-options/index.d.ts +0 -39
- package/src/rules/no-missing-cors-check/index.d.ts +0 -28
- package/src/rules/no-missing-csrf-protection/index.d.ts +0 -30
- package/src/rules/no-missing-security-headers/index.d.ts +0 -26
- package/src/rules/no-permissive-cors/index.d.ts +0 -28
- package/src/rules/no-user-controlled-redirect/index.d.ts +0 -44
- package/src/rules/require-csrf-protection/index.d.ts +0 -28
- package/src/rules/require-express-body-parser-limits/index.d.ts +0 -31
- package/src/rules/require-helmet/index.d.ts +0 -29
- package/src/rules/require-rate-limiting/index.d.ts +0 -31
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noMissingCorsCheck = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
5
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
-
/**
|
|
12
|
-
* Check if a string matches any ignore pattern
|
|
13
|
-
*/
|
|
14
6
|
function matchesIgnorePattern(text, ignorePatterns) {
|
|
15
7
|
return ignorePatterns.some(pattern => {
|
|
16
8
|
try {
|
|
@@ -91,7 +83,7 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
91
83
|
defaultOptions: [
|
|
92
84
|
{
|
|
93
85
|
allowInTests: false,
|
|
94
|
-
trustedLibraries: [],
|
|
86
|
+
trustedLibraries: [],
|
|
95
87
|
ignorePatterns: [],
|
|
96
88
|
},
|
|
97
89
|
],
|
|
@@ -105,31 +97,22 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
105
97
|
if (isTestFile) {
|
|
106
98
|
return;
|
|
107
99
|
}
|
|
108
|
-
// Check for wildcard CORS origin
|
|
109
100
|
if (node.value === '*' && typeof node.value === 'string') {
|
|
110
101
|
const text = sourceCode.getText(node);
|
|
111
|
-
// Check if it matches any ignore pattern
|
|
112
102
|
if (matchesIgnorePattern(text, ignorePatterns)) {
|
|
113
103
|
return;
|
|
114
104
|
}
|
|
115
|
-
// Check if it's in contexts handled by other checkers
|
|
116
|
-
// 1. setHeader/header calls - checkMemberExpression handles these
|
|
117
|
-
// 2. app.use(cors({ origin: "*" })) - checkCallExpression handles these with suggestions
|
|
118
105
|
let shouldSkip = false;
|
|
119
106
|
let current = node;
|
|
120
107
|
while (current && current.parent) {
|
|
121
108
|
current = current.parent;
|
|
122
109
|
if (current.type === 'CallExpression') {
|
|
123
110
|
const callText = sourceCode.getText(current);
|
|
124
|
-
// Check if it's a setHeader/header call with Access-Control-Allow-Origin
|
|
125
|
-
// Skip these - checkMemberExpression handles them
|
|
126
111
|
if (/\b(setHeader|header)\s*\(/i.test(callText) && /\bAccess-Control-Allow-Origin\b/i.test(callText)) {
|
|
127
112
|
shouldSkip = true;
|
|
128
113
|
break;
|
|
129
114
|
}
|
|
130
|
-
// Check if it's app.use(cors({ origin: "*" })) - checkCallExpression handles these with suggestions
|
|
131
115
|
if (/\buse\s*\(/i.test(callText) && /\bcors\s*\(/i.test(callText)) {
|
|
132
|
-
// Check if the literal is in an object property named "origin"
|
|
133
116
|
if (node.parent && node.parent.type === 'Property') {
|
|
134
117
|
const prop = node.parent;
|
|
135
118
|
if (prop.key.type === 'Identifier' && prop.key.name === 'origin') {
|
|
@@ -140,35 +123,26 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
140
123
|
}
|
|
141
124
|
}
|
|
142
125
|
}
|
|
143
|
-
// Skip if it's in a context handled by another checker
|
|
144
126
|
if (shouldSkip) {
|
|
145
127
|
return;
|
|
146
128
|
}
|
|
147
|
-
// Check if it's in a CORS-related context
|
|
148
|
-
// Only report if it's actually in a CORS configuration (app.use(cors(...)), etc.)
|
|
149
|
-
// Not just any object with origin: "*"
|
|
150
129
|
let isActualCorsContext = false;
|
|
151
|
-
// Check if it's in app.use(cors(...)) or similar
|
|
152
130
|
current = node;
|
|
153
131
|
while (current && current.parent) {
|
|
154
132
|
current = current.parent;
|
|
155
133
|
if (current.type === 'CallExpression') {
|
|
156
134
|
const callText = sourceCode.getText(current);
|
|
157
|
-
// Check if it's a CORS middleware call
|
|
158
135
|
if (/\b(use|cors)\s*\(/i.test(callText) && /\bcors\s*\(/i.test(callText)) {
|
|
159
136
|
isActualCorsContext = true;
|
|
160
137
|
break;
|
|
161
138
|
}
|
|
162
139
|
}
|
|
163
140
|
}
|
|
164
|
-
// Also check if it's in an object property with name "origin" or "allowedOrigins"
|
|
165
|
-
// but only if it's in a CORS-related call expression
|
|
166
141
|
if (node.parent && node.parent.type === 'Property') {
|
|
167
142
|
const prop = node.parent;
|
|
168
143
|
if (prop.key.type === 'Identifier') {
|
|
169
144
|
const keyName = prop.key.name.toLowerCase();
|
|
170
145
|
if (keyName === 'origin' || keyName === 'allowedorigins') {
|
|
171
|
-
// Check if this property is in a CORS call context
|
|
172
146
|
let inCorsCall = false;
|
|
173
147
|
let checkNode = prop;
|
|
174
148
|
while (checkNode && checkNode.parent) {
|
|
@@ -183,7 +157,6 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
183
157
|
}
|
|
184
158
|
}
|
|
185
159
|
if (inCorsCall) {
|
|
186
|
-
// Always report wildcard CORS origin - it's never safe
|
|
187
160
|
context.report({
|
|
188
161
|
node,
|
|
189
162
|
messageId: 'missingCorsCheck',
|
|
@@ -203,9 +176,7 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
203
176
|
}
|
|
204
177
|
}
|
|
205
178
|
}
|
|
206
|
-
// Only report if it's in an actual CORS context
|
|
207
179
|
if (isActualCorsContext) {
|
|
208
|
-
// Always report wildcard CORS origin - it's never safe
|
|
209
180
|
context.report({
|
|
210
181
|
node,
|
|
211
182
|
messageId: 'missingCorsCheck',
|
|
@@ -227,46 +198,35 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
227
198
|
if (isTestFile) {
|
|
228
199
|
return;
|
|
229
200
|
}
|
|
230
|
-
// Check for app.use(cors({ origin: "*" })) or similar
|
|
231
201
|
if (node.callee.type === 'MemberExpression') {
|
|
232
202
|
const property = node.callee.property;
|
|
233
203
|
if (property.type === 'Identifier' && property.name === 'use') {
|
|
234
|
-
// Check if CORS is being used
|
|
235
204
|
const text = sourceCode.getText(node);
|
|
236
|
-
// Check if it matches any ignore pattern
|
|
237
205
|
if (matchesIgnorePattern(text, ignorePatterns)) {
|
|
238
206
|
return;
|
|
239
207
|
}
|
|
240
|
-
// Check if it's a CORS middleware call
|
|
241
|
-
// Check for cors() or trusted library calls
|
|
242
208
|
const firstArg = node.arguments.length > 0 ? node.arguments[0] : null;
|
|
243
209
|
let isCorsCall = /\bcors\s*\(/i.test(text);
|
|
244
210
|
if (!isCorsCall && firstArg && firstArg.type === 'CallExpression' && firstArg.callee.type === 'Identifier') {
|
|
245
211
|
const callee = firstArg.callee;
|
|
246
212
|
const calleeName = callee.name.toLowerCase();
|
|
247
|
-
// Check if it's the standard 'cors' library or a trusted library
|
|
248
213
|
isCorsCall = calleeName === 'cors' || trustedLibraries.some(lib => {
|
|
249
214
|
return calleeName.includes(lib.toLowerCase());
|
|
250
215
|
});
|
|
251
216
|
}
|
|
252
|
-
// Check if it's a trusted library - skip if explicitly trusted
|
|
253
217
|
let isTrustedLibrary = false;
|
|
254
218
|
if (firstArg && firstArg.type === 'CallExpression' && firstArg.callee.type === 'Identifier') {
|
|
255
219
|
const calleeName = firstArg.callee.name.toLowerCase();
|
|
256
220
|
isTrustedLibrary = trustedLibraries.some(lib => calleeName.includes(lib.toLowerCase()));
|
|
257
221
|
}
|
|
258
222
|
if (isTrustedLibrary) {
|
|
259
|
-
return;
|
|
223
|
+
return;
|
|
260
224
|
}
|
|
261
|
-
// Check if it's a CORS call
|
|
262
225
|
if (/\bcors\s*\(/i.test(text) || isCorsCall) {
|
|
263
|
-
// Check arguments for wildcard origin
|
|
264
|
-
// For app.use(cors({ origin: "*" })), we need to check the arguments to cors(), not app.use()
|
|
265
226
|
const corsCallArg = firstArg && firstArg.type === 'CallExpression' ? firstArg : null;
|
|
266
227
|
const argsToCheck = corsCallArg ? corsCallArg.arguments : node.arguments;
|
|
267
228
|
for (const arg of argsToCheck) {
|
|
268
229
|
if (arg.type === 'ObjectExpression') {
|
|
269
|
-
// Check for origin property with wildcard value
|
|
270
230
|
for (const prop of arg.properties) {
|
|
271
231
|
if (prop.type === 'Property' &&
|
|
272
232
|
prop.key.type === 'Identifier' &&
|
|
@@ -291,21 +251,16 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
291
251
|
}
|
|
292
252
|
}
|
|
293
253
|
else if (arg.type === 'Identifier') {
|
|
294
|
-
// Check if this identifier was assigned an object literal with origin: "*"
|
|
295
|
-
// For cases like: const config = { origin: "*" }; app.use(cors(config));
|
|
296
254
|
const varName = arg.name;
|
|
297
|
-
// Traverse the AST to find the variable declaration
|
|
298
255
|
let current = node;
|
|
299
256
|
while (current) {
|
|
300
257
|
if (current.type === 'Program' || current.type === 'FunctionDeclaration' || current.type === 'FunctionExpression' || current.type === 'ArrowFunctionExpression') {
|
|
301
|
-
// Search for variable declarations in this scope
|
|
302
258
|
const scopeBody = current.type === 'Program' ? current.body :
|
|
303
259
|
(current.body.type === 'BlockStatement' ? current.body.body : []);
|
|
304
260
|
for (const stmt of scopeBody) {
|
|
305
261
|
if (stmt.type === 'VariableDeclaration') {
|
|
306
262
|
for (const declarator of stmt.declarations) {
|
|
307
263
|
if (declarator.id.type === 'Identifier' && declarator.id.name === varName && declarator.init) {
|
|
308
|
-
// Check if init is an object literal with origin: "*"
|
|
309
264
|
if (declarator.init.type === 'ObjectExpression') {
|
|
310
265
|
for (const prop of declarator.init.properties) {
|
|
311
266
|
if (prop.type === 'Property' &&
|
|
@@ -327,7 +282,7 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
327
282
|
},
|
|
328
283
|
],
|
|
329
284
|
});
|
|
330
|
-
return;
|
|
285
|
+
return;
|
|
331
286
|
}
|
|
332
287
|
}
|
|
333
288
|
}
|
|
@@ -335,7 +290,7 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
335
290
|
}
|
|
336
291
|
}
|
|
337
292
|
}
|
|
338
|
-
break;
|
|
293
|
+
break;
|
|
339
294
|
}
|
|
340
295
|
if (current.parent) {
|
|
341
296
|
current = current.parent;
|
|
@@ -354,22 +309,17 @@ exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
|
|
|
354
309
|
if (isTestFile) {
|
|
355
310
|
return;
|
|
356
311
|
}
|
|
357
|
-
// Check for Access-Control-Allow-Origin header without validation
|
|
358
312
|
if (node.property.type === 'Identifier') {
|
|
359
313
|
const propertyName = node.property.name;
|
|
360
314
|
if (propertyName === 'setHeader' || propertyName === 'header') {
|
|
361
|
-
// Check if it matches any ignore pattern
|
|
362
315
|
const text = sourceCode.getText(node);
|
|
363
316
|
if (matchesIgnorePattern(text, ignorePatterns)) {
|
|
364
317
|
return;
|
|
365
318
|
}
|
|
366
|
-
// Check if it's setting CORS headers
|
|
367
|
-
// Need to check the full call expression, not just the member expression
|
|
368
319
|
const parent = node.parent;
|
|
369
320
|
if (parent && parent.type === 'CallExpression') {
|
|
370
321
|
const callText = sourceCode.getText(parent);
|
|
371
322
|
if (/\bAccess-Control-Allow-Origin\b/i.test(callText)) {
|
|
372
|
-
// Check if the value is a wildcard
|
|
373
323
|
const args = parent.arguments;
|
|
374
324
|
if (args.length >= 2 && args[1].type === 'Literal' && args[1].value === '*') {
|
|
375
325
|
context.report({
|
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noMissingCsrfProtection = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
5
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
-
/**
|
|
12
|
-
* Default CSRF middleware patterns
|
|
13
|
-
*/
|
|
14
6
|
const DEFAULT_CSRF_MIDDLEWARE_PATTERNS = [
|
|
15
7
|
'csrf',
|
|
16
8
|
'csurf',
|
|
@@ -21,13 +13,7 @@ const DEFAULT_CSRF_MIDDLEWARE_PATTERNS = [
|
|
|
21
13
|
'checkCsrf',
|
|
22
14
|
'csrfMiddleware',
|
|
23
15
|
];
|
|
24
|
-
/**
|
|
25
|
-
* Default HTTP methods that require CSRF protection
|
|
26
|
-
*/
|
|
27
16
|
const DEFAULT_PROTECTED_METHODS = ['post', 'put', 'delete', 'patch'];
|
|
28
|
-
/**
|
|
29
|
-
* Check if a string matches any ignore pattern
|
|
30
|
-
*/
|
|
31
17
|
function matchesIgnorePattern(text, patterns) {
|
|
32
18
|
return patterns.some(pattern => {
|
|
33
19
|
try {
|
|
@@ -119,7 +105,6 @@ exports.noMissingCsrfProtection = (0, eslint_devkit_2.createRule)({
|
|
|
119
105
|
const protectedMethods = customProtectedMethods && customProtectedMethods.length > 0
|
|
120
106
|
? customProtectedMethods
|
|
121
107
|
: DEFAULT_PROTECTED_METHODS;
|
|
122
|
-
// Pre-compute Set for O(1) lookups (performance optimization)
|
|
123
108
|
const protectedMethodsSet = new Set(protectedMethods.map(m => m.toLowerCase()));
|
|
124
109
|
const filename = context.filename;
|
|
125
110
|
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
@@ -130,23 +115,16 @@ exports.noMissingCsrfProtection = (0, eslint_devkit_2.createRule)({
|
|
|
130
115
|
}
|
|
131
116
|
const callee = node.callee;
|
|
132
117
|
const callText = sourceCode.getText(node);
|
|
133
|
-
// Check if it matches any ignore pattern
|
|
134
118
|
if (matchesIgnorePattern(callText, ignorePatterns)) {
|
|
135
119
|
return;
|
|
136
120
|
}
|
|
137
|
-
// Check for route handler methods (app.post, router.put, etc.)
|
|
138
121
|
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression && callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
139
122
|
const methodName = callee.property.name;
|
|
140
|
-
// Only check if it's a route handler that requires CSRF (O(1) Set lookup)
|
|
141
123
|
if (protectedMethodsSet.has(methodName.toLowerCase())) {
|
|
142
|
-
// Must have at least 2 arguments (path and handler)
|
|
143
124
|
if (node.arguments.length < 2) {
|
|
144
125
|
return;
|
|
145
126
|
}
|
|
146
|
-
// Check if CSRF middleware is in the route chain arguments
|
|
147
127
|
let hasCsrfInChain = false;
|
|
148
|
-
// Check if any argument (after the first path argument) is a CSRF middleware
|
|
149
|
-
// Skip the first argument (path) and check the rest
|
|
150
128
|
for (let i = 1; i < node.arguments.length; i++) {
|
|
151
129
|
const arg = node.arguments[i];
|
|
152
130
|
const argText = sourceCode.getText(arg);
|
|
@@ -167,9 +145,6 @@ exports.noMissingCsrfProtection = (0, eslint_devkit_2.createRule)({
|
|
|
167
145
|
{
|
|
168
146
|
messageId: 'addCsrfValidation',
|
|
169
147
|
fix(fixer) {
|
|
170
|
-
// Add CSRF middleware after the first argument (path).
|
|
171
|
-
// arguments[0] always exists: the arguments.length < 2
|
|
172
|
-
// guard above returns before reporting.
|
|
173
148
|
return fixer.insertTextAfter(node.arguments[0], ', csrf()');
|
|
174
149
|
},
|
|
175
150
|
},
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noMissingSecurityHeaders = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -13,21 +8,32 @@ const DEFAULT_REQUIRED_HEADERS = [
|
|
|
13
8
|
'X-Frame-Options',
|
|
14
9
|
'X-Content-Type-Options',
|
|
15
10
|
];
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
const HEADER_METHODS = new Set(['setHeader', 'header', 'set']);
|
|
12
|
+
const RESPONSE_RECEIVER = /^(res|resp|response|reply)$/i;
|
|
13
|
+
function isResponseHeaderCall(node) {
|
|
14
|
+
const { callee } = node;
|
|
15
|
+
if (callee.type !== 'MemberExpression' ||
|
|
16
|
+
callee.property.type !== 'Identifier' ||
|
|
17
|
+
!HEADER_METHODS.has(callee.property.name)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const { object } = callee;
|
|
21
|
+
if (object.type === 'Identifier') {
|
|
22
|
+
return RESPONSE_RECEIVER.test(object.name);
|
|
23
|
+
}
|
|
24
|
+
if (object.type === 'MemberExpression' && object.property.type === 'Identifier') {
|
|
25
|
+
return RESPONSE_RECEIVER.test(object.property.name);
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
19
29
|
function extractHeaderName(node) {
|
|
20
30
|
if (node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
|
|
21
31
|
return String(node.arguments[0].value);
|
|
22
32
|
}
|
|
23
33
|
return null;
|
|
24
34
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Check if all security headers are set in the current scope
|
|
27
|
-
*/
|
|
28
35
|
function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
|
|
29
36
|
const setHeaders = new Set();
|
|
30
|
-
// Find the function that contains this setHeader call
|
|
31
37
|
let current = node;
|
|
32
38
|
let scopeNode = null;
|
|
33
39
|
while (current) {
|
|
@@ -39,22 +45,16 @@ function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
|
|
|
39
45
|
}
|
|
40
46
|
current = current.parent ?? null;
|
|
41
47
|
}
|
|
42
|
-
// If no function found, use the program scope (for test cases)
|
|
43
48
|
if (!scopeNode) {
|
|
44
49
|
scopeNode = context.sourceCode.ast;
|
|
45
50
|
}
|
|
46
|
-
// Collect all setHeader calls in this scope
|
|
47
51
|
function collectHeaders(astNode) {
|
|
48
|
-
if (astNode.type === 'CallExpression' &&
|
|
49
|
-
astNode.callee.type === 'MemberExpression' &&
|
|
50
|
-
astNode.callee.property.type === 'Identifier' &&
|
|
51
|
-
['setHeader', 'header', 'set'].includes(astNode.callee.property.name)) {
|
|
52
|
+
if (astNode.type === 'CallExpression' && isResponseHeaderCall(astNode)) {
|
|
52
53
|
const headerName = extractHeaderName(astNode);
|
|
53
54
|
if (headerName) {
|
|
54
55
|
setHeaders.add(headerName);
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
|
-
// Recursively check children - only traverse standard AST properties
|
|
58
58
|
if (astNode.type === 'Program' && astNode.body) {
|
|
59
59
|
astNode.body.forEach(collectHeaders);
|
|
60
60
|
}
|
|
@@ -70,9 +70,7 @@ function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
|
|
|
70
70
|
collectHeaders(astNode.expression);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
// scopeNode is always set at this point (function scope or program fallback)
|
|
74
73
|
collectHeaders(scopeNode);
|
|
75
|
-
// Return missing headers
|
|
76
74
|
return requiredHeaders.filter(header => !setHeaders.has(header));
|
|
77
75
|
}
|
|
78
76
|
exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
|
|
@@ -155,11 +153,7 @@ exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
|
|
|
155
153
|
return {};
|
|
156
154
|
}
|
|
157
155
|
const reportedScopes = new Set();
|
|
158
|
-
/**
|
|
159
|
-
* Get a unique key for the current scope
|
|
160
|
-
*/
|
|
161
156
|
function getScopeKey(node) {
|
|
162
|
-
// Find the function that contains this call
|
|
163
157
|
let current = node;
|
|
164
158
|
while (current) {
|
|
165
159
|
if (current.type === 'FunctionDeclaration' ||
|
|
@@ -169,53 +163,40 @@ exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
|
|
|
169
163
|
}
|
|
170
164
|
current = current.parent ?? null;
|
|
171
165
|
}
|
|
172
|
-
// If no function found, use program scope
|
|
173
166
|
return 'program';
|
|
174
167
|
}
|
|
175
|
-
/**
|
|
176
|
-
* Check for response header setting
|
|
177
|
-
*/
|
|
178
168
|
function checkCallExpression(node) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
},
|
|
211
|
-
],
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
// Mark as checked even if no error
|
|
216
|
-
reportedScopes.add(scopeKey);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
169
|
+
if (!isResponseHeaderCall(node)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const scopeKey = getScopeKey(node);
|
|
173
|
+
if (reportedScopes.has(scopeKey)) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const missing = checkFunctionForSecurityHeaders(node, requiredHeaders, context);
|
|
177
|
+
reportedScopes.add(scopeKey);
|
|
178
|
+
if (missing.length > 0) {
|
|
179
|
+
context.report({
|
|
180
|
+
node,
|
|
181
|
+
messageId: 'missingSecurityHeader',
|
|
182
|
+
data: {
|
|
183
|
+
headers: missing.join(', '),
|
|
184
|
+
},
|
|
185
|
+
suggest: [
|
|
186
|
+
{
|
|
187
|
+
messageId: 'addSecurityHeaders',
|
|
188
|
+
fix: () => null,
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
messageId: 'useMiddleware',
|
|
192
|
+
fix: () => null,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
messageId: 'setHeader',
|
|
196
|
+
fix: () => null,
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
});
|
|
219
200
|
}
|
|
220
201
|
}
|
|
221
202
|
return {
|
|
@@ -1,35 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noPermissiveCors = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
/**
|
|
11
|
-
* Check if node is a CORS configuration with wildcard or overly permissive origin
|
|
12
|
-
*/
|
|
13
5
|
function isPermissiveCorsConfig(node, sourceCode, options) {
|
|
14
6
|
const text = sourceCode.getText(node);
|
|
15
|
-
// Check for origin: '*'
|
|
16
7
|
if (/\borigin\s*:\s*['"`]\*['"`]/.test(text)) {
|
|
17
8
|
return {
|
|
18
9
|
isPermissive: true,
|
|
19
10
|
reason: "origin: '*' allows any domain to access your API",
|
|
20
11
|
};
|
|
21
12
|
}
|
|
22
|
-
// Check for origin: true (reflects request origin)
|
|
23
13
|
if (!options.allowOriginTrue && /\borigin\s*:\s*true\b/.test(text)) {
|
|
24
14
|
return {
|
|
25
15
|
isPermissive: true,
|
|
26
16
|
reason: 'origin: true reflects the request origin, allowing any domain',
|
|
27
17
|
};
|
|
28
18
|
}
|
|
29
|
-
// Check for credentials: true with permissive origin
|
|
30
19
|
if (/\bcredentials\s*:\s*true\b/.test(text)) {
|
|
31
|
-
// If credentials are enabled, origin cannot be '*' (browser blocks this)
|
|
32
|
-
// but origin: true with credentials is dangerous
|
|
33
20
|
if (/\borigin\s*:\s*true\b/.test(text)) {
|
|
34
21
|
return {
|
|
35
22
|
isPermissive: true,
|
|
@@ -39,14 +26,9 @@ function isPermissiveCorsConfig(node, sourceCode, options) {
|
|
|
39
26
|
}
|
|
40
27
|
return { isPermissive: false, reason: '' };
|
|
41
28
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Check if this is a standalone cors() call (not inside app.use)
|
|
44
|
-
*/
|
|
45
29
|
function isStandaloneCorsCall(node) {
|
|
46
30
|
const callee = node.callee;
|
|
47
|
-
// cors() or cors({...})
|
|
48
31
|
if (callee.type === 'Identifier' && callee.name === 'cors') {
|
|
49
|
-
// Check if parent is app.use() - if so, skip (handled by isAppUseCors)
|
|
50
32
|
const parent = node.parent;
|
|
51
33
|
if (parent &&
|
|
52
34
|
parent.type === 'CallExpression' &&
|
|
@@ -59,15 +41,11 @@ function isStandaloneCorsCall(node) {
|
|
|
59
41
|
}
|
|
60
42
|
return false;
|
|
61
43
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Check if this is app.use(cors(...))
|
|
64
|
-
*/
|
|
65
44
|
function isAppUseCors(node) {
|
|
66
45
|
const callee = node.callee;
|
|
67
46
|
if (callee.type === 'MemberExpression' &&
|
|
68
47
|
callee.property.type === 'Identifier' &&
|
|
69
48
|
callee.property.name === 'use') {
|
|
70
|
-
// Check if first argument is cors() call
|
|
71
49
|
const firstArg = node.arguments[0];
|
|
72
50
|
if (firstArg &&
|
|
73
51
|
firstArg.type === 'CallExpression' &&
|
|
@@ -150,10 +128,8 @@ exports.noPermissiveCors = (0, eslint_devkit_1.createRule)({
|
|
|
150
128
|
const sourceCode = context.sourceCode;
|
|
151
129
|
return {
|
|
152
130
|
CallExpression(node) {
|
|
153
|
-
// Check for cors({...}) call
|
|
154
131
|
if (isStandaloneCorsCall(node) || isAppUseCors(node)) {
|
|
155
132
|
let corsConfigNode = null;
|
|
156
|
-
// Get the config object
|
|
157
133
|
if (isStandaloneCorsCall(node) && node.arguments[0]?.type === 'ObjectExpression') {
|
|
158
134
|
corsConfigNode = node.arguments[0];
|
|
159
135
|
}
|
|
@@ -163,14 +139,11 @@ exports.noPermissiveCors = (0, eslint_devkit_1.createRule)({
|
|
|
163
139
|
corsConfigNode = corsCall.arguments[0];
|
|
164
140
|
}
|
|
165
141
|
}
|
|
166
|
-
// Check for cors() with no arguments - defaults to permissive
|
|
167
|
-
// Handle both standalone cors() and app.use(cors())
|
|
168
142
|
let corsCallNode = null;
|
|
169
143
|
if (isStandaloneCorsCall(node)) {
|
|
170
144
|
corsCallNode = node;
|
|
171
145
|
}
|
|
172
146
|
else {
|
|
173
|
-
// Guaranteed app.use(cors(...)) by the isAppUseCors guard above
|
|
174
147
|
corsCallNode = node.arguments[0];
|
|
175
148
|
}
|
|
176
149
|
if (corsCallNode && corsCallNode.arguments.length === 0) {
|