eslint-plugin-express-security 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/package.json +5 -8
- package/src/index.d.ts +17 -40
- package/src/index.js +26 -66
- package/src/oxlint.d.ts +17 -20
- package/src/oxlint.js +0 -18
- package/src/rules/no-client-controlled-authorization/index.js +194 -0
- package/src/rules/no-cors-credentials-wildcard/index.js +0 -22
- package/src/rules/no-disabled-helmet-protections/index.js +125 -0
- package/src/rules/no-error-details-in-response/index.js +0 -26
- package/src/rules/no-exposed-debug-endpoints/index.js +0 -19
- package/src/rules/no-express-unsafe-regex-route/index.js +1 -32
- package/src/rules/no-graphql-introspection-production/index.js +0 -21
- package/src/rules/no-host-header-in-links/index.js +0 -31
- package/src/rules/no-idor-resource-access/index.js +126 -0
- package/src/rules/no-insecure-cookie-options/index.js +0 -12
- package/src/rules/no-missing-cors-check/index.js +4 -54
- package/src/rules/no-missing-csrf-protection/index.js +0 -25
- package/src/rules/no-missing-security-headers/index.js +0 -40
- package/src/rules/no-permissive-cors/index.js +0 -27
- package/src/rules/no-permissive-trust-proxy/index.js +106 -0
- package/src/rules/no-sensitive-data-in-query/index.js +1 -26
- package/src/rules/no-static-root-exposure/index.js +0 -21
- package/src/rules/no-unsafe-csp-directives/index.js +264 -0
- package/src/rules/no-user-controlled-redirect/index.js +0 -26
- package/src/rules/no-user-controlled-render-locals/index.js +0 -45
- package/src/rules/require-case-insensitive-path-guard/index.js +0 -21
- package/src/rules/require-csrf-protection/index.js +0 -42
- package/src/rules/require-express-body-parser-limits/index.js +0 -24
- package/src/rules/require-helmet/index.js +1 -25
- package/src/rules/require-query-type-guard/index.js +0 -20
- package/src/rules/require-rate-limiting/index.js +0 -13
- package/src/rules/require-route-authentication/index.js +225 -0
- package/src/rules/require-strict-transport-security/index.js +171 -0
- package/src/types/index.js +0 -5
- package/src/utils/index.js +81 -0
- package/AGENTS.md +0 -100
- package/CHANGELOG.md +0 -192
- package/src/rules/no-cors-credentials-wildcard/index.d.ts +0 -26
- package/src/rules/no-error-details-in-response/index.d.ts +0 -57
- package/src/rules/no-exposed-debug-endpoints/index.d.ts +0 -14
- package/src/rules/no-express-unsafe-regex-route/index.d.ts +0 -27
- package/src/rules/no-graphql-introspection-production/index.d.ts +0 -26
- package/src/rules/no-host-header-in-links/index.d.ts +0 -56
- package/src/rules/no-insecure-cookie-options/index.d.ts +0 -39
- package/src/rules/no-missing-cors-check/index.d.ts +0 -28
- package/src/rules/no-missing-csrf-protection/index.d.ts +0 -30
- package/src/rules/no-missing-security-headers/index.d.ts +0 -26
- package/src/rules/no-permissive-cors/index.d.ts +0 -28
- package/src/rules/no-sensitive-data-in-query/index.d.ts +0 -51
- package/src/rules/no-static-root-exposure/index.d.ts +0 -45
- package/src/rules/no-user-controlled-redirect/index.d.ts +0 -44
- package/src/rules/no-user-controlled-render-locals/index.d.ts +0 -49
- package/src/rules/require-case-insensitive-path-guard/index.d.ts +0 -59
- package/src/rules/require-csrf-protection/index.d.ts +0 -28
- package/src/rules/require-express-body-parser-limits/index.d.ts +0 -31
- package/src/rules/require-helmet/index.d.ts +0 -29
- package/src/rules/require-query-type-guard/index.d.ts +0 -54
- package/src/rules/require-rate-limiting/index.d.ts +0 -31
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.requireRateLimiting = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -16,20 +11,15 @@ const RATE_LIMIT_PACKAGES = [
|
|
|
16
11
|
'slowDown',
|
|
17
12
|
'expressBrute',
|
|
18
13
|
];
|
|
19
|
-
/**
|
|
20
|
-
* Check if a node is a rate limiting middleware usage
|
|
21
|
-
*/
|
|
22
14
|
function isRateLimitMiddleware(node, alternatives) {
|
|
23
15
|
const allPatterns = [...RATE_LIMIT_PACKAGES, ...alternatives];
|
|
24
16
|
for (const arg of node.arguments) {
|
|
25
|
-
// rateLimit() or limiter()
|
|
26
17
|
if (arg.type === 'CallExpression' && arg.callee.type === 'Identifier') {
|
|
27
18
|
const calleeName = arg.callee.name.toLowerCase();
|
|
28
19
|
if (allPatterns.some((p) => calleeName.includes(p.toLowerCase()))) {
|
|
29
20
|
return true;
|
|
30
21
|
}
|
|
31
22
|
}
|
|
32
|
-
// rateLimit identifier without call
|
|
33
23
|
if (arg.type === 'Identifier') {
|
|
34
24
|
if (allPatterns.some((p) => arg.name.toLowerCase().includes(p.toLowerCase()))) {
|
|
35
25
|
return true;
|
|
@@ -100,7 +90,6 @@ exports.requireRateLimiting = (0, eslint_devkit_1.createRule)({
|
|
|
100
90
|
],
|
|
101
91
|
create(context, [options]) {
|
|
102
92
|
const { allowInTests = false, alternativeMiddleware = [], assumeRateLimiting = false } = options;
|
|
103
|
-
// Skip entirely if rate limiting is assumed (provided by infrastructure)
|
|
104
93
|
if (assumeRateLimiting) {
|
|
105
94
|
return {};
|
|
106
95
|
}
|
|
@@ -115,13 +104,11 @@ exports.requireRateLimiting = (0, eslint_devkit_1.createRule)({
|
|
|
115
104
|
return {
|
|
116
105
|
CallExpression(node) {
|
|
117
106
|
const callee = node.callee;
|
|
118
|
-
// Check for express() app creation
|
|
119
107
|
if (callee.type === 'Identifier' && callee.name === 'express') {
|
|
120
108
|
hasExpressApp = true;
|
|
121
109
|
expressAppNode = node;
|
|
122
110
|
return;
|
|
123
111
|
}
|
|
124
|
-
// Check for app.use() with rate limiting
|
|
125
112
|
if (callee.type === 'MemberExpression' &&
|
|
126
113
|
callee.property.type === 'Identifier' &&
|
|
127
114
|
callee.property.name === 'use') {
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireRouteAuthentication = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
const DEFAULT_CRITICAL_PATHS = [
|
|
7
|
+
'password',
|
|
8
|
+
'credential',
|
|
9
|
+
'secret',
|
|
10
|
+
'apikey',
|
|
11
|
+
'api-key',
|
|
12
|
+
'user',
|
|
13
|
+
'account',
|
|
14
|
+
'profile',
|
|
15
|
+
'role',
|
|
16
|
+
'permission',
|
|
17
|
+
'grant',
|
|
18
|
+
'payment',
|
|
19
|
+
'billing',
|
|
20
|
+
'invoice',
|
|
21
|
+
'charge',
|
|
22
|
+
'refund',
|
|
23
|
+
'transfer',
|
|
24
|
+
'withdraw',
|
|
25
|
+
'order',
|
|
26
|
+
'config',
|
|
27
|
+
'setting',
|
|
28
|
+
'export',
|
|
29
|
+
'import',
|
|
30
|
+
'mfa',
|
|
31
|
+
'2fa',
|
|
32
|
+
'otp',
|
|
33
|
+
'deploy',
|
|
34
|
+
'migrate',
|
|
35
|
+
'backup',
|
|
36
|
+
'restore',
|
|
37
|
+
'internal',
|
|
38
|
+
'private',
|
|
39
|
+
];
|
|
40
|
+
const DEFAULT_PUBLIC_PATHS = [
|
|
41
|
+
'login',
|
|
42
|
+
'signin',
|
|
43
|
+
'sign-in',
|
|
44
|
+
'logout',
|
|
45
|
+
'signup',
|
|
46
|
+
'sign-up',
|
|
47
|
+
'register',
|
|
48
|
+
'forgot',
|
|
49
|
+
'reset',
|
|
50
|
+
'confirm',
|
|
51
|
+
'verify',
|
|
52
|
+
'webhook',
|
|
53
|
+
'hook',
|
|
54
|
+
'health',
|
|
55
|
+
'healthz',
|
|
56
|
+
'readyz',
|
|
57
|
+
'livez',
|
|
58
|
+
'ping',
|
|
59
|
+
'status',
|
|
60
|
+
'public',
|
|
61
|
+
'static',
|
|
62
|
+
'assets',
|
|
63
|
+
'favicon',
|
|
64
|
+
'robots',
|
|
65
|
+
'callback',
|
|
66
|
+
'oauth',
|
|
67
|
+
'sso',
|
|
68
|
+
'docs',
|
|
69
|
+
'swagger',
|
|
70
|
+
'openapi',
|
|
71
|
+
];
|
|
72
|
+
const ROUTE_METHODS = new Set([
|
|
73
|
+
'get',
|
|
74
|
+
'post',
|
|
75
|
+
'put',
|
|
76
|
+
'patch',
|
|
77
|
+
'delete',
|
|
78
|
+
'all',
|
|
79
|
+
]);
|
|
80
|
+
const APP_RECEIVER = /^(app|router|express|api)$/i;
|
|
81
|
+
function matchesFragment(path, fragment) {
|
|
82
|
+
const escaped = fragment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
83
|
+
return new RegExp(`(^|[^a-z0-9])${escaped}s?([^a-z0-9]|$)`).test(path);
|
|
84
|
+
}
|
|
85
|
+
const AUTH_MIDDLEWARE = /auth|jwt|passport|token|session|guard|protect|ensure|require|permit|acl|rbac|verify|login|identity|principal/i;
|
|
86
|
+
exports.requireRouteAuthentication = (0, eslint_devkit_1.createRule)({
|
|
87
|
+
name: 'require-route-authentication',
|
|
88
|
+
meta: {
|
|
89
|
+
type: 'problem',
|
|
90
|
+
docs: {
|
|
91
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-route-authentication.md',
|
|
92
|
+
description: 'Require authentication on routes that expose a critical function (account, credential, payment, configuration)',
|
|
93
|
+
cwe: 'CWE-306',
|
|
94
|
+
cvss: 8.2,
|
|
95
|
+
confidence: 'medium',
|
|
96
|
+
},
|
|
97
|
+
messages: {
|
|
98
|
+
missingAuthentication: (0, eslint_devkit_1.formatLLMMessage)({
|
|
99
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
100
|
+
issueName: 'Critical Route Without Authentication (CWE-306)',
|
|
101
|
+
cwe: 'CWE-306',
|
|
102
|
+
cvss: 8.2,
|
|
103
|
+
description: '{{method}} {{path}} exposes a critical function but its chain has no authentication middleware and the handler never reads an authenticated principal. Anyone who can reach the port can call it.',
|
|
104
|
+
severity: 'HIGH',
|
|
105
|
+
fix: "Mount an authentication middleware on the route — app.{{method}}('{{path}}', requireAuth, handler) — or app.use(requireAuth) for the whole router.",
|
|
106
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/306.html',
|
|
107
|
+
}),
|
|
108
|
+
},
|
|
109
|
+
schema: [
|
|
110
|
+
{
|
|
111
|
+
type: 'object',
|
|
112
|
+
properties: {
|
|
113
|
+
criticalPaths: {
|
|
114
|
+
type: 'array',
|
|
115
|
+
items: { type: 'string' },
|
|
116
|
+
description: 'Path fragments that mark a route as critical',
|
|
117
|
+
},
|
|
118
|
+
publicPaths: {
|
|
119
|
+
type: 'array',
|
|
120
|
+
items: { type: 'string' },
|
|
121
|
+
description: 'Path fragments that are public by design',
|
|
122
|
+
},
|
|
123
|
+
authMiddleware: {
|
|
124
|
+
type: 'array',
|
|
125
|
+
items: { type: 'string' },
|
|
126
|
+
description: 'Extra middleware names accepted as authentication',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
additionalProperties: false,
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
},
|
|
133
|
+
defaultOptions: [{}],
|
|
134
|
+
create(context, [options]) {
|
|
135
|
+
const { criticalPaths, publicPaths, authMiddleware } = options;
|
|
136
|
+
const critical = (criticalPaths ?? DEFAULT_CRITICAL_PATHS).map((p) => p.toLowerCase());
|
|
137
|
+
const publics = (publicPaths ?? DEFAULT_PUBLIC_PATHS).map((p) => p.toLowerCase());
|
|
138
|
+
const extraAuth = new Set(authMiddleware ?? []);
|
|
139
|
+
const candidates = [];
|
|
140
|
+
let hasGlobalAuth = false;
|
|
141
|
+
function isAuthMiddlewareArg(arg) {
|
|
142
|
+
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
143
|
+
arg.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
144
|
+
arg.callee.name === 'require') {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
const text = context.sourceCode.getText(arg);
|
|
148
|
+
if (AUTH_MIDDLEWARE.test(text))
|
|
149
|
+
return true;
|
|
150
|
+
for (const name of extraAuth) {
|
|
151
|
+
if (text.includes(name))
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
function routeMethodOf(node) {
|
|
157
|
+
const callee = node.callee;
|
|
158
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
159
|
+
return null;
|
|
160
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
161
|
+
return null;
|
|
162
|
+
if (callee.computed)
|
|
163
|
+
return null;
|
|
164
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
165
|
+
return null;
|
|
166
|
+
if (!APP_RECEIVER.test(callee.object.name))
|
|
167
|
+
return null;
|
|
168
|
+
return callee.property.name;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
CallExpression(node) {
|
|
172
|
+
const method = routeMethodOf(node);
|
|
173
|
+
if (!method)
|
|
174
|
+
return;
|
|
175
|
+
if (method === 'use') {
|
|
176
|
+
const [first] = node.arguments;
|
|
177
|
+
const pathScoped = first?.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
178
|
+
typeof first.value === 'string';
|
|
179
|
+
if (!pathScoped) {
|
|
180
|
+
for (const arg of node.arguments) {
|
|
181
|
+
if (arg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
182
|
+
isAuthMiddlewareArg(arg)) {
|
|
183
|
+
hasGlobalAuth = true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (!ROUTE_METHODS.has(method))
|
|
190
|
+
return;
|
|
191
|
+
const [pathArg, ...rest] = node.arguments;
|
|
192
|
+
if (!pathArg ||
|
|
193
|
+
pathArg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal ||
|
|
194
|
+
typeof pathArg.value !== 'string') {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (rest.length === 0)
|
|
198
|
+
return;
|
|
199
|
+
const path = pathArg.value.toLowerCase();
|
|
200
|
+
if (publics.some((fragment) => matchesFragment(path, fragment)))
|
|
201
|
+
return;
|
|
202
|
+
if (!critical.some((fragment) => matchesFragment(path, fragment)))
|
|
203
|
+
return;
|
|
204
|
+
const chain = rest.slice(0, -1);
|
|
205
|
+
if (chain.some((arg) => isAuthMiddlewareArg(arg)))
|
|
206
|
+
return;
|
|
207
|
+
const handler = rest[rest.length - 1];
|
|
208
|
+
if ((0, utils_1.readsPrincipal)(handler))
|
|
209
|
+
return;
|
|
210
|
+
candidates.push({ node: pathArg, method, path: pathArg.value });
|
|
211
|
+
},
|
|
212
|
+
'Program:exit'() {
|
|
213
|
+
if (hasGlobalAuth)
|
|
214
|
+
return;
|
|
215
|
+
for (const candidate of candidates) {
|
|
216
|
+
context.report({
|
|
217
|
+
node: candidate.node,
|
|
218
|
+
messageId: 'missingAuthentication',
|
|
219
|
+
data: { method: candidate.method, path: candidate.path },
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
},
|
|
225
|
+
});
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireStrictTransportSecurity = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const DEFAULT_MIN_MAX_AGE = 15552000;
|
|
6
|
+
const HSTS_NAMES = new Set(['hsts', 'strictTransportSecurity']);
|
|
7
|
+
function propertyKeyName(prop) {
|
|
8
|
+
if (prop.computed)
|
|
9
|
+
return null;
|
|
10
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
11
|
+
return prop.key.name;
|
|
12
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
13
|
+
typeof prop.key.value === 'string') {
|
|
14
|
+
return prop.key.value;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
function isHelmetHstsFactory(node) {
|
|
19
|
+
const callee = node.callee;
|
|
20
|
+
if (callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
21
|
+
return false;
|
|
22
|
+
if (callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
23
|
+
return false;
|
|
24
|
+
if (callee.computed)
|
|
25
|
+
return false;
|
|
26
|
+
if (callee.object.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
27
|
+
return false;
|
|
28
|
+
return callee.object.name === 'helmet' && HSTS_NAMES.has(callee.property.name);
|
|
29
|
+
}
|
|
30
|
+
function isHelmetCall(node) {
|
|
31
|
+
return (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
32
|
+
node.callee.name === 'helmet');
|
|
33
|
+
}
|
|
34
|
+
exports.requireStrictTransportSecurity = (0, eslint_devkit_1.createRule)({
|
|
35
|
+
name: 'require-strict-transport-security',
|
|
36
|
+
meta: {
|
|
37
|
+
type: 'problem',
|
|
38
|
+
hasSuggestions: true,
|
|
39
|
+
docs: {
|
|
40
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-strict-transport-security.md',
|
|
41
|
+
description: 'Require a Strict-Transport-Security header with a long max-age and includeSubDomains',
|
|
42
|
+
cwe: 'CWE-319',
|
|
43
|
+
cvss: 7.4,
|
|
44
|
+
confidence: 'high',
|
|
45
|
+
},
|
|
46
|
+
messages: {
|
|
47
|
+
hstsDisabled: (0, eslint_devkit_1.formatLLMMessage)({
|
|
48
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
49
|
+
issueName: 'HSTS Disabled (CWE-319)',
|
|
50
|
+
cwe: 'CWE-319',
|
|
51
|
+
cvss: 7.4,
|
|
52
|
+
description: 'helmet({ {{option}}: false }) removes the Strict-Transport-Security header. Every first request of a session can then be downgraded to plaintext HTTP and stripped in transit.',
|
|
53
|
+
severity: 'HIGH',
|
|
54
|
+
fix: 'Remove the `{{option}}: false` entry, or configure it: { maxAge: 31536000, includeSubDomains: true, preload: true }.',
|
|
55
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/319.html',
|
|
56
|
+
}),
|
|
57
|
+
maxAgeTooShort: (0, eslint_devkit_1.formatLLMMessage)({
|
|
58
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
59
|
+
issueName: 'HSTS max-age Too Short (CWE-319)',
|
|
60
|
+
cwe: 'CWE-319',
|
|
61
|
+
cvss: 5.9,
|
|
62
|
+
description: 'Strict-Transport-Security max-age is {{maxAge}}s, below the {{minimum}}s floor. Once it lapses, the next visit is downgradeable again.',
|
|
63
|
+
severity: 'MEDIUM',
|
|
64
|
+
fix: 'Set maxAge to at least {{minimum}} seconds. Preload submission (hstspreload.org) requires 31536000 — one year — plus includeSubDomains and preload.',
|
|
65
|
+
documentationLink: 'https://hstspreload.org/',
|
|
66
|
+
}),
|
|
67
|
+
subdomainsExcluded: (0, eslint_devkit_1.formatLLMMessage)({
|
|
68
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
69
|
+
issueName: 'HSTS Excludes Subdomains (CWE-319)',
|
|
70
|
+
cwe: 'CWE-319',
|
|
71
|
+
cvss: 5.9,
|
|
72
|
+
description: 'includeSubDomains is off, so any subdomain still answers over plaintext HTTP — enough to plant a cookie the parent domain trusts.',
|
|
73
|
+
severity: 'MEDIUM',
|
|
74
|
+
fix: 'Set includeSubDomains: true once every subdomain serves HTTPS.',
|
|
75
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/319.html',
|
|
76
|
+
}),
|
|
77
|
+
raiseMaxAge: 'Raise maxAge to {{minimum}} seconds',
|
|
78
|
+
},
|
|
79
|
+
schema: [
|
|
80
|
+
{
|
|
81
|
+
type: 'object',
|
|
82
|
+
properties: {
|
|
83
|
+
minMaxAge: {
|
|
84
|
+
type: 'number',
|
|
85
|
+
minimum: 0,
|
|
86
|
+
description: 'Minimum accepted max-age in seconds',
|
|
87
|
+
},
|
|
88
|
+
requireSubDomains: {
|
|
89
|
+
type: 'boolean',
|
|
90
|
+
description: 'Require includeSubDomains',
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
additionalProperties: false,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
defaultOptions: [{}],
|
|
98
|
+
create(context, [options]) {
|
|
99
|
+
const { minMaxAge, requireSubDomains } = options;
|
|
100
|
+
const minimum = minMaxAge ?? DEFAULT_MIN_MAX_AGE;
|
|
101
|
+
const wantSubDomains = requireSubDomains ?? true;
|
|
102
|
+
function checkHstsOptions(config) {
|
|
103
|
+
for (const prop of config.properties) {
|
|
104
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
105
|
+
continue;
|
|
106
|
+
const name = propertyKeyName(prop);
|
|
107
|
+
if (!name)
|
|
108
|
+
continue;
|
|
109
|
+
if (name === 'maxAge' &&
|
|
110
|
+
prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
111
|
+
typeof prop.value.value === 'number' &&
|
|
112
|
+
prop.value.value < minimum) {
|
|
113
|
+
const maxAgeNode = prop.value;
|
|
114
|
+
context.report({
|
|
115
|
+
node: prop,
|
|
116
|
+
messageId: 'maxAgeTooShort',
|
|
117
|
+
data: { maxAge: String(maxAgeNode.value), minimum: String(minimum) },
|
|
118
|
+
suggest: [
|
|
119
|
+
{
|
|
120
|
+
messageId: 'raiseMaxAge',
|
|
121
|
+
data: { minimum: String(minimum) },
|
|
122
|
+
fix: (fixer) => fixer.replaceText(maxAgeNode, String(minimum)),
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (wantSubDomains &&
|
|
129
|
+
name === 'includeSubDomains' &&
|
|
130
|
+
prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
131
|
+
prop.value.value === false) {
|
|
132
|
+
context.report({ node: prop, messageId: 'subdomainsExcluded' });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function checkHstsValue(node, value, option) {
|
|
137
|
+
if (value.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
138
|
+
value.value === false) {
|
|
139
|
+
context.report({ node, messageId: 'hstsDisabled', data: { option } });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (value.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
143
|
+
checkHstsOptions(value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
CallExpression(node) {
|
|
148
|
+
if (isHelmetHstsFactory(node)) {
|
|
149
|
+
const config = node.arguments[0];
|
|
150
|
+
if (config && config.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
151
|
+
checkHstsOptions(config);
|
|
152
|
+
}
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!isHelmetCall(node))
|
|
156
|
+
return;
|
|
157
|
+
const config = node.arguments[0];
|
|
158
|
+
if (!config || config.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
|
|
159
|
+
return;
|
|
160
|
+
for (const prop of config.properties) {
|
|
161
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
162
|
+
continue;
|
|
163
|
+
const name = propertyKeyName(prop);
|
|
164
|
+
if (!name || !HSTS_NAMES.has(name))
|
|
165
|
+
continue;
|
|
166
|
+
checkHstsValue(prop, prop.value, name);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
},
|
|
171
|
+
});
|
package/src/types/index.js
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.walk = walk;
|
|
4
|
+
exports.readsPrincipal = readsPrincipal;
|
|
5
|
+
exports.isClientRequestMember = isClientRequestMember;
|
|
6
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
7
|
+
const PRINCIPAL_PROPERTIES = new Set([
|
|
8
|
+
'user',
|
|
9
|
+
'auth',
|
|
10
|
+
'session',
|
|
11
|
+
'currentUser',
|
|
12
|
+
'principal',
|
|
13
|
+
'identity',
|
|
14
|
+
'userId',
|
|
15
|
+
'authInfo',
|
|
16
|
+
]);
|
|
17
|
+
const PRINCIPAL_RECEIVER = /^(req|request|res|response|ctx|locals)$/i;
|
|
18
|
+
const CLIENT_CONTAINERS = new Set(['body', 'query', 'params']);
|
|
19
|
+
const REQUEST_RECEIVER = /^(req|request)$/i;
|
|
20
|
+
function walk(node, visit) {
|
|
21
|
+
visit(node);
|
|
22
|
+
for (const key of Object.keys(node)) {
|
|
23
|
+
if (key === 'parent')
|
|
24
|
+
continue;
|
|
25
|
+
const value = node[key];
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
for (const item of value) {
|
|
28
|
+
if (item && typeof item === 'object' && 'type' in item) {
|
|
29
|
+
walk(item, visit);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (value && typeof value === 'object' && 'type' in value) {
|
|
35
|
+
walk(value, visit);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function readsPrincipal(node) {
|
|
40
|
+
let found = false;
|
|
41
|
+
walk(node, (child) => {
|
|
42
|
+
if (found)
|
|
43
|
+
return;
|
|
44
|
+
if (child.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
45
|
+
return;
|
|
46
|
+
if (child.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
47
|
+
return;
|
|
48
|
+
if (!PRINCIPAL_PROPERTIES.has(child.property.name))
|
|
49
|
+
return;
|
|
50
|
+
const { object } = child;
|
|
51
|
+
if (object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
52
|
+
found = PRINCIPAL_RECEIVER.test(object.name);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (object.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
56
|
+
object.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
57
|
+
found = PRINCIPAL_RECEIVER.test(object.property.name);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
function isClientRequestMember(node) {
|
|
63
|
+
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
64
|
+
return false;
|
|
65
|
+
const { object } = node;
|
|
66
|
+
if (object.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
67
|
+
return false;
|
|
68
|
+
if (object.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
69
|
+
return false;
|
|
70
|
+
if (!CLIENT_CONTAINERS.has(object.property.name))
|
|
71
|
+
return false;
|
|
72
|
+
const root = object.object;
|
|
73
|
+
if (root.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
74
|
+
return REQUEST_RECEIVER.test(root.name);
|
|
75
|
+
}
|
|
76
|
+
if (root.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
77
|
+
root.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
78
|
+
return REQUEST_RECEIVER.test(root.property.name);
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
package/AGENTS.md
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
# AGENTS.md
|
|
2
|
-
|
|
3
|
-
> Context for AI coding agents working on eslint-plugin-express-security
|
|
4
|
-
|
|
5
|
-
## Setup Commands
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
# Install dependencies (from monorepo root)
|
|
9
|
-
npm install
|
|
10
|
-
|
|
11
|
-
# Build this package
|
|
12
|
-
nx build eslint-plugin-express-security
|
|
13
|
-
|
|
14
|
-
# Run tests
|
|
15
|
-
nx test eslint-plugin-express-security
|
|
16
|
-
|
|
17
|
-
# Run tests with coverage
|
|
18
|
-
nx test eslint-plugin-express-security --coverage
|
|
19
|
-
|
|
20
|
-
# Lint this package
|
|
21
|
-
nx lint eslint-plugin-express-security
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Code Style
|
|
25
|
-
|
|
26
|
-
- TypeScript strict mode with `@interlace/eslint-devkit` types
|
|
27
|
-
- Use `AST_NODE_TYPES` constants, never string literals for node types
|
|
28
|
-
- Use `formatLLMMessage()` for all rule error messages
|
|
29
|
-
- Include CWE, CVSS, OWASP in every security message
|
|
30
|
-
- Coverage-ignore comments (`c8 ignore`, `v8 ignore`, `istanbul ignore`) are banned — write a test for the branch (synthetic AST / mock context if parser-unreachable) or delete it if dead
|
|
31
|
-
- Single-pass AST traversal patterns (O(n) complexity)
|
|
32
|
-
- Detect Express objects through heuristics (variable naming, import analysis)
|
|
33
|
-
|
|
34
|
-
## Testing Instructions
|
|
35
|
-
|
|
36
|
-
- Tests use `@typescript-eslint/rule-tester` with Vitest
|
|
37
|
-
- Each rule has `index.ts` (implementation) and `*.test.ts` (tests) in same directory
|
|
38
|
-
- Run specific rule test: `nx test eslint-plugin-express-security --testPathPattern="require-helmet"`
|
|
39
|
-
- Coverage target: ≥90% lines, ≥95% functions
|
|
40
|
-
- All tests must pass before committing
|
|
41
|
-
|
|
42
|
-
## Project Structure
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
src/
|
|
46
|
-
├── index.ts # Plugin entry, exports rules and 4 configs
|
|
47
|
-
├── types/index.ts # Exported rule option types
|
|
48
|
-
└── rules/ # 9 rule directories
|
|
49
|
-
└── [rule-name]/
|
|
50
|
-
├── index.ts # Rule implementation
|
|
51
|
-
└── *.test.ts # Rule tests
|
|
52
|
-
docs/rules/ # Markdown documentation per rule
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Plugin Purpose
|
|
56
|
-
|
|
57
|
-
Security-focused ESLint plugin with **9 rules** for Express.js applications. Covers security headers (helmet), CORS misconfigurations, CSRF protection, cookie security, rate limiting, ReDoS prevention, and GraphQL security.
|
|
58
|
-
|
|
59
|
-
## Rule Categories
|
|
60
|
-
|
|
61
|
-
| Category | Rules | CWEs |
|
|
62
|
-
| ------------------- | ------------------------------------------------------------------------------------------------------------ | ------------- |
|
|
63
|
-
| Headers & CORS | `require-helmet`, `no-permissive-cors`, `no-cors-credentials-wildcard`, `require-express-body-parser-limits` | 693, 770, 942 |
|
|
64
|
-
| CSRF & Cookies | `require-csrf-protection`, `no-insecure-cookie-options` | 352, 614 |
|
|
65
|
-
| Rate Limiting & DoS | `require-rate-limiting`, `no-express-unsafe-regex-route` | 770, 1333 |
|
|
66
|
-
| GraphQL | `no-graphql-introspection-production` | 200 |
|
|
67
|
-
|
|
68
|
-
## Common Fix Patterns
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
// Helmet
|
|
72
|
-
// BAD: const app = express()
|
|
73
|
-
// GOOD: const app = express(); app.use(helmet())
|
|
74
|
-
|
|
75
|
-
// CORS
|
|
76
|
-
// BAD: app.use(cors({ origin: '*' }))
|
|
77
|
-
// GOOD: app.use(cors({ origin: ['https://your-app.com'] }))
|
|
78
|
-
|
|
79
|
-
// CSRF
|
|
80
|
-
// BAD: app.post('/api/transfer', handler)
|
|
81
|
-
// GOOD: app.use(csurf({ cookie: true })); app.post('/api/transfer', handler)
|
|
82
|
-
|
|
83
|
-
// Cookies
|
|
84
|
-
// BAD: res.cookie('session', token)
|
|
85
|
-
// GOOD: res.cookie('session', token, { httpOnly: true, secure: true, sameSite: 'strict' })
|
|
86
|
-
|
|
87
|
-
// Rate limiting
|
|
88
|
-
// BAD: app.post('/api/login', handler)
|
|
89
|
-
// GOOD: app.use('/api/', rateLimit({ windowMs: 15*60*1000, max: 100 }))
|
|
90
|
-
|
|
91
|
-
// Body parser limits
|
|
92
|
-
// BAD: app.use(express.json())
|
|
93
|
-
// GOOD: app.use(express.json({ limit: '100kb' }))
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
## Security Considerations
|
|
97
|
-
|
|
98
|
-
- All rules map to OWASP Top 10 2021: A01, A03, A05, A07
|
|
99
|
-
- CWE coverage: 200, 352, 614, 693, 770, 942, 1333
|
|
100
|
-
- Middleware-aware detection: helmet, cors, csurf, express-rate-limit
|