eslint-plugin-express-security 1.4.0 → 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.
Files changed (59) hide show
  1. package/README.md +7 -0
  2. package/package.json +5 -8
  3. package/src/index.d.ts +17 -40
  4. package/src/index.js +26 -66
  5. package/src/oxlint.d.ts +17 -20
  6. package/src/oxlint.js +0 -18
  7. package/src/rules/no-client-controlled-authorization/index.js +194 -0
  8. package/src/rules/no-cors-credentials-wildcard/index.js +0 -22
  9. package/src/rules/no-disabled-helmet-protections/index.js +125 -0
  10. package/src/rules/no-error-details-in-response/index.js +0 -26
  11. package/src/rules/no-exposed-debug-endpoints/index.js +0 -19
  12. package/src/rules/no-express-unsafe-regex-route/index.js +1 -32
  13. package/src/rules/no-graphql-introspection-production/index.js +0 -21
  14. package/src/rules/no-host-header-in-links/index.js +0 -31
  15. package/src/rules/no-idor-resource-access/index.js +126 -0
  16. package/src/rules/no-insecure-cookie-options/index.js +0 -12
  17. package/src/rules/no-missing-cors-check/index.js +4 -54
  18. package/src/rules/no-missing-csrf-protection/index.js +0 -25
  19. package/src/rules/no-missing-security-headers/index.js +0 -40
  20. package/src/rules/no-permissive-cors/index.js +0 -27
  21. package/src/rules/no-permissive-trust-proxy/index.js +106 -0
  22. package/src/rules/no-sensitive-data-in-query/index.js +1 -26
  23. package/src/rules/no-static-root-exposure/index.js +0 -21
  24. package/src/rules/no-unsafe-csp-directives/index.js +264 -0
  25. package/src/rules/no-user-controlled-redirect/index.js +0 -26
  26. package/src/rules/no-user-controlled-render-locals/index.js +0 -45
  27. package/src/rules/require-case-insensitive-path-guard/index.js +0 -21
  28. package/src/rules/require-csrf-protection/index.js +0 -42
  29. package/src/rules/require-express-body-parser-limits/index.js +0 -24
  30. package/src/rules/require-helmet/index.js +1 -25
  31. package/src/rules/require-query-type-guard/index.js +0 -20
  32. package/src/rules/require-rate-limiting/index.js +0 -13
  33. package/src/rules/require-route-authentication/index.js +225 -0
  34. package/src/rules/require-strict-transport-security/index.js +171 -0
  35. package/src/types/index.js +0 -5
  36. package/src/utils/index.js +81 -0
  37. package/AGENTS.md +0 -100
  38. package/CHANGELOG.md +0 -192
  39. package/src/rules/no-cors-credentials-wildcard/index.d.ts +0 -26
  40. package/src/rules/no-error-details-in-response/index.d.ts +0 -57
  41. package/src/rules/no-exposed-debug-endpoints/index.d.ts +0 -14
  42. package/src/rules/no-express-unsafe-regex-route/index.d.ts +0 -27
  43. package/src/rules/no-graphql-introspection-production/index.d.ts +0 -26
  44. package/src/rules/no-host-header-in-links/index.d.ts +0 -56
  45. package/src/rules/no-insecure-cookie-options/index.d.ts +0 -39
  46. package/src/rules/no-missing-cors-check/index.d.ts +0 -28
  47. package/src/rules/no-missing-csrf-protection/index.d.ts +0 -30
  48. package/src/rules/no-missing-security-headers/index.d.ts +0 -26
  49. package/src/rules/no-permissive-cors/index.d.ts +0 -28
  50. package/src/rules/no-sensitive-data-in-query/index.d.ts +0 -51
  51. package/src/rules/no-static-root-exposure/index.d.ts +0 -45
  52. package/src/rules/no-user-controlled-redirect/index.d.ts +0 -44
  53. package/src/rules/no-user-controlled-render-locals/index.d.ts +0 -49
  54. package/src/rules/require-case-insensitive-path-guard/index.d.ts +0 -59
  55. package/src/rules/require-csrf-protection/index.d.ts +0 -28
  56. package/src/rules/require-express-body-parser-limits/index.d.ts +0 -31
  57. package/src/rules/require-helmet/index.d.ts +0 -29
  58. package/src/rules/require-query-type-guard/index.d.ts +0 -54
  59. package/src/rules/require-rate-limiting/index.d.ts +0 -31
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noIdorResourceAccess = void 0;
4
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
+ const utils_1 = require("../../utils");
6
+ const DEFAULT_LOOKUP_METHODS = [
7
+ 'findById',
8
+ 'findByPk',
9
+ 'findOne',
10
+ 'findUnique',
11
+ 'findFirst',
12
+ 'findByIdAndUpdate',
13
+ 'findByIdAndDelete',
14
+ 'findByIdAndRemove',
15
+ 'getById',
16
+ 'deleteOne',
17
+ 'deleteById',
18
+ 'updateOne',
19
+ 'replaceOne',
20
+ ];
21
+ const REQUEST_PARAM = /^(req|request)$/i;
22
+ function isHandlerFunction(node) {
23
+ if (node.type !== eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration &&
24
+ node.type !== eslint_devkit_1.AST_NODE_TYPES.FunctionExpression &&
25
+ node.type !== eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression) {
26
+ return false;
27
+ }
28
+ return node.params.some((param) => param.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && REQUEST_PARAM.test(param.name));
29
+ }
30
+ function isRequestKeyArgument(arg) {
31
+ if ((0, utils_1.isClientRequestMember)(arg))
32
+ return true;
33
+ if (arg.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
34
+ let found = false;
35
+ (0, utils_1.walk)(arg, (child) => {
36
+ if (found)
37
+ return;
38
+ if ((0, utils_1.isClientRequestMember)(child))
39
+ found = true;
40
+ });
41
+ return found;
42
+ }
43
+ return false;
44
+ }
45
+ exports.noIdorResourceAccess = (0, eslint_devkit_1.createRule)({
46
+ name: 'no-idor-resource-access',
47
+ meta: {
48
+ type: 'problem',
49
+ docs: {
50
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-idor-resource-access.md',
51
+ description: 'Disallow fetching a resource by a request-supplied key in a handler with no principal in scope',
52
+ cwe: 'CWE-639',
53
+ cvss: 7.5,
54
+ confidence: 'medium',
55
+ },
56
+ messages: {
57
+ unscopedResourceLookup: (0, eslint_devkit_1.formatLLMMessage)({
58
+ icon: eslint_devkit_1.MessageIcons.SECURITY,
59
+ issueName: 'Resource Fetched By Client-Supplied Key (CWE-639)',
60
+ cwe: 'CWE-639',
61
+ cvss: 7.5,
62
+ description: '{{method}}() is keyed on {{key}} and nothing in this handler ties the lookup to the caller. Incrementing the id in the URL returns another tenant’s record.',
63
+ severity: 'HIGH',
64
+ fix: "Scope the query to the principal — {{method}}({ _id: {{key}}, owner: req.user.id }) — or verify ownership on the loaded document before responding.",
65
+ documentationLink: 'https://cwe.mitre.org/data/definitions/639.html',
66
+ }),
67
+ },
68
+ schema: [
69
+ {
70
+ type: 'object',
71
+ properties: {
72
+ lookupMethods: {
73
+ type: 'array',
74
+ items: { type: 'string' },
75
+ description: 'Lookup methods treated as a fetch-by-key',
76
+ },
77
+ },
78
+ additionalProperties: false,
79
+ },
80
+ ],
81
+ },
82
+ defaultOptions: [{}],
83
+ create(context, [options]) {
84
+ const { lookupMethods } = options;
85
+ const lookups = new Set(lookupMethods ?? DEFAULT_LOOKUP_METHODS);
86
+ function enclosingHandler(node) {
87
+ let current = node.parent;
88
+ while (current) {
89
+ if (isHandlerFunction(current))
90
+ return current;
91
+ current = current.parent;
92
+ }
93
+ return null;
94
+ }
95
+ return {
96
+ CallExpression(node) {
97
+ const callee = node.callee;
98
+ if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
99
+ return;
100
+ if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
101
+ return;
102
+ if (callee.computed)
103
+ return;
104
+ const method = callee.property.name;
105
+ if (!lookups.has(method))
106
+ return;
107
+ const keyArg = node.arguments.find((arg) => isRequestKeyArgument(arg));
108
+ if (!keyArg)
109
+ return;
110
+ const handler = enclosingHandler(node);
111
+ if (!handler)
112
+ return;
113
+ if ((0, utils_1.readsPrincipal)(handler))
114
+ return;
115
+ context.report({
116
+ node,
117
+ messageId: 'unscopedResourceLookup',
118
+ data: {
119
+ method,
120
+ key: context.sourceCode.getText(keyArg),
121
+ },
122
+ });
123
+ },
124
+ };
125
+ },
126
+ });
@@ -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.noInsecureCookieOptions = void 0;
9
4
  exports.checkCookieOptions = checkCookieOptions;
