eslint-plugin-express-security 1.4.0 → 1.5.1

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 -18
  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
@@ -1,50 +1,30 @@
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.noCorsCredentialsWildcard = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- /**
11
- * Checks if a configuration object has origin set to '*' or true
12
- */
13
5
  function hasWildcardOrigin(node, sourceCode) {
14
6
  const text = sourceCode.getText(node);
15
- // Check for origin: '*'
16
7
  if (/\borigin\s*:\s*['"`]\*['"`]/.test(text)) {
17
8
  return true;
18
9
  }
19
- // Check for origin: true (reflects request origin - effectively wildcard)
20
10
  if (/\borigin\s*:\s*true\b/.test(text)) {
21
11
  return true;
22
12
  }
23
13
  return false;
24
14
  }
25
- /**
26
- * Checks if a configuration object has credentials: true
27
- */
28
15
  function hasCredentialsTrue(node, sourceCode) {
29
16
  const text = sourceCode.getText(node);
30
17
  return /\bcredentials\s*:\s*true\b/.test(text);
31
18
  }
32
- /**
33
- * Check if this is a cors() call (standalone or in app.use)
34
- * Returns the config node only for the appropriate level to avoid double-triggering
35
- */
36
19
  function getCorsConfigNode(node) {
37
20
  const callee = node.callee;
38
- // Standalone cors({...}) call - but skip if parent is app.use()
39
21
  if (callee.type === 'Identifier' && callee.name === 'cors') {
40
- // Check if this cors() is inside app.use() - if so, let app.use() handle it
41
22
  const parent = node.parent;
42
23
  if (parent &&
43
24
  parent.type === 'CallExpression' &&
44
25
  parent.callee.type === 'MemberExpression' &&
45
26
  parent.callee.property.type === 'Identifier' &&
46
27
  parent.callee.property.name === 'use') {
47
- // Skip - let the app.use() case handle this
48
28
  return null;
49
29
  }
50
30
  if (node.arguments[0]?.type === 'ObjectExpression') {
@@ -52,7 +32,6 @@ function getCorsConfigNode(node) {
52
32
  }
53
33
  return null;
54
34
  }
55
- // app.use(cors({...}))
56
35
  if (callee.type === 'MemberExpression' &&
57
36
  callee.property.type === 'Identifier' &&
58
37
  callee.property.name === 'use') {
@@ -129,7 +108,6 @@ exports.noCorsCredentialsWildcard = (0, eslint_devkit_1.createRule)({
129
108
  if (corsConfigNode) {
130
109
  const hasWildcard = hasWildcardOrigin(corsConfigNode, sourceCode);
131
110
  const hasCredentials = hasCredentialsTrue(corsConfigNode, sourceCode);
132
- // Only report if BOTH conditions are met
133
111
  if (hasWildcard && hasCredentials) {
134
112
  context.report({
135
113
  node: corsConfigNode,
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noDisabledHelmetProtections = void 0;
4
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
+ const PROTECTIONS = {
6
+ contentSecurityPolicy: 'Content-Security-Policy',
7
+ frameguard: 'X-Frame-Options',
8
+ xFrameOptions: 'X-Frame-Options',
9
+ noSniff: 'X-Content-Type-Options',
10
+ xContentTypeOptions: 'X-Content-Type-Options',
11
+ referrerPolicy: 'Referrer-Policy',
12
+ hidePoweredBy: 'X-Powered-By (removal)',
13
+ xPoweredBy: 'X-Powered-By (removal)',
14
+ crossOriginResourcePolicy: 'Cross-Origin-Resource-Policy',
15
+ crossOriginOpenerPolicy: 'Cross-Origin-Opener-Policy',
16
+ };
17
+ function isHelmetCall(node) {
18
+ return (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
19
+ node.callee.name === 'helmet');
20
+ }
21
+ function propertyKeyName(prop) {
22
+ if (prop.computed)
23
+ return null;
24
+ if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier)
25
+ return prop.key.name;
26
+ if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
27
+ typeof prop.key.value === 'string') {
28
+ return prop.key.value;
29
+ }
30
+ return null;
31
+ }
32
+ exports.noDisabledHelmetProtections = (0, eslint_devkit_1.createRule)({
33
+ name: 'no-disabled-helmet-protections',
34
+ meta: {
35
+ type: 'problem',
36
+ hasSuggestions: true,
37
+ docs: {
38
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-disabled-helmet-protections.md',
39
+ description: 'Disallow disabling helmet security-header defaults: contentSecurityPolicy, frameguard/xFrameOptions, noSniff/xContentTypeOptions, referrerPolicy, hidePoweredBy/xPoweredBy, crossOriginResourcePolicy, crossOriginOpenerPolicy',
40
+ cwe: 'CWE-693',
41
+ cvss: 6.5,
42
+ confidence: 'high',
43
+ },
44
+ messages: {
45
+ disabledProtection: (0, eslint_devkit_1.formatLLMMessage)({
46
+ icon: eslint_devkit_1.MessageIcons.SECURITY,
47
+ issueName: 'Helmet Protection Disabled (CWE-693)',
48
+ cwe: 'CWE-693',
49
+ cvss: 6.5,
50
+ description: 'helmet({ {{option}}: false }) stops the {{header}} header from being sent. The middleware is mounted but this protection is off, so the app ships the same exposure as an app with no helmet at all.',
51
+ severity: 'HIGH',
52
+ fix: 'Remove the `{{option}}: false` entry and keep the helmet default. If the header must be customised, pass its options object instead of `false`.',
53
+ documentationLink: 'https://helmetjs.github.io/',
54
+ }),
55
+ restoreDefault: 'Remove `{{option}}: false` and keep the helmet default',
56
+ },
57
+ schema: [
58
+ {
59
+ type: 'object',
60
+ properties: {
61
+ allowDisabled: {
62
+ type: 'array',
63
+ items: { type: 'string' },
64
+ description: 'Helmet option names that may be disabled without a report',
65
+ },
66
+ },
67
+ additionalProperties: false,
68
+ },
69
+ ],
70
+ },
71
+ defaultOptions: [{}],
72
+ create(context, [options]) {
73
+ const { allowDisabled } = options;
74
+ const allowed = new Set(allowDisabled ?? []);
75
+ function removeProperty(fixer, prop) {
76
+ const sourceCode = context.sourceCode;
77
+ const after = sourceCode.getTokenAfter(prop);
78
+ if (after && after.value === ',') {
79
+ return fixer.removeRange([prop.range[0], after.range[1]]);
80
+ }
81
+ const before = sourceCode.getTokenBefore(prop);
82
+ if (before && before.value === ',') {
83
+ return fixer.removeRange([before.range[0], prop.range[1]]);
84
+ }
85
+ return fixer.remove(prop);
86
+ }
87
+ return {
88
+ CallExpression(node) {
89
+ if (!isHelmetCall(node))
90
+ return;
91
+ const config = node.arguments[0];
92
+ if (!config || config.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
93
+ return;
94
+ for (const prop of config.properties) {
95
+ if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
96
+ continue;
97
+ const name = propertyKeyName(prop);
98
+ if (!name)
99
+ continue;
100
+ const header = PROTECTIONS[name];
101
+ if (!header)
102
+ continue;
103
+ if (allowed.has(name))
104
+ continue;
105
+ if (prop.value.type !== eslint_devkit_1.AST_NODE_TYPES.Literal ||
106
+ prop.value.value !== false) {
107
+ continue;
108
+ }
109
+ context.report({
110
+ node: prop,
111
+ messageId: 'disabledProtection',
112
+ data: { option: name, header },
113
+ suggest: [
114
+ {
115
+ messageId: 'restoreDefault',
116
+ data: { option: name },
117
+ fix: (fixer) => removeProperty(fixer, prop),
118
+ },
119
+ ],
120
+ });
121
+ }
122
+ },
123
+ };
124
+ },
125
+ });
@@ -1,15 +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.noErrorDetailsInResponse = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- /** Response methods that write a body to the client. */
11
5
  const SEND_METHODS = new Set(['send', 'json', 'end']);
12
- /** First-parameter names that mark an error-first callback / middleware. */
13
6
  const ERROR_PARAM_NAME = /^(err|error)$/i;
14
7
  exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
15
8
  name: 'no-error-details-in-response',
@@ -58,7 +51,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
58
51
  const messageAllowed = allowMessage ?? true;
59
52
  const devAllowed = allowInDev ?? false;
60
53
  const sourceCode = context.sourceCode;
61
- /** Active error bindings (catch params + error-first callback params). */
62
54
  const frames = [];
63
55
  function isErrorName(name) {
64
56
  return frames.some((frame) => frame.name === name);
@@ -76,11 +68,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
76
68
  frames.pop();
77
69
  }
78
70
  }
79
- /**
80
- * True when the call is `res.send/json/end(...)` — including chained
81
- * builders like `res.status(500).json(...)` — on a response object
82
- * named res/response/reply.
83
- */
84
71
  function isResponseSend(node) {
85
72
  const callee = node.callee;
86
73
  if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
@@ -99,11 +86,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
99
86
  const lower = obj.name.toLowerCase();
100
87
  return lower === 'res' || lower === 'response' || lower === 'reply';
101
88
  }
102
- /**
103
- * Returns a description when `node` exposes error details directly:
104
- * the raw error identifier, `err.stack`, or `err.message` (when
105
- * disallowed). Returns null otherwise.
106
- */
107
89
  function detailOf(node) {
108
90
  if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && isErrorName(node.name)) {
109
91
  return `the raw error \`${node.name}\``;
@@ -122,7 +104,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
122
104
  }
123
105
  return null;
124
106
  }
125
- /** Findings in a response-send argument (direct or one object level). */
126
107
  function collectFindings(arg) {
127
108
  const direct = detailOf(arg);
128
109
  if (direct !== null)
@@ -147,7 +128,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
147
128
  }
148
129
  return findings;
149
130
  }
150
- /** True when an enclosing if-statement's test mentions NODE_ENV. */
151
131
  function isInDevGuard(node) {
152
132
  let current = node.parent;
153
133
  while (current != null) {
@@ -174,7 +154,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
174
154
  ArrowFunctionExpression: enterFunction,
175
155
  'ArrowFunctionExpression:exit': exitFrame,
176
156
  CallExpression(node) {
177
- // No active error binding — nothing can leak.
178
157
  if (frames.length === 0)
179
158
  return;
180
159
  if (!isResponseSend(node))
@@ -189,11 +168,6 @@ exports.noErrorDetailsInResponse = (0, eslint_devkit_1.createRule)({
189
168
  node: finding.node,
190
169
  messageId: 'errorDetailsExposed',
191
170
  data: { detail: finding.detail },
192
- // No suggestion: the safe replacement depends on the app's error
193
- // contract (incident ids, logger, status shape), so there is
194
- // nothing mechanical to offer. The remedy is in the message's
195
- // `fix:` line. A `fix: () => null` suggestion would be stripped
196
- // from the output by ESLint and render as nothing at all.
197
171
  });
198
172
  }
199
173
  },
@@ -1,18 +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.noExposedDebugEndpoints = void 0;
9
- /**
10
- * @fileoverview Detect debug endpoints without auth in Express applications
11
- */
12
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
13
5
  const DEFAULT_DEBUG_PATHS = ['/debug', '/__debug__', '/admin', '/_admin', '/test', '/health'];
14
- // Express route-registration methods, incl. `use` for mounted sub-routers and
15
- // `route` for the chained builder (`app.route('/admin').delete(handler)`).
16
6
  const HTTP_METHODS = new Set([
17
7
  'get',
18
8
  'post',
@@ -71,11 +61,9 @@ exports.noExposedDebugEndpoints = (0, eslint_devkit_1.createRule)({
71
61
  const debugPaths = options.endpoints || DEFAULT_DEBUG_PATHS;
72
62
  const ignoreFiles = options.ignoreFiles || [];
73
63
  const filename = context.filename;
74
- // Check if current file should be ignored
75
64
  if (ignoreFiles.some(pattern => filename.includes(pattern))) {
76
65
  return {};
77
66
  }
78
- // oxlint-disable-next-line consistent-function-scoping
79
67
  const isExpressRouteCall = (node) => {
80
68
  return (node.callee.type === 'MemberExpression' &&
81
69
  node.callee.object.type === 'Identifier' &&
@@ -84,17 +72,10 @@ exports.noExposedDebugEndpoints = (0, eslint_devkit_1.createRule)({
84
72
  HTTP_METHODS.has(node.callee.property.name));
85
73
  };
86
74
  return {
87
- // Only route *registrations* are flagged. A bare string literal that
88
- // happens to equal a debug path (a redirect target, a constant, a
89
- // comparison) is not an exposed endpoint — flagging it was a false
90
- // positive.
91
75
  CallExpression(node) {
92
76
  if (!isExpressRouteCall(node)) {
93
77
  return;
94
78
  }
95
- // `app.get(name)` with a single argument is an application-setting
96
- // lookup, not a route registration. Registering a route always passes
97
- // at least one handler.
98
79
  const isSettingLookup = node.callee.type === 'MemberExpression' &&
99
80
  node.callee.property.type === 'Identifier' &&
100
81
  node.callee.property.name === 'get' &&
@@ -1,47 +1,21 @@
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.noExpressUnsafeRegexRoute = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- /**
11
- * HTTP method names used for routing
12
- */
13
5
  const HTTP_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete', 'all', 'use']);
14
- /**
15
- * Patterns that indicate potential ReDoS vulnerability
16
- * These are simplified heuristics - a full ReDoS detector would be more complex
17
- */
18
6
  const VULNERABLE_PATTERNS = [
19
- // Nested quantifiers: (a+)+, (a*)*
20
7
  /\([^)]*[+*][^)]*\)[+*]/,
21
- // Overlapping alternatives: (a|a)+
22
8
  /\([^)]*\|[^)]*\)[+*]/,
23
- // Repetition of repetition: a++, a**
24
9
  /[+*]{2,}/,
25
- // Greedy patterns with backreferences
26
10
  /\([^)]+\)[+*].*\\[1-9]/,
27
11
  ];
28
- /**
29
- * Route parameter patterns that may cause issues
30
- */
31
12
  const VULNERABLE_PARAM_PATTERNS = [
32
- // :param+ or :param* - can match multiple segments
33
13
  /:[a-zA-Z_][a-zA-Z0-9_]*\+/,
34
14
  /:[a-zA-Z_][a-zA-Z0-9_]*\*/,
35
15
  ];
36
- /**
37
- * Check if a regex pattern is potentially vulnerable to ReDoS
38
- */
39
16
  function isVulnerableRegex(pattern) {
40
17
  return VULNERABLE_PATTERNS.some((regex) => regex.test(pattern));
41
18
  }
42
- /**
43
- * Check if a route string has vulnerable param patterns
44
- */
45
19
  function hasVulnerableParamPattern(route) {
46
20
  return VULNERABLE_PARAM_PATTERNS.some((regex) => regex.test(route));
47
21
  }
@@ -109,7 +83,6 @@ exports.noExpressUnsafeRegexRoute = (0, eslint_devkit_1.createRule)({
109
83
  return {
110
84
  CallExpression(node) {
111
85
  const callee = node.callee;
112
- // Check for app.get(), router.post(), etc.
113
86
  if (callee.type !== 'MemberExpression') {
114
87
  return;
115
88
  }
@@ -120,12 +93,10 @@ exports.noExpressUnsafeRegexRoute = (0, eslint_devkit_1.createRule)({
120
93
  if (!HTTP_METHODS.has(property.name)) {
121
94
  return;
122
95
  }
123
- // Check the route argument (first argument)
124
96
  const routeArg = node.arguments[0];
125
97
  if (!routeArg) {
126
98
  return;
127
99
  }
128
- // Check regex literal routes (e.g., /pattern/)
129
100
  if (routeArg.type === 'Literal' && 'regex' in routeArg && routeArg.regex) {
130
101
  const regexNode = routeArg;
131
102
  const pattern = regexNode.regex.pattern;
@@ -136,14 +107,13 @@ exports.noExpressUnsafeRegexRoute = (0, eslint_devkit_1.createRule)({
136
107
  suggest: [
137
108
  {
138
109
  messageId: 'refactorRoute',
139
- fix: () => null, // Manual fix required
110
+ fix: () => null,
140
111
  },
141
112
  ],
142
113
  });
143
114
  }
144
115
  return;
145
116
  }
146
- // Check new RegExp() routes
147
117
  if (routeArg.type === 'NewExpression' &&
148
118
  routeArg.callee.type === 'Identifier' &&
149
119
  routeArg.callee.name === 'RegExp') {
@@ -164,7 +134,6 @@ exports.noExpressUnsafeRegexRoute = (0, eslint_devkit_1.createRule)({
164
134
  }
165
135
  return;
166
136
  }
167
- // Check string routes with vulnerable param patterns
168
137
  if (routeArg.type === 'Literal' && typeof routeArg.value === 'string') {
169
138
  if (hasVulnerableParamPattern(routeArg.value)) {
170
139
  context.report({
@@ -1,31 +1,16 @@
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.noGraphqlIntrospectionProduction = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- /**
11
- * Check if introspection is explicitly enabled
12
- */
13
5
  function hasIntrospectionEnabled(node, sourceCode) {
14
6
  const text = sourceCode.getText(node);
15
- // Check for introspection: true
16
7
  if (/\bintrospection\s*:\s*true\b/.test(text)) {
17
8
  return true;
18
9
  }
19
- // Check for no introspection setting (defaults to true in many libraries)
20
- // We only flag explicit true or missing with warning
21
10
  return false;
22
11
  }
23
- /**
24
- * Check if there's a production guard for introspection
25
- */
26
12
  function hasProductionGuard(node, sourceCode) {
27
13
  const text = sourceCode.getText(node);
28
- // Check for common production guards
29
14
  const productionGuards = [
30
15
  /introspection\s*:\s*process\.env\.NODE_ENV\s*[!=]==?\s*['"]production['"]/,
31
16
  /introspection\s*:\s*!?\s*isProd/,
@@ -98,27 +83,22 @@ exports.noGraphqlIntrospectionProduction = (0, eslint_devkit_1.createRule)({
98
83
  return {
99
84
  CallExpression(node) {
100
85
  const callee = node.callee;
101
- // Check for Apollo Server, Express-GraphQL, or similar
102
86
  const isGraphQLServer = (callee.type === 'Identifier' &&
103
87
  ['ApolloServer', 'GraphQLServer', 'createServer'].includes(callee.name)) ||
104
88
  (callee.type === 'NewExpression' &&
105
89
  callee.callee.type === 'Identifier' &&
106
90
  ['ApolloServer', 'GraphQLServer'].includes(callee.callee.name));
107
- // Also check for graphqlHTTP (express-graphql)
108
91
  const isExpressGraphQL = callee.type === 'Identifier' && callee.name === 'graphqlHTTP';
109
92
  if (!isGraphQLServer && !isExpressGraphQL) {
110
93
  return;
111
94
  }
112
- // Get the options/config argument
113
95
  const configArg = node.arguments[0];
114
96
  if (!configArg || configArg.type !== 'ObjectExpression') {
115
97
  return;
116
98
  }
117
- // Check if there's a production guard
118
99
  if (hasProductionGuard(configArg, sourceCode)) {
119
100
  return;
120
101
  }
121
- // Check if introspection is explicitly enabled
122
102
  if (hasIntrospectionEnabled(configArg, sourceCode)) {
123
103
  context.report({
124
104
  node: configArg,
@@ -132,7 +112,6 @@ exports.noGraphqlIntrospectionProduction = (0, eslint_devkit_1.createRule)({
132
112
  });
133
113
  }
134
114
  },
135
- // Also check NewExpression for new ApolloServer({...})
136
115
  NewExpression(node) {
137
116
  const callee = node.callee;
138
117
  if (callee.type !== 'Identifier' ||
@@ -1,15 +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.noHostHeaderInLinks = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
- /** Header names whose value the client fully controls. */
11
5
  const HOST_HEADER_NAMES = new Set(['host', 'x-forwarded-host']);
12
- /** Express request methods that read a header by name. */
13
6
  const HOST_GETTER_METHODS = new Set(['get', 'header']);
14
7
  exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
15
8
  name: 'no-host-header-in-links',
@@ -66,18 +59,11 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
66
59
  const allowed = allowedHosts ?? [];
67
60
  const mailCallees = new Set(checkMailCallees ?? ['sendMail', 'send']);
68
61
  const sourceCode = context.sourceCode;
69
- /**
70
- * Variables assigned from a host-header read → source description. Keyed by
71
- * the RESOLVED scope variable, not by name: two handlers in the same file
72
- * can each declare `host`, and a name-keyed map would report the second
73
- * one as request-derived when it never was.
74
- */
75
62
  const hostVars = new Map();
76
63
  function isRequestIdent(name) {
77
64
  const lower = name.toLowerCase();
78
65
  return lower === 'req' || lower === 'request' || lower === 'ctx';
79
66
  }
80
- /** Static property name of a member expression, or null. */
81
67
  function propName(node) {
82
68
  if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && !node.computed) {
83
69
  return node.property.name;
@@ -88,13 +74,6 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
88
74
  }
89
75
  return null;
90
76
  }
91
- /**
92
- * Returns a description when `node` reads a client-controlled host
93
- * header, else null. Handles:
94
- * req.headers.host / req.headers['x-forwarded-host']
95
- * req.get('host') / req.header('X-Forwarded-Host')
96
- * a || b / a ?? b (either side tainted taints the result)
97
- */
98
77
  function getHostHeaderSource(node) {
99
78
  if (node.type === eslint_devkit_1.AST_NODE_TYPES.LogicalExpression) {
100
79
  return (getHostHeaderSource(node.left) ?? getHostHeaderSource(node.right));
@@ -131,7 +110,6 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
131
110
  }
132
111
  return null;
133
112
  }
134
- /** Direct host read, or an identifier tracked from one. */
135
113
  function resolveHostSource(node) {
136
114
  if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
137
115
  let scope = sourceCode.getScope(node);
@@ -145,11 +123,9 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
145
123
  }
146
124
  return getHostHeaderSource(node);
147
125
  }
148
- /** True when a static string fragment looks like URL building. */
149
126
  function isUrlish(text) {
150
127
  return text.includes('://') || text.startsWith('//');
151
128
  }
152
- /** True when `node` sits (at any depth) inside a mail-send call. */
153
129
  function isInMailCall(node) {
154
130
  let current = node.parent;
155
131
  while (current != null) {
@@ -169,10 +145,6 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
169
145
  }
170
146
  return false;
171
147
  }
172
- /**
173
- * True when an enclosing if-statement's test mentions one of the
174
- * configured `allowedHosts` literals — the host was validated.
175
- */
176
148
  function isValidatedByAllowlist(node) {
177
149
  if (allowed.length === 0)
178
150
  return false;
@@ -205,7 +177,6 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
205
177
  const source = getHostHeaderSource(node.init);
206
178
  if (source === null)
207
179
  return;
208
- // Exactly one variable for an Identifier id.
209
180
  for (const variable of sourceCode.getDeclaredVariables(node)) {
210
181
  hostVars.set(variable, source);
211
182
  }
@@ -222,8 +193,6 @@ exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
222
193
  BinaryExpression(node) {
223
194
  if (node.operator !== '+')
224
195
  return;
225
- // Only handle the top of a concat chain — inner `+` nodes are
226
- // flattened below.
227
196
  if (node.parent.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression &&
228
197
  node.parent.operator === '+') {
229
198
  return;