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,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireQueryTypeGuard = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const STRING_METHODS = new Set([
|
|
6
|
+
'replace',
|
|
7
|
+
'toLowerCase',
|
|
8
|
+
'toUpperCase',
|
|
9
|
+
'trim',
|
|
10
|
+
'split',
|
|
11
|
+
'substring',
|
|
12
|
+
'slice',
|
|
13
|
+
'startsWith',
|
|
14
|
+
'endsWith',
|
|
15
|
+
'includes',
|
|
16
|
+
'match',
|
|
17
|
+
]);
|
|
18
|
+
const EQUALITY_OPS = new Set(['===', '==', '!==', '!=']);
|
|
19
|
+
exports.requireQueryTypeGuard = (0, eslint_devkit_1.createRule)({
|
|
20
|
+
name: 'require-query-type-guard',
|
|
21
|
+
meta: {
|
|
22
|
+
type: 'problem',
|
|
23
|
+
hasSuggestions: true,
|
|
24
|
+
docs: {
|
|
25
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-query-type-guard.md',
|
|
26
|
+
description: 'Require a type guard or String() coercion before calling string methods on req.query values',
|
|
27
|
+
cwe: 'CWE-843',
|
|
28
|
+
cvss: 7.5,
|
|
29
|
+
},
|
|
30
|
+
messages: {
|
|
31
|
+
unguardedQueryStringMethod: (0, eslint_devkit_1.formatLLMMessage)({
|
|
32
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
33
|
+
issueName: 'req.query Type Confusion (CWE-843)',
|
|
34
|
+
cwe: 'CWE-843',
|
|
35
|
+
description: '.{{method}}() is called on {{target}}, but req.query values are string | string[] | ParsedQs — ?x=a&x=b yields an array, so this throws (DoS) or bypasses sanitization.',
|
|
36
|
+
severity: 'HIGH',
|
|
37
|
+
fix: "Coerce with String(...) or guard with typeof value === 'string' / Array.isArray(value) before treating the query value as a string.",
|
|
38
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/843.html',
|
|
39
|
+
}),
|
|
40
|
+
wrapString: 'Coerce the query value with String(...) before calling .{{method}}()',
|
|
41
|
+
coerceAtAssignment: 'Coerce at the assignment: wrap the req.query access in String(...)',
|
|
42
|
+
},
|
|
43
|
+
schema: [
|
|
44
|
+
{
|
|
45
|
+
type: 'object',
|
|
46
|
+
properties: {
|
|
47
|
+
coercers: {
|
|
48
|
+
type: 'array',
|
|
49
|
+
items: { type: 'string' },
|
|
50
|
+
description: 'Callee names whose result is a safe string coercion',
|
|
51
|
+
},
|
|
52
|
+
validators: {
|
|
53
|
+
type: 'array',
|
|
54
|
+
items: { type: 'string' },
|
|
55
|
+
description: 'Callee / method names treated as type-safe sources',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
additionalProperties: false,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
defaultOptions: [{}],
|
|
63
|
+
create(context, [options]) {
|
|
64
|
+
const { coercers, validators } = options;
|
|
65
|
+
const coercerNames = new Set(coercers ?? ['String']);
|
|
66
|
+
const validatorNames = new Set(validators ?? ['parse', 'safeParse']);
|
|
67
|
+
const sourceCode = context.sourceCode;
|
|
68
|
+
function newFrame() {
|
|
69
|
+
return {
|
|
70
|
+
tracked: new Map(),
|
|
71
|
+
guardedVars: new Set(),
|
|
72
|
+
guardedProps: new Set(),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const frames = [newFrame()];
|
|
76
|
+
const current = () => frames[frames.length - 1];
|
|
77
|
+
function isRequestIdent(name) {
|
|
78
|
+
const lower = name.toLowerCase();
|
|
79
|
+
return lower === 'req' || lower === 'request';
|
|
80
|
+
}
|
|
81
|
+
function isReqQuery(node) {
|
|
82
|
+
return (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
83
|
+
!node.computed &&
|
|
84
|
+
node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
85
|
+
isRequestIdent(node.object.name) &&
|
|
86
|
+
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
87
|
+
node.property.name === 'query');
|
|
88
|
+
}
|
|
89
|
+
function isQueryMember(node) {
|
|
90
|
+
return (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression && isReqQuery(node.object));
|
|
91
|
+
}
|
|
92
|
+
function propKey(node) {
|
|
93
|
+
return sourceCode.getText(node.property);
|
|
94
|
+
}
|
|
95
|
+
function guardVar(name) {
|
|
96
|
+
for (let i = frames.length - 1; i >= 0; i--) {
|
|
97
|
+
if (frames[i].tracked.has(name)) {
|
|
98
|
+
frames[i].guardedVars.add(name);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
current().guardedVars.add(name);
|
|
103
|
+
}
|
|
104
|
+
function markGuard(target) {
|
|
105
|
+
if (target.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
106
|
+
guardVar(target.name);
|
|
107
|
+
}
|
|
108
|
+
else if (isQueryMember(target)) {
|
|
109
|
+
current().guardedProps.add(propKey(target));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function isVarGuarded(name) {
|
|
113
|
+
return frames.some((f) => f.guardedVars.has(name));
|
|
114
|
+
}
|
|
115
|
+
function isPropGuarded(key) {
|
|
116
|
+
return frames.some((f) => f.guardedProps.has(key));
|
|
117
|
+
}
|
|
118
|
+
function lookupTracked(name) {
|
|
119
|
+
for (let i = frames.length - 1; i >= 0; i--) {
|
|
120
|
+
const init = frames[i].tracked.get(name);
|
|
121
|
+
if (init)
|
|
122
|
+
return init;
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
function extractTypeofTarget(a, b) {
|
|
127
|
+
if (a.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression &&
|
|
128
|
+
a.operator === 'typeof' &&
|
|
129
|
+
b.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
130
|
+
b.value === 'string') {
|
|
131
|
+
return a.argument;
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
function isArrayIsArrayCall(node) {
|
|
136
|
+
const callee = node.callee;
|
|
137
|
+
return (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
138
|
+
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
139
|
+
callee.object.name === 'Array' &&
|
|
140
|
+
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
141
|
+
callee.property.name === 'isArray');
|
|
142
|
+
}
|
|
143
|
+
function isSafeCall(node) {
|
|
144
|
+
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
145
|
+
return false;
|
|
146
|
+
const callee = node.callee;
|
|
147
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
148
|
+
return coercerNames.has(callee.name) || validatorNames.has(callee.name);
|
|
149
|
+
}
|
|
150
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
151
|
+
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
152
|
+
return validatorNames.has(callee.property.name);
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
function wrapWithString(fixer, target) {
|
|
157
|
+
return [
|
|
158
|
+
fixer.insertTextBefore(target, 'String('),
|
|
159
|
+
fixer.insertTextAfter(target, ')'),
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
function enterFunction() {
|
|
163
|
+
frames.push(newFrame());
|
|
164
|
+
}
|
|
165
|
+
function exitFunction() {
|
|
166
|
+
frames.pop();
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression': enterFunction,
|
|
170
|
+
'FunctionDeclaration:exit': exitFunction,
|
|
171
|
+
'FunctionExpression:exit': exitFunction,
|
|
172
|
+
'ArrowFunctionExpression:exit': exitFunction,
|
|
173
|
+
VariableDeclarator(node) {
|
|
174
|
+
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
175
|
+
return;
|
|
176
|
+
const init = node.init;
|
|
177
|
+
if (!init)
|
|
178
|
+
return;
|
|
179
|
+
if (isQueryMember(init)) {
|
|
180
|
+
current().tracked.set(node.id.name, init);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
AssignmentExpression(node) {
|
|
184
|
+
if (node.operator !== '=')
|
|
185
|
+
return;
|
|
186
|
+
if (node.left.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
187
|
+
return;
|
|
188
|
+
if (isQueryMember(node.right)) {
|
|
189
|
+
current().tracked.set(node.left.name, node.right);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (isSafeCall(node.right)) {
|
|
193
|
+
guardVar(node.left.name);
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
BinaryExpression(node) {
|
|
197
|
+
if (!EQUALITY_OPS.has(node.operator))
|
|
198
|
+
return;
|
|
199
|
+
const target = extractTypeofTarget(node.left, node.right) ??
|
|
200
|
+
extractTypeofTarget(node.right, node.left);
|
|
201
|
+
if (target)
|
|
202
|
+
markGuard(target);
|
|
203
|
+
},
|
|
204
|
+
CallExpression(node) {
|
|
205
|
+
if (isArrayIsArrayCall(node)) {
|
|
206
|
+
const arg = node.arguments[0];
|
|
207
|
+
if (arg)
|
|
208
|
+
markGuard(arg);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const callee = node.callee;
|
|
212
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
213
|
+
return;
|
|
214
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
215
|
+
return;
|
|
216
|
+
const method = callee.property.name;
|
|
217
|
+
if (!STRING_METHODS.has(method))
|
|
218
|
+
return;
|
|
219
|
+
const obj = callee.object;
|
|
220
|
+
if (isQueryMember(obj)) {
|
|
221
|
+
if (isPropGuarded(propKey(obj)))
|
|
222
|
+
return;
|
|
223
|
+
context.report({
|
|
224
|
+
node: callee,
|
|
225
|
+
messageId: 'unguardedQueryStringMethod',
|
|
226
|
+
data: { target: sourceCode.getText(obj), method },
|
|
227
|
+
suggest: [
|
|
228
|
+
{
|
|
229
|
+
messageId: 'wrapString',
|
|
230
|
+
data: { method },
|
|
231
|
+
fix: (fixer) => wrapWithString(fixer, obj),
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (obj.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
238
|
+
return;
|
|
239
|
+
const trackedInit = lookupTracked(obj.name);
|
|
240
|
+
if (!trackedInit)
|
|
241
|
+
return;
|
|
242
|
+
if (isVarGuarded(obj.name))
|
|
243
|
+
return;
|
|
244
|
+
context.report({
|
|
245
|
+
node: callee,
|
|
246
|
+
messageId: 'unguardedQueryStringMethod',
|
|
247
|
+
data: { target: obj.name, method },
|
|
248
|
+
suggest: [
|
|
249
|
+
{
|
|
250
|
+
messageId: 'coerceAtAssignment',
|
|
251
|
+
fix: (fixer) => wrapWithString(fixer, trackedInit),
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
});
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.requireRateLimiting = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -16,20 +11,15 @@ const RATE_LIMIT_PACKAGES = [
|
|
|
16
11
|
'slowDown',
|
|
17
12
|
'expressBrute',
|
|
18
13
|
];
|
|
19
|
-
/**
|
|
20
|
-
* Check if a node is a rate limiting middleware usage
|
|
21
|
-
*/
|
|
22
14
|
function isRateLimitMiddleware(node, alternatives) {
|
|
23
15
|
const allPatterns = [...RATE_LIMIT_PACKAGES, ...alternatives];
|
|
24
16
|
for (const arg of node.arguments) {
|
|
25
|
-
// rateLimit() or limiter()
|
|
26
17
|
if (arg.type === 'CallExpression' && arg.callee.type === 'Identifier') {
|
|
27
18
|
const calleeName = arg.callee.name.toLowerCase();
|
|
28
19
|
if (allPatterns.some((p) => calleeName.includes(p.toLowerCase()))) {
|
|
29
20
|
return true;
|
|
30
21
|
}
|
|
31
22
|
}
|
|
32
|
-
// rateLimit identifier without call
|
|
33
23
|
if (arg.type === 'Identifier') {
|
|
34
24
|
if (allPatterns.some((p) => arg.name.toLowerCase().includes(p.toLowerCase()))) {
|
|
35
25
|
return true;
|
|
@@ -100,7 +90,6 @@ exports.requireRateLimiting = (0, eslint_devkit_1.createRule)({
|
|
|
100
90
|
],
|
|
101
91
|
create(context, [options]) {
|
|
102
92
|
const { allowInTests = false, alternativeMiddleware = [], assumeRateLimiting = false } = options;
|
|
103
|
-
// Skip entirely if rate limiting is assumed (provided by infrastructure)
|
|
104
93
|
if (assumeRateLimiting) {
|
|
105
94
|
return {};
|
|
106
95
|
}
|
|
@@ -115,13 +104,11 @@ exports.requireRateLimiting = (0, eslint_devkit_1.createRule)({
|
|
|
115
104
|
return {
|
|
116
105
|
CallExpression(node) {
|
|
117
106
|
const callee = node.callee;
|
|
118
|
-
// Check for express() app creation
|
|
119
107
|
if (callee.type === 'Identifier' && callee.name === 'express') {
|
|
120
108
|
hasExpressApp = true;
|
|
121
109
|
expressAppNode = node;
|
|
122
110
|
return;
|
|
123
111
|
}
|
|
124
|
-
// Check for app.use() with rate limiting
|
|
125
112
|
if (callee.type === 'MemberExpression' &&
|
|
126
113
|
callee.property.type === 'Identifier' &&
|
|
127
114
|
callee.property.name === 'use') {
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireRouteAuthentication = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
const DEFAULT_CRITICAL_PATHS = [
|
|
7
|
+
'password',
|
|
8
|
+
'credential',
|
|
9
|
+
'secret',
|
|
10
|
+
'apikey',
|
|
11
|
+
'api-key',
|
|
12
|
+
'user',
|
|
13
|
+
'account',
|
|
14
|
+
'profile',
|
|
15
|
+
'role',
|
|
16
|
+
'permission',
|
|
17
|
+
'grant',
|
|
18
|
+
'payment',
|
|
19
|
+
'billing',
|
|
20
|
+
'invoice',
|
|
21
|
+
'charge',
|
|
22
|
+
'refund',
|
|
23
|
+
'transfer',
|
|
24
|
+
'withdraw',
|
|
25
|
+
'order',
|
|
26
|
+
'config',
|
|
27
|
+
'setting',
|
|
28
|
+
'export',
|
|
29
|
+
'import',
|
|
30
|
+
'mfa',
|
|
31
|
+
'2fa',
|
|
32
|
+
'otp',
|
|
33
|
+
'deploy',
|
|
34
|
+
'migrate',
|
|
35
|
+
'backup',
|
|
36
|
+
'restore',
|
|
37
|
+
'internal',
|
|
38
|
+
'private',
|
|
39
|
+
];
|
|
40
|
+
const DEFAULT_PUBLIC_PATHS = [
|
|
41
|
+
'login',
|
|
42
|
+
'signin',
|
|
43
|
+
'sign-in',
|
|
44
|
+
'logout',
|
|
45
|
+
'signup',
|
|
46
|
+
'sign-up',
|
|
47
|
+
'register',
|
|
48
|
+
'forgot',
|
|
49
|
+
'reset',
|
|
50
|
+
'confirm',
|
|
51
|
+
'verify',
|
|
52
|
+
'webhook',
|
|
53
|
+
'hook',
|
|
54
|
+
'health',
|
|
55
|
+
'healthz',
|
|
56
|
+
'readyz',
|
|
57
|
+
'livez',
|
|
58
|
+
'ping',
|
|
59
|
+
'status',
|
|
60
|
+
'public',
|
|
61
|
+
'static',
|
|
62
|
+
'assets',
|
|
63
|
+
'favicon',
|
|
64
|
+
'robots',
|
|
65
|
+
'callback',
|
|
66
|
+
'oauth',
|
|
67
|
+
'sso',
|
|
68
|
+
'docs',
|
|
69
|
+
'swagger',
|
|
70
|
+
'openapi',
|
|
71
|
+
];
|
|
72
|
+
const ROUTE_METHODS = new Set([
|
|
73
|
+
'get',
|
|
74
|
+
'post',
|
|
75
|
+
'put',
|
|
76
|
+
'patch',
|
|
77
|
+
'delete',
|
|
78
|
+
'all',
|
|
79
|
+
]);
|
|
80
|
+
const APP_RECEIVER = /^(app|router|express|api)$/i;
|
|
81
|
+
function matchesFragment(path, fragment) {
|
|
82
|
+
const escaped = fragment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
83
|
+
return new RegExp(`(^|[^a-z0-9])${escaped}s?([^a-z0-9]|$)`).test(path);
|
|
84
|
+
}
|
|
85
|
+
const AUTH_MIDDLEWARE = /auth|jwt|passport|token|session|guard|protect|ensure|require|permit|acl|rbac|verify|login|identity|principal/i;
|
|
86
|
+
exports.requireRouteAuthentication = (0, eslint_devkit_1.createRule)({
|
|
87
|
+
name: 'require-route-authentication',
|
|
88
|
+
meta: {
|
|
89
|
+
type: 'problem',
|
|
90
|
+
docs: {
|
|
91
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-route-authentication.md',
|
|
92
|
+
description: 'Require authentication on routes that expose a critical function (account, credential, payment, configuration)',
|
|
93
|
+
cwe: 'CWE-306',
|
|
94
|
+
cvss: 8.2,
|
|
95
|
+
confidence: 'medium',
|
|
96
|
+
},
|
|
97
|
+
messages: {
|
|
98
|
+
missingAuthentication: (0, eslint_devkit_1.formatLLMMessage)({
|
|
99
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
100
|
+
issueName: 'Critical Route Without Authentication (CWE-306)',
|
|
101
|
+
cwe: 'CWE-306',
|
|
102
|
+
cvss: 8.2,
|
|
103
|
+
description: '{{method}} {{path}} exposes a critical function but its chain has no authentication middleware and the handler never reads an authenticated principal. Anyone who can reach the port can call it.',
|
|
104
|
+
severity: 'HIGH',
|
|
105
|
+
fix: "Mount an authentication middleware on the route — app.{{method}}('{{path}}', requireAuth, handler) — or app.use(requireAuth) for the whole router.",
|
|
106
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/306.html',
|
|
107
|
+
}),
|
|
108
|
+
},
|
|
109
|
+
schema: [
|
|
110
|
+
{
|
|
111
|
+
type: 'object',
|
|
112
|
+
properties: {
|
|
113
|
+
criticalPaths: {
|
|
114
|
+
type: 'array',
|
|
115
|
+
items: { type: 'string' },
|
|
116
|
+
description: 'Path fragments that mark a route as critical',
|
|
117
|
+
},
|
|
118
|
+
publicPaths: {
|
|
119
|
+
type: 'array',
|
|
120
|
+
items: { type: 'string' },
|
|
121
|
+
description: 'Path fragments that are public by design',
|
|
122
|
+
},
|
|
123
|
+
authMiddleware: {
|
|
124
|
+
type: 'array',
|
|
125
|
+
items: { type: 'string' },
|
|
126
|
+
description: 'Extra middleware names accepted as authentication',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
additionalProperties: false,
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
},
|
|
133
|
+
defaultOptions: [{}],
|
|
134
|
+
create(context, [options]) {
|
|
135
|
+
const { criticalPaths, publicPaths, authMiddleware } = options;
|
|
136
|
+
const critical = (criticalPaths ?? DEFAULT_CRITICAL_PATHS).map((p) => p.toLowerCase());
|
|
137
|
+
const publics = (publicPaths ?? DEFAULT_PUBLIC_PATHS).map((p) => p.toLowerCase());
|
|
138
|
+
const extraAuth = new Set(authMiddleware ?? []);
|
|
139
|
+
const candidates = [];
|
|
140
|
+
let hasGlobalAuth = false;
|
|
141
|
+
function isAuthMiddlewareArg(arg) {
|
|
142
|
+
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
143
|
+
arg.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
144
|
+
arg.callee.name === 'require') {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
const text = context.sourceCode.getText(arg);
|
|
148
|
+
if (AUTH_MIDDLEWARE.test(text))
|
|
149
|
+
return true;
|
|
150
|
+
for (const name of extraAuth) {
|
|
151
|
+
if (text.includes(name))
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
function routeMethodOf(node) {
|
|
157
|
+
const callee = node.callee;
|
|
158
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
159
|
+
return null;
|
|
160
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
161
|
+
return null;
|
|
162
|
+
if (callee.computed)
|
|
163
|
+
return null;
|
|
164
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
165
|
+
return null;
|
|
166
|
+
if (!APP_RECEIVER.test(callee.object.name))
|
|
167
|
+
return null;
|
|
168
|
+
return callee.property.name;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
CallExpression(node) {
|
|
172
|
+
const method = routeMethodOf(node);
|
|
173
|
+
if (!method)
|
|
174
|
+
return;
|
|
175
|
+
if (method === 'use') {
|
|
176
|
+
const [first] = node.arguments;
|
|
177
|
+
const pathScoped = first?.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
178
|
+
typeof first.value === 'string';
|
|
179
|
+
if (!pathScoped) {
|
|
180
|
+
for (const arg of node.arguments) {
|
|
181
|
+
if (arg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
182
|
+
isAuthMiddlewareArg(arg)) {
|
|
183
|
+
hasGlobalAuth = true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (!ROUTE_METHODS.has(method))
|
|
190
|
+
return;
|
|
191
|
+
const [pathArg, ...rest] = node.arguments;
|
|
192
|
+
if (!pathArg ||
|
|
193
|
+
pathArg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal ||
|
|
194
|
+
typeof pathArg.value !== 'string') {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (rest.length === 0)
|
|
198
|
+
return;
|
|
199
|
+
const path = pathArg.value.toLowerCase();
|
|
200
|
+
if (publics.some((fragment) => matchesFragment(path, fragment)))
|
|
201
|
+
return;
|
|
202
|
+
if (!critical.some((fragment) => matchesFragment(path, fragment)))
|
|
203
|
+
return;
|
|
204
|
+
const chain = rest.slice(0, -1);
|
|
205
|
+
if (chain.some((arg) => isAuthMiddlewareArg(arg)))
|
|
206
|
+
return;
|
|
207
|
+
const handler = rest[rest.length - 1];
|
|
208
|
+
if ((0, utils_1.readsPrincipal)(handler))
|
|
209
|
+
return;
|
|
210
|
+
candidates.push({ node: pathArg, method, path: pathArg.value });
|
|
211
|
+
},
|
|
212
|
+
'Program:exit'() {
|
|
213
|
+
if (hasGlobalAuth)
|
|
214
|
+
return;
|
|
215
|
+
for (const candidate of candidates) {
|
|
216
|
+
context.report({
|
|
217
|
+
node: candidate.node,
|
|
218
|
+
messageId: 'missingAuthentication',
|
|
219
|
+
data: { method: candidate.method, path: candidate.path },
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
},
|
|
225
|
+
});
|