eslint-plugin-express-security 1.5.4 → 1.5.6

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.
Files changed (34) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -3
  3. package/src/index.js +1 -113
  4. package/src/oxlint.js +1 -3
  5. package/src/rules/no-client-controlled-authorization/index.js +1 -194
  6. package/src/rules/no-cors-credentials-wildcard/index.js +1 -127
  7. package/src/rules/no-disabled-helmet-protections/index.js +1 -125
  8. package/src/rules/no-error-details-in-response/index.js +1 -176
  9. package/src/rules/no-exposed-debug-endpoints/index.js +1 -96
  10. package/src/rules/no-express-unsafe-regex-route/index.js +1 -154
  11. package/src/rules/no-graphql-introspection-production/index.js +1 -143
  12. package/src/rules/no-host-header-in-links/index.js +1 -235
  13. package/src/rules/no-idor-resource-access/index.js +1 -126
  14. package/src/rules/no-insecure-cookie-options/index.js +1 -158
  15. package/src/rules/no-missing-cors-check/index.js +1 -351
  16. package/src/rules/no-missing-csrf-protection/index.js +1 -161
  17. package/src/rules/no-missing-security-headers/index.js +1 -206
  18. package/src/rules/no-permissive-cors/index.js +1 -185
  19. package/src/rules/no-permissive-trust-proxy/index.js +1 -106
  20. package/src/rules/no-sensitive-data-in-query/index.js +1 -182
  21. package/src/rules/no-static-root-exposure/index.js +1 -267
  22. package/src/rules/no-unsafe-csp-directives/index.js +1 -264
  23. package/src/rules/no-user-controlled-redirect/index.js +1 -117
  24. package/src/rules/no-user-controlled-render-locals/index.js +1 -268
  25. package/src/rules/require-case-insensitive-path-guard/index.js +1 -180
  26. package/src/rules/require-csrf-protection/index.js +1 -201
  27. package/src/rules/require-express-body-parser-limits/index.js +1 -181
  28. package/src/rules/require-helmet/index.js +1 -166
  29. package/src/rules/require-query-type-guard/index.js +1 -258
  30. package/src/rules/require-rate-limiting/index.js +1 -136
  31. package/src/rules/require-route-authentication/index.js +1 -225
  32. package/src/rules/require-strict-transport-security/index.js +1 -171
  33. package/src/types/index.js +1 -2
  34. package/src/utils/index.js +1 -81