10
5
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
11
- /**
12
- * Check cookie options object for security flags
13
- */
14
6
  function checkCookieOptions(node, sourceCode, options) {
15
7
  const text = sourceCode.getText(node);
16
8
  const issues = [];
@@ -118,13 +110,10 @@ exports.noInsecureCookieOptions = (0, eslint_devkit_1.createRule)({
118
110
  return {
119
111
  CallExpression(node) {
120
112
  const callee = node.callee;
121
- // Check for res.cookie() calls
122
113
  if (callee.type === 'MemberExpression' &&
123
114
  callee.property.type === 'Identifier' &&
124
115
  callee.property.name === 'cookie') {
125
- // res.cookie(name, value, options)
126
116
  const optionsArg = node.arguments[2];
127
- // No options provided
128
117
  if (!optionsArg) {
129
118
  context.report({
130
119
  node,
@@ -144,7 +133,6 @@ exports.noInsecureCookieOptions = (0, eslint_devkit_1.createRule)({
144
133
  });
145
134
  return;
146
135
  }
147
- // Options is an object expression
148
136
  if (optionsArg.type === 'ObjectExpression') {
149
137
  const { issues } = checkCookieOptions(optionsArg, sourceCode, options);
150
138
  if (issues.length > 0) {
@@ -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: [], // Empty by default - users can add custom CORS libraries they trust
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; // Trusted library, skip
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; // Found and reported, exit
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; // Only check the immediate scope
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,18 +8,8 @@ const DEFAULT_REQUIRED_HEADERS = [
13
8
  'X-Frame-Options',
14
9
  'X-Content-Type-Options',
15
10
  ];
16
- /** Methods that write a response header. */
17
11
  const HEADER_METHODS = new Set(['setHeader', 'header', 'set']);
18
- /**
19
- * Receivers that actually are an HTTP response. `set` and `header` are far too
20
- * common to match on the method name alone — `url.searchParams.set(…)` and
21
- * `app.set('view engine', …)` are not header calls.
22
- */
23
12
  const RESPONSE_RECEIVER = /^(res|resp|response|reply)$/i;
24
- /**
25
- * Is this call a response header setter (res.setHeader / res.header / res.set,
26
- * including `ctx.res.set(…)` and `this.response.header(…)`)?
27
- */
28
13
  function isResponseHeaderCall(node) {
29
14
  const { callee } = node;
30
15
  if (callee.type !== 'MemberExpression' ||
@@ -39,25 +24,16 @@ function isResponseHeaderCall(node) {
39
24
  if (object.type === 'MemberExpression' && object.property.type === 'Identifier') {
40
25
  return RESPONSE_RECEIVER.test(object.property.name);
41
26
  }
42
- // ponytail: anything else (call results, computed access) is not tracked —
43
- // a rule that guesses at aliases trades this false positive for a worse one.
44
27
  return false;
45
28
  }
46
- /**
47
- * Extract header name from setHeader call
48
- */
49
29
  function extractHeaderName(node) {
50
30
  if (node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
51
31
  return String(node.arguments[0].value);
52
32
  }
53
33
  return null;
54
34
  }
55
- /**
56
- * Check if all security headers are set in the current scope
57
- */
58
35
  function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
59
36
  const setHeaders = new Set();
60
- // Find the function that contains this setHeader call
61
37
  let current = node;
62
38
  let scopeNode = null;
63
39
  while (current) {
@@ -69,11 +45,9 @@ function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
69
45
  }
70
46
  current = current.parent ?? null;
71
47
  }
72
- // If no function found, use the program scope (for test cases)
73
48
  if (!scopeNode) {
74
49
  scopeNode = context.sourceCode.ast;
75
50
  }
76
- // Collect all setHeader calls in this scope
77
51
  function collectHeaders(astNode) {
78
52
  if (astNode.type === 'CallExpression' && isResponseHeaderCall(astNode)) {
79
53
  const headerName = extractHeaderName(astNode);
@@ -81,7 +55,6 @@ function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
81
55
  setHeaders.add(headerName);
82
56
  }
83
57
  }
84
- // Recursively check children - only traverse standard AST properties
85
58
  if (astNode.type === 'Program' && astNode.body) {
86
59
  astNode.body.forEach(collectHeaders);
87
60
  }
@@ -97,9 +70,7 @@ function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
97
70
  collectHeaders(astNode.expression);
98
71
  }
99
72
  }
100
- // scopeNode is always set at this point (function scope or program fallback)
101
73
  collectHeaders(scopeNode);
102
- // Return missing headers
103
74
  return requiredHeaders.filter(header => !setHeaders.has(header));
104
75
  }
105
76
  exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
@@ -182,11 +153,7 @@ exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
182
153
  return {};
183
154
  }
184
155
  const reportedScopes = new Set();
185
- /**
186
- * Get a unique key for the current scope
187
- */
188
156
  function getScopeKey(node) {
189
- // Find the function that contains this call
190
157
  let current = node;
191
158
  while (current) {
192
159
  if (current.type === 'FunctionDeclaration' ||
@@ -196,24 +163,17 @@ exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
196
163
  }
197
164
  current = current.parent ?? null;
198
165
  }
199
- // If no function found, use program scope
200
166
  return 'program';
201
167
  }
202
- /**
203
- * Check for response header setting
204
- */
205
168
  function checkCallExpression(node) {
206
- // Check for res.setHeader, res.header, res.set
207
169
  if (!isResponseHeaderCall(node)) {
208
170
  return;
209
171
  }
210
172
  const scopeKey = getScopeKey(node);
211
- // Only check once per scope
212
173
  if (reportedScopes.has(scopeKey)) {
213
174
  return;
214
175
  }
215
176
  const missing = checkFunctionForSecurityHeaders(node, requiredHeaders, context);
216
- // Mark as checked either way
217
177
  reportedScopes.add(scopeKey);
218
178
  if (missing.length > 0) {
219
179
  context.report({
@@ -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) {