eslint-plugin-express-security 1.5.3 → 1.5.5

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 (33) hide show
  1. package/package.json +2 -3
  2. package/src/index.js +1 -140
  3. package/src/oxlint.js +1 -3
  4. package/src/rules/no-client-controlled-authorization/index.js +1 -194
  5. package/src/rules/no-cors-credentials-wildcard/index.js +1 -127
  6. package/src/rules/no-disabled-helmet-protections/index.js +1 -125
  7. package/src/rules/no-error-details-in-response/index.js +1 -176
  8. package/src/rules/no-exposed-debug-endpoints/index.js +1 -96
  9. package/src/rules/no-express-unsafe-regex-route/index.js +1 -154
  10. package/src/rules/no-graphql-introspection-production/index.js +1 -143
  11. package/src/rules/no-host-header-in-links/index.js +1 -235
  12. package/src/rules/no-idor-resource-access/index.js +1 -126
  13. package/src/rules/no-insecure-cookie-options/index.js +1 -158
  14. package/src/rules/no-missing-cors-check/index.js +1 -351
  15. package/src/rules/no-missing-csrf-protection/index.js +1 -161
  16. package/src/rules/no-missing-security-headers/index.js +1 -206
  17. package/src/rules/no-permissive-cors/index.js +1 -185
  18. package/src/rules/no-permissive-trust-proxy/index.js +1 -106
  19. package/src/rules/no-sensitive-data-in-query/index.js +1 -182
  20. package/src/rules/no-static-root-exposure/index.js +1 -267
  21. package/src/rules/no-unsafe-csp-directives/index.js +1 -264
  22. package/src/rules/no-user-controlled-redirect/index.js +1 -117
  23. package/src/rules/no-user-controlled-render-locals/index.js +1 -268
  24. package/src/rules/require-case-insensitive-path-guard/index.js +1 -180
  25. package/src/rules/require-csrf-protection/index.js +1 -201
  26. package/src/rules/require-express-body-parser-limits/index.js +1 -181
  27. package/src/rules/require-helmet/index.js +1 -166
  28. package/src/rules/require-query-type-guard/index.js +1 -258
  29. package/src/rules/require-rate-limiting/index.js +1 -136
  30. package/src/rules/require-route-authentication/index.js +1 -225
  31. package/src/rules/require-strict-transport-security/index.js +1 -171
  32. package/src/types/index.js +1 -2
  33. package/src/utils/index.js +1 -81
