eslint-plugin-express-security 0.0.1
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/AGENTS.md +100 -0
- package/CHANGELOG.md +51 -0
- package/README.md +424 -0
- package/package.json +99 -0
- package/src/index.d.ts +35 -0
- package/src/index.js +144 -0
- package/src/index.js.map +1 -0
- package/src/rules/no-cors-credentials-wildcard/index.d.ts +5 -0
- package/src/rules/no-cors-credentials-wildcard/index.js +142 -0
- package/src/rules/no-cors-credentials-wildcard/index.js.map +1 -0
- package/src/rules/no-express-unsafe-regex-route/index.d.ts +5 -0
- package/src/rules/no-express-unsafe-regex-route/index.js +181 -0
- package/src/rules/no-express-unsafe-regex-route/index.js.map +1 -0
- package/src/rules/no-graphql-introspection-production/index.d.ts +7 -0
- package/src/rules/no-graphql-introspection-production/index.js +161 -0
- package/src/rules/no-graphql-introspection-production/index.js.map +1 -0
- package/src/rules/no-insecure-cookie-options/index.d.ts +13 -0
- package/src/rules/no-insecure-cookie-options/index.js +162 -0
- package/src/rules/no-insecure-cookie-options/index.js.map +1 -0
- package/src/rules/no-permissive-cors/index.d.ts +9 -0
- package/src/rules/no-permissive-cors/index.js +205 -0
- package/src/rules/no-permissive-cors/index.js.map +1 -0
- package/src/rules/require-csrf-protection/index.d.ts +9 -0
- package/src/rules/require-csrf-protection/index.js +240 -0
- package/src/rules/require-csrf-protection/index.js.map +1 -0
- package/src/rules/require-express-body-parser-limits/index.d.ts +9 -0
- package/src/rules/require-express-body-parser-limits/index.js +201 -0
- package/src/rules/require-express-body-parser-limits/index.js.map +1 -0
- package/src/rules/require-helmet/index.d.ts +9 -0
- package/src/rules/require-helmet/index.js +185 -0
- package/src/rules/require-helmet/index.js.map +1 -0
- package/src/rules/require-rate-limiting/index.d.ts +12 -0
- package/src/rules/require-rate-limiting/index.js +142 -0
- package/src/rules/require-rate-limiting/index.js.map +1 -0
- package/src/types/index.d.ts +27 -0
- package/src/types/index.js +3 -0
- package/src/types/index.js.map +1 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireCsrfProtection = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const DEFAULT_PROTECTED_METHODS = ['post', 'put', 'patch', 'delete'];
|
|
6
|
+
/**
|
|
7
|
+
* Check if a call is a CSRF middleware usage
|
|
8
|
+
*/
|
|
9
|
+
function isCsrfMiddleware(node) {
|
|
10
|
+
const callee = node.callee;
|
|
11
|
+
// app.use(csrf()) or app.use(csurf())
|
|
12
|
+
if (callee.type === 'Identifier') {
|
|
13
|
+
const name = callee.name.toLowerCase();
|
|
14
|
+
return name === 'csrf' || name === 'csurf' || name === 'csrfprotection';
|
|
15
|
+
}
|
|
16
|
+
// lusca.csrf()
|
|
17
|
+
if (callee.type === 'MemberExpression' &&
|
|
18
|
+
callee.object.type === 'Identifier' &&
|
|
19
|
+
callee.object.name === 'lusca' &&
|
|
20
|
+
callee.property.type === 'Identifier' &&
|
|
21
|
+
callee.property.name === 'csrf') {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Check if route handler has CSRF middleware in its chain
|
|
28
|
+
*/
|
|
29
|
+
function hasCsrfInMiddlewareChain(node, sourceCode) {
|
|
30
|
+
const text = sourceCode.getText(node);
|
|
31
|
+
// Check for csrf/csurf in the middleware chain
|
|
32
|
+
return /\b(csrf|csurf|csrfProtection|csrfMiddleware)\s*\(?\)?/.test(text);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Known Express app/router variable names
|
|
36
|
+
*/
|
|
37
|
+
const EXPRESS_IDENTIFIERS = new Set([
|
|
38
|
+
'app',
|
|
39
|
+
'router',
|
|
40
|
+
'api',
|
|
41
|
+
'apiRouter',
|
|
42
|
+
'routes',
|
|
43
|
+
'express',
|
|
44
|
+
]);
|
|
45
|
+
/**
|
|
46
|
+
* Check if the callee object is likely an Express app or router
|
|
47
|
+
* This prevents false positives on non-Express objects
|
|
48
|
+
*/
|
|
49
|
+
function isLikelyExpressObject(callee) {
|
|
50
|
+
const obj = callee.object;
|
|
51
|
+
// Direct identifier: app.post(), router.post()
|
|
52
|
+
if (obj.type === 'Identifier') {
|
|
53
|
+
// Check known Express variable names
|
|
54
|
+
if (EXPRESS_IDENTIFIERS.has(obj.name)) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
// Skip unknown identifiers to avoid FPs
|
|
58
|
+
// Names like 'server', 'customApi', 'controller' are not Express
|
|
59
|
+
/* c8 ignore next */
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
// Call expression: express().post(), express.Router().post()
|
|
63
|
+
if (obj.type === 'CallExpression') {
|
|
64
|
+
const objCallee = obj.callee;
|
|
65
|
+
// express()
|
|
66
|
+
if (objCallee.type === 'Identifier' && objCallee.name === 'express') {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
// express.Router()
|
|
70
|
+
if (objCallee.type === 'MemberExpression' &&
|
|
71
|
+
objCallee.object.type === 'Identifier' &&
|
|
72
|
+
objCallee.object.name === 'express' &&
|
|
73
|
+
objCallee.property.type === 'Identifier' &&
|
|
74
|
+
objCallee.property.name === 'Router') {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Member expression: this.app, this.router - skip to avoid FPs
|
|
79
|
+
if (obj.type === 'MemberExpression') {
|
|
80
|
+
/* c8 ignore next */
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
/* c8 ignore next */
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
exports.requireCsrfProtection = (0, eslint_devkit_1.createRule)({
|
|
87
|
+
name: 'require-csrf-protection',
|
|
88
|
+
meta: {
|
|
89
|
+
type: 'problem',
|
|
90
|
+
docs: {
|
|
91
|
+
description: 'Require CSRF protection middleware for state-changing HTTP methods',
|
|
92
|
+
},
|
|
93
|
+
hasSuggestions: true,
|
|
94
|
+
messages: {
|
|
95
|
+
missingCsrf: (0, eslint_devkit_1.formatLLMMessage)({
|
|
96
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
97
|
+
issueName: 'Missing CSRF Protection',
|
|
98
|
+
cwe: 'CWE-352',
|
|
99
|
+
description: 'Route handler for {{method}} request lacks CSRF protection. Attackers can forge requests from malicious sites.',
|
|
100
|
+
severity: 'HIGH',
|
|
101
|
+
fix: "Add CSRF middleware: app.use(csrf()) or use csurf package. Include csrfToken in forms.",
|
|
102
|
+
documentationLink: 'https://owasp.org/www-community/attacks/csrf',
|
|
103
|
+
}),
|
|
104
|
+
addCsrf: (0, eslint_devkit_1.formatLLMMessage)({
|
|
105
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
106
|
+
issueName: 'Add CSRF Protection',
|
|
107
|
+
description: 'Add CSRF middleware to protect state-changing requests',
|
|
108
|
+
severity: 'LOW',
|
|
109
|
+
fix: "npm install csurf; app.use(csurf({ cookie: true }))",
|
|
110
|
+
documentationLink: 'https://www.npmjs.com/package/csurf',
|
|
111
|
+
}),
|
|
112
|
+
},
|
|
113
|
+
schema: [
|
|
114
|
+
{
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: {
|
|
117
|
+
allowInTests: {
|
|
118
|
+
type: 'boolean',
|
|
119
|
+
default: false,
|
|
120
|
+
description: 'Allow missing CSRF in test files',
|
|
121
|
+
},
|
|
122
|
+
protectedMethods: {
|
|
123
|
+
type: 'array',
|
|
124
|
+
items: { type: 'string' },
|
|
125
|
+
default: ['post', 'put', 'patch', 'delete'],
|
|
126
|
+
description: 'HTTP methods that require CSRF protection',
|
|
127
|
+
},
|
|
128
|
+
ignorePatterns: {
|
|
129
|
+
type: 'array',
|
|
130
|
+
items: { type: 'string' },
|
|
131
|
+
default: [],
|
|
132
|
+
description: 'Route patterns to ignore',
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
additionalProperties: false,
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
},
|
|
139
|
+
defaultOptions: [
|
|
140
|
+
{
|
|
141
|
+
allowInTests: false,
|
|
142
|
+
protectedMethods: DEFAULT_PROTECTED_METHODS,
|
|
143
|
+
ignorePatterns: [],
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
create(context, [options = {}]) {
|
|
147
|
+
const { allowInTests = false, protectedMethods = DEFAULT_PROTECTED_METHODS, ignorePatterns = [], } = options;
|
|
148
|
+
const filename = context.filename;
|
|
149
|
+
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
150
|
+
if (isTestFile) {
|
|
151
|
+
return {};
|
|
152
|
+
}
|
|
153
|
+
const sourceCode = context.sourceCode;
|
|
154
|
+
let hasGlobalCsrf = false;
|
|
155
|
+
/**
|
|
156
|
+
* Check if route matches ignore patterns
|
|
157
|
+
*/
|
|
158
|
+
function shouldIgnoreRoute(routeArg) {
|
|
159
|
+
if (routeArg.type !== 'Literal' || typeof routeArg.value !== 'string') {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
const route = routeArg.value;
|
|
163
|
+
return ignorePatterns.some((pattern) => {
|
|
164
|
+
try {
|
|
165
|
+
return new RegExp(pattern).test(route);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
/* c8 ignore next */
|
|
169
|
+
return route.includes(pattern);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
CallExpression(node) {
|
|
175
|
+
const callee = node.callee;
|
|
176
|
+
// Check for global app.use(csrf()) or app.use(csrfMiddleware)
|
|
177
|
+
if (callee.type === 'MemberExpression' &&
|
|
178
|
+
callee.property.type === 'Identifier' &&
|
|
179
|
+
callee.property.name === 'use') {
|
|
180
|
+
for (const arg of node.arguments) {
|
|
181
|
+
// app.use(csrf())
|
|
182
|
+
if (arg.type === 'CallExpression' && isCsrfMiddleware(arg)) {
|
|
183
|
+
hasGlobalCsrf = true;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
// app.use(csrfMiddleware) - variable reference
|
|
187
|
+
if (arg.type === 'Identifier') {
|
|
188
|
+
const name = arg.name.toLowerCase();
|
|
189
|
+
if (name.includes('csrf') || name.includes('csurf')) {
|
|
190
|
+
hasGlobalCsrf = true;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Check for route handlers: app.post(), router.put(), etc.
|
|
197
|
+
if (callee.type === 'MemberExpression' &&
|
|
198
|
+
callee.property.type === 'Identifier') {
|
|
199
|
+
const method = callee.property.name.toLowerCase();
|
|
200
|
+
if (!protectedMethods.includes(method)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// Only flag if this looks like an Express app/router
|
|
204
|
+
// This prevents FPs on non-Express objects like server.post(), customApi.post()
|
|
205
|
+
if (!isLikelyExpressObject(callee)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
// Skip if global CSRF is already set up
|
|
209
|
+
if (hasGlobalCsrf) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
// Check if route should be ignored
|
|
213
|
+
const routeArg = node.arguments[0];
|
|
214
|
+
if (routeArg && shouldIgnoreRoute(routeArg)) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// Check if CSRF middleware is in the handler chain
|
|
218
|
+
if (hasCsrfInMiddlewareChain(node, sourceCode)) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// Report missing CSRF
|
|
222
|
+
context.report({
|
|
223
|
+
node,
|
|
224
|
+
messageId: 'missingCsrf',
|
|
225
|
+
data: {
|
|
226
|
+
method: method.toUpperCase(),
|
|
227
|
+
},
|
|
228
|
+
suggest: [
|
|
229
|
+
{
|
|
230
|
+
messageId: 'addCsrf',
|
|
231
|
+
fix: () => null,
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-express-security/src/rules/require-csrf-protection/index.ts"],"names":[],"mappings":";;;AASA,4DAIkC;AAiBlC,MAAM,yBAAyB,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAErE;;GAEG;AACH,SAAS,gBAAgB,CAAC,IAA6B;IACrD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAE3B,sCAAsC;IACtC,IAAI,MAAM,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;QACvC,OAAO,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,gBAAgB,CAAC;IAC1E,CAAC;IAED,eAAe;IACf,IACE,MAAM,CAAC,IAAI,KAAK,kBAAkB;QAClC,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,YAAY;QACnC,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,OAAO;QAC9B,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,YAAY;QACrC,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,MAAM,EAC/B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,SAAS,wBAAwB,CAC/B,IAA6B,EAC7B,UAA+B;IAE/B,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,+CAA+C;IAC/C,OAAO,u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ACxC,IAAI,aAAa,EAAE,CAAC;wBAClB,OAAO;oBACT,CAAC;oBAED,mCAAmC;oBACnC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;oBACnC,IAAI,QAAQ,IAAI,iBAAiB,CAAC,QAAQ,CAAC,EAAE,CAAC;wBAC5C,OAAO;oBACT,CAAC;oBAED,mDAAmD;oBACnD,IAAI,wBAAwB,CAAC,IAAI,EAAE,UAAU,CAAC,EAAE,CAAC;wBAC/C,OAAO;oBACT,CAAC;oBAED,sBAAsB;oBACtB,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI;wBACJ,SAAS,EAAE,aAAa;wBACxB,IAAI,EAAE;4BACJ,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE;yBAC7B;wBACD,OAAO,EAAE;4BACP;gCACE,SAAS,EAAE,SAAS;gCACpB,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;6BAChB;yBACF;qBACF,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
/** Allow in test files. Default: false */
|
|
3
|
+
allowInTests?: boolean;
|
|
4
|
+
/** Maximum allowed limit in bytes. Default: 102400 (100kb) */
|
|
5
|
+
maxLimit?: number;
|
|
6
|
+
/** Limits that are considered excessive (as strings). Default: ['50mb', '100mb', '500mb', '1gb'] */
|
|
7
|
+
excessiveLimits?: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare const requireExpressBodyParserLimits: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireExpressBodyParserLimits = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
const DEFAULT_EXCESSIVE_LIMITS = [
|
|
6
|
+
'10mb',
|
|
7
|
+
'50mb',
|
|
8
|
+
'100mb',
|
|
9
|
+
'500mb',
|
|
10
|
+
'1gb',
|
|
11
|
+
'1GB',
|
|
12
|
+
'10MB',
|
|
13
|
+
'50MB',
|
|
14
|
+
'100MB',
|
|
15
|
+
'500MB',
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* Body parser function names to check
|
|
19
|
+
*/
|
|
20
|
+
const BODY_PARSER_METHODS = ['json', 'urlencoded', 'raw', 'text'];
|
|
21
|
+
/**
|
|
22
|
+
* Check if a property has a limit option
|
|
23
|
+
*/
|
|
24
|
+
function getLimitOption(properties) {
|
|
25
|
+
for (const prop of properties) {
|
|
26
|
+
if (prop.type === 'Property' &&
|
|
27
|
+
prop.key.type === 'Identifier' &&
|
|
28
|
+
prop.key.name === 'limit') {
|
|
29
|
+
return prop;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Check if the limit value is considered excessive
|
|
36
|
+
*/
|
|
37
|
+
function isExcessiveLimit(value, excessiveLimits) {
|
|
38
|
+
if (value.type === 'Literal' && typeof value.value === 'string') {
|
|
39
|
+
return excessiveLimits.some((limit) => value.value === limit || String(value.value).toLowerCase() === limit.toLowerCase());
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
exports.requireExpressBodyParserLimits = (0, eslint_devkit_1.createRule)({
|
|
44
|
+
name: 'require-express-body-parser-limits',
|
|
45
|
+
meta: {
|
|
46
|
+
type: 'problem',
|
|
47
|
+
docs: {
|
|
48
|
+
description: 'Require size limits on Express.js body parsers to prevent DoS attacks',
|
|
49
|
+
},
|
|
50
|
+
hasSuggestions: true,
|
|
51
|
+
messages: {
|
|
52
|
+
missingLimit: (0, eslint_devkit_1.formatLLMMessage)({
|
|
53
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
54
|
+
issueName: 'Missing Body Parser Limit',
|
|
55
|
+
cwe: 'CWE-400',
|
|
56
|
+
description: 'Body parser used without explicit size limit. Attackers can exhaust server memory with large payloads. CVE-2024-45590.',
|
|
57
|
+
severity: 'HIGH',
|
|
58
|
+
fix: "Add a size limit: express.json({ limit: '10kb' }) or express.json({ limit: '100kb' })",
|
|
59
|
+
documentationLink: 'https://expressjs.com/en/4x/api.html#express.json',
|
|
60
|
+
}),
|
|
61
|
+
excessiveLimit: (0, eslint_devkit_1.formatLLMMessage)({
|
|
62
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
63
|
+
issueName: 'Excessive Body Parser Limit',
|
|
64
|
+
cwe: 'CWE-400',
|
|
65
|
+
description: 'Body parser limit is excessively high, making the application vulnerable to DoS attacks via large payloads.',
|
|
66
|
+
severity: 'MEDIUM',
|
|
67
|
+
fix: "Reduce the limit to a reasonable size: '100kb' for JSON APIs, '1mb' for file uploads with proper handling",
|
|
68
|
+
documentationLink: 'https://expressjs.com/en/4x/api.html#express.json',
|
|
69
|
+
}),
|
|
70
|
+
addLimit: (0, eslint_devkit_1.formatLLMMessage)({
|
|
71
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
72
|
+
issueName: 'Add Size Limit',
|
|
73
|
+
description: 'Add a size limit to the body parser',
|
|
74
|
+
severity: 'LOW',
|
|
75
|
+
fix: "Add limit: '100kb' or appropriate size for your use case",
|
|
76
|
+
documentationLink: 'https://expressjs.com/en/4x/api.html#express.json',
|
|
77
|
+
}),
|
|
78
|
+
},
|
|
79
|
+
schema: [
|
|
80
|
+
{
|
|
81
|
+
type: 'object',
|
|
82
|
+
properties: {
|
|
83
|
+
allowInTests: {
|
|
84
|
+
type: 'boolean',
|
|
85
|
+
default: false,
|
|
86
|
+
description: 'Allow in test files',
|
|
87
|
+
},
|
|
88
|
+
maxLimit: {
|
|
89
|
+
type: 'number',
|
|
90
|
+
default: 102400,
|
|
91
|
+
description: 'Maximum allowed limit in bytes',
|
|
92
|
+
},
|
|
93
|
+
excessiveLimits: {
|
|
94
|
+
type: 'array',
|
|
95
|
+
items: { type: 'string' },
|
|
96
|
+
default: DEFAULT_EXCESSIVE_LIMITS,
|
|
97
|
+
description: 'Limits considered excessive',
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
additionalProperties: false,
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
},
|
|
104
|
+
defaultOptions: [
|
|
105
|
+
{
|
|
106
|
+
allowInTests: false,
|
|
107
|
+
maxLimit: 102400,
|
|
108
|
+
excessiveLimits: DEFAULT_EXCESSIVE_LIMITS,
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
create(context, [options = {}]) {
|
|
112
|
+
const { allowInTests = false, excessiveLimits = DEFAULT_EXCESSIVE_LIMITS, } = options;
|
|
113
|
+
const filename = context.filename;
|
|
114
|
+
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
115
|
+
if (isTestFile) {
|
|
116
|
+
return {};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
CallExpression(node) {
|
|
120
|
+
const callee = node.callee;
|
|
121
|
+
// Check for express.json(), express.urlencoded(), etc.
|
|
122
|
+
// or bodyParser.json(), bodyParser.urlencoded(), etc.
|
|
123
|
+
if (callee.type !== 'MemberExpression') {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const object = callee.object;
|
|
127
|
+
const property = callee.property;
|
|
128
|
+
// Check if it's express.* or bodyParser.*
|
|
129
|
+
/* c8 ignore next 2 */
|
|
130
|
+
if (object.type !== 'Identifier') {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (object.name !== 'express' && object.name !== 'bodyParser') {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
/* c8 ignore next 2 */
|
|
137
|
+
if (property.type !== 'Identifier') {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (!BODY_PARSER_METHODS.includes(property.name)) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Check arguments
|
|
144
|
+
const firstArg = node.arguments[0];
|
|
145
|
+
// No options provided - missing limit
|
|
146
|
+
if (!firstArg) {
|
|
147
|
+
const sourceCode = context.sourceCode;
|
|
148
|
+
const calleeText = sourceCode.getText(callee);
|
|
149
|
+
context.report({
|
|
150
|
+
node,
|
|
151
|
+
messageId: 'missingLimit',
|
|
152
|
+
suggest: [
|
|
153
|
+
{
|
|
154
|
+
messageId: 'addLimit',
|
|
155
|
+
fix: (fixer) => {
|
|
156
|
+
// Replace the entire call with options
|
|
157
|
+
return fixer.replaceText(node, `${calleeText}({ limit: '100kb' })`);
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// Options provided but check for limit
|
|
165
|
+
if (firstArg.type === 'ObjectExpression') {
|
|
166
|
+
const limitProp = getLimitOption(firstArg.properties);
|
|
167
|
+
if (!limitProp) {
|
|
168
|
+
// No limit in options
|
|
169
|
+
context.report({
|
|
170
|
+
node,
|
|
171
|
+
messageId: 'missingLimit',
|
|
172
|
+
suggest: [
|
|
173
|
+
{
|
|
174
|
+
messageId: 'addLimit',
|
|
175
|
+
fix: (fixer) => {
|
|
176
|
+
// Add limit to existing options
|
|
177
|
+
const lastProp = firstArg.properties[firstArg.properties.length - 1];
|
|
178
|
+
if (lastProp) {
|
|
179
|
+
return fixer.insertTextAfter(lastProp, ", limit: '100kb'");
|
|
180
|
+
}
|
|
181
|
+
/* c8 ignore next */
|
|
182
|
+
return null;
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// Check if limit is excessive
|
|
190
|
+
if (isExcessiveLimit(limitProp.value, excessiveLimits)) {
|
|
191
|
+
context.report({
|
|
192
|
+
node: limitProp,
|
|
193
|
+
messageId: 'excessiveLimit',
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-express-security/src/rules/require-express-body-parser-limits/index.ts"],"names":[],"mappings":";;;AAYA,4DAIkC;AAelC,MAAM,wBAAwB,GAAG;IAC/B,MAAM;IACN,MAAM;IACN,OAAO;IACP,OAAO;IACP,KAAK;IACL,KAAK;IACL,MAAM;IACN,MAAM;IACN,OAAO;IACP,OAAO;CACR,CAAC;AAEF;;GAEG;AACH,MAAM,mBAAmB,GAAG,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAElE;;GAEG;AACH,SAAS,cAAc,CACrB,UAA2C;IAE3C,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;QAC9B,IACE,IAAI,CAAC,IAAI,KAAK,UAAU;YACxB,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,YAAY;YAC9B,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,OAAO,EACzB,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;GAEG;AACH,SAAS,gBAAgB,CACvB,KAAoB,EACpB,eAAyB;IAEzB,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;QAChE,OAAO,eAAe,CAAC,IAAI,CACzB,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,KAAK,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,WAAW,EAAE,CAC9F,CAAC;IACJ,C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GAAG,CAAC,CAAC,CAAC;wCACtD,IAAI,QAAQ,EAAE,CAAC;4CACb,OAAO,KAAK,CAAC,eAAe,CAC1B,QAAQ,EACR,kBAAkB,CACnB,CAAC;wCACJ,CAAC;wCACD,oBAAoB;wCACpB,OAAO,IAAI,CAAC;oCACd,CAAC;iCACF;6BACF;yBACF,CAAC,CAAC;wBACH,OAAO;oBACT,CAAC;oBAED,8BAA8B;oBAC9B,IAAI,gBAAgB,CAAC,SAAS,CAAC,KAAK,EAAE,eAAe,CAAC,EAAE,CAAC;wBACvD,OAAO,CAAC,MAAM,CAAC;4BACb,IAAI,EAAE,SAAS;4BACf,SAAS,EAAE,gBAAgB;yBAC5B,CAAC,CAAC;oBACL,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
/** Allow missing helmet in test files. Default: false */
|
|
3
|
+
allowInTests?: boolean;
|
|
4
|
+
/** Alternative security headers middleware names to accept. Default: [] */
|
|
5
|
+
alternativeMiddleware?: string[];
|
|
6
|
+
/** Skip rule if helmet/security headers are provided elsewhere (e.g., reverse proxy). Default: false */
|
|
7
|
+
assumeHelmetMiddleware?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const requireHelmet: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireHelmet = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
/**
|
|
6
|
+
* Check if a node is an app.use() call with helmet
|
|
7
|
+
*/
|
|
8
|
+
function isHelmetMiddleware(node) {
|
|
9
|
+
const callee = node.callee;
|
|
10
|
+
// Check for app.use(helmet()) or app.use(helmet.contentSecurityPolicy())
|
|
11
|
+
if (callee.type !== 'MemberExpression') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (callee.property.type !== 'Identifier' ||
|
|
15
|
+
callee.property.name !== 'use') {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
// Check arguments for helmet usage
|
|
19
|
+
for (const arg of node.arguments) {
|
|
20
|
+
if (arg.type === 'CallExpression') {
|
|
21
|
+
const argCallee = arg.callee;
|
|
22
|
+
// helmet()
|
|
23
|
+
if (argCallee.type === 'Identifier' && argCallee.name === 'helmet') {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
// helmet.contentSecurityPolicy() etc.
|
|
27
|
+
if (argCallee.type === 'MemberExpression' &&
|
|
28
|
+
argCallee.object.type === 'Identifier' &&
|
|
29
|
+
argCallee.object.name === 'helmet') {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// app.use(helmet) without call (unlikely but possible)
|
|
34
|
+
/* c8 ignore next 3 */
|
|
35
|
+
if (arg.type === 'Identifier' && arg.name === 'helmet') {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if middleware is an alternative security headers middleware
|
|
43
|
+
*/
|
|
44
|
+
function isAlternativeMiddleware(node, alternatives) {
|
|
45
|
+
for (const arg of node.arguments) {
|
|
46
|
+
if (arg.type === 'CallExpression' && arg.callee.type === 'Identifier') {
|
|
47
|
+
if (alternatives.includes(arg.callee.name)) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/* c8 ignore next 3 */
|
|
52
|
+
if (arg.type === 'Identifier' && alternatives.includes(arg.name)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
exports.requireHelmet = (0, eslint_devkit_1.createRule)({
|
|
59
|
+
name: 'require-helmet',
|
|
60
|
+
meta: {
|
|
61
|
+
type: 'problem',
|
|
62
|
+
docs: {
|
|
63
|
+
description: 'Require helmet middleware for security headers in Express.js applications',
|
|
64
|
+
},
|
|
65
|
+
hasSuggestions: true,
|
|
66
|
+
messages: {
|
|
67
|
+
missingHelmet: (0, eslint_devkit_1.formatLLMMessage)({
|
|
68
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
69
|
+
issueName: 'Missing Security Headers',
|
|
70
|
+
cwe: 'CWE-693',
|
|
71
|
+
description: 'Express app created without helmet middleware. Missing security headers: X-Content-Type-Options, X-Frame-Options, Strict-Transport-Security, etc.',
|
|
72
|
+
severity: 'HIGH',
|
|
73
|
+
fix: "Add helmet middleware: app.use(helmet()). Install with 'npm install helmet'.",
|
|
74
|
+
documentationLink: 'https://helmetjs.github.io/',
|
|
75
|
+
}),
|
|
76
|
+
addHelmet: (0, eslint_devkit_1.formatLLMMessage)({
|
|
77
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
78
|
+
issueName: 'Add Helmet',
|
|
79
|
+
description: 'Add helmet middleware for security headers',
|
|
80
|
+
severity: 'LOW',
|
|
81
|
+
fix: "import helmet from 'helmet'; app.use(helmet());",
|
|
82
|
+
documentationLink: 'https://helmetjs.github.io/',
|
|
83
|
+
}),
|
|
84
|
+
},
|
|
85
|
+
schema: [
|
|
86
|
+
{
|
|
87
|
+
type: 'object',
|
|
88
|
+
properties: {
|
|
89
|
+
allowInTests: {
|
|
90
|
+
type: 'boolean',
|
|
91
|
+
default: false,
|
|
92
|
+
description: 'Allow missing helmet in test files',
|
|
93
|
+
},
|
|
94
|
+
alternativeMiddleware: {
|
|
95
|
+
type: 'array',
|
|
96
|
+
items: { type: 'string' },
|
|
97
|
+
default: [],
|
|
98
|
+
description: 'Alternative security headers middleware names to accept',
|
|
99
|
+
},
|
|
100
|
+
assumeHelmetMiddleware: {
|
|
101
|
+
type: 'boolean',
|
|
102
|
+
default: false,
|
|
103
|
+
description: 'Skip rule if security headers are provided elsewhere (e.g., reverse proxy)',
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
additionalProperties: false,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
defaultOptions: [
|
|
111
|
+
{
|
|
112
|
+
allowInTests: false,
|
|
113
|
+
alternativeMiddleware: [],
|
|
114
|
+
assumeHelmetMiddleware: false,
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
create(context, [options = {}]) {
|
|
118
|
+
const { allowInTests = false, alternativeMiddleware = [], assumeHelmetMiddleware = false } = options;
|
|
119
|
+
// Skip entirely if helmet/security headers are assumed to be provided elsewhere
|
|
120
|
+
if (assumeHelmetMiddleware) {
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
const filename = context.filename;
|
|
124
|
+
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
125
|
+
if (isTestFile) {
|
|
126
|
+
return {};
|
|
127
|
+
}
|
|
128
|
+
// Track if we've seen express() call and helmet usage
|
|
129
|
+
let hasExpressApp = false;
|
|
130
|
+
let hasHelmet = false;
|
|
131
|
+
let expressAppNode = null;
|
|
132
|
+
return {
|
|
133
|
+
// Detect express() app creation
|
|
134
|
+
CallExpression(node) {
|
|
135
|
+
const callee = node.callee;
|
|
136
|
+
// Check for express() call
|
|
137
|
+
if (callee.type === 'Identifier' && callee.name === 'express') {
|
|
138
|
+
hasExpressApp = true;
|
|
139
|
+
expressAppNode = node;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// Check for require('express')() pattern
|
|
143
|
+
/* c8 ignore next 15 */
|
|
144
|
+
if (callee.type === 'CallExpression' &&
|
|
145
|
+
callee.callee.type === 'Identifier' &&
|
|
146
|
+
callee.callee.name === 'require') {
|
|
147
|
+
const firstArg = callee.arguments[0];
|
|
148
|
+
if (firstArg &&
|
|
149
|
+
firstArg.type === 'Literal' &&
|
|
150
|
+
firstArg.value === 'express') {
|
|
151
|
+
hasExpressApp = true;
|
|
152
|
+
expressAppNode = node;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Check for helmet middleware usage
|
|
157
|
+
if (isHelmetMiddleware(node)) {
|
|
158
|
+
hasHelmet = true;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Check for alternative middleware
|
|
162
|
+
if (isAlternativeMiddleware(node, alternativeMiddleware)) {
|
|
163
|
+
hasHelmet = true;
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
// Report at the end of the file if express app exists but no helmet
|
|
168
|
+
'Program:exit'() {
|
|
169
|
+
if (hasExpressApp && !hasHelmet && expressAppNode) {
|
|
170
|
+
context.report({
|
|
171
|
+
node: expressAppNode,
|
|
172
|
+
messageId: 'missingHelmet',
|
|
173
|
+
suggest: [
|
|
174
|
+
{
|
|
175
|
+
messageId: 'addHelmet',
|
|
176
|
+
fix: () => null, // Manual fix required - need to add import and app.use()
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
//# sourceMappingURL=index.js.map
|