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
|
@@ -0,0 +1,264 @@
|
|
|
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,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.noUserControlledRedirect = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
/** Property names on the request object that carry user-controlled values. */
|
|
11
5
|
const USER_SOURCE_PROPS = new Set(['query', 'body', 'params', 'headers', 'cookies']);
|
|
12
|
-
/** Method names that perform HTTP redirects. */
|
|
13
6
|
const REDIRECT_METHODS = new Set(['redirect', 'location']);
|
|
14
7
|
exports.noUserControlledRedirect = (0, eslint_devkit_1.createRule)({
|
|
15
8
|
name: 'no-user-controlled-redirect',
|
|
@@ -56,11 +49,6 @@ exports.noUserControlledRedirect = (0, eslint_devkit_1.createRule)({
|
|
|
56
49
|
const { responseObjects, requestObjects } = options;
|
|
57
50
|
const extraResNames = new Set(responseObjects ?? []);
|
|
58
51
|
const extraReqNames = new Set(requestObjects ?? []);
|
|
59
|
-
/**
|
|
60
|
-
* Returns true if `node` is a member expression whose root object is a
|
|
61
|
-
* known response object name and whose property is a redirect method.
|
|
62
|
-
* e.g. `res.redirect`, `response.location`, `reply.redirect`
|
|
63
|
-
*/
|
|
64
52
|
function isRedirectCall(node) {
|
|
65
53
|
const callee = node.callee;
|
|
66
54
|
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
@@ -78,22 +66,9 @@ exports.noUserControlledRedirect = (0, eslint_devkit_1.createRule)({
|
|
|
78
66
|
name === 'reply' ||
|
|
79
67
|
extraResNames.has(obj.name));
|
|
80
68
|
}
|
|
81
|
-
/**
|
|
82
|
-
* Returns a human-readable description if `node` is a direct access on a
|
|
83
|
-
* user-controlled request property (req.query.foo, req.body.bar, etc.)
|
|
84
|
-
* Returns null if not a user-source access.
|
|
85
|
-
*
|
|
86
|
-
* Valid patterns:
|
|
87
|
-
* req.query.returnUrl → MemberExpr(MemberExpr(req, query), returnUrl)
|
|
88
|
-
* req.body['redirect'] → MemberExpr(MemberExpr(req, body), computed)
|
|
89
|
-
* req.params.slug → same
|
|
90
|
-
* req.headers['x-redirect'] → same
|
|
91
|
-
* req.query → direct property (whole query object)
|
|
92
|
-
*/
|
|
93
69
|
function getUserSourceDescription(node) {
|
|
94
70
|
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
95
71
|
return null;
|
|
96
|
-
// Case 1: req.query.foo (three levels)
|
|
97
72
|
const obj = node.object;
|
|
98
73
|
if (obj.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
99
74
|
const root = obj.object;
|
|
@@ -105,7 +80,6 @@ exports.noUserControlledRedirect = (0, eslint_devkit_1.createRule)({
|
|
|
105
80
|
return `req.${sourceProp.name}`;
|
|
106
81
|
}
|
|
107
82
|
}
|
|
108
|
-
// Case 2: req.query (two levels — whole user-source object)
|
|
109
83
|
const prop = node.property;
|
|
110
84
|
if (obj.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
111
85
|
prop.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noUserControlledRenderLocals = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const USER_SOURCE_PROPS = new Set(['body', 'query', 'params']);
|
|
6
|
+
const RESPONSE_NAMES = new Set(['res', 'response', 'reply']);
|
|
7
|
+
const REQUEST_NAMES = new Set(['req', 'request', 'ctx']);
|
|
8
|
+
exports.noUserControlledRenderLocals = (0, eslint_devkit_1.createRule)({
|
|
9
|
+
name: 'no-user-controlled-render-locals',
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'problem',
|
|
12
|
+
hasSuggestions: true,
|
|
13
|
+
docs: {
|
|
14
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-user-controlled-render-locals.md',
|
|
15
|
+
description: 'Disallow res.render() with locals or view names sourced wholesale from req.body / req.query / req.params',
|
|
16
|
+
cwe: 'CWE-73',
|
|
17
|
+
cvss: 7.3,
|
|
18
|
+
},
|
|
19
|
+
messages: {
|
|
20
|
+
unsafeRenderLocals: (0, eslint_devkit_1.formatLLMMessage)({
|
|
21
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
22
|
+
issueName: 'Template Object Injection (CWE-73)',
|
|
23
|
+
cwe: 'CWE-73',
|
|
24
|
+
description: 'res.render() receives locals wholly controlled by {{source}}. Express merges locals into the view-engine options, so keys like "layout", "settings" or "cache" can point the engine at an attacker-chosen file.',
|
|
25
|
+
severity: 'HIGH',
|
|
26
|
+
fix: 'Pick only the fields the template needs into an explicit object literal ({ title: req.body.title }) instead of forwarding the whole request object.',
|
|
27
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/73.html',
|
|
28
|
+
}),
|
|
29
|
+
userControlledView: (0, eslint_devkit_1.formatLLMMessage)({
|
|
30
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
31
|
+
issueName: 'User-Controlled View Path (CWE-73)',
|
|
32
|
+
cwe: 'CWE-73',
|
|
33
|
+
description: 'The view name passed to res.render() derives from {{source}}. An attacker can traverse into unintended templates or files within the views directory.',
|
|
34
|
+
severity: 'HIGH',
|
|
35
|
+
fix: 'Map user input to a fixed set of view names via an allowlist; never build the template path from request data.',
|
|
36
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/73.html',
|
|
37
|
+
}),
|
|
38
|
+
pickFieldsExplicitly: 'Remove the request-object spread and pick the required fields explicitly',
|
|
39
|
+
},
|
|
40
|
+
schema: [
|
|
41
|
+
{
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
allowSanitizers: {
|
|
45
|
+
type: 'array',
|
|
46
|
+
items: { type: 'string' },
|
|
47
|
+
description: 'Callee names that sanitize locals (e.g. ["pick", "sanitizeLocals"]); calls to them are not flagged',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
defaultOptions: [{}],
|
|
55
|
+
create(context, [options]) {
|
|
56
|
+
const { allowSanitizers } = options;
|
|
57
|
+
const sanitizers = new Set(allowSanitizers ?? []);
|
|
58
|
+
const trackedVars = new Map();
|
|
59
|
+
function resolveVariable(node) {
|
|
60
|
+
let scope = context.sourceCode.getScope(node);
|
|
61
|
+
while (scope) {
|
|
62
|
+
const found = scope.variables.find((v) => v.name === node.name);
|
|
63
|
+
if (found)
|
|
64
|
+
return found;
|
|
65
|
+
scope = scope.upper;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
function getTracked(node) {
|
|
70
|
+
const variable = resolveVariable(node);
|
|
71
|
+
return variable ? trackedVars.get(variable) : undefined;
|
|
72
|
+
}
|
|
73
|
+
function isRequestName(name) {
|
|
74
|
+
return REQUEST_NAMES.has(name.toLowerCase());
|
|
75
|
+
}
|
|
76
|
+
function getWholeSource(node) {
|
|
77
|
+
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
78
|
+
return null;
|
|
79
|
+
const { object, property } = node;
|
|
80
|
+
if (object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
81
|
+
return null;
|
|
82
|
+
if (property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
83
|
+
return null;
|
|
84
|
+
if (node.computed)
|
|
85
|
+
return null;
|
|
86
|
+
if (!USER_SOURCE_PROPS.has(property.name))
|
|
87
|
+
return null;
|
|
88
|
+
if (!isRequestName(object.name))
|
|
89
|
+
return null;
|
|
90
|
+
return `req.${property.name}`;
|
|
91
|
+
}
|
|
92
|
+
function isSanitizerCall(node) {
|
|
93
|
+
const callee = node.callee;
|
|
94
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
95
|
+
return sanitizers.has(callee.name);
|
|
96
|
+
}
|
|
97
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
98
|
+
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
99
|
+
return sanitizers.has(callee.property.name);
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
function getUnsafeLocalsSource(node) {
|
|
104
|
+
const direct = getWholeSource(node);
|
|
105
|
+
if (direct)
|
|
106
|
+
return direct;
|
|
107
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
108
|
+
const tracked = getTracked(node);
|
|
109
|
+
if (tracked && tracked.whole)
|
|
110
|
+
return tracked.source;
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
114
|
+
if (isSanitizerCall(node))
|
|
115
|
+
return null;
|
|
116
|
+
for (const arg of node.arguments) {
|
|
117
|
+
const argSource = getWholeSource(arg);
|
|
118
|
+
if (argSource)
|
|
119
|
+
return argSource;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function getDerivedSource(node) {
|
|
125
|
+
const whole = getWholeSource(node);
|
|
126
|
+
if (whole)
|
|
127
|
+
return whole;
|
|
128
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
129
|
+
return getDerivedSource(node.object);
|
|
130
|
+
}
|
|
131
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
|
|
132
|
+
for (const expr of node.expressions) {
|
|
133
|
+
const inner = getDerivedSource(expr);
|
|
134
|
+
if (inner)
|
|
135
|
+
return inner;
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
|
|
140
|
+
if (node.operator !== '+')
|
|
141
|
+
return null;
|
|
142
|
+
const left = getDerivedSource(node.left);
|
|
143
|
+
if (left)
|
|
144
|
+
return left;
|
|
145
|
+
return getDerivedSource(node.right);
|
|
146
|
+
}
|
|
147
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
148
|
+
const tracked = getTracked(node);
|
|
149
|
+
if (tracked)
|
|
150
|
+
return tracked.source;
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
function isRenderCall(node) {
|
|
156
|
+
const callee = node.callee;
|
|
157
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
158
|
+
return false;
|
|
159
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
160
|
+
return false;
|
|
161
|
+
if (callee.property.name !== 'render')
|
|
162
|
+
return false;
|
|
163
|
+
const obj = callee.object;
|
|
164
|
+
if (obj.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
165
|
+
return false;
|
|
166
|
+
return RESPONSE_NAMES.has(obj.name.toLowerCase());
|
|
167
|
+
}
|
|
168
|
+
function buildRemoveSpreadFix(spread) {
|
|
169
|
+
return (fixer) => {
|
|
170
|
+
const sourceCode = context.sourceCode;
|
|
171
|
+
const tokenAfter = sourceCode.getTokenAfter(spread);
|
|
172
|
+
const tokenBefore = sourceCode.getTokenBefore(spread);
|
|
173
|
+
if (tokenAfter.value === ',') {
|
|
174
|
+
return fixer.removeRange([spread.range[0], tokenAfter.range[1]]);
|
|
175
|
+
}
|
|
176
|
+
if (tokenBefore.value === ',') {
|
|
177
|
+
return fixer.removeRange([tokenBefore.range[0], spread.range[1]]);
|
|
178
|
+
}
|
|
179
|
+
return fixer.remove(spread);
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function checkLocalsArg(node) {
|
|
183
|
+
const source = getUnsafeLocalsSource(node);
|
|
184
|
+
if (source) {
|
|
185
|
+
context.report({
|
|
186
|
+
node,
|
|
187
|
+
messageId: 'unsafeRenderLocals',
|
|
188
|
+
data: { source },
|
|
189
|
+
});
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
|
|
193
|
+
return;
|
|
194
|
+
for (const prop of node.properties) {
|
|
195
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.SpreadElement)
|
|
196
|
+
continue;
|
|
197
|
+
const spreadSource = getUnsafeLocalsSource(prop.argument);
|
|
198
|
+
if (!spreadSource)
|
|
199
|
+
continue;
|
|
200
|
+
context.report({
|
|
201
|
+
node: prop,
|
|
202
|
+
messageId: 'unsafeRenderLocals',
|
|
203
|
+
data: { source: spreadSource },
|
|
204
|
+
suggest: [
|
|
205
|
+
{
|
|
206
|
+
messageId: 'pickFieldsExplicitly',
|
|
207
|
+
fix: buildRemoveSpreadFix(prop),
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function trackDeclarator(declared, init) {
|
|
214
|
+
const unsafe = getUnsafeLocalsSource(init);
|
|
215
|
+
if (unsafe) {
|
|
216
|
+
trackedVars.set(declared, { source: unsafe, whole: true });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (init.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
220
|
+
for (const prop of init.properties) {
|
|
221
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.SpreadElement)
|
|
222
|
+
continue;
|
|
223
|
+
const spreadSource = getUnsafeLocalsSource(prop.argument);
|
|
224
|
+
if (spreadSource) {
|
|
225
|
+
trackedVars.set(declared, { source: spreadSource, whole: true });
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const derived = getDerivedSource(init);
|
|
232
|
+
if (derived) {
|
|
233
|
+
trackedVars.set(declared, { source: derived, whole: false });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
VariableDeclarator(node) {
|
|
238
|
+
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
239
|
+
return;
|
|
240
|
+
const init = node.init;
|
|
241
|
+
if (!init)
|
|
242
|
+
return;
|
|
243
|
+
for (const declared of context.sourceCode.getDeclaredVariables(node)) {
|
|
244
|
+
trackDeclarator(declared, init);
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
CallExpression(node) {
|
|
248
|
+
if (!isRenderCall(node))
|
|
249
|
+
return;
|
|
250
|
+
const viewArg = node.arguments[0];
|
|
251
|
+
if (!viewArg)
|
|
252
|
+
return;
|
|
253
|
+
const viewSource = getDerivedSource(viewArg);
|
|
254
|
+
if (viewSource) {
|
|
255
|
+
context.report({
|
|
256
|
+
node: viewArg,
|
|
257
|
+
messageId: 'userControlledView',
|
|
258
|
+
data: { source: viewSource },
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const localsArg = node.arguments[1];
|
|
262
|
+
if (!localsArg)
|
|
263
|
+
return;
|
|
264
|
+
checkLocalsArg(localsArg);
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
},
|
|
268
|
+
});
|