@@ -1,264 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUnsafeCspDirectives = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- function canonicalDirective(name) {
6
- return name
7
- .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
8
- .toLowerCase();
9
- }
10
- const SCRIPT_DIRECTIVES = new Set([
11
- 'default-src',
12
- 'script-src',
13
- 'script-src-elem',
14
- 'script-src-attr',
15
- 'object-src',
16
- 'worker-src',
17
- ]);
18
- const STYLE_DIRECTIVES = new Set(['style-src', 'style-src-elem', 'style-src-attr']);
19
- const UNSAFE_KEYWORDS = new Set(["'unsafe-inline'", "'unsafe-eval'"]);
20
- const WILDCARD_SOURCES = new Set(['*', 'data:', 'http:', 'https:', 'http://*', 'https://*']);
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
- function isCspFactory(node) {
33
- const callee = node.callee;
34
- if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
35
- return false;
36
- if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
37
- return false;
38
- if (callee.computed)
39
- return false;
40
- if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
41
- return false;
42
- return (callee.object.name === 'helmet' &&
43
- callee.property.name === 'contentSecurityPolicy');
44
- }
45
- function isHelmetCall(node) {
46
- return (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
47
- node.callee.name === 'helmet');
48
- }
49
- exports.noUnsafeCspDirectives = (0, eslint_devkit_1.createRule)({
50
- name: 'no-unsafe-csp-directives',
51
- meta: {
52
- type: 'problem',
53
- hasSuggestions: true,
54
- docs: {
55
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-unsafe-csp-directives.md',
56
- description: "Disallow Content-Security-Policy directives that permit 'unsafe-inline', 'unsafe-eval', wildcard sources, or unrestricted framing",
57
- cwe: 'CWE-79',
58
- cvss: 6.5,
59
- confidence: 'high',
60
- },
61
- messages: {
62
- unsafeInlineSource: (0, eslint_devkit_1.formatLLMMessage)({
63
- icon: eslint_devkit_1.MessageIcons.SECURITY,
64
- issueName: 'CSP Allows Unsafe Script Execution (CWE-79)',
65
- cwe: 'CWE-79',
66
- cvss: 6.5,
67
- description: "{{directive}} includes {{source}}, which re-enables exactly the script execution CSP is there to block — an injected <script> or eval() runs normally.",
68
- severity: 'HIGH',
69
- fix: "Remove {{source}} and use a per-response nonce or hash source instead: scriptSrc: [\"'self'\", (req, res) => `'nonce-${res.locals.nonce}'`].",
70
- documentationLink: 'https://cwe.mitre.org/data/definitions/79.html',
71
- }),
72
- wildcardSource: (0, eslint_devkit_1.formatLLMMessage)({
73
- icon: eslint_devkit_1.MessageIcons.SECURITY,
74
- issueName: 'CSP Wildcard Source (CWE-79)',
75
- cwe: 'CWE-79',
76
- cvss: 6.5,
77
- description: '{{directive}} includes the wildcard source {{source}}, so script can be loaded from any host — the directive stops constraining anything.',
78
- severity: 'HIGH',
79
- fix: 'List the exact origins the app loads code from instead of a wildcard scheme or host.',
80
- documentationLink: 'https://cwe.mitre.org/data/definitions/79.html',
81
- }),
82
- permissiveFrameAncestors: (0, eslint_devkit_1.formatLLMMessage)({
83
- icon: eslint_devkit_1.MessageIcons.SECURITY,
84
- issueName: 'CSP Permits Framing By Any Origin (CWE-1021)',
85
- cwe: 'CWE-1021',
86
- cvss: 5.4,
87
- description: "frame-ancestors is set to {{source}}, so any site can embed this app in an iframe and drive clicks through it (clickjacking).",
88
- severity: 'MEDIUM',
89
- fix: "Set frameAncestors: [\"'self'\"] — or name the exact origins allowed to frame the app.",
90
- documentationLink: 'https://cwe.mitre.org/data/definitions/1021.html',
91
- }),
92
- missingFrameAncestors: (0, eslint_devkit_1.formatLLMMessage)({
93
- icon: eslint_devkit_1.MessageIcons.SECURITY,
94
- issueName: 'CSP Without frame-ancestors (CWE-1021)',
95
- cwe: 'CWE-1021',
96
- cvss: 5.4,
97
- description: 'useDefaults is false and no frame-ancestors directive is set. frame-ancestors does NOT fall back to default-src, so this policy places no limit on who may frame the app.',
98
- severity: 'MEDIUM',
99
- fix: "Add frameAncestors: [\"'self'\"] to the directives object, or drop `useDefaults: false` and keep helmet's defaults.",
100
- documentationLink: 'https://cwe.mitre.org/data/definitions/1021.html',
101
- }),
102
- mixedContentAllowed: (0, eslint_devkit_1.formatLLMMessage)({
103
- icon: eslint_devkit_1.MessageIcons.SECURITY,
104
- issueName: 'CSP Mixed-Content Upgrade Removed (CWE-311)',
105
- cwe: 'CWE-311',
106
- cvss: 5.9,
107
- description: 'upgradeInsecureRequests is set to null, which removes the directive from the policy. Sub-resources referenced over http:// are then fetched in cleartext from an https:// page.',
108
- severity: 'MEDIUM',
109
- fix: 'Delete the `upgradeInsecureRequests: null` entry so the directive stays in the policy.',
110
- documentationLink: 'https://cwe.mitre.org/data/definitions/311.html',
111
- }),
112
- removeUnsafeSource: 'Remove {{source}} from {{directive}}',
113
- },
114
- schema: [
115
- {
116
- type: 'object',
117
- properties: {
118
- checkStyleSrc: {
119
- type: 'boolean',
120
- description: 'Also report unsafe sources in style-executing directives',
121
- },
122
- },
123
- additionalProperties: false,
124
- },
125
- ],
126
- },
127
- defaultOptions: [{}],
128
- create(context, [options]) {
129
- const { checkStyleSrc } = options;
130
- const withStyle = checkStyleSrc ?? true;
131
- function removeElement(fixer, element) {
132
- const sourceCode = context.sourceCode;
133
- const after = sourceCode.getTokenAfter(element);
134
- if (after && after.value === ',') {
135
- return fixer.removeRange([element.range[0], after.range[1]]);
136
- }
137
- const before = sourceCode.getTokenBefore(element);
138
- if (before && before.value === ',') {
139
- return fixer.removeRange([before.range[0], element.range[1]]);
140
- }
141
- return fixer.remove(element);
142
- }
143
- function reportSource(element, messageId, directive, source) {
144
- context.report({
145
- node: element,
146
- messageId,
147
- data: { directive, source },
148
- suggest: [
149
- {
150
- messageId: 'removeUnsafeSource',
151
- data: { directive, source },
152
- fix: (fixer) => removeElement(fixer, element),
153
- },
154
- ],
155
- });
156
- }
157
- function checkSources(directive, value) {
158
- if (value.type !== eslint_devkit_1.AST_NODE_TYPES.ArrayExpression)
159
- return;
160
- const isScript = SCRIPT_DIRECTIVES.has(directive);
161
- const isStyle = withStyle && STYLE_DIRECTIVES.has(directive);
162
- const isFrameAncestors = directive === 'frame-ancestors';
163
- if (!isScript && !isStyle && !isFrameAncestors)
164
- return;
165
- for (const element of value.elements) {
166
- if (!element)
167
- continue;
168
- if (element.type !== eslint_devkit_1.AST_NODE_TYPES.Literal)
169
- continue;
170
- if (typeof element.value !== 'string')
171
- continue;
172
- const source = element.value;
173
- if (isFrameAncestors) {
174
- if (source === '*') {
175
- context.report({
176
- node: element,
177
- messageId: 'permissiveFrameAncestors',
178
- data: { source: "'*'" },
179
- });
180
- }
181
- continue;
182
- }
183
- if (UNSAFE_KEYWORDS.has(source)) {
184
- reportSource(element, 'unsafeInlineSource', directive, source);
185
- continue;
186
- }
187
- if (WILDCARD_SOURCES.has(source)) {
188
- reportSource(element, 'wildcardSource', directive, `'${source}'`);
189
- }
190
- }
191
- }
192
- function checkDirectives(directives, useDefaultsDisabled) {
193
- let sawFrameAncestors = false;
194
- for (const prop of directives.properties) {
195
- if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
196
- continue;
197
- const name = propertyKeyName(prop);
198
- if (!name)
199
- continue;
200
- const directive = canonicalDirective(name);
201
- if (directive === 'frame-ancestors') {
202
- sawFrameAncestors = true;
203
- }
204
- if (directive === 'upgrade-insecure-requests' &&
205
- prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
206
- prop.value.value === null) {
207
- context.report({ node: prop, messageId: 'mixedContentAllowed' });
208
- continue;
209
- }
210
- checkSources(directive, prop.value);
211
- }
212
- if (useDefaultsDisabled && !sawFrameAncestors) {
213
- context.report({ node: directives, messageId: 'missingFrameAncestors' });
214
- }
215
- }
216
- function checkCspConfig(config) {
217
- if (config.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
218
- return;
219
- let directives = null;
220
- let useDefaultsDisabled = false;
221
- for (const prop of config.properties) {
222
- if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
223
- continue;
224
- const name = propertyKeyName(prop);
225
- if (name === 'directives') {
226
- if (prop.value.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
227
- directives = prop.value;
228
- }
229
- continue;
230
- }
231
- if (name === 'useDefaults' &&
232
- prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
233
- prop.value.value === false) {
234
- useDefaultsDisabled = true;
235
- }
236
- }
237
- if (directives) {
238
- checkDirectives(directives, useDefaultsDisabled);
239
- }
240
- }
241
- return {
242
- CallExpression(node) {
243
- if (isCspFactory(node)) {
244
- const config = node.arguments[0];
245
- if (config)
246
- checkCspConfig(config);
247
- return;
248
- }
249
- if (!isHelmetCall(node))
250
- return;
251
- const config = node.arguments[0];
252
- if (!config || config.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
253
- return;
254
- for (const prop of config.properties) {
255
- if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
256
- continue;
257
- if (propertyKeyName(prop) !== 'contentSecurityPolicy')
258
- continue;
259
- checkCspConfig(prop.value);
260
- }
261
- },
262
- };
263
- },
264
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeCspDirectives=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");function canonicalDirective(name){return name.replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase()}const SCRIPT_DIRECTIVES=new Set(["default-src","script-src","script-src-elem","script-src-attr","object-src","worker-src"]);const STYLE_DIRECTIVES=new Set(["style-src","style-src-elem","style-src-attr"]);const UNSAFE_KEYWORDS=new Set(["'unsafe-inline'","'unsafe-eval'"]);const WILDCARD_SOURCES=new Set(["*","data:","http:","https:","http://*","https://*"]);function propertyKeyName(prop){if(prop.computed)return null;if(prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Identifier)return prop.key.name;if(prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"){return prop.key.value}return null}function isCspFactory(node){const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return false;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;if(callee.computed)return false;if(callee.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;return callee.object.name==="helmet"&&callee.property.name==="contentSecurityPolicy"}function isHelmetCall(node){return node.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.name==="helmet"}exports.noUnsafeCspDirectives=(0,eslint_devkit_1.createRule)({name:"no-unsafe-csp-directives",meta:{type:"problem",hasSuggestions:true,docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-unsafe-csp-directives.md",description:"Disallow Content-Security-Policy directives that permit 'unsafe-inline', 'unsafe-eval', wildcard sources, or unrestricted framing",cwe:"CWE-79",cvss:6.5,confidence:"high"},messages:{unsafeInlineSource:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"CSP Allows Unsafe Script Execution (CWE-79)",cwe:"CWE-79",cvss:6.5,description:"{{directive}} includes {{source}}, which re-enables exactly the script execution CSP is there to block \u2014 an injected <script> or eval() runs normally.",severity:"HIGH",fix:"Remove {{source}} and use a per-response nonce or hash source instead: scriptSrc: [\"'self'\", (req, res) => `'nonce-${res.locals.nonce}'`].",documentationLink:"https://cwe.mitre.org/data/definitions/79.html"}),wildcardSource:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"CSP Wildcard Source (CWE-79)",cwe:"CWE-79",cvss:6.5,description:"{{directive}} includes the wildcard source {{source}}, so script can be loaded from any host \u2014 the directive stops constraining anything.",severity:"HIGH",fix:"List the exact origins the app loads code from instead of a wildcard scheme or host.",documentationLink:"https://cwe.mitre.org/data/definitions/79.html"}),permissiveFrameAncestors:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"CSP Permits Framing By Any Origin (CWE-1021)",cwe:"CWE-1021",cvss:5.4,description:"frame-ancestors is set to {{source}}, so any site can embed this app in an iframe and drive clicks through it (clickjacking).",severity:"MEDIUM",fix:`Set frameAncestors: ["'self'"] \u2014 or name the exact origins allowed to frame the app.`,documentationLink:"https://cwe.mitre.org/data/definitions/1021.html"}),missingFrameAncestors:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"CSP Without frame-ancestors (CWE-1021)",cwe:"CWE-1021",cvss:5.4,description:"useDefaults is false and no frame-ancestors directive is set. frame-ancestors does NOT fall back to default-src, so this policy places no limit on who may frame the app.",severity:"MEDIUM",fix:"Add frameAncestors: [\"'self'\"] to the directives object, or drop `useDefaults: false` and keep helmet's defaults.",documentationLink:"https://cwe.mitre.org/data/definitions/1021.html"}),mixedContentAllowed:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"CSP Mixed-Content Upgrade Removed (CWE-311)",cwe:"CWE-311",cvss:5.9,description:"upgradeInsecureRequests is set to null, which removes the directive from the policy. Sub-resources referenced over http:// are then fetched in cleartext from an https:// page.",severity:"MEDIUM",fix:"Delete the `upgradeInsecureRequests: null` entry so the directive stays in the policy.",documentationLink:"https://cwe.mitre.org/data/definitions/311.html"}),removeUnsafeSource:"Remove {{source}} from {{directive}}"},schema:[{type:"object",properties:{checkStyleSrc:{type:"boolean",description:"Also report unsafe sources in style-executing directives"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{checkStyleSrc}=options;const withStyle=checkStyleSrc??true;function removeElement(fixer,element){const sourceCode=context.sourceCode;const after=sourceCode.getTokenAfter(element);if(after&&after.value===","){return fixer.removeRange([element.range[0],after.range[1]])}const before=sourceCode.getTokenBefore(element);if(before&&before.value===","){return fixer.removeRange([before.range[0],element.range[1]])}return fixer.remove(element)}function reportSource(element,messageId,directive,source){context.report({node:element,messageId,data:{directive,source},suggest:[{messageId:"removeUnsafeSource",data:{directive,source},fix:fixer=>removeElement(fixer,element)}]})}function checkSources(directive,value){if(value.type!==eslint_devkit_1.AST_NODE_TYPES.ArrayExpression)return;const isScript=SCRIPT_DIRECTIVES.has(directive);const isStyle=withStyle&&STYLE_DIRECTIVES.has(directive);const isFrameAncestors=directive==="frame-ancestors";if(!isScript&&!isStyle&&!isFrameAncestors)return;for(const element of value.elements){if(!element)continue;if(element.type!==eslint_devkit_1.AST_NODE_TYPES.Literal)continue;if(typeof element.value!=="string")continue;const source=element.value;if(isFrameAncestors){if(source==="*"){context.report({node:element,messageId:"permissiveFrameAncestors",data:{source:"'*'"}})}continue}if(UNSAFE_KEYWORDS.has(source)){reportSource(element,"unsafeInlineSource",directive,source);continue}if(WILDCARD_SOURCES.has(source)){reportSource(element,"wildcardSource",directive,`'${source}'`)}}}function checkDirectives(directives,useDefaultsDisabled){let sawFrameAncestors=false;for(const prop of directives.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.Property)continue;const name=propertyKeyName(prop);if(!name)continue;const directive=canonicalDirective(name);if(directive==="frame-ancestors"){sawFrameAncestors=true}if(directive==="upgrade-insecure-requests"&&prop.value.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&prop.value.value===null){context.report({node:prop,messageId:"mixedContentAllowed"});continue}checkSources(directive,prop.value)}if(useDefaultsDisabled&&!sawFrameAncestors){context.report({node:directives,messageId:"missingFrameAncestors"})}}function checkCspConfig(config){if(config.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)return;let directives=null;let useDefaultsDisabled=false;for(const prop of config.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.Property)continue;const name=propertyKeyName(prop);if(name==="directives"){if(prop.value.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression){directives=prop.value}continue}if(name==="useDefaults"&&prop.value.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&prop.value.value===false){useDefaultsDisabled=true}}if(directives){checkDirectives(directives,useDefaultsDisabled)}}return{CallExpression(node){if(isCspFactory(node)){const config2=node.arguments[0];if(config2)checkCspConfig(config2);return}if(!isHelmetCall(node))return;const config=node.arguments[0];if(!config||config.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)return;for(const prop of config.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.Property)continue;if(propertyKeyName(prop)!=="contentSecurityPolicy")continue;checkCspConfig(prop.value)}}}}});
@@ -1,117 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUserControlledRedirect = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const USER_SOURCE_PROPS = new Set(['query', 'body', 'params', 'headers', 'cookies']);
6
- const REDIRECT_METHODS = new Set(['redirect', 'location']);
7
- exports.noUserControlledRedirect = (0, eslint_devkit_1.createRule)({
8
- name: 'no-user-controlled-redirect',
9
- meta: {
10
- type: 'problem',
11
- docs: {
12
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-user-controlled-redirect.md',
13
- description: 'Disallow res.redirect() with values directly from req.query / req.body / req.params',
14
- cwe: 'CWE-601',
15
- cvss: 6.1,
16
- },
17
- messages: {
18
- openRedirect: (0, eslint_devkit_1.formatLLMMessage)({
19
- icon: eslint_devkit_1.MessageIcons.SECURITY,
20
- issueName: 'Open Redirect (CWE-601)',
21
- cwe: 'CWE-601',
22
- description: 'res.redirect() receives a value directly from {{source}}, which is user-controlled. An attacker can redirect victims to a malicious site.',
23
- severity: 'HIGH',
24
- fix: 'Validate the redirect target against an allowlist of trusted paths/origins, or use a relative path that cannot be redirected off-domain.',
25
- documentationLink: 'https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html',
26
- }),
27
- },
28
- schema: [
29
- {
30
- type: 'object',
31
- properties: {
32
- responseObjects: {
33
- type: 'array',
34
- items: { type: 'string' },
35
- description: 'Additional response object names (e.g. ["reply"] for Fastify)',
36
- },
37
- requestObjects: {
38
- type: 'array',
39
- items: { type: 'string' },
40
- description: 'Additional request object names',
41
- },
42
- },
43
- additionalProperties: false,
44
- },
45
- ],
46
- },
47
- defaultOptions: [{}],
48
- create(context, [options]) {
49
- const { responseObjects, requestObjects } = options;
50
- const extraResNames = new Set(responseObjects ?? []);
51
- const extraReqNames = new Set(requestObjects ?? []);
52
- function isRedirectCall(node) {
53
- const callee = node.callee;
54
- if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
55
- return false;
56
- if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
57
- return false;
58
- if (!REDIRECT_METHODS.has(callee.property.name))
59
- return false;
60
- const obj = callee.object;
61
- if (obj.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
62
- return false;
63
- const name = obj.name.toLowerCase();
64
- return (name === 'res' ||
65
- name === 'response' ||
66
- name === 'reply' ||
67
- extraResNames.has(obj.name));
68
- }
69
- function getUserSourceDescription(node) {
70
- if (node.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
71
- return null;
72
- const obj = node.object;
73
- if (obj.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
74
- const root = obj.object;
75
- const sourceProp = obj.property;
76
- if (root.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
77
- sourceProp.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
78
- USER_SOURCE_PROPS.has(sourceProp.name) &&
79
- isRequestIdent(root.name)) {
80
- return `req.${sourceProp.name}`;
81
- }
82
- }
83
- const prop = node.property;
84
- if (obj.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
85
- prop.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
86
- USER_SOURCE_PROPS.has(prop.name) &&
87
- isRequestIdent(obj.name)) {
88
- return `req.${prop.name}`;
89
- }
90
- return null;
91
- }
92
- function isRequestIdent(name) {
93
- const lower = name.toLowerCase();
94
- return (lower === 'req' ||
95
- lower === 'request' ||
96
- lower === 'ctx' ||
97
- extraReqNames.has(name));
98
- }
99
- return {
100
- CallExpression(node) {
101
- if (!isRedirectCall(node))
102
- return;
103
- const firstArg = node.arguments[0];
104
- if (!firstArg)
105
- return;
106
- const source = getUserSourceDescription(firstArg);
107
- if (!source)
108
- return;
109
- context.report({
110
- node: firstArg,
111
- messageId: 'openRedirect',
112
- data: { source },
113
- });
114
- },
115
- };
116
- },
117
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUserControlledRedirect=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const USER_SOURCE_PROPS=new Set(["query","body","params","headers","cookies"]);const REDIRECT_METHODS=new Set(["redirect","location"]);exports.noUserControlledRedirect=(0,eslint_devkit_1.createRule)({name:"no-user-controlled-redirect",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-user-controlled-redirect.md",description:"Disallow res.redirect() with values directly from req.query / req.body / req.params",cwe:"CWE-601",cvss:6.1},messages:{openRedirect:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Open Redirect (CWE-601)",cwe:"CWE-601",description:"res.redirect() receives a value directly from {{source}}, which is user-controlled. An attacker can redirect victims to a malicious site.",severity:"HIGH",fix:"Validate the redirect target against an allowlist of trusted paths/origins, or use a relative path that cannot be redirected off-domain.",documentationLink:"https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html"})},schema:[{type:"object",properties:{responseObjects:{type:"array",items:{type:"string"},description:'Additional response object names (e.g. ["reply"] for Fastify)'},requestObjects:{type:"array",items:{type:"string"},description:"Additional request object names"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{responseObjects,requestObjects}=options;const extraResNames=new Set(responseObjects??[]);const extraReqNames=new Set(requestObjects??[]);function isRedirectCall(node){const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return false;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;if(!REDIRECT_METHODS.has(callee.property.name))return false;const obj=callee.object;if(obj.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;const name=obj.name.toLowerCase();return name==="res"||name==="response"||name==="reply"||extraResNames.has(obj.name)}function getUserSourceDescription(node){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return null;const obj=node.object;if(obj.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){const root=obj.object;const sourceProp=obj.property;if(root.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&sourceProp.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&USER_SOURCE_PROPS.has(sourceProp.name)&&isRequestIdent(root.name)){return`req.${sourceProp.name}`}}const prop=node.property;if(obj.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&prop.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&USER_SOURCE_PROPS.has(prop.name)&&isRequestIdent(obj.name)){return`req.${prop.name}`}return null}function isRequestIdent(name){const lower=name.toLowerCase();return lower==="req"||lower==="request"||lower==="ctx"||extraReqNames.has(name)}return{CallExpression(node){if(!isRedirectCall(node))return;const firstArg=node.arguments[0];if(!firstArg)return;const source=getUserSourceDescription(firstArg);if(!source)return;context.report({node:firstArg,messageId:"openRedirect",data:{source}})}}}});