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.
- package/README.md +7 -0
- package/package.json +5 -8
- package/src/index.d.ts +17 -40
- package/src/index.js +26 -66
- 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 +0 -26
- package/src/rules/no-exposed-debug-endpoints/index.js +0 -19
- 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 +0 -31
- 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 +0 -40
- 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 +1 -26
- package/src/rules/no-static-root-exposure/index.js +0 -21
- 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 +0 -45
- package/src/rules/require-case-insensitive-path-guard/index.js +0 -21
- 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 +0 -20
- 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 -192
- package/src/rules/no-cors-credentials-wildcard/index.d.ts +0 -26
- package/src/rules/no-error-details-in-response/index.d.ts +0 -57
- 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-host-header-in-links/index.d.ts +0 -56
- 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-sensitive-data-in-query/index.d.ts +0 -51
- package/src/rules/no-static-root-exposure/index.d.ts +0 -45
- package/src/rules/no-user-controlled-redirect/index.d.ts +0 -44
- package/src/rules/no-user-controlled-render-locals/index.d.ts +0 -49
- package/src/rules/require-case-insensitive-path-guard/index.d.ts +0 -59
- 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-query-type-guard/index.d.ts +0 -54
- package/src/rules/require-rate-limiting/index.d.ts +0 -31
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noPermissiveTrustProxy = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const TRUST_PROXY = 'trust proxy';
|
|
6
|
+
const APP_RECEIVER = /^(app|server|router|express)$/i;
|
|
7
|
+
function isAppMethodCall(node, method) {
|
|
8
|
+
const callee = node.callee;
|
|
9
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
10
|
+
return false;
|
|
11
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
12
|
+
return false;
|
|
13
|
+
if (callee.computed)
|
|
14
|
+
return false;
|
|
15
|
+
if (callee.property.name !== method)
|
|
16
|
+
return false;
|
|
17
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
18
|
+
return false;
|
|
19
|
+
return APP_RECEIVER.test(callee.object.name);
|
|
20
|
+
}
|
|
21
|
+
function isTrustProxyLiteral(node) {
|
|
22
|
+
return (node !== undefined &&
|
|
23
|
+
node.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
24
|
+
node.value === TRUST_PROXY);
|
|
25
|
+
}
|
|
26
|
+
exports.noPermissiveTrustProxy = (0, eslint_devkit_1.createRule)({
|
|
27
|
+
name: 'no-permissive-trust-proxy',
|
|
28
|
+
meta: {
|
|
29
|
+
type: 'problem',
|
|
30
|
+
hasSuggestions: true,
|
|
31
|
+
docs: {
|
|
32
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-permissive-trust-proxy.md',
|
|
33
|
+
description: "Disallow unconditional 'trust proxy' — it makes req.ip client-controlled",
|
|
34
|
+
cwe: 'CWE-348',
|
|
35
|
+
cvss: 5.3,
|
|
36
|
+
confidence: 'high',
|
|
37
|
+
},
|
|
38
|
+
messages: {
|
|
39
|
+
permissiveTrustProxy: (0, eslint_devkit_1.formatLLMMessage)({
|
|
40
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
41
|
+
issueName: 'Unconditional Proxy Trust (CWE-348)',
|
|
42
|
+
cwe: 'CWE-348',
|
|
43
|
+
cvss: 5.3,
|
|
44
|
+
description: "'trust proxy' is enabled unconditionally, so Express takes the left-most X-Forwarded-For entry as req.ip. A client can send any address it likes — rate-limit buckets, IP allowlists and audit logs all follow the forged value.",
|
|
45
|
+
severity: 'MEDIUM',
|
|
46
|
+
fix: "Set the number of proxies you actually run behind — app.set('trust proxy', {{hops}}) — or a subnet/'loopback'/predicate that names them.",
|
|
47
|
+
documentationLink: 'https://expressjs.com/en/guide/behind-proxies.html',
|
|
48
|
+
}),
|
|
49
|
+
useHopCount: "Trust exactly {{hops}} proxy hop(s) instead",
|
|
50
|
+
},
|
|
51
|
+
schema: [
|
|
52
|
+
{
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
hopCount: {
|
|
56
|
+
type: 'number',
|
|
57
|
+
minimum: 1,
|
|
58
|
+
description: 'Number of reverse proxies used in the suggested fix',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
additionalProperties: false,
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
defaultOptions: [{}],
|
|
66
|
+
create(context, [options]) {
|
|
67
|
+
const { hopCount } = options;
|
|
68
|
+
const hops = hopCount ?? 1;
|
|
69
|
+
function report(node, replacement) {
|
|
70
|
+
context.report({
|
|
71
|
+
node,
|
|
72
|
+
messageId: 'permissiveTrustProxy',
|
|
73
|
+
data: { hops: String(hops) },
|
|
74
|
+
suggest: [
|
|
75
|
+
{
|
|
76
|
+
messageId: 'useHopCount',
|
|
77
|
+
data: { hops: String(hops) },
|
|
78
|
+
fix: (fixer) => fixer.replaceText(node, replacement),
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
CallExpression(node) {
|
|
85
|
+
if (isAppMethodCall(node, 'enable')) {
|
|
86
|
+
if (!isTrustProxyLiteral(node.arguments[0]))
|
|
87
|
+
return;
|
|
88
|
+
const receiver = context.sourceCode.getText(node.callee.object);
|
|
89
|
+
report(node, `${receiver}.set('trust proxy', ${hops})`);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!isAppMethodCall(node, 'set'))
|
|
93
|
+
return;
|
|
94
|
+
if (!isTrustProxyLiteral(node.arguments[0]))
|
|
95
|
+
return;
|
|
96
|
+
const value = node.arguments[1];
|
|
97
|
+
if (!value ||
|
|
98
|
+
value.type !== eslint_devkit_1.AST_NODE_TYPES.Literal ||
|
|
99
|
+
value.value !== true) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
report(value, String(hops));
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
});
|
|
@@ -1,13 +1,7 @@
|
|
|
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.noSensitiveDataInQuery = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
/** Default sensitive parameter names (token-matched, not substring-matched). */
|
|
11
5
|
const DEFAULT_SENSITIVE = [
|
|
12
6
|
'password',
|
|
13
7
|
'token',
|
|
@@ -19,12 +13,6 @@ const DEFAULT_SENSITIVE = [
|
|
|
19
13
|
'ssn',
|
|
20
14
|
'card',
|
|
21
15
|
];
|
|
22
|
-
/** Split a parameter name into lowercase word tokens (camelCase + snake_case). */
|
|
23
|
-
/**
|
|
24
|
-
* Ceiling on the name length fed to config-supplied `extraPatterns`. Real query
|
|
25
|
-
* parameter names are far shorter; the cap denies a catastrophically
|
|
26
|
-
* backtracking pattern the long input it would need to amplify (CWE-1333).
|
|
27
|
-
*/
|
|
28
16
|
const MAX_PARAM_NAME_LENGTH = 128;
|
|
29
17
|
function tokenize(name) {
|
|
30
18
|
return name
|
|
@@ -33,7 +21,6 @@ function tokenize(name) {
|
|
|
33
21
|
.split(/[^a-z0-9]+/)
|
|
34
22
|
.filter((token) => token.length > 0);
|
|
35
23
|
}
|
|
36
|
-
/** True when `tokens` contains `term` as a contiguous subsequence. */
|
|
37
24
|
function containsSubsequence(tokens, term) {
|
|
38
25
|
for (let i = 0; i + term.length <= tokens.length; i++) {
|
|
39
26
|
let match = true;
|
|
@@ -106,20 +93,12 @@ exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
|
106
93
|
create(context, [options]) {
|
|
107
94
|
const { sensitiveParams, extraPatterns, allowedParams } = options;
|
|
108
95
|
const terms = [...DEFAULT_SENSITIVE, ...(sensitiveParams ?? [])].map((term) => tokenize(term));
|
|
109
|
-
// Config-supplied patterns are compiled once and only ever matched against
|
|
110
|
-
// parameter names, which MAX_PARAM_NAME_LENGTH caps — a catastrophically
|
|
111
|
-
// backtracking pattern from an inherited config therefore has no input long
|
|
112
|
-
// enough to amplify against (CWE-1333).
|
|
113
|
-
// An invalid pattern would throw out of create() and abort the whole lint
|
|
114
|
-
// run for the file with an opaque error; a bad config entry is skipped
|
|
115
|
-
// instead, leaving the rest of the rule working.
|
|
116
96
|
const patterns = [];
|
|
117
97
|
for (const pattern of extraPatterns ?? []) {
|
|
118
98
|
try {
|
|
119
99
|
patterns.push(new RegExp(pattern, 'i'));
|
|
120
100
|
}
|
|
121
101
|
catch {
|
|
122
|
-
// not a valid regular expression — ignore this entry
|
|
123
102
|
}
|
|
124
103
|
}
|
|
125
104
|
const allowedSet = new Set((allowedParams ?? []).map((name) => name.toLowerCase()));
|
|
@@ -127,7 +106,6 @@ exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
|
127
106
|
const lower = name.toLowerCase();
|
|
128
107
|
return lower === 'req' || lower === 'request' || lower === 'ctx';
|
|
129
108
|
}
|
|
130
|
-
/** True when `node` is `<req>.query` (non-computed). */
|
|
131
109
|
function isReqQuery(node) {
|
|
132
110
|
return (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
133
111
|
!node.computed &&
|
|
@@ -143,7 +121,6 @@ exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
|
143
121
|
for (const term of terms) {
|
|
144
122
|
if (term.length === 1) {
|
|
145
123
|
const single = term[0];
|
|
146
|
-
// Exact token match, plus the trivial plural.
|
|
147
124
|
if (tokens.some((token) => token === single || token === `${single}s`)) {
|
|
148
125
|
return true;
|
|
149
126
|
}
|
|
@@ -165,7 +142,6 @@ exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
|
165
142
|
});
|
|
166
143
|
}
|
|
167
144
|
return {
|
|
168
|
-
// req.query.password / req.query['api_key']
|
|
169
145
|
MemberExpression(node) {
|
|
170
146
|
if (!isReqQuery(node.object))
|
|
171
147
|
return;
|
|
@@ -181,7 +157,6 @@ exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
|
181
157
|
if (name !== null && isSensitive(name))
|
|
182
158
|
report(node, name);
|
|
183
159
|
},
|
|
184
|
-
// const { password } = req.query;
|
|
185
160
|
VariableDeclarator(node) {
|
|
186
161
|
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectPattern)
|
|
187
162
|
return;
|
|
@@ -189,7 +164,7 @@ exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
|
189
164
|
return;
|
|
190
165
|
for (const prop of node.id.properties) {
|
|
191
166
|
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
192
|
-
continue;
|
|
167
|
+
continue;
|
|
193
168
|
let name = null;
|
|
194
169
|
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && !prop.computed) {
|
|
195
170
|
name = prop.key.name;
|
|
@@ -1,20 +1,9 @@
|
|
|
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.noStaticRootExposure = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
5
|
const DEFAULT_ALLOWED_ROOTS = ['public', 'static', 'dist', 'build', 'assets'];
|
|
11
|
-
/** path methods that assemble a root directory from segments. */
|
|
12
6
|
const JOIN_METHODS = new Set(['join', 'resolve']);
|
|
13
|
-
/**
|
|
14
|
-
* Quotes a config-supplied string for source output. Prefers single quotes,
|
|
15
|
-
* falling back to `JSON.stringify`'s escaped form when the value contains a
|
|
16
|
-
* quote or backslash — naive interpolation would emit invalid JavaScript.
|
|
17
|
-
*/
|
|
18
7
|
function quoteLiteral(value) {
|
|
19
8
|
const json = JSON.stringify(value);
|
|
20
9
|
const inner = json.slice(1, -1);
|
|
@@ -98,7 +87,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
98
87
|
const { allowedRoots } = options;
|
|
99
88
|
const roots = new Set(allowedRoots ?? DEFAULT_ALLOWED_ROOTS);
|
|
100
89
|
const suggestionRoot = (allowedRoots ?? DEFAULT_ALLOWED_ROOTS)[0] ?? 'public';
|
|
101
|
-
/** Local bindings of the serve-index package (require + import). */
|
|
102
90
|
const serveIndexNames = new Set(['serveIndex']);
|
|
103
91
|
function isDirnameNode(node) {
|
|
104
92
|
return (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.name === '__dirname');
|
|
@@ -143,7 +131,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
143
131
|
return false;
|
|
144
132
|
return callee.object.name.toLowerCase() === 'express';
|
|
145
133
|
}
|
|
146
|
-
/** Split a path string into meaningful segments ('' and '.' dropped). */
|
|
147
134
|
function segmentsOf(value) {
|
|
148
135
|
return value.split(/[\\/]+/).filter((seg) => seg !== '' && seg !== '.');
|
|
149
136
|
}
|
|
@@ -161,13 +148,11 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
161
148
|
],
|
|
162
149
|
});
|
|
163
150
|
}
|
|
164
|
-
/** Analyze the argument list of a path.join()/path.resolve() root. */
|
|
165
151
|
function checkJoinRoot(call) {
|
|
166
152
|
const literalSegments = [];
|
|
167
153
|
let hasNonLiteral = false;
|
|
168
154
|
let hasTraversal = false;
|
|
169
155
|
for (const arg of call.arguments) {
|
|
170
|
-
// __dirname / process.cwd() are anchors, not served subdirectories
|
|
171
156
|
if (isDirnameNode(arg) || isProcessCwdCall(arg))
|
|
172
157
|
continue;
|
|
173
158
|
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
@@ -189,7 +174,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
189
174
|
return;
|
|
190
175
|
}
|
|
191
176
|
if (literalSegments.length === 0) {
|
|
192
|
-
// path.join(__dirname) — anchors only, no subdirectory
|
|
193
177
|
reportRoot(call, context.sourceCode.getText(call));
|
|
194
178
|
return;
|
|
195
179
|
}
|
|
@@ -201,7 +185,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
201
185
|
});
|
|
202
186
|
}
|
|
203
187
|
}
|
|
204
|
-
/** Analyze the first argument of express.static(). */
|
|
205
188
|
function checkStaticRoot(node) {
|
|
206
189
|
if (isDirnameNode(node)) {
|
|
207
190
|
reportRoot(node, '__dirname');
|
|
@@ -220,7 +203,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
220
203
|
return;
|
|
221
204
|
}
|
|
222
205
|
if (segments.length === 0) {
|
|
223
|
-
// '', '.', './', '/'
|
|
224
206
|
reportRoot(node, `'${node.value}'`);
|
|
225
207
|
return;
|
|
226
208
|
}
|
|
@@ -236,8 +218,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
236
218
|
if (isPathJoinCall(node)) {
|
|
237
219
|
checkJoinRoot(node);
|
|
238
220
|
}
|
|
239
|
-
// Anything else (variables, template literals) is not analyzed —
|
|
240
|
-
// documented false negative to avoid taint analysis.
|
|
241
221
|
}
|
|
242
222
|
return {
|
|
243
223
|
ImportDeclaration(node) {
|
|
@@ -269,7 +249,6 @@ exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
|
269
249
|
serveIndexNames.add(node.id.name);
|
|
270
250
|
},
|
|
271
251
|
CallExpression(node) {
|
|
272
|
-
// serve-index usage is a finding on its own — directory listing
|
|
273
252
|
const callee = node.callee;
|
|
274
253
|
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
275
254
|
serveIndexNames.has(callee.name)) {
|
|
@@ -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 &&
|