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,268 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noUserControlledRenderLocals = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const USER_SOURCE_PROPS = new Set(['body', 'query', 'params']);
|
|
6
|
-
const RESPONSE_NAMES = new Set(['res', 'response', 'reply']);
|
|
7
|
-
const REQUEST_NAMES = new Set(['req', 'request', 'ctx']);
|
|
8
|
-
exports.noUserControlledRenderLocals = (0, eslint_devkit_1.createRule)({
|
|
9
|
-
name: 'no-user-controlled-render-locals',
|
|
10
|
-
meta: {
|
|
11
|
-
type: 'problem',
|
|
12
|
-
hasSuggestions: true,
|
|
13
|
-
docs: {
|
|
14
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-user-controlled-render-locals.md',
|
|
15
|
-
description: 'Disallow res.render() with locals or view names sourced wholesale from req.body / req.query / req.params',
|
|
16
|
-
cwe: 'CWE-73',
|
|
17
|
-
cvss: 7.3,
|
|
18
|
-
},
|
|
19
|
-
messages: {
|
|
20
|
-
unsafeRenderLocals: (0, eslint_devkit_1.formatLLMMessage)({
|
|
21
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
22
|
-
issueName: 'Template Object Injection (CWE-73)',
|
|
23
|
-
cwe: 'CWE-73',
|
|
24
|
-
description: 'res.render() receives locals wholly controlled by {{source}}. Express merges locals into the view-engine options, so keys like "layout", "settings" or "cache" can point the engine at an attacker-chosen file.',
|
|
25
|
-
severity: 'HIGH',
|
|
26
|
-
fix: 'Pick only the fields the template needs into an explicit object literal ({ title: req.body.title }) instead of forwarding the whole request object.',
|
|
27
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/73.html',
|
|
28
|
-
}),
|
|
29
|
-
userControlledView: (0, eslint_devkit_1.formatLLMMessage)({
|
|
30
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
31
|
-
issueName: 'User-Controlled View Path (CWE-73)',
|
|
32
|
-
cwe: 'CWE-73',
|
|
33
|
-
description: 'The view name passed to res.render() derives from {{source}}. An attacker can traverse into unintended templates or files within the views directory.',
|
|
34
|
-
severity: 'HIGH',
|
|
35
|
-
fix: 'Map user input to a fixed set of view names via an allowlist; never build the template path from request data.',
|
|
36
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/73.html',
|
|
37
|
-
}),
|
|
38
|
-
pickFieldsExplicitly: 'Remove the request-object spread and pick the required fields explicitly',
|
|
39
|
-
},
|
|
40
|
-
schema: [
|
|
41
|
-
{
|
|
42
|
-
type: 'object',
|
|
43
|
-
properties: {
|
|
44
|
-
allowSanitizers: {
|
|
45
|
-
type: 'array',
|
|
46
|
-
items: { type: 'string' },
|
|
47
|
-
description: 'Callee names that sanitize locals (e.g. ["pick", "sanitizeLocals"]); calls to them are not flagged',
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
additionalProperties: false,
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
},
|
|
54
|
-
defaultOptions: [{}],
|
|
55
|
-
create(context, [options]) {
|
|
56
|
-
const { allowSanitizers } = options;
|
|
57
|
-
const sanitizers = new Set(allowSanitizers ?? []);
|
|
58
|
-
const trackedVars = new Map();
|
|
59
|
-
function resolveVariable(node) {
|
|
60
|
-
let scope = context.sourceCode.getScope(node);
|
|
61
|
-
while (scope) {
|
|
62
|
-
const found = scope.variables.find((v) => v.name === node.name);
|
|
63
|
-
if (found)
|
|
64
|
-
return found;
|
|
65
|
-
scope = scope.upper;
|
|
66
|
-
}
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
function getTracked(node) {
|
|
70
|
-
const variable = resolveVariable(node);
|
|
71
|
-
return variable ? trackedVars.get(variable) : undefined;
|
|
72
|
-
}
|
|
73
|
-
function isRequestName(name) {
|
|
74
|
-
return REQUEST_NAMES.has(name.toLowerCase());
|
|
75
|
-
}
|
|
76
|
-
function getWholeSource(node) {
|
|
77
|
-
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
78
|
-
return null;
|
|
79
|
-
const { object, property } = node;
|
|
80
|
-
if (object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
81
|
-
return null;
|
|
82
|
-
if (property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
83
|
-
return null;
|
|
84
|
-
if (node.computed)
|
|
85
|
-
return null;
|
|
86
|
-
if (!USER_SOURCE_PROPS.has(property.name))
|
|
87
|
-
return null;
|
|
88
|
-
if (!isRequestName(object.name))
|
|
89
|
-
return null;
|
|
90
|
-
return `req.${property.name}`;
|
|
91
|
-
}
|
|
92
|
-
function isSanitizerCall(node) {
|
|
93
|
-
const callee = node.callee;
|
|
94
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
95
|
-
return sanitizers.has(callee.name);
|
|
96
|
-
}
|
|
97
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
98
|
-
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
99
|
-
return sanitizers.has(callee.property.name);
|
|
100
|
-
}
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
function getUnsafeLocalsSource(node) {
|
|
104
|
-
const direct = getWholeSource(node);
|
|
105
|
-
if (direct)
|
|
106
|
-
return direct;
|
|
107
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
108
|
-
const tracked = getTracked(node);
|
|
109
|
-
if (tracked && tracked.whole)
|
|
110
|
-
return tracked.source;
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
114
|
-
if (isSanitizerCall(node))
|
|
115
|
-
return null;
|
|
116
|
-
for (const arg of node.arguments) {
|
|
117
|
-
const argSource = getWholeSource(arg);
|
|
118
|
-
if (argSource)
|
|
119
|
-
return argSource;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
function getDerivedSource(node) {
|
|
125
|
-
const whole = getWholeSource(node);
|
|
126
|
-
if (whole)
|
|
127
|
-
return whole;
|
|
128
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
129
|
-
return getDerivedSource(node.object);
|
|
130
|
-
}
|
|
131
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
|
|
132
|
-
for (const expr of node.expressions) {
|
|
133
|
-
const inner = getDerivedSource(expr);
|
|
134
|
-
if (inner)
|
|
135
|
-
return inner;
|
|
136
|
-
}
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
|
|
140
|
-
if (node.operator !== '+')
|
|
141
|
-
return null;
|
|
142
|
-
const left = getDerivedSource(node.left);
|
|
143
|
-
if (left)
|
|
144
|
-
return left;
|
|
145
|
-
return getDerivedSource(node.right);
|
|
146
|
-
}
|
|
147
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
148
|
-
const tracked = getTracked(node);
|
|
149
|
-
if (tracked)
|
|
150
|
-
return tracked.source;
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
function isRenderCall(node) {
|
|
156
|
-
const callee = node.callee;
|
|
157
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
158
|
-
return false;
|
|
159
|
-
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
160
|
-
return false;
|
|
161
|
-
if (callee.property.name !== 'render')
|
|
162
|
-
return false;
|
|
163
|
-
const obj = callee.object;
|
|
164
|
-
if (obj.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
165
|
-
return false;
|
|
166
|
-
return RESPONSE_NAMES.has(obj.name.toLowerCase());
|
|
167
|
-
}
|
|
168
|
-
function buildRemoveSpreadFix(spread) {
|
|
169
|
-
return (fixer) => {
|
|
170
|
-
const sourceCode = context.sourceCode;
|
|
171
|
-
const tokenAfter = sourceCode.getTokenAfter(spread);
|
|
172
|
-
const tokenBefore = sourceCode.getTokenBefore(spread);
|
|
173
|
-
if (tokenAfter.value === ',') {
|
|
174
|
-
return fixer.removeRange([spread.range[0], tokenAfter.range[1]]);
|
|
175
|
-
}
|
|
176
|
-
if (tokenBefore.value === ',') {
|
|
177
|
-
return fixer.removeRange([tokenBefore.range[0], spread.range[1]]);
|
|
178
|
-
}
|
|
179
|
-
return fixer.remove(spread);
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
function checkLocalsArg(node) {
|
|
183
|
-
const source = getUnsafeLocalsSource(node);
|
|
184
|
-
if (source) {
|
|
185
|
-
context.report({
|
|
186
|
-
node,
|
|
187
|
-
messageId: 'unsafeRenderLocals',
|
|
188
|
-
data: { source },
|
|
189
|
-
});
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
|
|
193
|
-
return;
|
|
194
|
-
for (const prop of node.properties) {
|
|
195
|
-
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.SpreadElement)
|
|
196
|
-
continue;
|
|
197
|
-
const spreadSource = getUnsafeLocalsSource(prop.argument);
|
|
198
|
-
if (!spreadSource)
|
|
199
|
-
continue;
|
|
200
|
-
context.report({
|
|
201
|
-
node: prop,
|
|
202
|
-
messageId: 'unsafeRenderLocals',
|
|
203
|
-
data: { source: spreadSource },
|
|
204
|
-
suggest: [
|
|
205
|
-
{
|
|
206
|
-
messageId: 'pickFieldsExplicitly',
|
|
207
|
-
fix: buildRemoveSpreadFix(prop),
|
|
208
|
-
},
|
|
209
|
-
],
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
function trackDeclarator(declared, init) {
|
|
214
|
-
const unsafe = getUnsafeLocalsSource(init);
|
|
215
|
-
if (unsafe) {
|
|
216
|
-
trackedVars.set(declared, { source: unsafe, whole: true });
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (init.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
220
|
-
for (const prop of init.properties) {
|
|
221
|
-
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.SpreadElement)
|
|
222
|
-
continue;
|
|
223
|
-
const spreadSource = getUnsafeLocalsSource(prop.argument);
|
|
224
|
-
if (spreadSource) {
|
|
225
|
-
trackedVars.set(declared, { source: spreadSource, whole: true });
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
const derived = getDerivedSource(init);
|
|
232
|
-
if (derived) {
|
|
233
|
-
trackedVars.set(declared, { source: derived, whole: false });
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return {
|
|
237
|
-
VariableDeclarator(node) {
|
|
238
|
-
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
239
|
-
return;
|
|
240
|
-
const init = node.init;
|
|
241
|
-
if (!init)
|
|
242
|
-
return;
|
|
243
|
-
for (const declared of context.sourceCode.getDeclaredVariables(node)) {
|
|
244
|
-
trackDeclarator(declared, init);
|
|
245
|
-
}
|
|
246
|
-
},
|
|
247
|
-
CallExpression(node) {
|
|
248
|
-
if (!isRenderCall(node))
|
|
249
|
-
return;
|
|
250
|
-
const viewArg = node.arguments[0];
|
|
251
|
-
if (!viewArg)
|
|
252
|
-
return;
|
|
253
|
-
const viewSource = getDerivedSource(viewArg);
|
|
254
|
-
if (viewSource) {
|
|
255
|
-
context.report({
|
|
256
|
-
node: viewArg,
|
|
257
|
-
messageId: 'userControlledView',
|
|
258
|
-
data: { source: viewSource },
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
const localsArg = node.arguments[1];
|
|
262
|
-
if (!localsArg)
|
|
263
|
-
return;
|
|
264
|
-
checkLocalsArg(localsArg);
|
|
265
|
-
},
|
|
266
|
-
};
|
|
267
|
-
},
|
|
268
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUserControlledRenderLocals=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const USER_SOURCE_PROPS=new Set(["body","query","params"]);const RESPONSE_NAMES=new Set(["res","response","reply"]);const REQUEST_NAMES=new Set(["req","request","ctx"]);exports.noUserControlledRenderLocals=(0,eslint_devkit_1.createRule)({name:"no-user-controlled-render-locals",meta:{type:"problem",hasSuggestions:true,docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-user-controlled-render-locals.md",description:"Disallow res.render() with locals or view names sourced wholesale from req.body / req.query / req.params",cwe:"CWE-73",cvss:7.3},messages:{unsafeRenderLocals:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Template Object Injection (CWE-73)",cwe:"CWE-73",description:'res.render() receives locals wholly controlled by {{source}}. Express merges locals into the view-engine options, so keys like "layout", "settings" or "cache" can point the engine at an attacker-chosen file.',severity:"HIGH",fix:"Pick only the fields the template needs into an explicit object literal ({ title: req.body.title }) instead of forwarding the whole request object.",documentationLink:"https://cwe.mitre.org/data/definitions/73.html"}),userControlledView:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"User-Controlled View Path (CWE-73)",cwe:"CWE-73",description:"The view name passed to res.render() derives from {{source}}. An attacker can traverse into unintended templates or files within the views directory.",severity:"HIGH",fix:"Map user input to a fixed set of view names via an allowlist; never build the template path from request data.",documentationLink:"https://cwe.mitre.org/data/definitions/73.html"}),pickFieldsExplicitly:"Remove the request-object spread and pick the required fields explicitly"},schema:[{type:"object",properties:{allowSanitizers:{type:"array",items:{type:"string"},description:'Callee names that sanitize locals (e.g. ["pick", "sanitizeLocals"]); calls to them are not flagged'}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{allowSanitizers}=options;const sanitizers=new Set(allowSanitizers??[]);const trackedVars=new Map;function resolveVariable(node){let scope=context.sourceCode.getScope(node);while(scope){const found=scope.variables.find(v=>v.name===node.name);if(found)return found;scope=scope.upper}return null}function getTracked(node){const variable=resolveVariable(node);return variable?trackedVars.get(variable):void 0}function isRequestName(name){return REQUEST_NAMES.has(name.toLowerCase())}function getWholeSource(node){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return null;const{object,property}=node;if(object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return null;if(property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return null;if(node.computed)return null;if(!USER_SOURCE_PROPS.has(property.name))return null;if(!isRequestName(object.name))return null;return`req.${property.name}`}function isSanitizerCall(node){const callee=node.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return sanitizers.has(callee.name)}if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return sanitizers.has(callee.property.name)}return false}function getUnsafeLocalsSource(node){const direct=getWholeSource(node);if(direct)return direct;if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const tracked=getTracked(node);if(tracked&&tracked.whole)return tracked.source;return null}if(node.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){if(isSanitizerCall(node))return null;for(const arg of node.arguments){const argSource=getWholeSource(arg);if(argSource)return argSource}}return null}function getDerivedSource(node){const whole=getWholeSource(node);if(whole)return whole;if(node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){return getDerivedSource(node.object)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){for(const expr of node.expressions){const inner=getDerivedSource(expr);if(inner)return inner}return null}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression){if(node.operator!=="+")return null;const left=getDerivedSource(node.left);if(left)return left;return getDerivedSource(node.right)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const tracked=getTracked(node);if(tracked)return tracked.source;return null}return null}function isRenderCall(node){const callee=node.callee;if(callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return false;if(callee.property.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;if(callee.property.name!=="render")return false;const obj=callee.object;if(obj.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;return RESPONSE_NAMES.has(obj.name.toLowerCase())}function buildRemoveSpreadFix(spread){return fixer=>{const sourceCode=context.sourceCode;const tokenAfter=sourceCode.getTokenAfter(spread);const tokenBefore=sourceCode.getTokenBefore(spread);if(tokenAfter.value===","){return fixer.removeRange([spread.range[0],tokenAfter.range[1]])}if(tokenBefore.value===","){return fixer.removeRange([tokenBefore.range[0],spread.range[1]])}return fixer.remove(spread)}}function checkLocalsArg(node){const source=getUnsafeLocalsSource(node);if(source){context.report({node,messageId:"unsafeRenderLocals",data:{source}});return}if(node.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)return;for(const prop of node.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement)continue;const spreadSource=getUnsafeLocalsSource(prop.argument);if(!spreadSource)continue;context.report({node:prop,messageId:"unsafeRenderLocals",data:{source:spreadSource},suggest:[{messageId:"pickFieldsExplicitly",fix:buildRemoveSpreadFix(prop)}]})}}function trackDeclarator(declared,init){const unsafe=getUnsafeLocalsSource(init);if(unsafe){trackedVars.set(declared,{source:unsafe,whole:true});return}if(init.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression){for(const prop of init.properties){if(prop.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement)continue;const spreadSource=getUnsafeLocalsSource(prop.argument);if(spreadSource){trackedVars.set(declared,{source:spreadSource,whole:true});return}}return}const derived=getDerivedSource(init);if(derived){trackedVars.set(declared,{source:derived,whole:false})}}return{VariableDeclarator(node){if(node.id.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;const init=node.init;if(!init)return;for(const declared of context.sourceCode.getDeclaredVariables(node)){trackDeclarator(declared,init)}},CallExpression(node){if(!isRenderCall(node))return;const viewArg=node.arguments[0];if(!viewArg)return;const viewSource=getDerivedSource(viewArg);if(viewSource){context.report({node:viewArg,messageId:"userControlledView",data:{source:viewSource}})}const localsArg=node.arguments[1];if(!localsArg)return;checkLocalsArg(localsArg)}}}});
|
|
@@ -1,180 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.requireCaseInsensitivePathGuard = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const PATH_PROPS = new Set(['path', 'url', 'originalUrl']);
|
|
6
|
-
const GUARD_METHODS = new Set([
|
|
7
|
-
'startsWith',
|
|
8
|
-
'endsWith',
|
|
9
|
-
'includes',
|
|
10
|
-
'indexOf',
|
|
11
|
-
'match',
|
|
12
|
-
]);
|
|
13
|
-
const EQUALITY_OPS = new Set(['===', '==', '!==', '!=']);
|
|
14
|
-
function quoteLiteral(value) {
|
|
15
|
-
const json = JSON.stringify(value.toLowerCase());
|
|
16
|
-
const inner = json.slice(1, -1);
|
|
17
|
-
return /['\\]/.test(inner) ? json : `'${inner}'`;
|
|
18
|
-
}
|
|
19
|
-
const DEFAULT_PROTECTED_PATHS = [
|
|
20
|
-
'admin',
|
|
21
|
-
'api',
|
|
22
|
-
'dashboard',
|
|
23
|
-
'internal',
|
|
24
|
-
'private',
|
|
25
|
-
];
|
|
26
|
-
exports.requireCaseInsensitivePathGuard = (0, eslint_devkit_1.createRule)({
|
|
27
|
-
name: 'require-case-insensitive-path-guard',
|
|
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/require-case-insensitive-path-guard.md',
|
|
33
|
-
description: 'Require case-insensitive comparison when guarding protected paths via req.path / req.url / req.originalUrl',
|
|
34
|
-
cwe: 'CWE-178',
|
|
35
|
-
cvss: 7.3,
|
|
36
|
-
},
|
|
37
|
-
messages: {
|
|
38
|
-
caseSensitivePathGuard: (0, eslint_devkit_1.formatLLMMessage)({
|
|
39
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
40
|
-
issueName: 'Case-Sensitive Path Guard (CWE-178)',
|
|
41
|
-
cwe: 'CWE-178',
|
|
42
|
-
description: "{{pathExpr}} is compared case-sensitively against '{{value}}', but Express routes can match case-insensitively. A request to an upper-cased variant (e.g. /Admin) reaches the handler without ever hitting this guard.",
|
|
43
|
-
severity: 'HIGH',
|
|
44
|
-
fix: 'Normalize the path with .toLowerCase() before comparing (and lower-case the compared literal), or add the /i flag to the guard regex.',
|
|
45
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/178.html',
|
|
46
|
-
}),
|
|
47
|
-
addToLowerCase: 'Normalize with .toLowerCase() before comparing',
|
|
48
|
-
addIgnoreCaseFlag: 'Add the i flag to the guard regex',
|
|
49
|
-
},
|
|
50
|
-
schema: [
|
|
51
|
-
{
|
|
52
|
-
type: 'object',
|
|
53
|
-
properties: {
|
|
54
|
-
protectedPaths: {
|
|
55
|
-
type: 'array',
|
|
56
|
-
items: { type: 'string' },
|
|
57
|
-
description: 'Substrings that mark a compared value as a protected path',
|
|
58
|
-
},
|
|
59
|
-
checkAllPaths: {
|
|
60
|
-
type: 'boolean',
|
|
61
|
-
description: 'Flag every case-sensitive path guard regardless of the compared value',
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
additionalProperties: false,
|
|
65
|
-
},
|
|
66
|
-
],
|
|
67
|
-
},
|
|
68
|
-
defaultOptions: [{}],
|
|
69
|
-
create(context, [options]) {
|
|
70
|
-
const { protectedPaths, checkAllPaths } = options;
|
|
71
|
-
const patterns = (protectedPaths ?? DEFAULT_PROTECTED_PATHS).map((p) => p.toLowerCase());
|
|
72
|
-
const flagAll = checkAllPaths ?? false;
|
|
73
|
-
const sourceCode = context.sourceCode;
|
|
74
|
-
function isRequestIdent(name) {
|
|
75
|
-
const lower = name.toLowerCase();
|
|
76
|
-
return lower === 'req' || lower === 'request';
|
|
77
|
-
}
|
|
78
|
-
function isPathAccess(node) {
|
|
79
|
-
return (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
80
|
-
!node.computed &&
|
|
81
|
-
node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
82
|
-
isRequestIdent(node.object.name) &&
|
|
83
|
-
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
84
|
-
PATH_PROPS.has(node.property.name));
|
|
85
|
-
}
|
|
86
|
-
function isProtectedValue(value) {
|
|
87
|
-
if (flagAll)
|
|
88
|
-
return true;
|
|
89
|
-
const lower = value.toLowerCase();
|
|
90
|
-
return patterns.some((p) => lower.includes(p));
|
|
91
|
-
}
|
|
92
|
-
function isStringLiteral(node) {
|
|
93
|
-
return (node.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof node.value === 'string');
|
|
94
|
-
}
|
|
95
|
-
function reportWithLowerCaseSuggestion(node, pathNode, literalNode) {
|
|
96
|
-
const value = literalNode.value;
|
|
97
|
-
context.report({
|
|
98
|
-
node,
|
|
99
|
-
messageId: 'caseSensitivePathGuard',
|
|
100
|
-
data: { pathExpr: sourceCode.getText(pathNode), value },
|
|
101
|
-
suggest: [
|
|
102
|
-
{
|
|
103
|
-
messageId: 'addToLowerCase',
|
|
104
|
-
fix(fixer) {
|
|
105
|
-
const fixes = [fixer.insertTextAfter(pathNode, '.toLowerCase()')];
|
|
106
|
-
if (/[A-Z]/.test(value)) {
|
|
107
|
-
fixes.push(fixer.replaceText(literalNode, quoteLiteral(value)));
|
|
108
|
-
}
|
|
109
|
-
return fixes;
|
|
110
|
-
},
|
|
111
|
-
},
|
|
112
|
-
],
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return {
|
|
116
|
-
CallExpression(node) {
|
|
117
|
-
const callee = node.callee;
|
|
118
|
-
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
119
|
-
return;
|
|
120
|
-
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
121
|
-
return;
|
|
122
|
-
if (!GUARD_METHODS.has(callee.property.name))
|
|
123
|
-
return;
|
|
124
|
-
if (!isPathAccess(callee.object))
|
|
125
|
-
return;
|
|
126
|
-
const arg = node.arguments[0];
|
|
127
|
-
if (!arg)
|
|
128
|
-
return;
|
|
129
|
-
if (arg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal)
|
|
130
|
-
return;
|
|
131
|
-
if ('regex' in arg) {
|
|
132
|
-
if (arg.regex.flags.includes('i'))
|
|
133
|
-
return;
|
|
134
|
-
if (!isProtectedValue(arg.regex.pattern))
|
|
135
|
-
return;
|
|
136
|
-
context.report({
|
|
137
|
-
node,
|
|
138
|
-
messageId: 'caseSensitivePathGuard',
|
|
139
|
-
data: {
|
|
140
|
-
pathExpr: sourceCode.getText(callee.object),
|
|
141
|
-
value: arg.regex.pattern,
|
|
142
|
-
},
|
|
143
|
-
suggest: [
|
|
144
|
-
{
|
|
145
|
-
messageId: 'addIgnoreCaseFlag',
|
|
146
|
-
fix: (fixer) => fixer.insertTextAfter(arg, 'i'),
|
|
147
|
-
},
|
|
148
|
-
],
|
|
149
|
-
});
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
if (typeof arg.value !== 'string')
|
|
153
|
-
return;
|
|
154
|
-
if (!isProtectedValue(arg.value))
|
|
155
|
-
return;
|
|
156
|
-
reportWithLowerCaseSuggestion(node, callee.object, arg);
|
|
157
|
-
},
|
|
158
|
-
BinaryExpression(node) {
|
|
159
|
-
if (!EQUALITY_OPS.has(node.operator))
|
|
160
|
-
return;
|
|
161
|
-
let pathNode;
|
|
162
|
-
let literalNode;
|
|
163
|
-
if (isPathAccess(node.left) && isStringLiteral(node.right)) {
|
|
164
|
-
pathNode = node.left;
|
|
165
|
-
literalNode = node.right;
|
|
166
|
-
}
|
|
167
|
-
else if (isPathAccess(node.right) && isStringLiteral(node.left)) {
|
|
168
|
-
pathNode = node.right;
|
|
169
|
-
literalNode = node.left;
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
if (!isProtectedValue(literalNode.value))
|
|
175
|
-
return;
|
|
176
|
-
reportWithLowerCaseSuggestion(node, pathNode, literalNode);
|
|
177
|
-
},
|
|
178
|
-
};
|
|
179
|
-
},
|
|
180
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireCaseInsensitivePathGuard=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const PATH_PROPS=new Set(["path","url","originalUrl"]);const GUARD_METHODS=new Set(["startsWith","endsWith","includes","indexOf","match"]);const EQUALITY_OPS=new Set(["===","==","!==","!="]);function quoteLiteral(value){const json=JSON.stringify(value.toLowerCase());const inner=json.slice(1,-1);return/['\\]/.test(inner)?json:`'${inner}'`}const DEFAULT_PROTECTED_PATHS=["admin","api","dashboard","internal","private"];exports.requireCaseInsensitivePathGuard=(0,eslint_devkit_1.createRule)({name:"require-case-insensitive-path-guard",meta:{type:"problem",hasSuggestions:true,docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-case-insensitive-path-guard.md",description:"Require case-insensitive comparison when guarding protected paths via req.path / req.url / req.originalUrl",cwe:"CWE-178",cvss:7.3},messages:{caseSensitivePathGuard:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Case-Sensitive Path Guard (CWE-178)",cwe:"CWE-178",description:"{{pathExpr}} is compared case-sensitively against '{{value}}', but Express routes can match case-insensitively. A request to an upper-cased variant (e.g. /Admin) reaches the handler without ever hitting this guard.",severity:"HIGH",fix:"Normalize the path with .toLowerCase() before comparing (and lower-case the compared literal), or add the /i flag to the guard regex.",documentationLink:"https://cwe.mitre.org/data/definitions/178.html"}),addToLowerCase:"Normalize with .toLowerCase() before comparing",addIgnoreCaseFlag:"Add the i flag to the guard regex"},schema:[{type:"object",properties:{protectedPaths:{type:"array",items:{type:"string"},description:"Substrings that mark a compared value as a protected path"},checkAllPaths:{type:"boolean",description:"Flag every case-sensitive path guard regardless of the compared value"}},additionalProperties:false}]},defaultOptions:[{}],create(context,[options]){const{protectedPaths,checkAllPaths}=options;const patterns=(protectedPaths??DEFAULT_PROTECTED_PATHS).map(p=>p.toLowerCase());const flagAll=checkAllPaths??false;const sourceCode=context.sourceCode;function isRequestIdent(name){const lower=name.toLowerCase();return lower==="req"||lower==="request"}function isPathAccess(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&&PATH_PROPS.has(node.property.name)}function isProtectedValue(value){if(flagAll)return true;const lower=value.toLowerCase();return patterns.some(p=>lower.includes(p))}function isStringLiteral(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof node.value==="string"}function reportWithLowerCaseSuggestion(node,pathNode,literalNode){const value=literalNode.value;context.report({node,messageId:"caseSensitivePathGuard",data:{pathExpr:sourceCode.getText(pathNode),value},suggest:[{messageId:"addToLowerCase",fix(fixer){const fixes=[fixer.insertTextAfter(pathNode,".toLowerCase()")];if(/[A-Z]/.test(value)){fixes.push(fixer.replaceText(literalNode,quoteLiteral(value)))}return fixes}}]})}return{CallExpression(node){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;if(!GUARD_METHODS.has(callee.property.name))return;if(!isPathAccess(callee.object))return;const arg=node.arguments[0];if(!arg)return;if(arg.type!==eslint_devkit_1.AST_NODE_TYPES.Literal)return;if("regex"in arg){if(arg.regex.flags.includes("i"))return;if(!isProtectedValue(arg.regex.pattern))return;context.report({node,messageId:"caseSensitivePathGuard",data:{pathExpr:sourceCode.getText(callee.object),value:arg.regex.pattern},suggest:[{messageId:"addIgnoreCaseFlag",fix:fixer=>fixer.insertTextAfter(arg,"i")}]});return}if(typeof arg.value!=="string")return;if(!isProtectedValue(arg.value))return;reportWithLowerCaseSuggestion(node,callee.object,arg)},BinaryExpression(node){if(!EQUALITY_OPS.has(node.operator))return;let pathNode;let literalNode;if(isPathAccess(node.left)&&isStringLiteral(node.right)){pathNode=node.left;literalNode=node.right}else if(isPathAccess(node.right)&&isStringLiteral(node.left)){pathNode=node.right;literalNode=node.left}else{return}if(!isProtectedValue(literalNode.value))return;reportWithLowerCaseSuggestion(node,pathNode,literalNode)}}}});
|