@@ -1,201 +1 @@
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
- function isCsrfMiddleware(node) {
7
- const callee = node.callee;
8
- if (callee.type === 'Identifier') {
9
- const name = callee.name.toLowerCase();
10
- return name === 'csrf' || name === 'csurf' || name === 'csrfprotection';
11
- }
12
- if (callee.type === 'MemberExpression' &&
13
- callee.object.type === 'Identifier' &&
14
- callee.object.name === 'lusca' &&
15
- callee.property.type === 'Identifier' &&
16
- callee.property.name === 'csrf') {
17
- return true;
18
- }
19
- return false;
20
- }
21
- function hasCsrfInMiddlewareChain(node, sourceCode) {
22
- const text = sourceCode.getText(node);
23
- return /\b(csrf|csurf|csrfProtection|csrfMiddleware)\s*\(?\)?/.test(text);
24
- }
25
- const EXPRESS_IDENTIFIERS = new Set([
26
- 'app',
27
- 'router',
28
- 'api',
29
- 'apiRouter',
30
- 'routes',
31
- 'express',
32
- ]);
33
- function isLikelyExpressObject(callee) {
34
- const obj = callee.object;
35
- if (obj.type === 'Identifier') {
36
- if (EXPRESS_IDENTIFIERS.has(obj.name)) {
37
- return true;
38
- }
39
- return false;
40
- }
41
- if (obj.type === 'CallExpression') {
42
- const objCallee = obj.callee;
43
- if (objCallee.type === 'Identifier' && objCallee.name === 'express') {
44
- return true;
45
- }
46
- if (objCallee.type === 'MemberExpression' &&
47
- objCallee.object.type === 'Identifier' &&
48
- objCallee.object.name === 'express' &&
49
- objCallee.property.type === 'Identifier' &&
50
- objCallee.property.name === 'Router') {
51
- return true;
52
- }
53
- }
54
- if (obj.type === 'MemberExpression') {
55
- return false;
56
- }
57
- return false;
58
- }
59
- exports.requireCsrfProtection = (0, eslint_devkit_1.createRule)({
60
- name: 'require-csrf-protection',
61
- meta: {
62
- type: 'problem',
63
- docs: {
64
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-csrf-protection.md',
65
- description: 'Require CSRF protection middleware for state-changing HTTP methods',
66
- cwe: 'CWE-352',
67
- cvss: 8.8,
68
- },
69
- hasSuggestions: true,
70
- messages: {
71
- missingCsrf: (0, eslint_devkit_1.formatLLMMessage)({
72
- icon: eslint_devkit_1.MessageIcons.SECURITY,
73
- issueName: 'Missing CSRF Protection',
74
- cwe: 'CWE-352',
75
- description: 'Route handler for {{method}} request lacks CSRF protection. Attackers can forge requests from malicious sites.',
76
- severity: 'HIGH',
77
- fix: "Add CSRF middleware: app.use(csrf()) or use csurf package. Include csrfToken in forms.",
78
- documentationLink: 'https://owasp.org/www-community/attacks/csrf',
79
- }),
80
- addCsrf: (0, eslint_devkit_1.formatLLMMessage)({
81
- icon: eslint_devkit_1.MessageIcons.INFO,
82
- issueName: 'Add CSRF Protection',
83
- description: 'Add CSRF middleware to protect state-changing requests',
84
- severity: 'LOW',
85
- fix: "npm install csurf; app.use(csurf({ cookie: true }))",
86
- documentationLink: 'https://www.npmjs.com/package/csurf',
87
- }),
88
- },
89
- schema: [
90
- {
91
- type: 'object',
92
- properties: {
93
- allowInTests: {
94
- type: 'boolean',
95
- default: false,
96
- description: 'Allow missing CSRF in test files',
97
- },
98
- protectedMethods: {
99
- type: 'array',
100
- items: { type: 'string' },
101
- default: ['post', 'put', 'patch', 'delete'],
102
- description: 'HTTP methods that require CSRF protection',
103
- },
104
- ignorePatterns: {
105
- type: 'array',
106
- items: { type: 'string' },
107
- default: [],
108
- description: 'Route patterns to ignore',
109
- },
110
- },
111
- additionalProperties: false,
112
- },
113
- ],
114
- },
115
- defaultOptions: [
116
- {
117
- allowInTests: false,
118
- protectedMethods: DEFAULT_PROTECTED_METHODS,
119
- ignorePatterns: [],
120
- },
121
- ],
122
- create(context, [options]) {
123
- const { allowInTests = false, protectedMethods = DEFAULT_PROTECTED_METHODS, ignorePatterns = [], } = options;
124
- const filename = context.filename;
125
- const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
126
- if (isTestFile) {
127
- return {};
128
- }
129
- const sourceCode = context.sourceCode;
130
- let hasGlobalCsrf = false;
131
- function shouldIgnoreRoute(routeArg) {
132
- if (routeArg.type !== 'Literal' || typeof routeArg.value !== 'string') {
133
- return false;
134
- }
135
- const route = routeArg.value;
136
- return ignorePatterns.some((pattern) => {
137
- try {
138
- return new RegExp(pattern).test(route);
139
- }
140
- catch {
141
- return route.includes(pattern);
142
- }
143
- });
144
- }
145
- return {
146
- CallExpression(node) {
147
- const callee = node.callee;
148
- if (callee.type === 'MemberExpression' &&
149
- callee.property.type === 'Identifier' &&
150
- callee.property.name === 'use') {
151
- for (const arg of node.arguments) {
152
- if (arg.type === 'CallExpression' && isCsrfMiddleware(arg)) {
153
- hasGlobalCsrf = true;
154
- return;
155
- }
156
- if (arg.type === 'Identifier') {
157
- const name = arg.name.toLowerCase();
158
- if (name.includes('csrf') || name.includes('csurf')) {
159
- hasGlobalCsrf = true;
160
- return;
161
- }
162
- }
163
- }
164
- }
165
- if (callee.type === 'MemberExpression' &&
166
- callee.property.type === 'Identifier') {
167
- const method = callee.property.name.toLowerCase();
168
- if (!protectedMethods.includes(method)) {
169
- return;
170
- }
171
- if (!isLikelyExpressObject(callee)) {
172
- return;
173
- }
174
- if (hasGlobalCsrf) {
175
- return;
176
- }
177
- const routeArg = node.arguments[0];
178
- if (routeArg && shouldIgnoreRoute(routeArg)) {
179
- return;
180
- }
181
- if (hasCsrfInMiddlewareChain(node, sourceCode)) {
182
- return;
183
- }
184
- context.report({
185
- node,
186
- messageId: 'missingCsrf',
187
- data: {
188
- method: method.toUpperCase(),
189
- },
190
- suggest: [
191
- {
192
- messageId: 'addCsrf',
193
- fix: () => null,
194
- },
195
- ],
196
- });
197
- }
198
- },
199
- };
200
- },
201
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireCsrfProtection=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DEFAULT_PROTECTED_METHODS=["post","put","patch","delete"];function isCsrfMiddleware(node){const callee=node.callee;if(callee.type==="Identifier"){const name=callee.name.toLowerCase();return name==="csrf"||name==="csurf"||name==="csrfprotection"}if(callee.type==="MemberExpression"&&callee.object.type==="Identifier"&&callee.object.name==="lusca"&&callee.property.type==="Identifier"&&callee.property.name==="csrf"){return true}return false}function hasCsrfInMiddlewareChain(node,sourceCode){const text=sourceCode.getText(node);return/\b(csrf|csurf|csrfProtection|csrfMiddleware)\s*\(?\)?/.test(text)}const EXPRESS_IDENTIFIERS=new Set(["app","router","api","apiRouter","routes","express"]);function isLikelyExpressObject(callee){const obj=callee.object;if(obj.type==="Identifier"){if(EXPRESS_IDENTIFIERS.has(obj.name)){return true}return false}if(obj.type==="CallExpression"){const objCallee=obj.callee;if(objCallee.type==="Identifier"&&objCallee.name==="express"){return true}if(objCallee.type==="MemberExpression"&&objCallee.object.type==="Identifier"&&objCallee.object.name==="express"&&objCallee.property.type==="Identifier"&&objCallee.property.name==="Router"){return true}}if(obj.type==="MemberExpression"){return false}return false}exports.requireCsrfProtection=(0,eslint_devkit_1.createRule)({name:"require-csrf-protection",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-csrf-protection.md",description:"Require CSRF protection middleware for state-changing HTTP methods",cwe:"CWE-352",cvss:8.8},hasSuggestions:true,messages:{missingCsrf:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing CSRF Protection",cwe:"CWE-352",description:"Route handler for {{method}} request lacks CSRF protection. Attackers can forge requests from malicious sites.",severity:"HIGH",fix:"Add CSRF middleware: app.use(csrf()) or use csurf package. Include csrfToken in forms.",documentationLink:"https://owasp.org/www-community/attacks/csrf"}),addCsrf:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add CSRF Protection",description:"Add CSRF middleware to protect state-changing requests",severity:"LOW",fix:"npm install csurf; app.use(csurf({ cookie: true }))",documentationLink:"https://www.npmjs.com/package/csurf"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow missing CSRF in test files"},protectedMethods:{type:"array",items:{type:"string"},default:["post","put","patch","delete"],description:"HTTP methods that require CSRF protection"},ignorePatterns:{type:"array",items:{type:"string"},default:[],description:"Route patterns to ignore"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,protectedMethods:DEFAULT_PROTECTED_METHODS,ignorePatterns:[]}],create(context,[options]){const{allowInTests=false,protectedMethods=DEFAULT_PROTECTED_METHODS,ignorePatterns=[]}=options;const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}const sourceCode=context.sourceCode;let hasGlobalCsrf=false;function shouldIgnoreRoute(routeArg){if(routeArg.type!=="Literal"||typeof routeArg.value!=="string"){return false}const route=routeArg.value;return ignorePatterns.some(pattern=>{try{return new RegExp(pattern).test(route)}catch{return route.includes(pattern)}})}return{CallExpression(node){const callee=node.callee;if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"&&callee.property.name==="use"){for(const arg of node.arguments){if(arg.type==="CallExpression"&&isCsrfMiddleware(arg)){hasGlobalCsrf=true;return}if(arg.type==="Identifier"){const name=arg.name.toLowerCase();if(name.includes("csrf")||name.includes("csurf")){hasGlobalCsrf=true;return}}}}if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"){const method=callee.property.name.toLowerCase();if(!protectedMethods.includes(method)){return}if(!isLikelyExpressObject(callee)){return}if(hasGlobalCsrf){return}const routeArg=node.arguments[0];if(routeArg&&shouldIgnoreRoute(routeArg)){return}if(hasCsrfInMiddlewareChain(node,sourceCode)){return}context.report({node,messageId:"missingCsrf",data:{method:method.toUpperCase()},suggest:[{messageId:"addCsrf",fix:()=>null}]})}}}}});
@@ -1,181 +1 @@
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
- const BODY_PARSER_METHODS = new Set(['json', 'urlencoded', 'raw', 'text']);
18
- function getLimitOption(properties) {
19
- for (const prop of properties) {
20
- if (prop.type === 'Property' &&
21
- prop.key.type === 'Identifier' &&
22
- prop.key.name === 'limit') {
23
- return prop;
24
- }
25
- }
26
- return null;
27
- }
28
- function isExcessiveLimit(value, excessiveLimits) {
29
- if (value.type === 'Literal' && typeof value.value === 'string') {
30
- return excessiveLimits.some((limit) => value.value === limit || String(value.value).toLowerCase() === limit.toLowerCase());
31
- }
32
- return false;
33
- }
34
- exports.requireExpressBodyParserLimits = (0, eslint_devkit_1.createRule)({
35
- name: 'require-express-body-parser-limits',
36
- meta: {
37
- type: 'problem',
38
- docs: {
39
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-express-body-parser-limits.md',
40
- description: 'Require size limits on Express.js body parsers to prevent DoS attacks',
41
- cwe: 'CWE-400',
42
- cvss: 7.5,
43
- },
44
- hasSuggestions: true,
45
- messages: {
46
- missingLimit: (0, eslint_devkit_1.formatLLMMessage)({
47
- icon: eslint_devkit_1.MessageIcons.SECURITY,
48
- issueName: 'Missing Body Parser Limit',
49
- cwe: 'CWE-400',
50
- description: 'Body parser used without explicit size limit. Attackers can exhaust server memory with large payloads. CVE-2024-45590.',
51
- severity: 'HIGH',
52
- fix: "Add a size limit: express.json({ limit: '10kb' }) or express.json({ limit: '100kb' })",
53
- documentationLink: 'https://expressjs.com/en/4x/api.html#express.json',
54
- }),
55
- excessiveLimit: (0, eslint_devkit_1.formatLLMMessage)({
56
- icon: eslint_devkit_1.MessageIcons.SECURITY,
57
- issueName: 'Excessive Body Parser Limit',
58
- cwe: 'CWE-400',
59
- description: 'Body parser limit is excessively high, making the application vulnerable to DoS attacks via large payloads.',
60
- severity: 'MEDIUM',
61
- fix: "Reduce the limit to a reasonable size: '100kb' for JSON APIs, '1mb' for file uploads with proper handling",
62
- documentationLink: 'https://expressjs.com/en/4x/api.html#express.json',
63
- }),
64
- addLimit: (0, eslint_devkit_1.formatLLMMessage)({
65
- icon: eslint_devkit_1.MessageIcons.INFO,
66
- issueName: 'Add Size Limit',
67
- description: 'Add a size limit to the body parser',
68
- severity: 'LOW',
69
- fix: "Add limit: '100kb' or appropriate size for your use case",
70
- documentationLink: 'https://expressjs.com/en/4x/api.html#express.json',
71
- }),
72
- },
73
- schema: [
74
- {
75
- type: 'object',
76
- properties: {
77
- allowInTests: {
78
- type: 'boolean',
79
- default: false,
80
- description: 'Allow in test files',
81
- },
82
- maxLimit: {
83
- type: 'number',
84
- default: 102400,
85
- description: 'Maximum allowed limit in bytes',
86
- },
87
- excessiveLimits: {
88
- type: 'array',
89
- items: { type: 'string' },
90
- default: DEFAULT_EXCESSIVE_LIMITS,
91
- description: 'Limits considered excessive',
92
- },
93
- },
94
- additionalProperties: false,
95
- },
96
- ],
97
- },
98
- defaultOptions: [
99
- {
100
- allowInTests: false,
101
- maxLimit: 102400,
102
- excessiveLimits: DEFAULT_EXCESSIVE_LIMITS,
103
- },
104
- ],
105
- create(context, [options]) {
106
- const { allowInTests = false, excessiveLimits = DEFAULT_EXCESSIVE_LIMITS, } = options;
107
- const filename = context.filename;
108
- const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
109
- if (isTestFile) {
110
- return {};
111
- }
112
- return {
113
- CallExpression(node) {
114
- const callee = node.callee;
115
- if (callee.type !== 'MemberExpression') {
116
- return;
117
- }
118
- const object = callee.object;
119
- const property = callee.property;
120
- if (object.type !== 'Identifier') {
121
- return;
122
- }
123
- if (object.name !== 'express' && object.name !== 'bodyParser') {
124
- return;
125
- }
126
- if (property.type !== 'Identifier') {
127
- return;
128
- }
129
- if (!BODY_PARSER_METHODS.has(property.name)) {
130
- return;
131
- }
132
- const firstArg = node.arguments[0];
133
- if (!firstArg) {
134
- const sourceCode = context.sourceCode;
135
- const calleeText = sourceCode.getText(callee);
136
- context.report({
137
- node,
138
- messageId: 'missingLimit',
139
- suggest: [
140
- {
141
- messageId: 'addLimit',
142
- fix: (fixer) => {
143
- return fixer.replaceText(node, `${calleeText}({ limit: '100kb' })`);
144
- },
145
- },
146
- ],
147
- });
148
- return;
149
- }
150
- if (firstArg.type === 'ObjectExpression') {
151
- const limitProp = getLimitOption(firstArg.properties);
152
- if (!limitProp) {
153
- context.report({
154
- node,
155
- messageId: 'missingLimit',
156
- suggest: [
157
- {
158
- messageId: 'addLimit',
159
- fix: (fixer) => {
160
- const lastProp = firstArg.properties[firstArg.properties.length - 1];
161
- if (lastProp) {
162
- return fixer.insertTextAfter(lastProp, ", limit: '100kb'");
163
- }
164
- return null;
165
- },
166
- },
167
- ],
168
- });
169
- return;
170
- }
171
- if (isExcessiveLimit(limitProp.value, excessiveLimits)) {
172
- context.report({
173
- node: limitProp,
174
- messageId: 'excessiveLimit',
175
- });
176
- }
177
- }
178
- },
179
- };
180
- },
181
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireExpressBodyParserLimits=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DEFAULT_EXCESSIVE_LIMITS=["10mb","50mb","100mb","500mb","1gb","1GB","10MB","50MB","100MB","500MB"];const BODY_PARSER_METHODS=new Set(["json","urlencoded","raw","text"]);function getLimitOption(properties){for(const prop of properties){if(prop.type==="Property"&&prop.key.type==="Identifier"&&prop.key.name==="limit"){return prop}}return null}function isExcessiveLimit(value,excessiveLimits){if(value.type==="Literal"&&typeof value.value==="string"){return excessiveLimits.some(limit=>value.value===limit||String(value.value).toLowerCase()===limit.toLowerCase())}return false}exports.requireExpressBodyParserLimits=(0,eslint_devkit_1.createRule)({name:"require-express-body-parser-limits",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-express-body-parser-limits.md",description:"Require size limits on Express.js body parsers to prevent DoS attacks",cwe:"CWE-400",cvss:7.5},hasSuggestions:true,messages:{missingLimit:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing Body Parser Limit",cwe:"CWE-400",description:"Body parser used without explicit size limit. Attackers can exhaust server memory with large payloads. CVE-2024-45590.",severity:"HIGH",fix:"Add a size limit: express.json({ limit: '10kb' }) or express.json({ limit: '100kb' })",documentationLink:"https://expressjs.com/en/4x/api.html#express.json"}),excessiveLimit:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Excessive Body Parser Limit",cwe:"CWE-400",description:"Body parser limit is excessively high, making the application vulnerable to DoS attacks via large payloads.",severity:"MEDIUM",fix:"Reduce the limit to a reasonable size: '100kb' for JSON APIs, '1mb' for file uploads with proper handling",documentationLink:"https://expressjs.com/en/4x/api.html#express.json"}),addLimit:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add Size Limit",description:"Add a size limit to the body parser",severity:"LOW",fix:"Add limit: '100kb' or appropriate size for your use case",documentationLink:"https://expressjs.com/en/4x/api.html#express.json"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow in test files"},maxLimit:{type:"number",default:102400,description:"Maximum allowed limit in bytes"},excessiveLimits:{type:"array",items:{type:"string"},default:DEFAULT_EXCESSIVE_LIMITS,description:"Limits considered excessive"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,maxLimit:102400,excessiveLimits:DEFAULT_EXCESSIVE_LIMITS}],create(context,[options]){const{allowInTests=false,excessiveLimits=DEFAULT_EXCESSIVE_LIMITS}=options;const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}return{CallExpression(node){const callee=node.callee;if(callee.type!=="MemberExpression"){return}const object=callee.object;const property=callee.property;if(object.type!=="Identifier"){return}if(object.name!=="express"&&object.name!=="bodyParser"){return}if(property.type!=="Identifier"){return}if(!BODY_PARSER_METHODS.has(property.name)){return}const firstArg=node.arguments[0];if(!firstArg){const sourceCode=context.sourceCode;const calleeText=sourceCode.getText(callee);context.report({node,messageId:"missingLimit",suggest:[{messageId:"addLimit",fix:fixer=>{return fixer.replaceText(node,`${calleeText}({ limit: '100kb' })`)}}]});return}if(firstArg.type==="ObjectExpression"){const limitProp=getLimitOption(firstArg.properties);if(!limitProp){context.report({node,messageId:"missingLimit",suggest:[{messageId:"addLimit",fix:fixer=>{const lastProp=firstArg.properties[firstArg.properties.length-1];if(lastProp){return fixer.insertTextAfter(lastProp,", limit: '100kb'")}return null}}]});return}if(isExcessiveLimit(limitProp.value,excessiveLimits)){context.report({node:limitProp,messageId:"excessiveLimit"})}}}}}});
@@ -1,166 +1 @@
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
- function isHelmetMiddleware(node) {
6
- const callee = node.callee;
7
- if (callee.type !== 'MemberExpression') {
8
- return false;
9
- }
10
- if (callee.property.type !== 'Identifier' ||
11
- callee.property.name !== 'use') {
12
- return false;
13
- }
14
- for (const arg of node.arguments) {
15
- if (arg.type === 'CallExpression') {
16
- const argCallee = arg.callee;
17
- if (argCallee.type === 'Identifier' && argCallee.name === 'helmet') {
18
- return true;
19
- }
20
- if (argCallee.type === 'MemberExpression' &&
21
- argCallee.object.type === 'Identifier' &&
22
- argCallee.object.name === 'helmet') {
23
- return true;
24
- }
25
- }
26
- if (arg.type === 'Identifier' && arg.name === 'helmet') {
27
- return true;
28
- }
29
- }
30
- return false;
31
- }
32
- function isAlternativeMiddleware(node, alternatives) {
33
- for (const arg of node.arguments) {
34
- if (arg.type === 'CallExpression' && arg.callee.type === 'Identifier') {
35
- if (alternatives.includes(arg.callee.name)) {
36
- return true;
37
- }
38
- }
39
- if (arg.type === 'Identifier' && alternatives.includes(arg.name)) {
40
- return true;
41
- }
42
- }
43
- return false;
44
- }
45
- exports.requireHelmet = (0, eslint_devkit_1.createRule)({
46
- name: 'require-helmet',
47
- meta: {
48
- type: 'problem',
49
- docs: {
50
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-helmet.md',
51
- description: 'Require helmet middleware for security headers in Express.js applications',
52
- cwe: 'CWE-693',
53
- cvss: 7.5,
54
- confidence: 'high',
55
- },
56
- hasSuggestions: true,
57
- messages: {
58
- missingHelmet: (0, eslint_devkit_1.formatLLMMessage)({
59
- icon: eslint_devkit_1.MessageIcons.SECURITY,
60
- issueName: 'Missing Security Headers',
61
- cwe: 'CWE-693',
62
- description: 'Express app created without helmet middleware. Missing security headers: X-Content-Type-Options, X-Frame-Options, Strict-Transport-Security, etc.',
63
- severity: 'HIGH',
64
- fix: "Add helmet middleware: app.use(helmet()). Install with 'npm install helmet'.",
65
- documentationLink: 'https://helmetjs.github.io/',
66
- }),
67
- addHelmet: (0, eslint_devkit_1.formatLLMMessage)({
68
- icon: eslint_devkit_1.MessageIcons.INFO,
69
- issueName: 'Add Helmet',
70
- description: 'Add helmet middleware for security headers',
71
- severity: 'LOW',
72
- fix: "import helmet from 'helmet'; app.use(helmet());",
73
- documentationLink: 'https://helmetjs.github.io/',
74
- }),
75
- },
76
- schema: [
77
- {
78
- type: 'object',
79
- properties: {
80
- allowInTests: {
81
- type: 'boolean',
82
- default: false,
83
- description: 'Allow missing helmet in test files',
84
- },
85
- alternativeMiddleware: {
86
- type: 'array',
87
- items: { type: 'string' },
88
- default: [],
89
- description: 'Alternative security headers middleware names to accept',
90
- },
91
- assumeHelmetMiddleware: {
92
- type: 'boolean',
93
- default: false,
94
- description: 'Skip rule if security headers are provided elsewhere (e.g., reverse proxy)',
95
- },
96
- },
97
- additionalProperties: false,
98
- },
99
- ],
100
- },
101
- defaultOptions: [
102
- {
103
- allowInTests: false,
104
- alternativeMiddleware: [],
105
- assumeHelmetMiddleware: false,
106
- },
107
- ],
108
- create(context, [options]) {
109
- const { allowInTests = false, alternativeMiddleware = [], assumeHelmetMiddleware = false } = options;
110
- if (assumeHelmetMiddleware) {
111
- return {};
112
- }
113
- const filename = context.filename;
114
- const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
115
- if (isTestFile) {
116
- return {};
117
- }
118
- let hasExpressApp = false;
119
- let hasHelmet = false;
120
- let expressAppNode = null;
121
- return {
122
- CallExpression(node) {
123
- const callee = node.callee;
124
- if (callee.type === 'Identifier' && callee.name === 'express') {
125
- hasExpressApp = true;
126
- expressAppNode = node;
127
- return;
128
- }
129
- if (callee.type === 'CallExpression' &&
130
- callee.callee.type === 'Identifier' &&
131
- callee.callee.name === 'require') {
132
- const firstArg = callee.arguments[0];
133
- if (firstArg &&
134
- firstArg.type === 'Literal' &&
135
- firstArg.value === 'express') {
136
- hasExpressApp = true;
137
- expressAppNode = node;
138
- return;
139
- }
140
- }
141
- if (isHelmetMiddleware(node)) {
142
- hasHelmet = true;
143
- return;
144
- }
145
- if (isAlternativeMiddleware(node, alternativeMiddleware)) {
146
- hasHelmet = true;
147
- return;
148
- }
149
- },
150
- 'Program:exit'() {
151
- if (hasExpressApp && !hasHelmet && expressAppNode) {
152
- context.report({
153
- node: expressAppNode,
154
- messageId: 'missingHelmet',
155
- suggest: [
156
- {
157
- messageId: 'addHelmet',
158
- fix: () => null,
159
- },
160
- ],
161
- });
162
- }
163
- },
164
- };
165
- },
166
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.requireHelmet=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");function isHelmetMiddleware(node){const callee=node.callee;if(callee.type!=="MemberExpression"){return false}if(callee.property.type!=="Identifier"||callee.property.name!=="use"){return false}for(const arg of node.arguments){if(arg.type==="CallExpression"){const argCallee=arg.callee;if(argCallee.type==="Identifier"&&argCallee.name==="helmet"){return true}if(argCallee.type==="MemberExpression"&&argCallee.object.type==="Identifier"&&argCallee.object.name==="helmet"){return true}}if(arg.type==="Identifier"&&arg.name==="helmet"){return true}}return false}function isAlternativeMiddleware(node,alternatives){for(const arg of node.arguments){if(arg.type==="CallExpression"&&arg.callee.type==="Identifier"){if(alternatives.includes(arg.callee.name)){return true}}if(arg.type==="Identifier"&&alternatives.includes(arg.name)){return true}}return false}exports.requireHelmet=(0,eslint_devkit_1.createRule)({name:"require-helmet",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-express-security/docs/rules/require-helmet.md",description:"Require helmet middleware for security headers in Express.js applications",cwe:"CWE-693",cvss:7.5,confidence:"high"},hasSuggestions:true,messages:{missingHelmet:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing Security Headers",cwe:"CWE-693",description:"Express app created without helmet middleware. Missing security headers: X-Content-Type-Options, X-Frame-Options, Strict-Transport-Security, etc.",severity:"HIGH",fix:"Add helmet middleware: app.use(helmet()). Install with 'npm install helmet'.",documentationLink:"https://helmetjs.github.io/"}),addHelmet:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add Helmet",description:"Add helmet middleware for security headers",severity:"LOW",fix:"import helmet from 'helmet'; app.use(helmet());",documentationLink:"https://helmetjs.github.io/"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow missing helmet in test files"},alternativeMiddleware:{type:"array",items:{type:"string"},default:[],description:"Alternative security headers middleware names to accept"},assumeHelmetMiddleware:{type:"boolean",default:false,description:"Skip rule if security headers are provided elsewhere (e.g., reverse proxy)"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,alternativeMiddleware:[],assumeHelmetMiddleware:false}],create(context,[options]){const{allowInTests=false,alternativeMiddleware=[],assumeHelmetMiddleware=false}=options;if(assumeHelmetMiddleware){return{}}const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}let hasExpressApp=false;let hasHelmet=false;let expressAppNode=null;return{CallExpression(node){const callee=node.callee;if(callee.type==="Identifier"&&callee.name==="express"){hasExpressApp=true;expressAppNode=node;return}if(callee.type==="CallExpression"&&callee.callee.type==="Identifier"&&callee.callee.name==="require"){const firstArg=callee.arguments[0];if(firstArg&&firstArg.type==="Literal"&&firstArg.value==="express"){hasExpressApp=true;expressAppNode=node;return}}if(isHelmetMiddleware(node)){hasHelmet=true;return}if(isAlternativeMiddleware(node,alternativeMiddleware)){hasHelmet=true;return}},"Program:exit"(){if(hasExpressApp&&!hasHelmet&&expressAppNode){context.report({node:expressAppNode,messageId:"missingHelmet",suggest:[{messageId:"addHelmet",fix:()=>null}]})}}}}});