eslint-plugin-express-security 1.3.3 → 1.4.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/CHANGELOG.md +52 -0
- package/README.md +47 -29
- package/package.json +24 -12
- package/src/index.js +26 -1
- package/src/rules/no-error-details-in-response/index.d.ts +57 -0
- package/src/rules/no-error-details-in-response/index.js +202 -0
- package/src/rules/no-exposed-debug-endpoints/index.js +36 -22
- package/src/rules/no-host-header-in-links/index.d.ts +56 -0
- package/src/rules/no-host-header-in-links/index.js +266 -0
- package/src/rules/no-missing-security-headers/index.js +64 -43
- package/src/rules/no-sensitive-data-in-query/index.d.ts +51 -0
- package/src/rules/no-sensitive-data-in-query/index.js +207 -0
- package/src/rules/no-static-root-exposure/index.d.ts +45 -0
- package/src/rules/no-static-root-exposure/index.js +288 -0
- package/src/rules/no-user-controlled-render-locals/index.d.ts +49 -0
- package/src/rules/no-user-controlled-render-locals/index.js +313 -0
- package/src/rules/require-case-insensitive-path-guard/index.d.ts +59 -0
- package/src/rules/require-case-insensitive-path-guard/index.js +201 -0
- package/src/rules/require-query-type-guard/index.d.ts +54 -0
- package/src/rules/require-query-type-guard/index.js +278 -0
|
@@ -0,0 +1,207 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.noSensitiveDataInQuery = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
/** Default sensitive parameter names (token-matched, not substring-matched). */
|
|
11
|
+
const DEFAULT_SENSITIVE = [
|
|
12
|
+
'password',
|
|
13
|
+
'token',
|
|
14
|
+
'secret',
|
|
15
|
+
'apiKey',
|
|
16
|
+
'api_key',
|
|
17
|
+
'auth',
|
|
18
|
+
'credential',
|
|
19
|
+
'ssn',
|
|
20
|
+
'card',
|
|
21
|
+
];
|
|
22
|
+
/** Split a parameter name into lowercase word tokens (camelCase + snake_case). */
|
|
23
|
+
/**
|
|
24
|
+
* Ceiling on the name length fed to config-supplied `extraPatterns`. Real query
|
|
25
|
+
* parameter names are far shorter; the cap denies a catastrophically
|
|
26
|
+
* backtracking pattern the long input it would need to amplify (CWE-1333).
|
|
27
|
+
*/
|
|
28
|
+
const MAX_PARAM_NAME_LENGTH = 128;
|
|
29
|
+
function tokenize(name) {
|
|
30
|
+
return name
|
|
31
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
32
|
+
.toLowerCase()
|
|
33
|
+
.split(/[^a-z0-9]+/)
|
|
34
|
+
.filter((token) => token.length > 0);
|
|
35
|
+
}
|
|
36
|
+
/** True when `tokens` contains `term` as a contiguous subsequence. */
|
|
37
|
+
function containsSubsequence(tokens, term) {
|
|
38
|
+
for (let i = 0; i + term.length <= tokens.length; i++) {
|
|
39
|
+
let match = true;
|
|
40
|
+
for (let j = 0; j < term.length; j++) {
|
|
41
|
+
if (tokens[i + j] !== term[j]) {
|
|
42
|
+
match = false;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (match)
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
exports.noSensitiveDataInQuery = (0, eslint_devkit_1.createRule)({
|
|
52
|
+
name: 'no-sensitive-data-in-query',
|
|
53
|
+
meta: {
|
|
54
|
+
type: 'problem',
|
|
55
|
+
hasSuggestions: true,
|
|
56
|
+
docs: {
|
|
57
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-sensitive-data-in-query.md',
|
|
58
|
+
description: 'Disallow reading sensitive-named parameters (password, token, secret, ...) from req.query',
|
|
59
|
+
cwe: 'CWE-598',
|
|
60
|
+
cvss: 6.5,
|
|
61
|
+
},
|
|
62
|
+
messages: {
|
|
63
|
+
sensitiveQueryParam: (0, eslint_devkit_1.formatLLMMessage)({
|
|
64
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
65
|
+
issueName: 'Sensitive Data in Query String (CWE-598)',
|
|
66
|
+
cwe: 'CWE-598',
|
|
67
|
+
description: "Query parameter '{{name}}' is sensitive. Query strings are stored in access logs, proxy logs, browser history and leak via the Referer header.",
|
|
68
|
+
severity: 'MEDIUM',
|
|
69
|
+
fix: 'Move the value to the POST request body (req.body) or an Authorization header; never accept secrets via the query string.',
|
|
70
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/598.html',
|
|
71
|
+
}),
|
|
72
|
+
moveToBody: (0, eslint_devkit_1.formatLLMMessage)({
|
|
73
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
74
|
+
issueName: 'Move the secret out of the query string',
|
|
75
|
+
description: 'Accept the value from the POST body or an Authorization header instead',
|
|
76
|
+
severity: 'LOW',
|
|
77
|
+
fix: "app.post('/login', (req, res) => { const { password } = req.body; })",
|
|
78
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/598.html',
|
|
79
|
+
}),
|
|
80
|
+
},
|
|
81
|
+
schema: [
|
|
82
|
+
{
|
|
83
|
+
type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
sensitiveParams: {
|
|
86
|
+
type: 'array',
|
|
87
|
+
items: { type: 'string' },
|
|
88
|
+
description: 'Additional sensitive parameter names (extends the defaults)',
|
|
89
|
+
},
|
|
90
|
+
extraPatterns: {
|
|
91
|
+
type: 'array',
|
|
92
|
+
items: { type: 'string' },
|
|
93
|
+
description: 'Additional regex patterns (case-insensitive) matched against the raw parameter name',
|
|
94
|
+
},
|
|
95
|
+
allowedParams: {
|
|
96
|
+
type: 'array',
|
|
97
|
+
items: { type: 'string' },
|
|
98
|
+
description: 'Parameter names explicitly allowed in the query string (exact, case-insensitive)',
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
additionalProperties: false,
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
defaultOptions: [{}],
|
|
106
|
+
create(context, [options]) {
|
|
107
|
+
const { sensitiveParams, extraPatterns, allowedParams } = options;
|
|
108
|
+
const terms = [...DEFAULT_SENSITIVE, ...(sensitiveParams ?? [])].map((term) => tokenize(term));
|
|
109
|
+
// Config-supplied patterns are compiled once and only ever matched against
|
|
110
|
+
// parameter names, which MAX_PARAM_NAME_LENGTH caps — a catastrophically
|
|
111
|
+
// backtracking pattern from an inherited config therefore has no input long
|
|
112
|
+
// enough to amplify against (CWE-1333).
|
|
113
|
+
// An invalid pattern would throw out of create() and abort the whole lint
|
|
114
|
+
// run for the file with an opaque error; a bad config entry is skipped
|
|
115
|
+
// instead, leaving the rest of the rule working.
|
|
116
|
+
const patterns = [];
|
|
117
|
+
for (const pattern of extraPatterns ?? []) {
|
|
118
|
+
try {
|
|
119
|
+
patterns.push(new RegExp(pattern, 'i'));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// not a valid regular expression — ignore this entry
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const allowedSet = new Set((allowedParams ?? []).map((name) => name.toLowerCase()));
|
|
126
|
+
function isRequestIdent(name) {
|
|
127
|
+
const lower = name.toLowerCase();
|
|
128
|
+
return lower === 'req' || lower === 'request' || lower === 'ctx';
|
|
129
|
+
}
|
|
130
|
+
/** True when `node` is `<req>.query` (non-computed). */
|
|
131
|
+
function isReqQuery(node) {
|
|
132
|
+
return (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
133
|
+
!node.computed &&
|
|
134
|
+
node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
135
|
+
isRequestIdent(node.object.name) &&
|
|
136
|
+
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
137
|
+
node.property.name === 'query');
|
|
138
|
+
}
|
|
139
|
+
function isSensitive(name) {
|
|
140
|
+
if (allowedSet.has(name.toLowerCase()))
|
|
141
|
+
return false;
|
|
142
|
+
const tokens = tokenize(name);
|
|
143
|
+
for (const term of terms) {
|
|
144
|
+
if (term.length === 1) {
|
|
145
|
+
const single = term[0];
|
|
146
|
+
// Exact token match, plus the trivial plural.
|
|
147
|
+
if (tokens.some((token) => token === single || token === `${single}s`)) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else if (containsSubsequence(tokens, term)) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (name.length > MAX_PARAM_NAME_LENGTH)
|
|
156
|
+
return false;
|
|
157
|
+
return patterns.some((pattern) => pattern.test(name));
|
|
158
|
+
}
|
|
159
|
+
function report(node, name) {
|
|
160
|
+
context.report({
|
|
161
|
+
node,
|
|
162
|
+
messageId: 'sensitiveQueryParam',
|
|
163
|
+
data: { name },
|
|
164
|
+
suggest: [{ messageId: 'moveToBody', fix: () => null }],
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
// req.query.password / req.query['api_key']
|
|
169
|
+
MemberExpression(node) {
|
|
170
|
+
if (!isReqQuery(node.object))
|
|
171
|
+
return;
|
|
172
|
+
let name = null;
|
|
173
|
+
if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
174
|
+
!node.computed) {
|
|
175
|
+
name = node.property.name;
|
|
176
|
+
}
|
|
177
|
+
else if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
178
|
+
typeof node.property.value === 'string') {
|
|
179
|
+
name = node.property.value;
|
|
180
|
+
}
|
|
181
|
+
if (name !== null && isSensitive(name))
|
|
182
|
+
report(node, name);
|
|
183
|
+
},
|
|
184
|
+
// const { password } = req.query;
|
|
185
|
+
VariableDeclarator(node) {
|
|
186
|
+
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectPattern)
|
|
187
|
+
return;
|
|
188
|
+
if (node.init === null || !isReqQuery(node.init))
|
|
189
|
+
return;
|
|
190
|
+
for (const prop of node.id.properties) {
|
|
191
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
192
|
+
continue; // RestElement
|
|
193
|
+
let name = null;
|
|
194
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && !prop.computed) {
|
|
195
|
+
name = prop.key.name;
|
|
196
|
+
}
|
|
197
|
+
else if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
198
|
+
typeof prop.key.value === 'string') {
|
|
199
|
+
name = prop.key.value;
|
|
200
|
+
}
|
|
201
|
+
if (name !== null && isSensitive(name))
|
|
202
|
+
report(prop, name);
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: no-static-root-exposure
|
|
8
|
+
*
|
|
9
|
+
* Detects static-file middleware that exposes the application root — serving
|
|
10
|
+
* __dirname, '.', '..', process.cwd(), or a path.join()/path.resolve() with
|
|
11
|
+
* no subdirectory or with '..' segments — plus any use of serve-index
|
|
12
|
+
* (directory listing). Serving the application root publishes .env,
|
|
13
|
+
* package-lock.json, .git metadata and the server source itself.
|
|
14
|
+
*
|
|
15
|
+
* CWE-548: Exposure of Information Through Directory Listing
|
|
16
|
+
* OWASP A05:2021 – Security Misconfiguration
|
|
17
|
+
*
|
|
18
|
+
* ## Detection method: structural-api
|
|
19
|
+
*
|
|
20
|
+
* This rule passes the litmus test: it fires on the AST shape of
|
|
21
|
+
* `express.static(<root expression>)` and `serveIndex(...)` — not on route
|
|
22
|
+
* strings or variable names. The safe pattern is a dedicated asset directory:
|
|
23
|
+
* `express.static(path.join(__dirname, 'public'))` (any allowlisted root).
|
|
24
|
+
*
|
|
25
|
+
* The rule does not resolve variables holding paths
|
|
26
|
+
* (`const root = __dirname; express.static(root)`) — a documented false
|
|
27
|
+
* negative kept to avoid taint analysis.
|
|
28
|
+
*
|
|
29
|
+
* @see https://cwe.mitre.org/data/definitions/548.html
|
|
30
|
+
* @see https://expressjs.com/en/4x/api.html#express.static
|
|
31
|
+
*/
|
|
32
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
33
|
+
type MessageIds = 'staticRoot' | 'traversalSegments' | 'nonLiteralPath' | 'unknownRoot' | 'directoryListing' | 'scopeToSubdir';
|
|
34
|
+
export interface Options {
|
|
35
|
+
/**
|
|
36
|
+
* Directory names accepted as the first path segment of a static root.
|
|
37
|
+
* Default: ['public', 'static', 'dist', 'build', 'assets']
|
|
38
|
+
*/
|
|
39
|
+
allowedRoots?: string[];
|
|
40
|
+
}
|
|
41
|
+
type RuleOptions = [Options?];
|
|
42
|
+
export declare const noStaticRootExposure: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
43
|
+
name: string;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,288 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.noStaticRootExposure = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
const DEFAULT_ALLOWED_ROOTS = ['public', 'static', 'dist', 'build', 'assets'];
|
|
11
|
+
/** path methods that assemble a root directory from segments. */
|
|
12
|
+
const JOIN_METHODS = new Set(['join', 'resolve']);
|
|
13
|
+
/**
|
|
14
|
+
* Quotes a config-supplied string for source output. Prefers single quotes,
|
|
15
|
+
* falling back to `JSON.stringify`'s escaped form when the value contains a
|
|
16
|
+
* quote or backslash — naive interpolation would emit invalid JavaScript.
|
|
17
|
+
*/
|
|
18
|
+
function quoteLiteral(value) {
|
|
19
|
+
const json = JSON.stringify(value);
|
|
20
|
+
const inner = json.slice(1, -1);
|
|
21
|
+
return /['\\]/.test(inner) ? json : `'${inner}'`;
|
|
22
|
+
}
|
|
23
|
+
exports.noStaticRootExposure = (0, eslint_devkit_1.createRule)({
|
|
24
|
+
name: 'no-static-root-exposure',
|
|
25
|
+
meta: {
|
|
26
|
+
type: 'problem',
|
|
27
|
+
hasSuggestions: true,
|
|
28
|
+
docs: {
|
|
29
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-static-root-exposure.md',
|
|
30
|
+
description: 'Disallow express.static() roots that expose the application directory and any serve-index usage',
|
|
31
|
+
cwe: 'CWE-548',
|
|
32
|
+
cvss: 7.5,
|
|
33
|
+
},
|
|
34
|
+
messages: {
|
|
35
|
+
staticRoot: (0, eslint_devkit_1.formatLLMMessage)({
|
|
36
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
37
|
+
issueName: 'Application Root Served Statically (CWE-548)',
|
|
38
|
+
cwe: 'CWE-548',
|
|
39
|
+
description: 'express.static() serves {{exposed}} — the application root. This publishes .env, package-lock.json, .git metadata and the server source itself.',
|
|
40
|
+
severity: 'HIGH',
|
|
41
|
+
fix: "Serve a dedicated asset directory instead: express.static(path.join(__dirname, 'public')).",
|
|
42
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
43
|
+
}),
|
|
44
|
+
traversalSegments: (0, eslint_devkit_1.formatLLMMessage)({
|
|
45
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
46
|
+
issueName: 'Static Root Escapes the Application Directory (CWE-548)',
|
|
47
|
+
cwe: 'CWE-548',
|
|
48
|
+
description: 'The static root contains a ".." segment, so files OUTSIDE the intended directory become publicly reachable.',
|
|
49
|
+
severity: 'HIGH',
|
|
50
|
+
fix: 'Point express.static() at a dedicated asset directory inside the project, with no ".." segments.',
|
|
51
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
52
|
+
}),
|
|
53
|
+
nonLiteralPath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
54
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
55
|
+
issueName: 'Dynamic Static-Root Segment (CWE-548)',
|
|
56
|
+
cwe: 'CWE-548',
|
|
57
|
+
description: 'The static root is built from a non-literal segment, so the served directory cannot be verified at lint time and may expose unintended files.',
|
|
58
|
+
severity: 'MEDIUM',
|
|
59
|
+
fix: "Use a string literal for every path segment: path.join(__dirname, 'public').",
|
|
60
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
61
|
+
}),
|
|
62
|
+
unknownRoot: (0, eslint_devkit_1.formatLLMMessage)({
|
|
63
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
64
|
+
issueName: 'Unrecognized Static-Assets Directory (CWE-548)',
|
|
65
|
+
cwe: 'CWE-548',
|
|
66
|
+
description: '"{{root}}" is not an allowlisted static-assets directory. Serving arbitrary directories risks exposing files that were never meant to be public.',
|
|
67
|
+
severity: 'MEDIUM',
|
|
68
|
+
fix: 'Serve a dedicated asset directory (public/, static/, dist/, build/, assets/) or add the directory to the allowedRoots option.',
|
|
69
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
70
|
+
}),
|
|
71
|
+
directoryListing: (0, eslint_devkit_1.formatLLMMessage)({
|
|
72
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
73
|
+
issueName: 'Directory Listing Enabled (CWE-548)',
|
|
74
|
+
cwe: 'CWE-548',
|
|
75
|
+
description: 'serve-index exposes a browsable listing of every file in the served directory, giving attackers a map of the filesystem.',
|
|
76
|
+
severity: 'HIGH',
|
|
77
|
+
fix: 'Remove the serve-index middleware; serve an explicit index file or a curated file list instead.',
|
|
78
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/548.html',
|
|
79
|
+
}),
|
|
80
|
+
scopeToSubdir: "Serve path.join(__dirname, '{{root}}') instead",
|
|
81
|
+
},
|
|
82
|
+
schema: [
|
|
83
|
+
{
|
|
84
|
+
type: 'object',
|
|
85
|
+
properties: {
|
|
86
|
+
allowedRoots: {
|
|
87
|
+
type: 'array',
|
|
88
|
+
items: { type: 'string' },
|
|
89
|
+
description: 'Directory names accepted as the first path segment of a static root',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
additionalProperties: false,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
defaultOptions: [{}],
|
|
97
|
+
create(context, [options]) {
|
|
98
|
+
const { allowedRoots } = options;
|
|
99
|
+
const roots = new Set(allowedRoots ?? DEFAULT_ALLOWED_ROOTS);
|
|
100
|
+
const suggestionRoot = (allowedRoots ?? DEFAULT_ALLOWED_ROOTS)[0] ?? 'public';
|
|
101
|
+
/** Local bindings of the serve-index package (require + import). */
|
|
102
|
+
const serveIndexNames = new Set(['serveIndex']);
|
|
103
|
+
function isDirnameNode(node) {
|
|
104
|
+
return (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.name === '__dirname');
|
|
105
|
+
}
|
|
106
|
+
function isProcessCwdCall(node) {
|
|
107
|
+
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
108
|
+
return false;
|
|
109
|
+
const callee = node.callee;
|
|
110
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
111
|
+
return false;
|
|
112
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
113
|
+
return false;
|
|
114
|
+
if (callee.object.name !== 'process')
|
|
115
|
+
return false;
|
|
116
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
117
|
+
return false;
|
|
118
|
+
return callee.property.name === 'cwd';
|
|
119
|
+
}
|
|
120
|
+
function isPathJoinCall(node) {
|
|
121
|
+
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
122
|
+
return false;
|
|
123
|
+
const callee = node.callee;
|
|
124
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
125
|
+
return false;
|
|
126
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
127
|
+
return false;
|
|
128
|
+
if (callee.object.name !== 'path')
|
|
129
|
+
return false;
|
|
130
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
131
|
+
return false;
|
|
132
|
+
return JOIN_METHODS.has(callee.property.name);
|
|
133
|
+
}
|
|
134
|
+
function isExpressStaticCall(node) {
|
|
135
|
+
const callee = node.callee;
|
|
136
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
137
|
+
return false;
|
|
138
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
139
|
+
return false;
|
|
140
|
+
if (callee.property.name !== 'static')
|
|
141
|
+
return false;
|
|
142
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
143
|
+
return false;
|
|
144
|
+
return callee.object.name.toLowerCase() === 'express';
|
|
145
|
+
}
|
|
146
|
+
/** Split a path string into meaningful segments ('' and '.' dropped). */
|
|
147
|
+
function segmentsOf(value) {
|
|
148
|
+
return value.split(/[\\/]+/).filter((seg) => seg !== '' && seg !== '.');
|
|
149
|
+
}
|
|
150
|
+
function reportRoot(node, exposed) {
|
|
151
|
+
context.report({
|
|
152
|
+
node,
|
|
153
|
+
messageId: 'staticRoot',
|
|
154
|
+
data: { exposed },
|
|
155
|
+
suggest: [
|
|
156
|
+
{
|
|
157
|
+
messageId: 'scopeToSubdir',
|
|
158
|
+
data: { root: suggestionRoot },
|
|
159
|
+
fix: (fixer) => fixer.replaceText(node, `path.join(__dirname, ${quoteLiteral(suggestionRoot)})`),
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/** Analyze the argument list of a path.join()/path.resolve() root. */
|
|
165
|
+
function checkJoinRoot(call) {
|
|
166
|
+
const literalSegments = [];
|
|
167
|
+
let hasNonLiteral = false;
|
|
168
|
+
let hasTraversal = false;
|
|
169
|
+
for (const arg of call.arguments) {
|
|
170
|
+
// __dirname / process.cwd() are anchors, not served subdirectories
|
|
171
|
+
if (isDirnameNode(arg) || isProcessCwdCall(arg))
|
|
172
|
+
continue;
|
|
173
|
+
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
174
|
+
typeof arg.value === 'string') {
|
|
175
|
+
const segments = segmentsOf(arg.value);
|
|
176
|
+
if (segments.includes('..'))
|
|
177
|
+
hasTraversal = true;
|
|
178
|
+
literalSegments.push(...segments);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
hasNonLiteral = true;
|
|
182
|
+
}
|
|
183
|
+
if (hasTraversal) {
|
|
184
|
+
context.report({ node: call, messageId: 'traversalSegments' });
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (hasNonLiteral) {
|
|
188
|
+
context.report({ node: call, messageId: 'nonLiteralPath' });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (literalSegments.length === 0) {
|
|
192
|
+
// path.join(__dirname) — anchors only, no subdirectory
|
|
193
|
+
reportRoot(call, context.sourceCode.getText(call));
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (!roots.has(literalSegments[0])) {
|
|
197
|
+
context.report({
|
|
198
|
+
node: call,
|
|
199
|
+
messageId: 'unknownRoot',
|
|
200
|
+
data: { root: literalSegments[0] },
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Analyze the first argument of express.static(). */
|
|
205
|
+
function checkStaticRoot(node) {
|
|
206
|
+
if (isDirnameNode(node)) {
|
|
207
|
+
reportRoot(node, '__dirname');
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (isProcessCwdCall(node)) {
|
|
211
|
+
reportRoot(node, 'process.cwd()');
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Literal) {
|
|
215
|
+
if (typeof node.value !== 'string')
|
|
216
|
+
return;
|
|
217
|
+
const segments = segmentsOf(node.value);
|
|
218
|
+
if (segments.includes('..')) {
|
|
219
|
+
context.report({ node, messageId: 'traversalSegments' });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (segments.length === 0) {
|
|
223
|
+
// '', '.', './', '/'
|
|
224
|
+
reportRoot(node, `'${node.value}'`);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (!roots.has(segments[0])) {
|
|
228
|
+
context.report({
|
|
229
|
+
node,
|
|
230
|
+
messageId: 'unknownRoot',
|
|
231
|
+
data: { root: segments[0] },
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (isPathJoinCall(node)) {
|
|
237
|
+
checkJoinRoot(node);
|
|
238
|
+
}
|
|
239
|
+
// Anything else (variables, template literals) is not analyzed —
|
|
240
|
+
// documented false negative to avoid taint analysis.
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
ImportDeclaration(node) {
|
|
244
|
+
if (node.source.value !== 'serve-index')
|
|
245
|
+
return;
|
|
246
|
+
for (const specifier of node.specifiers) {
|
|
247
|
+
serveIndexNames.add(specifier.local.name);
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
VariableDeclarator(node) {
|
|
251
|
+
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
252
|
+
return;
|
|
253
|
+
if (!node.init)
|
|
254
|
+
return;
|
|
255
|
+
if (node.init.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
256
|
+
return;
|
|
257
|
+
const callee = node.init.callee;
|
|
258
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
259
|
+
return;
|
|
260
|
+
if (callee.name !== 'require')
|
|
261
|
+
return;
|
|
262
|
+
const requiredModule = node.init.arguments[0];
|
|
263
|
+
if (!requiredModule)
|
|
264
|
+
return;
|
|
265
|
+
if (requiredModule.type !== eslint_devkit_1.AST_NODE_TYPES.Literal)
|
|
266
|
+
return;
|
|
267
|
+
if (requiredModule.value !== 'serve-index')
|
|
268
|
+
return;
|
|
269
|
+
serveIndexNames.add(node.id.name);
|
|
270
|
+
},
|
|
271
|
+
CallExpression(node) {
|
|
272
|
+
// serve-index usage is a finding on its own — directory listing
|
|
273
|
+
const callee = node.callee;
|
|
274
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
275
|
+
serveIndexNames.has(callee.name)) {
|
|
276
|
+
context.report({ node, messageId: 'directoryListing' });
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (!isExpressStaticCall(node))
|
|
280
|
+
return;
|
|
281
|
+
const rootArg = node.arguments[0];
|
|
282
|
+
if (!rootArg)
|
|
283
|
+
return;
|
|
284
|
+
checkStaticRoot(rootArg);
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* ESLint Rule: no-user-controlled-render-locals
|
|
8
|
+
*
|
|
9
|
+
* Detects template-object injection where res.render() receives locals that
|
|
10
|
+
* are wholly user-controlled (req.body / req.query / req.params) — directly,
|
|
11
|
+
* via an object spread ({ ...req.query }), or through an identifier that was
|
|
12
|
+
* assigned a whole request source without field-picking. Also flags render
|
|
13
|
+
* calls whose VIEW argument derives from request input (path traversal into
|
|
14
|
+
* the template directory).
|
|
15
|
+
*
|
|
16
|
+
* Express merges the locals object into the view-engine options, so a request
|
|
17
|
+
* key such as "layout", "settings", "cache" or "filename" reconfigures the
|
|
18
|
+
* engine and can point it at an attacker-chosen file.
|
|
19
|
+
*
|
|
20
|
+
* CWE-73: External Control of File Name or Path
|
|
21
|
+
* OWASP A03:2021 – Injection
|
|
22
|
+
*
|
|
23
|
+
* ## Detection method: structural-api
|
|
24
|
+
*
|
|
25
|
+
* This rule passes the litmus test: it fires on the AST shape of
|
|
26
|
+
* `<res>.render(view, <req>.<userSourceProp>)` and its spread / single-
|
|
27
|
+
* assignment variants — not on variable names alone. Field-picked object
|
|
28
|
+
* literals (`{ title: req.body.title }`) are the safe pattern and are never
|
|
29
|
+
* flagged. The rule does not perform full taint analysis: reassignments,
|
|
30
|
+
* cross-function flow, and deep aliasing are documented false negatives.
|
|
31
|
+
*
|
|
32
|
+
* @see https://cwe.mitre.org/data/definitions/73.html
|
|
33
|
+
* @see https://expressjs.com/en/4x/api.html#res.render
|
|
34
|
+
*/
|
|
35
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
36
|
+
type MessageIds = 'unsafeRenderLocals' | 'userControlledView' | 'pickFieldsExplicitly';
|
|
37
|
+
export interface Options {
|
|
38
|
+
/**
|
|
39
|
+
* Callee names that make a locals expression safe when they wrap it,
|
|
40
|
+
* e.g. ['pick', 'sanitizeLocals']. Matches plain calls (pick(...)) and
|
|
41
|
+
* member calls (_.pick(...)). Default: []
|
|
42
|
+
*/
|
|
43
|
+
allowSanitizers?: string[];
|
|
44
|
+
}
|
|
45
|
+
type RuleOptions = [Options?];
|
|
46
|
+
export declare const noUserControlledRenderLocals: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
47
|
+
name: string;
|
|
48
|
+
};
|
|
49
|
+
export {};
|