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.
- package/package.json +2 -3
- package/src/index.js +1 -140
- package/src/oxlint.js +1 -3
- package/src/rules/no-client-controlled-authorization/index.js +1 -194
- package/src/rules/no-cors-credentials-wildcard/index.js +1 -127
- package/src/rules/no-disabled-helmet-protections/index.js +1 -125
- package/src/rules/no-error-details-in-response/index.js +1 -176
- package/src/rules/no-exposed-debug-endpoints/index.js +1 -96
- package/src/rules/no-express-unsafe-regex-route/index.js +1 -154
- package/src/rules/no-graphql-introspection-production/index.js +1 -143
- package/src/rules/no-host-header-in-links/index.js +1 -235
- package/src/rules/no-idor-resource-access/index.js +1 -126
- package/src/rules/no-insecure-cookie-options/index.js +1 -158
- package/src/rules/no-missing-cors-check/index.js +1 -351
- package/src/rules/no-missing-csrf-protection/index.js +1 -161
- package/src/rules/no-missing-security-headers/index.js +1 -206
- package/src/rules/no-permissive-cors/index.js +1 -185
- package/src/rules/no-permissive-trust-proxy/index.js +1 -106
- package/src/rules/no-sensitive-data-in-query/index.js +1 -182
- package/src/rules/no-static-root-exposure/index.js +1 -267
- package/src/rules/no-unsafe-csp-directives/index.js +1 -264
- package/src/rules/no-user-controlled-redirect/index.js +1 -117
- package/src/rules/no-user-controlled-render-locals/index.js +1 -268
- package/src/rules/require-case-insensitive-path-guard/index.js +1 -180
- package/src/rules/require-csrf-protection/index.js +1 -201
- package/src/rules/require-express-body-parser-limits/index.js +1 -181
- package/src/rules/require-helmet/index.js +1 -166
- package/src/rules/require-query-type-guard/index.js +1 -258
- package/src/rules/require-rate-limiting/index.js +1 -136
- package/src/rules/require-route-authentication/index.js +1 -225
- package/src/rules/require-strict-transport-security/index.js +1 -171
- package/src/types/index.js +1 -2
- package/src/utils/index.js +1 -81
|
@@ -1,258 +1 @@
|
|
|
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
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireQueryTypeGuard=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const STRING_METHODS=new Set(["replace","toLowerCase","toUpperCase","trim","split","substring","slice","startsWith","endsWith","includes","match"]);const EQUALITY_OPS=new Set(["===","==","!==","!="]);exports.requireQueryTypeGuard=(0,eslint_devkit_1.createRule)({name:"require-query-type-guard",meta:{type:"problem",hasSuggestions:true,docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-query-type-guard.md",description:"Require a type guard or String() coercion before calling string methods on req.query values",cwe:"CWE-843",cvss:7.5},messages:{unguardedQueryStringMethod:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"req.query Type Confusion (CWE-843)",cwe:"CWE-843",description:".{{method}}() is called on {{target}}, but req.query values are string | string[] | ParsedQs \u2014 ?x=a&x=b yields an array, so this throws (DoS) or bypasses sanitization.",severity:"HIGH",fix:"Coerce with String(...) or guard with typeof value === 'string' / Array.isArray(value) before treating the query value as a string.",documentationLink:"https://cwe.mitre.org/data/definitions/843.html"}),wrapString:"Coerce the query value with String(...) before calling .{{method}}()",coerceAtAssignment:"Coerce at the assignment: wrap the req.query access in String(...)"},schema:[{type:"object",properties:{coercers:{type:"array",items:{type:"string"},description:"Callee names whose result is a safe string coercion"},validators:{type:"array",items:{type:"string"},description:"Callee / method names treated as type-safe sources"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{coercers,validators}=options;const coercerNames=new Set(coercers??["String"]);const validatorNames=new Set(validators??["parse","safeParse"]);const sourceCode=context.sourceCode;function newFrame(){return{tracked:new Map,guardedVars:new Set,guardedProps:new Set}}const frames=[newFrame()];const current=()=>frames[frames.length-1];function isRequestIdent(name){const lower=name.toLowerCase();return lower==="req"||lower==="request"}function isReqQuery(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&!node.computed&&node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&isRequestIdent(node.object.name)&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.property.name==="query"}function isQueryMember(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&isReqQuery(node.object)}function propKey(node){return sourceCode.getText(node.property)}function guardVar(name){for(let i=frames.length-1;i>=0;i--){if(frames[i].tracked.has(name)){frames[i].guardedVars.add(name);return}}current().guardedVars.add(name)}function markGuard(target){if(target.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){guardVar(target.name)}else if(isQueryMember(target)){current().guardedProps.add(propKey(target))}}function isVarGuarded(name){return frames.some(f=>f.guardedVars.has(name))}function isPropGuarded(key){return frames.some(f=>f.guardedProps.has(key))}function lookupTracked(name){for(let i=frames.length-1;i>=0;i--){const init=frames[i].tracked.get(name);if(init)return init}return null}function extractTypeofTarget(a,b){if(a.type===eslint_devkit_1.AST_NODE_TYPES.UnaryExpression&&a.operator==="typeof"&&b.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&b.value==="string"){return a.argument}return null}function isArrayIsArrayCall(node){const callee=node.callee;return callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.object.name==="Array"&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.property.name==="isArray"}function isSafeCall(node){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression)return false;const callee=node.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return coercerNames.has(callee.name)||validatorNames.has(callee.name)}if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return validatorNames.has(callee.property.name)}return false}function wrapWithString(fixer,target){return[fixer.insertTextBefore(target,"String("),fixer.insertTextAfter(target,")")]}function enterFunction(){frames.push(newFrame())}function exitFunction(){frames.pop()}return{"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression":enterFunction,"FunctionDeclaration:exit":exitFunction,"FunctionExpression:exit":exitFunction,"ArrowFunctionExpression:exit":exitFunction,VariableDeclarator(node){if(node.id.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;const init=node.init;if(!init)return;if(isQueryMember(init)){current().tracked.set(node.id.name,init)}},AssignmentExpression(node){if(node.operator!=="=")return;if(node.left.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;if(isQueryMember(node.right)){current().tracked.set(node.left.name,node.right);return}if(isSafeCall(node.right)){guardVar(node.left.name)}},BinaryExpression(node){if(!EQUALITY_OPS.has(node.operator))return;const target=extractTypeofTarget(node.left,node.right)??extractTypeofTarget(node.right,node.left);if(target)markGuard(target)},CallExpression(node){if(isArrayIsArrayCall(node)){const arg=node.arguments[0];if(arg)markGuard(arg);return}const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;const method=callee.property.name;if(!STRING_METHODS.has(method))return;const obj=callee.object;if(isQueryMember(obj)){if(isPropGuarded(propKey(obj)))return;context.report({node:callee,messageId:"unguardedQueryStringMethod",data:{target:sourceCode.getText(obj),method},suggest:[{messageId:"wrapString",data:{method},fix:fixer=>wrapWithString(fixer,obj)}]});return}if(obj.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;const trackedInit=lookupTracked(obj.name);if(!trackedInit)return;if(isVarGuarded(obj.name))return;context.report({node:callee,messageId:"unguardedQueryStringMethod",data:{target:obj.name,method},suggest:[{messageId:"coerceAtAssignment",fix:fixer=>wrapWithString(fixer,trackedInit)}]})}}}});
|
|
@@ -1,136 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.requireRateLimiting = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const RATE_LIMIT_PACKAGES = [
|
|
6
|
-
'ratelimit',
|
|
7
|
-
'rateLimit',
|
|
8
|
-
'rateLimiter',
|
|
9
|
-
'limiter',
|
|
10
|
-
'expressRateLimit',
|
|
11
|
-
'slowDown',
|
|
12
|
-
'expressBrute',
|
|
13
|
-
];
|
|
14
|
-
function isRateLimitMiddleware(node, alternatives) {
|
|
15
|
-
const allPatterns = [...RATE_LIMIT_PACKAGES, ...alternatives];
|
|
16
|
-
for (const arg of node.arguments) {
|
|
17
|
-
if (arg.type === 'CallExpression' && arg.callee.type === 'Identifier') {
|
|
18
|
-
const calleeName = arg.callee.name.toLowerCase();
|
|
19
|
-
if (allPatterns.some((p) => calleeName.includes(p.toLowerCase()))) {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
if (arg.type === 'Identifier') {
|
|
24
|
-
if (allPatterns.some((p) => arg.name.toLowerCase().includes(p.toLowerCase()))) {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
exports.requireRateLimiting = (0, eslint_devkit_1.createRule)({
|
|
32
|
-
name: 'require-rate-limiting',
|
|
33
|
-
meta: {
|
|
34
|
-
type: 'problem',
|
|
35
|
-
docs: {
|
|
36
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-rate-limiting.md',
|
|
37
|
-
description: 'Require rate limiting middleware in Express.js applications',
|
|
38
|
-
cwe: 'CWE-770',
|
|
39
|
-
cvss: 7.5,
|
|
40
|
-
},
|
|
41
|
-
hasSuggestions: true,
|
|
42
|
-
messages: {
|
|
43
|
-
missingRateLimiting: (0, eslint_devkit_1.formatLLMMessage)({
|
|
44
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
45
|
-
issueName: 'Missing Rate Limiting',
|
|
46
|
-
cwe: 'CWE-770',
|
|
47
|
-
description: 'Express app created without rate limiting. Vulnerable to DDoS and brute-force attacks.',
|
|
48
|
-
severity: 'HIGH',
|
|
49
|
-
fix: "Add rate limiting: npm install express-rate-limit; app.use(rateLimit({ windowMs: 15*60*1000, max: 100 }))",
|
|
50
|
-
documentationLink: 'https://www.npmjs.com/package/express-rate-limit',
|
|
51
|
-
}),
|
|
52
|
-
addRateLimiting: (0, eslint_devkit_1.formatLLMMessage)({
|
|
53
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
54
|
-
issueName: 'Add Rate Limiting',
|
|
55
|
-
description: 'Add rate limiting middleware to protect against abuse',
|
|
56
|
-
severity: 'LOW',
|
|
57
|
-
fix: "import rateLimit from 'express-rate-limit'; app.use(rateLimit({ windowMs: 15*60*1000, max: 100 }));",
|
|
58
|
-
documentationLink: 'https://www.npmjs.com/package/express-rate-limit',
|
|
59
|
-
}),
|
|
60
|
-
},
|
|
61
|
-
schema: [
|
|
62
|
-
{
|
|
63
|
-
type: 'object',
|
|
64
|
-
properties: {
|
|
65
|
-
allowInTests: {
|
|
66
|
-
type: 'boolean',
|
|
67
|
-
default: false,
|
|
68
|
-
},
|
|
69
|
-
alternativeMiddleware: {
|
|
70
|
-
type: 'array',
|
|
71
|
-
items: { type: 'string' },
|
|
72
|
-
default: [],
|
|
73
|
-
},
|
|
74
|
-
assumeRateLimiting: {
|
|
75
|
-
type: 'boolean',
|
|
76
|
-
default: false,
|
|
77
|
-
description: 'Skip if rate limiting is provided by infrastructure (API Gateway, nginx, etc.)',
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
additionalProperties: false,
|
|
81
|
-
},
|
|
82
|
-
],
|
|
83
|
-
},
|
|
84
|
-
defaultOptions: [
|
|
85
|
-
{
|
|
86
|
-
allowInTests: false,
|
|
87
|
-
alternativeMiddleware: [],
|
|
88
|
-
assumeRateLimiting: false,
|
|
89
|
-
},
|
|
90
|
-
],
|
|
91
|
-
create(context, [options]) {
|
|
92
|
-
const { allowInTests = false, alternativeMiddleware = [], assumeRateLimiting = false } = options;
|
|
93
|
-
if (assumeRateLimiting) {
|
|
94
|
-
return {};
|
|
95
|
-
}
|
|
96
|
-
const filename = context.filename;
|
|
97
|
-
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
98
|
-
if (isTestFile) {
|
|
99
|
-
return {};
|
|
100
|
-
}
|
|
101
|
-
let hasExpressApp = false;
|
|
102
|
-
let hasRateLimiting = false;
|
|
103
|
-
let expressAppNode = null;
|
|
104
|
-
return {
|
|
105
|
-
CallExpression(node) {
|
|
106
|
-
const callee = node.callee;
|
|
107
|
-
if (callee.type === 'Identifier' && callee.name === 'express') {
|
|
108
|
-
hasExpressApp = true;
|
|
109
|
-
expressAppNode = node;
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (callee.type === 'MemberExpression' &&
|
|
113
|
-
callee.property.type === 'Identifier' &&
|
|
114
|
-
callee.property.name === 'use') {
|
|
115
|
-
if (isRateLimitMiddleware(node, alternativeMiddleware)) {
|
|
116
|
-
hasRateLimiting = true;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
'Program:exit'() {
|
|
121
|
-
if (hasExpressApp && !hasRateLimiting && expressAppNode) {
|
|
122
|
-
context.report({
|
|
123
|
-
node: expressAppNode,
|
|
124
|
-
messageId: 'missingRateLimiting',
|
|
125
|
-
suggest: [
|
|
126
|
-
{
|
|
127
|
-
messageId: 'addRateLimiting',
|
|
128
|
-
fix: () => null,
|
|
129
|
-
},
|
|
130
|
-
],
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
},
|
|
136
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireRateLimiting=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const RATE_LIMIT_PACKAGES=["ratelimit","rateLimit","rateLimiter","limiter","expressRateLimit","slowDown","expressBrute"];function isRateLimitMiddleware(node,alternatives){const allPatterns=[...RATE_LIMIT_PACKAGES,...alternatives];for(const arg of node.arguments){if(arg.type==="CallExpression"&&arg.callee.type==="Identifier"){const calleeName=arg.callee.name.toLowerCase();if(allPatterns.some(p=>calleeName.includes(p.toLowerCase()))){return true}}if(arg.type==="Identifier"){if(allPatterns.some(p=>arg.name.toLowerCase().includes(p.toLowerCase()))){return true}}}return false}exports.requireRateLimiting=(0,eslint_devkit_1.createRule)({name:"require-rate-limiting",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-rate-limiting.md",description:"Require rate limiting middleware in Express.js applications",cwe:"CWE-770",cvss:7.5},hasSuggestions:true,messages:{missingRateLimiting:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing Rate Limiting",cwe:"CWE-770",description:"Express app created without rate limiting. Vulnerable to DDoS and brute-force attacks.",severity:"HIGH",fix:"Add rate limiting: npm install express-rate-limit; app.use(rateLimit({ windowMs: 15*60*1000, max: 100 }))",documentationLink:"https://www.npmjs.com/package/express-rate-limit"}),addRateLimiting:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add Rate Limiting",description:"Add rate limiting middleware to protect against abuse",severity:"LOW",fix:"import rateLimit from 'express-rate-limit'; app.use(rateLimit({ windowMs: 15*60*1000, max: 100 }));",documentationLink:"https://www.npmjs.com/package/express-rate-limit"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false},alternativeMiddleware:{type:"array",items:{type:"string"},default:[],description:"Extra middleware names that count as rate limiting"},assumeRateLimiting:{type:"boolean",default:false,description:"Skip if rate limiting is provided by infrastructure (API Gateway, nginx, etc.)"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,alternativeMiddleware:[],assumeRateLimiting:false}],create(context,[options]){const{allowInTests=false,alternativeMiddleware=[],assumeRateLimiting=false}=options;if(assumeRateLimiting){return{}}const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}let hasExpressApp=false;let hasRateLimiting=false;let expressAppNode=null;return{CallExpression(node){const callee=node.callee;if(callee.type==="Identifier"&&callee.name==="express"){hasExpressApp=true;expressAppNode=node;return}if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"&&callee.property.name==="use"){if(isRateLimitMiddleware(node,alternativeMiddleware)){hasRateLimiting=true}}},"Program:exit"(){if(hasExpressApp&&!hasRateLimiting&&expressAppNode){context.report({node:expressAppNode,messageId:"missingRateLimiting",suggest:[{messageId:"addRateLimiting",fix:()=>null}]})}}}}});
|
|
@@ -1,225 +1 @@
|
|
|
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
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireRouteAuthentication=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const DEFAULT_CRITICAL_PATHS=["password","credential","secret","apikey","api-key","user","account","profile","role","permission","grant","payment","billing","invoice","charge","refund","transfer","withdraw","order","config","setting","export","import","mfa","2fa","otp","deploy","migrate","backup","restore","internal","private"];const DEFAULT_PUBLIC_PATHS=["login","signin","sign-in","logout","signup","sign-up","register","forgot","reset","confirm","verify","webhook","hook","health","healthz","readyz","livez","ping","status","public","static","assets","favicon","robots","callback","oauth","sso","docs","swagger","openapi"];const ROUTE_METHODS=new Set(["get","post","put","patch","delete","all"]);const APP_RECEIVER=/^(app|router|express|api)$/i;function matchesFragment(path,fragment){const escaped=fragment.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`(^|[^a-z0-9])${escaped}s?([^a-z0-9]|$)`).test(path)}const AUTH_MIDDLEWARE=/auth|jwt|passport|token|session|guard|protect|ensure|require|permit|acl|rbac|verify|login|identity|principal/i;exports.requireRouteAuthentication=(0,eslint_devkit_1.createRule)({name:"require-route-authentication",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-route-authentication.md",description:"Require authentication on routes that expose a critical function (account, credential, payment, configuration)",cwe:"CWE-306",cvss:8.2,confidence:"medium"},messages:{missingAuthentication:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Critical Route Without Authentication (CWE-306)",cwe:"CWE-306",cvss:8.2,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.",severity:"HIGH",fix:"Mount an authentication middleware on the route \u2014 app.{{method}}('{{path}}', requireAuth, handler) \u2014 or app.use(requireAuth) for the whole router.",documentationLink:"https://cwe.mitre.org/data/definitions/306.html"})},schema:[{type:"object",properties:{criticalPaths:{type:"array",items:{type:"string"},description:"Path fragments that mark a route as critical"},publicPaths:{type:"array",items:{type:"string"},description:"Path fragments that are public by design"},authMiddleware:{type:"array",items:{type:"string"},description:"Extra middleware names accepted as authentication"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{criticalPaths,publicPaths,authMiddleware}=options;const critical=(criticalPaths??DEFAULT_CRITICAL_PATHS).map(p=>p.toLowerCase());const publics=(publicPaths??DEFAULT_PUBLIC_PATHS).map(p=>p.toLowerCase());const extraAuth=new Set(authMiddleware??[]);const candidates=[];let hasGlobalAuth=false;function isAuthMiddlewareArg(arg){if(arg.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&arg.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&arg.callee.name==="require"){return false}const text=context.sourceCode.getText(arg);if(AUTH_MIDDLEWARE.test(text))return true;for(const name of extraAuth){if(text.includes(name))return true}return false}function routeMethodOf(node){const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return null;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return null;if(callee.computed)return null;if(callee.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return null;if(!APP_RECEIVER.test(callee.object.name))return null;return callee.property.name}return{CallExpression(node){const method=routeMethodOf(node);if(!method)return;if(method==="use"){const[first]=node.arguments;const pathScoped=first?.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof first.value==="string";if(!pathScoped){for(const arg of node.arguments){if(arg.type!==eslint_devkit_1.AST_NODE_TYPES.Literal&&isAuthMiddlewareArg(arg)){hasGlobalAuth=true}}}return}if(!ROUTE_METHODS.has(method))return;const[pathArg,...rest]=node.arguments;if(!pathArg||pathArg.type!==eslint_devkit_1.AST_NODE_TYPES.Literal||typeof pathArg.value!=="string"){return}if(rest.length===0)return;const path=pathArg.value.toLowerCase();if(publics.some(fragment=>matchesFragment(path,fragment)))return;if(!critical.some(fragment=>matchesFragment(path,fragment)))return;const chain=rest.slice(0,-1);if(chain.some(arg=>isAuthMiddlewareArg(arg)))return;const handler=rest[rest.length-1];if((0,utils_1.readsPrincipal)(handler))return;candidates.push({node:pathArg,method,path:pathArg.value})},"Program:exit"(){if(hasGlobalAuth)return;for(const candidate of candidates){context.report({node:candidate.node,messageId:"missingAuthentication",data:{method:candidate.method,path:candidate.path}})}}}}});
|