eslint-plugin-express-security 1.3.4 → 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 +43 -0
- package/README.md +39 -27
- 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
|
@@ -11,6 +11,20 @@ exports.noExposedDebugEndpoints = void 0;
|
|
|
11
11
|
*/
|
|
12
12
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
13
13
|
const DEFAULT_DEBUG_PATHS = ['/debug', '/__debug__', '/admin', '/_admin', '/test', '/health'];
|
|
14
|
+
// Express route-registration methods, incl. `use` for mounted sub-routers and
|
|
15
|
+
// `route` for the chained builder (`app.route('/admin').delete(handler)`).
|
|
16
|
+
const HTTP_METHODS = new Set([
|
|
17
|
+
'get',
|
|
18
|
+
'post',
|
|
19
|
+
'put',
|
|
20
|
+
'patch',
|
|
21
|
+
'delete',
|
|
22
|
+
'head',
|
|
23
|
+
'options',
|
|
24
|
+
'all',
|
|
25
|
+
'use',
|
|
26
|
+
'route',
|
|
27
|
+
]);
|
|
14
28
|
exports.noExposedDebugEndpoints = (0, eslint_devkit_1.createRule)({
|
|
15
29
|
name: 'no-exposed-debug-endpoints',
|
|
16
30
|
meta: {
|
|
@@ -61,38 +75,38 @@ exports.noExposedDebugEndpoints = (0, eslint_devkit_1.createRule)({
|
|
|
61
75
|
if (ignoreFiles.some(pattern => filename.includes(pattern))) {
|
|
62
76
|
return {};
|
|
63
77
|
}
|
|
64
|
-
function report(node) {
|
|
65
|
-
context.report({ node, messageId: 'violationDetected' });
|
|
66
|
-
}
|
|
67
78
|
// oxlint-disable-next-line consistent-function-scoping
|
|
68
79
|
const isExpressRouteCall = (node) => {
|
|
69
80
|
return (node.callee.type === 'MemberExpression' &&
|
|
70
81
|
node.callee.object.type === 'Identifier' &&
|
|
71
82
|
['app', 'router', 'express'].includes(node.callee.object.name) &&
|
|
72
83
|
node.callee.property.type === 'Identifier' &&
|
|
73
|
-
|
|
84
|
+
HTTP_METHODS.has(node.callee.property.name));
|
|
74
85
|
};
|
|
75
86
|
return {
|
|
87
|
+
// Only route *registrations* are flagged. A bare string literal that
|
|
88
|
+
// happens to equal a debug path (a redirect target, a constant, a
|
|
89
|
+
// comparison) is not an exposed endpoint — flagging it was a false
|
|
90
|
+
// positive.
|
|
76
91
|
CallExpression(node) {
|
|
77
|
-
if (isExpressRouteCall(node)) {
|
|
78
|
-
|
|
79
|
-
if (pathArg && pathArg.type === 'Literal' && typeof pathArg.value === 'string') {
|
|
80
|
-
const path = pathArg.value.toLowerCase();
|
|
81
|
-
if (debugPaths.some(dp => path.includes(dp.toLowerCase()))) {
|
|
82
|
-
report(pathArg);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
92
|
+
if (!isExpressRouteCall(node)) {
|
|
93
|
+
return;
|
|
85
94
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
// `app.get(name)` with a single argument is an application-setting
|
|
96
|
+
// lookup, not a route registration. Registering a route always passes
|
|
97
|
+
// at least one handler.
|
|
98
|
+
const isSettingLookup = node.callee.type === 'MemberExpression' &&
|
|
99
|
+
node.callee.property.type === 'Identifier' &&
|
|
100
|
+
node.callee.property.name === 'get' &&
|
|
101
|
+
node.arguments.length === 1;
|
|
102
|
+
if (isSettingLookup) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const pathArg = node.arguments[0];
|
|
106
|
+
if (pathArg && pathArg.type === 'Literal' && typeof pathArg.value === 'string') {
|
|
107
|
+
const path = pathArg.value.toLowerCase();
|
|
108
|
+
if (debugPaths.some(dp => path.includes(dp.toLowerCase()))) {
|
|
109
|
+
context.report({ node: pathArg, messageId: 'violationDetected' });
|
|
96
110
|
}
|
|
97
111
|
}
|
|
98
112
|
},
|
|
@@ -0,0 +1,56 @@
|
|
|
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-host-header-in-links
|
|
8
|
+
*
|
|
9
|
+
* Detects Host-header poisoning (CWE-640): building absolute URLs — most
|
|
10
|
+
* critically password-reset and account-verification links — from
|
|
11
|
+
* `req.headers.host`, `req.headers['x-forwarded-host']`, or
|
|
12
|
+
* `req.get('host')`. Both headers are attacker-controlled on any request
|
|
13
|
+
* that reaches the app directly, so a mailed link built from them can point
|
|
14
|
+
* the recovery token at an attacker-owned host.
|
|
15
|
+
*
|
|
16
|
+
* CWE-640: Weak Password Recovery Mechanism for Forgotten Password
|
|
17
|
+
* OWASP A07:2021 – Identification and Authentication Failures
|
|
18
|
+
*
|
|
19
|
+
* ## Detection method: structural-api + single-hop flow
|
|
20
|
+
*
|
|
21
|
+
* The rule fires when a host-header read (or a variable assigned from one in
|
|
22
|
+
* the same file) participates in URL-building string concatenation or a
|
|
23
|
+
* template literal. "URL-building" means a static part of the string
|
|
24
|
+
* contains `://` or starts with `//`, OR the string is an argument of a
|
|
25
|
+
* mail-send call (`sendMail` / `send` by default, configurable via
|
|
26
|
+
* `checkMailCallees`).
|
|
27
|
+
*
|
|
28
|
+
* It deliberately does NOT flag:
|
|
29
|
+
* - URLs built from config/env constants (`process.env.PUBLIC_ORIGIN + ...`)
|
|
30
|
+
* - host values used only for logging (`console.log('host ' + req.headers.host)`)
|
|
31
|
+
* - host reads validated inside an if-guard against `allowedHosts`
|
|
32
|
+
*
|
|
33
|
+
* @see https://cwe.mitre.org/data/definitions/640.html
|
|
34
|
+
* @see https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/06-Test_HTTP_Methods
|
|
35
|
+
*/
|
|
36
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
37
|
+
type MessageIds = 'hostHeaderInLink' | 'useConfiguredOrigin';
|
|
38
|
+
export interface Options {
|
|
39
|
+
/**
|
|
40
|
+
* Literal host values considered trusted. If an enclosing `if` statement
|
|
41
|
+
* validates the host against one of these literals, the use is not
|
|
42
|
+
* reported. Default: []
|
|
43
|
+
*/
|
|
44
|
+
allowedHosts?: string[];
|
|
45
|
+
/**
|
|
46
|
+
* Callee names treated as mail-send sinks — a host header flowing into any
|
|
47
|
+
* argument of these calls is flagged even without a `://` marker.
|
|
48
|
+
* Default: ['sendMail', 'send']
|
|
49
|
+
*/
|
|
50
|
+
checkMailCallees?: string[];
|
|
51
|
+
}
|
|
52
|
+
type RuleOptions = [Options?];
|
|
53
|
+
export declare const noHostHeaderInLinks: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
54
|
+
name: string;
|
|
55
|
+
};
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,266 @@
|
|
|
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.noHostHeaderInLinks = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
/** Header names whose value the client fully controls. */
|
|
11
|
+
const HOST_HEADER_NAMES = new Set(['host', 'x-forwarded-host']);
|
|
12
|
+
/** Express request methods that read a header by name. */
|
|
13
|
+
const HOST_GETTER_METHODS = new Set(['get', 'header']);
|
|
14
|
+
exports.noHostHeaderInLinks = (0, eslint_devkit_1.createRule)({
|
|
15
|
+
name: 'no-host-header-in-links',
|
|
16
|
+
meta: {
|
|
17
|
+
type: 'problem',
|
|
18
|
+
hasSuggestions: true,
|
|
19
|
+
docs: {
|
|
20
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/no-host-header-in-links.md',
|
|
21
|
+
description: 'Disallow building absolute URLs (reset/verification links) from the Host or X-Forwarded-Host request header',
|
|
22
|
+
cwe: 'CWE-640',
|
|
23
|
+
cvss: 8.1,
|
|
24
|
+
},
|
|
25
|
+
messages: {
|
|
26
|
+
hostHeaderInLink: (0, eslint_devkit_1.formatLLMMessage)({
|
|
27
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
28
|
+
issueName: 'Host-Header Poisoning (CWE-640)',
|
|
29
|
+
cwe: 'CWE-640',
|
|
30
|
+
description: 'A URL is built from {{source}}, which the client controls. Reset/verification links built this way can be redirected to an attacker-owned host, leaking the token.',
|
|
31
|
+
severity: 'HIGH',
|
|
32
|
+
fix: 'Build absolute links from a server-side constant (e.g. process.env.PUBLIC_ORIGIN), never from request headers.',
|
|
33
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/640.html',
|
|
34
|
+
}),
|
|
35
|
+
useConfiguredOrigin: (0, eslint_devkit_1.formatLLMMessage)({
|
|
36
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
37
|
+
issueName: 'Use a configured origin',
|
|
38
|
+
description: 'Replace the header-derived host with a deployment constant such as process.env.PUBLIC_ORIGIN',
|
|
39
|
+
severity: 'LOW',
|
|
40
|
+
fix: "const origin = process.env.PUBLIC_ORIGIN; // 'https://app.example.com'",
|
|
41
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/640.html',
|
|
42
|
+
}),
|
|
43
|
+
},
|
|
44
|
+
schema: [
|
|
45
|
+
{
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
allowedHosts: {
|
|
49
|
+
type: 'array',
|
|
50
|
+
items: { type: 'string' },
|
|
51
|
+
description: 'Trusted literal hosts — an if-guard comparing against one of these suppresses the report',
|
|
52
|
+
},
|
|
53
|
+
checkMailCallees: {
|
|
54
|
+
type: 'array',
|
|
55
|
+
items: { type: 'string' },
|
|
56
|
+
description: 'Callee names treated as mail-send sinks (default: ["sendMail", "send"])',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
defaultOptions: [{}],
|
|
64
|
+
create(context, [options]) {
|
|
65
|
+
const { allowedHosts, checkMailCallees } = options;
|
|
66
|
+
const allowed = allowedHosts ?? [];
|
|
67
|
+
const mailCallees = new Set(checkMailCallees ?? ['sendMail', 'send']);
|
|
68
|
+
const sourceCode = context.sourceCode;
|
|
69
|
+
/**
|
|
70
|
+
* Variables assigned from a host-header read → source description. Keyed by
|
|
71
|
+
* the RESOLVED scope variable, not by name: two handlers in the same file
|
|
72
|
+
* can each declare `host`, and a name-keyed map would report the second
|
|
73
|
+
* one as request-derived when it never was.
|
|
74
|
+
*/
|
|
75
|
+
const hostVars = new Map();
|
|
76
|
+
function isRequestIdent(name) {
|
|
77
|
+
const lower = name.toLowerCase();
|
|
78
|
+
return lower === 'req' || lower === 'request' || lower === 'ctx';
|
|
79
|
+
}
|
|
80
|
+
/** Static property name of a member expression, or null. */
|
|
81
|
+
function propName(node) {
|
|
82
|
+
if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && !node.computed) {
|
|
83
|
+
return node.property.name;
|
|
84
|
+
}
|
|
85
|
+
if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
86
|
+
typeof node.property.value === 'string') {
|
|
87
|
+
return node.property.value;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Returns a description when `node` reads a client-controlled host
|
|
93
|
+
* header, else null. Handles:
|
|
94
|
+
* req.headers.host / req.headers['x-forwarded-host']
|
|
95
|
+
* req.get('host') / req.header('X-Forwarded-Host')
|
|
96
|
+
* a || b / a ?? b (either side tainted taints the result)
|
|
97
|
+
*/
|
|
98
|
+
function getHostHeaderSource(node) {
|
|
99
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.LogicalExpression) {
|
|
100
|
+
return (getHostHeaderSource(node.left) ?? getHostHeaderSource(node.right));
|
|
101
|
+
}
|
|
102
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
103
|
+
const callee = node.callee;
|
|
104
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
105
|
+
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
106
|
+
HOST_GETTER_METHODS.has(callee.property.name) &&
|
|
107
|
+
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
108
|
+
isRequestIdent(callee.object.name)) {
|
|
109
|
+
const arg = node.arguments[0];
|
|
110
|
+
if (arg !== undefined &&
|
|
111
|
+
arg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
112
|
+
typeof arg.value === 'string' &&
|
|
113
|
+
HOST_HEADER_NAMES.has(arg.value.toLowerCase())) {
|
|
114
|
+
return `req.${callee.property.name}('${arg.value}')`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
120
|
+
const obj = node.object;
|
|
121
|
+
if (obj.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
122
|
+
obj.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
123
|
+
obj.property.name === 'headers' &&
|
|
124
|
+
obj.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
125
|
+
isRequestIdent(obj.object.name)) {
|
|
126
|
+
const name = propName(node);
|
|
127
|
+
if (name !== null && HOST_HEADER_NAMES.has(name.toLowerCase())) {
|
|
128
|
+
return `req.headers['${name}']`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
/** Direct host read, or an identifier tracked from one. */
|
|
135
|
+
function resolveHostSource(node) {
|
|
136
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
137
|
+
let scope = sourceCode.getScope(node);
|
|
138
|
+
while (scope) {
|
|
139
|
+
const variable = scope.variables.find((v) => v.name === node.name);
|
|
140
|
+
if (variable)
|
|
141
|
+
return hostVars.get(variable) ?? null;
|
|
142
|
+
scope = scope.upper;
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return getHostHeaderSource(node);
|
|
147
|
+
}
|
|
148
|
+
/** True when a static string fragment looks like URL building. */
|
|
149
|
+
function isUrlish(text) {
|
|
150
|
+
return text.includes('://') || text.startsWith('//');
|
|
151
|
+
}
|
|
152
|
+
/** True when `node` sits (at any depth) inside a mail-send call. */
|
|
153
|
+
function isInMailCall(node) {
|
|
154
|
+
let current = node.parent;
|
|
155
|
+
while (current != null) {
|
|
156
|
+
if (current.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
157
|
+
const callee = current.callee;
|
|
158
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
159
|
+
mailCallees.has(callee.name)) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
163
|
+
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
164
|
+
mailCallees.has(callee.property.name)) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
current = current.parent;
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* True when an enclosing if-statement's test mentions one of the
|
|
174
|
+
* configured `allowedHosts` literals — the host was validated.
|
|
175
|
+
*/
|
|
176
|
+
function isValidatedByAllowlist(node) {
|
|
177
|
+
if (allowed.length === 0)
|
|
178
|
+
return false;
|
|
179
|
+
let current = node.parent;
|
|
180
|
+
while (current != null) {
|
|
181
|
+
if (current.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement) {
|
|
182
|
+
const testText = sourceCode.getText(current.test);
|
|
183
|
+
if (allowed.some((host) => testText.includes(host)))
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
current = current.parent;
|
|
187
|
+
}
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
function reportHostUse(node, source) {
|
|
191
|
+
if (isValidatedByAllowlist(node))
|
|
192
|
+
return;
|
|
193
|
+
context.report({
|
|
194
|
+
node,
|
|
195
|
+
messageId: 'hostHeaderInLink',
|
|
196
|
+
data: { source },
|
|
197
|
+
suggest: [{ messageId: 'useConfiguredOrigin', fix: () => null }],
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
VariableDeclarator(node) {
|
|
202
|
+
if (node.id.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier || node.init == null) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const source = getHostHeaderSource(node.init);
|
|
206
|
+
if (source === null)
|
|
207
|
+
return;
|
|
208
|
+
// Exactly one variable for an Identifier id.
|
|
209
|
+
for (const variable of sourceCode.getDeclaredVariables(node)) {
|
|
210
|
+
hostVars.set(variable, source);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
TemplateLiteral(node) {
|
|
214
|
+
const urlish = node.quasis.some((q) => isUrlish(q.value.raw));
|
|
215
|
+
for (const expr of node.expressions) {
|
|
216
|
+
const source = resolveHostSource(expr);
|
|
217
|
+
if (source !== null && (urlish || isInMailCall(node))) {
|
|
218
|
+
reportHostUse(expr, source);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
BinaryExpression(node) {
|
|
223
|
+
if (node.operator !== '+')
|
|
224
|
+
return;
|
|
225
|
+
// Only handle the top of a concat chain — inner `+` nodes are
|
|
226
|
+
// flattened below.
|
|
227
|
+
if (node.parent.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression &&
|
|
228
|
+
node.parent.operator === '+') {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const operands = [];
|
|
232
|
+
const stack = [node];
|
|
233
|
+
while (stack.length > 0) {
|
|
234
|
+
const current = stack.pop();
|
|
235
|
+
if (current.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression &&
|
|
236
|
+
current.operator === '+') {
|
|
237
|
+
stack.push(current.left, current.right);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
operands.push(current);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
let urlish = false;
|
|
244
|
+
let hostNode = null;
|
|
245
|
+
let hostSource = null;
|
|
246
|
+
for (const operand of operands) {
|
|
247
|
+
if (operand.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
248
|
+
typeof operand.value === 'string' &&
|
|
249
|
+
isUrlish(operand.value)) {
|
|
250
|
+
urlish = true;
|
|
251
|
+
}
|
|
252
|
+
if (hostSource === null) {
|
|
253
|
+
const source = resolveHostSource(operand);
|
|
254
|
+
if (source !== null) {
|
|
255
|
+
hostSource = source;
|
|
256
|
+
hostNode = operand;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (hostNode !== null && (urlish || isInMailCall(node))) {
|
|
261
|
+
reportHostUse(hostNode, hostSource);
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
},
|
|
266
|
+
});
|
|
@@ -13,6 +13,36 @@ const DEFAULT_REQUIRED_HEADERS = [
|
|
|
13
13
|
'X-Frame-Options',
|
|
14
14
|
'X-Content-Type-Options',
|
|
15
15
|
];
|
|
16
|
+
/** Methods that write a response header. */
|
|
17
|
+
const HEADER_METHODS = new Set(['setHeader', 'header', 'set']);
|
|
18
|
+
/**
|
|
19
|
+
* Receivers that actually are an HTTP response. `set` and `header` are far too
|
|
20
|
+
* common to match on the method name alone — `url.searchParams.set(…)` and
|
|
21
|
+
* `app.set('view engine', …)` are not header calls.
|
|
22
|
+
*/
|
|
23
|
+
const RESPONSE_RECEIVER = /^(res|resp|response|reply)$/i;
|
|
24
|
+
/**
|
|
25
|
+
* Is this call a response header setter (res.setHeader / res.header / res.set,
|
|
26
|
+
* including `ctx.res.set(…)` and `this.response.header(…)`)?
|
|
27
|
+
*/
|
|
28
|
+
function isResponseHeaderCall(node) {
|
|
29
|
+
const { callee } = node;
|
|
30
|
+
if (callee.type !== 'MemberExpression' ||
|
|
31
|
+
callee.property.type !== 'Identifier' ||
|
|
32
|
+
!HEADER_METHODS.has(callee.property.name)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const { object } = callee;
|
|
36
|
+
if (object.type === 'Identifier') {
|
|
37
|
+
return RESPONSE_RECEIVER.test(object.name);
|
|
38
|
+
}
|
|
39
|
+
if (object.type === 'MemberExpression' && object.property.type === 'Identifier') {
|
|
40
|
+
return RESPONSE_RECEIVER.test(object.property.name);
|
|
41
|
+
}
|
|
42
|
+
// ponytail: anything else (call results, computed access) is not tracked —
|
|
43
|
+
// a rule that guesses at aliases trades this false positive for a worse one.
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
16
46
|
/**
|
|
17
47
|
* Extract header name from setHeader call
|
|
18
48
|
*/
|
|
@@ -45,10 +75,7 @@ function checkFunctionForSecurityHeaders(node, requiredHeaders, context) {
|
|
|
45
75
|
}
|
|
46
76
|
// Collect all setHeader calls in this scope
|
|
47
77
|
function collectHeaders(astNode) {
|
|
48
|
-
if (astNode.type === 'CallExpression' &&
|
|
49
|
-
astNode.callee.type === 'MemberExpression' &&
|
|
50
|
-
astNode.callee.property.type === 'Identifier' &&
|
|
51
|
-
['setHeader', 'header', 'set'].includes(astNode.callee.property.name)) {
|
|
78
|
+
if (astNode.type === 'CallExpression' && isResponseHeaderCall(astNode)) {
|
|
52
79
|
const headerName = extractHeaderName(astNode);
|
|
53
80
|
if (headerName) {
|
|
54
81
|
setHeaders.add(headerName);
|
|
@@ -177,45 +204,39 @@ exports.noMissingSecurityHeaders = (0, eslint_devkit_2.createRule)({
|
|
|
177
204
|
*/
|
|
178
205
|
function checkCallExpression(node) {
|
|
179
206
|
// Check for res.setHeader, res.header, res.set
|
|
180
|
-
if (node
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
// Mark as checked even if no error
|
|
216
|
-
reportedScopes.add(scopeKey);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
207
|
+
if (!isResponseHeaderCall(node)) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const scopeKey = getScopeKey(node);
|
|
211
|
+
// Only check once per scope
|
|
212
|
+
if (reportedScopes.has(scopeKey)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const missing = checkFunctionForSecurityHeaders(node, requiredHeaders, context);
|
|
216
|
+
// Mark as checked either way
|
|
217
|
+
reportedScopes.add(scopeKey);
|
|
218
|
+
if (missing.length > 0) {
|
|
219
|
+
context.report({
|
|
220
|
+
node,
|
|
221
|
+
messageId: 'missingSecurityHeader',
|
|
222
|
+
data: {
|
|
223
|
+
headers: missing.join(', '),
|
|
224
|
+
},
|
|
225
|
+
suggest: [
|
|
226
|
+
{
|
|
227
|
+
messageId: 'addSecurityHeaders',
|
|
228
|
+
fix: () => null,
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
messageId: 'useMiddleware',
|
|
232
|
+
fix: () => null,
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
messageId: 'setHeader',
|
|
236
|
+
fix: () => null,
|
|
237
|
+
},
|
|
238
|
+
],
|
|
239
|
+
});
|
|
219
240
|
}
|
|
220
241
|
}
|
|
221
242
|
return {
|
|
@@ -0,0 +1,51 @@
|
|
|
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-sensitive-data-in-query
|
|
8
|
+
*
|
|
9
|
+
* Detects sensitive data carried in the URL query string (CWE-598): reading
|
|
10
|
+
* sensitive-named parameters (password, token, secret, apiKey, ...) from
|
|
11
|
+
* `req.query` — either via member access (`req.query.password`) or
|
|
12
|
+
* destructuring (`const { password } = req.query`). Query strings land in
|
|
13
|
+
* access logs, proxy logs, browser history and the Referer header of every
|
|
14
|
+
* outbound link, so secrets must travel in a POST body or header instead.
|
|
15
|
+
*
|
|
16
|
+
* CWE-598: Use of GET Request Method With Sensitive Query Strings
|
|
17
|
+
* OWASP A04:2021 – Insecure Design
|
|
18
|
+
*
|
|
19
|
+
* ## Detection method: structural-api + token matching
|
|
20
|
+
*
|
|
21
|
+
* The rule fires on the AST shape `<req>.query.<name>` (req/request/ctx)
|
|
22
|
+
* and on object-pattern destructuring from `<req>.query`. Sensitivity is
|
|
23
|
+
* decided by tokenizing the parameter name (camelCase and snake_case both
|
|
24
|
+
* split) and matching whole tokens — so `api_token` and `accessToken` match
|
|
25
|
+
* `token`, while `author` does NOT match `auth` and `cardinality` does NOT
|
|
26
|
+
* match `card`.
|
|
27
|
+
*
|
|
28
|
+
* It deliberately does NOT flag:
|
|
29
|
+
* - the same names read from `req.body` or `req.params`
|
|
30
|
+
* - non-sensitive query fields (`req.query.page`)
|
|
31
|
+
* - names listed in the `allowedParams` option (e.g. `token` on a dedicated
|
|
32
|
+
* email-verification route)
|
|
33
|
+
*
|
|
34
|
+
* @see https://cwe.mitre.org/data/definitions/598.html
|
|
35
|
+
* @see https://owasp.org/www-community/vulnerabilities/Information_exposure_through_query_strings_in_url
|
|
36
|
+
*/
|
|
37
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
38
|
+
type MessageIds = 'sensitiveQueryParam' | 'moveToBody';
|
|
39
|
+
export interface Options {
|
|
40
|
+
/** Additional sensitive parameter names (extends the defaults). Default: [] */
|
|
41
|
+
sensitiveParams?: string[];
|
|
42
|
+
/** Additional regex patterns (strings, case-insensitive) matched against the raw parameter name. Default: [] */
|
|
43
|
+
extraPatterns?: string[];
|
|
44
|
+
/** Parameter names explicitly allowed in the query string (exact, case-insensitive). Default: [] */
|
|
45
|
+
allowedParams?: string[];
|
|
46
|
+
}
|
|
47
|
+
type RuleOptions = [Options?];
|
|
48
|
+
export declare const noSensitiveDataInQuery: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener> & {
|
|
49
|
+
name: string;
|
|
50
|
+
};
|
|
51
|
+
export {};
|