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,351 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noMissingCorsCheck = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- function matchesIgnorePattern(text, ignorePatterns) {
7
- return ignorePatterns.some(pattern => {
8
- try {
9
- const regex = new RegExp(pattern, 'i');
10
- return regex.test(text);
11
- }
12
- catch {
13
- return false;
14
- }
15
- });
16
- }
17
- exports.noMissingCorsCheck = (0, eslint_devkit_2.createRule)({
18
- name: 'no-missing-cors-check',
19
- meta: {
20
- type: 'problem',
21
- deprecated: true,
22
- replacedBy: ['@see eslint-plugin-express-security/no-permissive-cors'],
23
- docs: {
24
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-missing-cors-check.md',
25
- description: 'Detects missing CORS validation (wildcard CORS, missing origin check)',
26
- cwe: 'CWE-346',
27
- cvss: 7.5,
28
- },
29
- hasSuggestions: true,
30
- messages: {
31
- missingCorsCheck: (0, eslint_devkit_1.formatLLMMessage)({
32
- icon: eslint_devkit_1.MessageIcons.SECURITY,
33
- issueName: 'Missing CORS Validation',
34
- cwe: 'CWE-346',
35
- description: 'Missing CORS validation detected: {{issue}}',
36
- severity: 'HIGH',
37
- fix: '{{safeAlternative}}',
38
- documentationLink: 'https://cwe.mitre.org/data/definitions/346.html',
39
- }),
40
- useOriginValidation: (0, eslint_devkit_1.formatLLMMessage)({
41
- icon: eslint_devkit_1.MessageIcons.INFO,
42
- issueName: 'Validate Origin',
43
- description: 'Validate CORS origin',
44
- severity: 'LOW',
45
- fix: 'cors({ origin: (origin, cb) => allowedOrigins.includes(origin) ? cb(null, true) : cb(new Error()) })',
46
- documentationLink: 'https://github.com/expressjs/cors#configuration-options',
47
- }),
48
- useCorsMiddleware: (0, eslint_devkit_1.formatLLMMessage)({
49
- icon: eslint_devkit_1.MessageIcons.INFO,
50
- issueName: 'Use CORS Middleware',
51
- description: 'Use CORS middleware with origin validation',
52
- severity: 'LOW',
53
- fix: 'app.use(cors({ origin: allowedOrigins }))',
54
- documentationLink: 'https://github.com/expressjs/cors',
55
- }),
56
- },
57
- schema: [
58
- {
59
- type: 'object',
60
- properties: {
61
- allowInTests: {
62
- type: 'boolean',
63
- default: false,
64
- description: 'Allow missing CORS checks in test files',
65
- },
66
- trustedLibraries: {
67
- type: 'array',
68
- items: { type: 'string' },
69
- default: [],
70
- description: 'Custom CORS libraries to trust (wildcard origins in these libraries will not be reported)',
71
- },
72
- ignorePatterns: {
73
- type: 'array',
74
- items: { type: 'string' },
75
- default: [],
76
- description: 'Additional safe patterns to ignore',
77
- },
78
- },
79
- additionalProperties: false,
80
- },
81
- ],
82
- },
83
- defaultOptions: [
84
- {
85
- allowInTests: false,
86
- trustedLibraries: [],
87
- ignorePatterns: [],
88
- },
89
- ],
90
- create(context, [options]) {
91
- const { allowInTests = false, trustedLibraries: corsTrustedLibraries = [], ignorePatterns = [], } = options;
92
- const trustedLibraries = corsTrustedLibraries;
93
- const filename = context.filename;
94
- const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
95
- const sourceCode = context.sourceCode;
96
- function checkLiteral(node) {
97
- if (isTestFile) {
98
- return;
99
- }
100
- if (node.value === '*' && typeof node.value === 'string') {
101
- const text = sourceCode.getText(node);
102
- if (matchesIgnorePattern(text, ignorePatterns)) {
103
- return;
104
- }
105
- let shouldSkip = false;
106
- let current = node;
107
- while (current && current.parent) {
108
- current = current.parent;
109
- if (current.type === 'CallExpression') {
110
- const callText = sourceCode.getText(current);
111
- if (/\b(setHeader|header)\s*\(/i.test(callText) && /\bAccess-Control-Allow-Origin\b/i.test(callText)) {
112
- shouldSkip = true;
113
- break;
114
- }
115
- if (/\buse\s*\(/i.test(callText) && /\bcors\s*\(/i.test(callText)) {
116
- if (node.parent && node.parent.type === 'Property') {
117
- const prop = node.parent;
118
- if (prop.key.type === 'Identifier' && prop.key.name === 'origin') {
119
- shouldSkip = true;
120
- break;
121
- }
122
- }
123
- }
124
- }
125
- }
126
- if (shouldSkip) {
127
- return;
128
- }
129
- let isActualCorsContext = false;
130
- current = node;
131
- while (current && current.parent) {
132
- current = current.parent;
133
- if (current.type === 'CallExpression') {
134
- const callText = sourceCode.getText(current);
135
- if (/\b(use|cors)\s*\(/i.test(callText) && /\bcors\s*\(/i.test(callText)) {
136
- isActualCorsContext = true;
137
- break;
138
- }
139
- }
140
- }
141
- if (node.parent && node.parent.type === 'Property') {
142
- const prop = node.parent;
143
- if (prop.key.type === 'Identifier') {
144
- const keyName = prop.key.name.toLowerCase();
145
- if (keyName === 'origin' || keyName === 'allowedorigins') {
146
- let inCorsCall = false;
147
- let checkNode = prop;
148
- while (checkNode && checkNode.parent) {
149
- checkNode = checkNode.parent;
150
- if (checkNode.type === 'CallExpression') {
151
- const callText = sourceCode.getText(checkNode);
152
- if (/\bcors\s*\(/i.test(callText) ||
153
- (/\buse\s*\(/i.test(callText) && /\bcors/i.test(callText))) {
154
- inCorsCall = true;
155
- break;
156
- }
157
- }
158
- }
159
- if (inCorsCall) {
160
- context.report({
161
- node,
162
- messageId: 'missingCorsCheck',
163
- data: {
164
- issue: 'Wildcard CORS origin (*) allows all origins',
165
- safeAlternative: 'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));',
166
- },
167
- suggest: [
168
- {
169
- messageId: 'useOriginValidation',
170
- fix: (_fixer) => null,
171
- },
172
- ],
173
- });
174
- return;
175
- }
176
- }
177
- }
178
- }
179
- if (isActualCorsContext) {
180
- context.report({
181
- node,
182
- messageId: 'missingCorsCheck',
183
- data: {
184
- issue: 'Wildcard CORS origin (*) allows all origins',
185
- safeAlternative: 'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));',
186
- },
187
- suggest: [
188
- {
189
- messageId: 'useOriginValidation',
190
- fix: (_fixer) => null,
191
- },
192
- ],
193
- });
194
- }
195
- }
196
- }
197
- function checkCallExpression(node) {
198
- if (isTestFile) {
199
- return;
200
- }
201
- if (node.callee.type === 'MemberExpression') {
202
- const property = node.callee.property;
203
- if (property.type === 'Identifier' && property.name === 'use') {
204
- const text = sourceCode.getText(node);
205
- if (matchesIgnorePattern(text, ignorePatterns)) {
206
- return;
207
- }
208
- const firstArg = node.arguments.length > 0 ? node.arguments[0] : null;
209
- let isCorsCall = /\bcors\s*\(/i.test(text);
210
- if (!isCorsCall && firstArg && firstArg.type === 'CallExpression' && firstArg.callee.type === 'Identifier') {
211
- const callee = firstArg.callee;
212
- const calleeName = callee.name.toLowerCase();
213
- isCorsCall = calleeName === 'cors' || trustedLibraries.some(lib => {
214
- return calleeName.includes(lib.toLowerCase());
215
- });
216
- }
217
- let isTrustedLibrary = false;
218
- if (firstArg && firstArg.type === 'CallExpression' && firstArg.callee.type === 'Identifier') {
219
- const calleeName = firstArg.callee.name.toLowerCase();
220
- isTrustedLibrary = trustedLibraries.some(lib => calleeName.includes(lib.toLowerCase()));
221
- }
222
- if (isTrustedLibrary) {
223
- return;
224
- }
225
- if (/\bcors\s*\(/i.test(text) || isCorsCall) {
226
- const corsCallArg = firstArg && firstArg.type === 'CallExpression' ? firstArg : null;
227
- const argsToCheck = corsCallArg ? corsCallArg.arguments : node.arguments;
228
- for (const arg of argsToCheck) {
229
- if (arg.type === 'ObjectExpression') {
230
- for (const prop of arg.properties) {
231
- if (prop.type === 'Property' &&
232
- prop.key.type === 'Identifier' &&
233
- prop.key.name === 'origin' &&
234
- prop.value.type === 'Literal' &&
235
- prop.value.value === '*') {
236
- context.report({
237
- node: prop.value,
238
- messageId: 'missingCorsCheck',
239
- data: {
240
- issue: 'Wildcard CORS origin (*) allows all origins',
241
- safeAlternative: 'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));',
242
- },
243
- suggest: [
244
- {
245
- messageId: 'useOriginValidation',
246
- fix: (_fixer) => null,
247
- },
248
- ],
249
- });
250
- }
251
- }
252
- }
253
- else if (arg.type === 'Identifier') {
254
- const varName = arg.name;
255
- let current = node;
256
- while (current) {
257
- if (current.type === 'Program' || current.type === 'FunctionDeclaration' || current.type === 'FunctionExpression' || current.type === 'ArrowFunctionExpression') {
258
- const scopeBody = current.type === 'Program' ? current.body :
259
- (current.body.type === 'BlockStatement' ? current.body.body : []);
260
- for (const stmt of scopeBody) {
261
- if (stmt.type === 'VariableDeclaration') {
262
- for (const declarator of stmt.declarations) {
263
- if (declarator.id.type === 'Identifier' && declarator.id.name === varName && declarator.init) {
264
- if (declarator.init.type === 'ObjectExpression') {
265
- for (const prop of declarator.init.properties) {
266
- if (prop.type === 'Property' &&
267
- prop.key.type === 'Identifier' &&
268
- prop.key.name === 'origin' &&
269
- prop.value.type === 'Literal' &&
270
- prop.value.value === '*') {
271
- context.report({
272
- node: arg,
273
- messageId: 'missingCorsCheck',
274
- data: {
275
- issue: 'Wildcard CORS origin (*) allows all origins',
276
- safeAlternative: 'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));',
277
- },
278
- suggest: [
279
- {
280
- messageId: 'useOriginValidation',
281
- fix: (_fixer) => null,
282
- },
283
- ],
284
- });
285
- return;
286
- }
287
- }
288
- }
289
- }
290
- }
291
- }
292
- }
293
- break;
294
- }
295
- if (current.parent) {
296
- current = current.parent;
297
- }
298
- else {
299
- break;
300
- }
301
- }
302
- }
303
- }
304
- }
305
- }
306
- }
307
- }
308
- function checkMemberExpression(node) {
309
- if (isTestFile) {
310
- return;
311
- }
312
- if (node.property.type === 'Identifier') {
313
- const propertyName = node.property.name;
314
- if (propertyName === 'setHeader' || propertyName === 'header') {
315
- const text = sourceCode.getText(node);
316
- if (matchesIgnorePattern(text, ignorePatterns)) {
317
- return;
318
- }
319
- const parent = node.parent;
320
- if (parent && parent.type === 'CallExpression') {
321
- const callText = sourceCode.getText(parent);
322
- if (/\bAccess-Control-Allow-Origin\b/i.test(callText)) {
323
- const args = parent.arguments;
324
- if (args.length >= 2 && args[1].type === 'Literal' && args[1].value === '*') {
325
- context.report({
326
- node: args[1],
327
- messageId: 'missingCorsCheck',
328
- data: {
329
- issue: 'Wildcard CORS header allows all origins',
330
- safeAlternative: 'Validate origin before setting header: res.setHeader("Access-Control-Allow-Origin", allowedOrigins.includes(origin) ? origin : "null");',
331
- },
332
- suggest: [
333
- {
334
- messageId: 'useOriginValidation',
335
- fix: (_fixer) => null,
336
- },
337
- ],
338
- });
339
- }
340
- }
341
- }
342
- }
343
- }
344
- }
345
- return {
346
- Literal: checkLiteral,
347
- CallExpression: checkCallExpression,
348
- MemberExpression: checkMemberExpression,
349
- };
350
- },
351
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noMissingCorsCheck=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");function matchesIgnorePattern(text,ignorePatterns){return ignorePatterns.some(pattern=>{try{const regex=new RegExp(pattern,"i");return regex.test(text)}catch{return false}})}exports.noMissingCorsCheck=(0,eslint_devkit_2.createRule)({name:"no-missing-cors-check",meta:{type:"problem",deprecated:true,replacedBy:["@see eslint-plugin-express-security/no-permissive-cors"],docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-missing-cors-check.md",description:"Detects missing CORS validation (wildcard CORS, missing origin check)",cwe:"CWE-346",cvss:7.5},hasSuggestions:true,messages:{missingCorsCheck:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing CORS Validation",cwe:"CWE-346",description:"Missing CORS validation detected: {{issue}}",severity:"HIGH",fix:"{{safeAlternative}}",documentationLink:"https://cwe.mitre.org/data/definitions/346.html"}),useOriginValidation:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Validate Origin",description:"Validate CORS origin",severity:"LOW",fix:"cors({ origin: (origin, cb) => allowedOrigins.includes(origin) ? cb(null, true) : cb(new Error()) })",documentationLink:"https://github.com/expressjs/cors#configuration-options"}),useCorsMiddleware:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use CORS Middleware",description:"Use CORS middleware with origin validation",severity:"LOW",fix:"app.use(cors({ origin: allowedOrigins }))",documentationLink:"https://github.com/expressjs/cors"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow missing CORS checks in test files"},trustedLibraries:{type:"array",items:{type:"string"},default:[],description:"Custom CORS libraries to trust (wildcard origins in these libraries will not be reported)"},ignorePatterns:{type:"array",items:{type:"string"},default:[],description:"Additional safe patterns to ignore"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,trustedLibraries:[],ignorePatterns:[]}],create(context,[options]){const{allowInTests=false,trustedLibraries:corsTrustedLibraries=[],ignorePatterns=[]}=options;const trustedLibraries=corsTrustedLibraries;const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);const sourceCode=context.sourceCode;function checkLiteral(node){if(isTestFile){return}if(node.value==="*"&&typeof node.value==="string"){const text=sourceCode.getText(node);if(matchesIgnorePattern(text,ignorePatterns)){return}let shouldSkip=false;let current=node;while(current&&current.parent){current=current.parent;if(current.type==="CallExpression"){const callText=sourceCode.getText(current);if(/\b(setHeader|header)\s*\(/i.test(callText)&&/\bAccess-Control-Allow-Origin\b/i.test(callText)){shouldSkip=true;break}if(/\buse\s*\(/i.test(callText)&&/\bcors\s*\(/i.test(callText)){if(node.parent&&node.parent.type==="Property"){const prop=node.parent;if(prop.key.type==="Identifier"&&prop.key.name==="origin"){shouldSkip=true;break}}}}}if(shouldSkip){return}let isActualCorsContext=false;current=node;while(current&&current.parent){current=current.parent;if(current.type==="CallExpression"){const callText=sourceCode.getText(current);if(/\b(use|cors)\s*\(/i.test(callText)&&/\bcors\s*\(/i.test(callText)){isActualCorsContext=true;break}}}if(node.parent&&node.parent.type==="Property"){const prop=node.parent;if(prop.key.type==="Identifier"){const keyName=prop.key.name.toLowerCase();if(keyName==="origin"||keyName==="allowedorigins"){let inCorsCall=false;let checkNode=prop;while(checkNode&&checkNode.parent){checkNode=checkNode.parent;if(checkNode.type==="CallExpression"){const callText=sourceCode.getText(checkNode);if(/\bcors\s*\(/i.test(callText)||/\buse\s*\(/i.test(callText)&&/\bcors/i.test(callText)){inCorsCall=true;break}}}if(inCorsCall){context.report({node,messageId:"missingCorsCheck",data:{issue:"Wildcard CORS origin (*) allows all origins",safeAlternative:'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));'},suggest:[{messageId:"useOriginValidation",fix:_fixer=>null}]});return}}}}if(isActualCorsContext){context.report({node,messageId:"missingCorsCheck",data:{issue:"Wildcard CORS origin (*) allows all origins",safeAlternative:'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));'},suggest:[{messageId:"useOriginValidation",fix:_fixer=>null}]})}}}function checkCallExpression(node){if(isTestFile){return}if(node.callee.type==="MemberExpression"){const property=node.callee.property;if(property.type==="Identifier"&&property.name==="use"){const text=sourceCode.getText(node);if(matchesIgnorePattern(text,ignorePatterns)){return}const firstArg=node.arguments.length>0?node.arguments[0]:null;let isCorsCall=/\bcors\s*\(/i.test(text);if(!isCorsCall&&firstArg&&firstArg.type==="CallExpression"&&firstArg.callee.type==="Identifier"){const callee=firstArg.callee;const calleeName=callee.name.toLowerCase();isCorsCall=calleeName==="cors"||trustedLibraries.some(lib=>{return calleeName.includes(lib.toLowerCase())})}let isTrustedLibrary=false;if(firstArg&&firstArg.type==="CallExpression"&&firstArg.callee.type==="Identifier"){const calleeName=firstArg.callee.name.toLowerCase();isTrustedLibrary=trustedLibraries.some(lib=>calleeName.includes(lib.toLowerCase()))}if(isTrustedLibrary){return}if(/\bcors\s*\(/i.test(text)||isCorsCall){const corsCallArg=firstArg&&firstArg.type==="CallExpression"?firstArg:null;const argsToCheck=corsCallArg?corsCallArg.arguments:node.arguments;for(const arg of argsToCheck){if(arg.type==="ObjectExpression"){for(const prop of arg.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"&&prop.key.name==="origin"&&prop.value.type==="Literal"&&prop.value.value==="*"){context.report({node:prop.value,messageId:"missingCorsCheck",data:{issue:"Wildcard CORS origin (*) allows all origins",safeAlternative:'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));'},suggest:[{messageId:"useOriginValidation",fix:_fixer=>null}]})}}}else if(arg.type==="Identifier"){const varName=arg.name;let current=node;while(current){if(current.type==="Program"||current.type==="FunctionDeclaration"||current.type==="FunctionExpression"||current.type==="ArrowFunctionExpression"){const scopeBody=current.type==="Program"?current.body:current.body.type==="BlockStatement"?current.body.body:[];for(const stmt of scopeBody){if(stmt.type==="VariableDeclaration"){for(const declarator of stmt.declarations){if(declarator.id.type==="Identifier"&&declarator.id.name===varName&&declarator.init){if(declarator.init.type==="ObjectExpression"){for(const prop of declarator.init.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"&&prop.key.name==="origin"&&prop.value.type==="Literal"&&prop.value.value==="*"){context.report({node:arg,messageId:"missingCorsCheck",data:{issue:"Wildcard CORS origin (*) allows all origins",safeAlternative:'Use origin validation: app.use(cors({ origin: (origin, callback) => { if (allowedOrigins.includes(origin)) callback(null, true); else callback(new Error("Not allowed")); } } }));'},suggest:[{messageId:"useOriginValidation",fix:_fixer=>null}]});return}}}}}}}break}if(current.parent){current=current.parent}else{break}}}}}}}}function checkMemberExpression(node){if(isTestFile){return}if(node.property.type==="Identifier"){const propertyName=node.property.name;if(propertyName==="setHeader"||propertyName==="header"){const text=sourceCode.getText(node);if(matchesIgnorePattern(text,ignorePatterns)){return}const parent=node.parent;if(parent&&parent.type==="CallExpression"){const callText=sourceCode.getText(parent);if(/\bAccess-Control-Allow-Origin\b/i.test(callText)){const args=parent.arguments;if(args.length>=2&&args[1].type==="Literal"&&args[1].value==="*"){context.report({node:args[1],messageId:"missingCorsCheck",data:{issue:"Wildcard CORS header allows all origins",safeAlternative:'Validate origin before setting header: res.setHeader("Access-Control-Allow-Origin", allowedOrigins.includes(origin) ? origin : "null");'},suggest:[{messageId:"useOriginValidation",fix:_fixer=>null}]})}}}}}}return{Literal:checkLiteral,CallExpression:checkCallExpression,MemberExpression:checkMemberExpression}}});
@@ -1,161 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noMissingCsrfProtection = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- const DEFAULT_CSRF_MIDDLEWARE_PATTERNS = [
7
- 'csrf',
8
- 'csurf',
9
- 'csrfProtection',
10
- 'verifyCsrfToken',
11
- 'csrfToken',
12
- 'validateCsrf',
13
- 'checkCsrf',
14
- 'csrfMiddleware',
15
- ];
16
- const DEFAULT_PROTECTED_METHODS = ['post', 'put', 'delete', 'patch'];
17
- function matchesIgnorePattern(text, patterns) {
18
- return patterns.some(pattern => {
19
- try {
20
- const regex = new RegExp(pattern, 'i');
21
- return regex.test(text);
22
- }
23
- catch {
24
- return text.toLowerCase().includes(pattern.toLowerCase());
25
- }
26
- });
27
- }
28
- exports.noMissingCsrfProtection = (0, eslint_devkit_2.createRule)({
29
- name: 'no-missing-csrf-protection',
30
- meta: {
31
- type: 'problem',
32
- deprecated: true,
33
- replacedBy: ['@see eslint-plugin-express-security/require-csrf-protection'],
34
- docs: {
35
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-missing-csrf-protection.md',
36
- description: 'Detects missing CSRF token validation in POST/PUT/DELETE requests',
37
- cwe: 'CWE-352',
38
- cvss: 8.8,
39
- },
40
- hasSuggestions: true,
41
- messages: {
42
- missingCsrfProtection: (0, eslint_devkit_1.formatLLMMessage)({
43
- icon: eslint_devkit_1.MessageIcons.SECURITY,
44
- issueName: 'Missing CSRF Protection',
45
- cwe: 'CWE-352',
46
- description: 'Missing CSRF protection detected: {{issue}}',
47
- severity: 'HIGH',
48
- fix: '{{safeAlternative}}',
49
- documentationLink: 'https://cwe.mitre.org/data/definitions/352.html',
50
- }),
51
- addCsrfValidation: (0, eslint_devkit_1.formatLLMMessage)({
52
- icon: eslint_devkit_1.MessageIcons.INFO,
53
- issueName: 'Add CSRF Validation',
54
- description: 'Add CSRF middleware',
55
- severity: 'LOW',
56
- fix: 'app.use(csrf({ cookie: true }))',
57
- documentationLink: 'https://github.com/expressjs/csurf',
58
- }),
59
- },
60
- schema: [
61
- {
62
- type: 'object',
63
- properties: {
64
- allowInTests: {
65
- type: 'boolean',
66
- default: false,
67
- description: 'Allow missing CSRF protection in test files',
68
- },
69
- csrfMiddlewarePatterns: {
70
- type: 'array',
71
- items: { type: 'string' },
72
- default: [],
73
- description: 'CSRF middleware patterns to recognize',
74
- },
75
- protectedMethods: {
76
- type: 'array',
77
- items: { type: 'string' },
78
- default: [],
79
- description: 'HTTP methods that require CSRF protection',
80
- },
81
- ignorePatterns: {
82
- type: 'array',
83
- items: { type: 'string' },
84
- default: [],
85
- description: 'Additional safe patterns to ignore',
86
- },
87
- },
88
- additionalProperties: false,
89
- },
90
- ],
91
- },
92
- defaultOptions: [
93
- {
94
- allowInTests: false,
95
- csrfMiddlewarePatterns: [],
96
- protectedMethods: [],
97
- ignorePatterns: [],
98
- },
99
- ],
100
- create(context, [options]) {
101
- const { allowInTests = false, csrfMiddlewarePatterns, protectedMethods: customProtectedMethods, ignorePatterns = [], } = options;
102
- const csrfPatterns = csrfMiddlewarePatterns && csrfMiddlewarePatterns.length > 0
103
- ? csrfMiddlewarePatterns
104
- : DEFAULT_CSRF_MIDDLEWARE_PATTERNS;
105
- const protectedMethods = customProtectedMethods && customProtectedMethods.length > 0
106
- ? customProtectedMethods
107
- : DEFAULT_PROTECTED_METHODS;
108
- const protectedMethodsSet = new Set(protectedMethods.map(m => m.toLowerCase()));
109
- const filename = context.filename;
110
- const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
111
- const sourceCode = context.sourceCode;
112
- function checkCallExpression(node) {
113
- if (isTestFile) {
114
- return;
115
- }
116
- const callee = node.callee;
117
- const callText = sourceCode.getText(node);
118
- if (matchesIgnorePattern(callText, ignorePatterns)) {
119
- return;
120
- }
121
- if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression && callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
122
- const methodName = callee.property.name;
123
- if (protectedMethodsSet.has(methodName.toLowerCase())) {
124
- if (node.arguments.length < 2) {
125
- return;
126
- }
127
- let hasCsrfInChain = false;
128
- for (let i = 1; i < node.arguments.length; i++) {
129
- const arg = node.arguments[i];
130
- const argText = sourceCode.getText(arg);
131
- if (csrfPatterns.some(pattern => argText.toLowerCase().includes(pattern.toLowerCase()))) {
132
- hasCsrfInChain = true;
133
- break;
134
- }
135
- }
136
- if (!hasCsrfInChain) {
137
- context.report({
138
- node,
139
- messageId: 'missingCsrfProtection',
140
- data: {
141
- issue: `${methodName.toUpperCase()} route handler missing CSRF protection`,
142
- safeAlternative: `Add CSRF middleware: app.${methodName}("/path", csrf(), handler) or use app.use(csrf()) globally`,
143
- },
144
- suggest: [
145
- {
146
- messageId: 'addCsrfValidation',
147
- fix(fixer) {
148
- return fixer.insertTextAfter(node.arguments[0], ', csrf()');
149
- },
150
- },
151
- ],
152
- });
153
- }
154
- }
155
- }
156
- }
157
- return {
158
- CallExpression: checkCallExpression,
159
- };
160
- },
161
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noMissingCsrfProtection=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const DEFAULT_CSRF_MIDDLEWARE_PATTERNS=["csrf","csurf","csrfProtection","verifyCsrfToken","csrfToken","validateCsrf","checkCsrf","csrfMiddleware"];const DEFAULT_PROTECTED_METHODS=["post","put","delete","patch"];function matchesIgnorePattern(text,patterns){return patterns.some(pattern=>{try{const regex=new RegExp(pattern,"i");return regex.test(text)}catch{return text.toLowerCase().includes(pattern.toLowerCase())}})}exports.noMissingCsrfProtection=(0,eslint_devkit_2.createRule)({name:"no-missing-csrf-protection",meta:{type:"problem",deprecated:true,replacedBy:["@see eslint-plugin-express-security/require-csrf-protection"],docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-missing-csrf-protection.md",description:"Detects missing CSRF token validation in POST/PUT/DELETE requests",cwe:"CWE-352",cvss:8.8},hasSuggestions:true,messages:{missingCsrfProtection:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing CSRF Protection",cwe:"CWE-352",description:"Missing CSRF protection detected: {{issue}}",severity:"HIGH",fix:"{{safeAlternative}}",documentationLink:"https://cwe.mitre.org/data/definitions/352.html"}),addCsrfValidation:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add CSRF Validation",description:"Add CSRF middleware",severity:"LOW",fix:"app.use(csrf({ cookie: true }))",documentationLink:"https://github.com/expressjs/csurf"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow missing CSRF protection in test files"},csrfMiddlewarePatterns:{type:"array",items:{type:"string"},default:[],description:"CSRF middleware patterns to recognize"},protectedMethods:{type:"array",items:{type:"string"},default:[],description:"HTTP methods that require CSRF protection"},ignorePatterns:{type:"array",items:{type:"string"},default:[],description:"Additional safe patterns to ignore"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,csrfMiddlewarePatterns:[],protectedMethods:[],ignorePatterns:[]}],create(context,[options]){const{allowInTests=false,csrfMiddlewarePatterns,protectedMethods:customProtectedMethods,ignorePatterns=[]}=options;const csrfPatterns=csrfMiddlewarePatterns&&csrfMiddlewarePatterns.length>0?csrfMiddlewarePatterns:DEFAULT_CSRF_MIDDLEWARE_PATTERNS;const protectedMethods=customProtectedMethods&&customProtectedMethods.length>0?customProtectedMethods:DEFAULT_PROTECTED_METHODS;const protectedMethodsSet=new Set(protectedMethods.map(m=>m.toLowerCase()));const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);const sourceCode=context.sourceCode;function checkCallExpression(node){if(isTestFile){return}const callee=node.callee;const callText=sourceCode.getText(node);if(matchesIgnorePattern(callText,ignorePatterns)){return}if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const methodName=callee.property.name;if(protectedMethodsSet.has(methodName.toLowerCase())){if(node.arguments.length<2){return}let hasCsrfInChain=false;for(let i=1;i<node.arguments.length;i++){const arg=node.arguments[i];const argText=sourceCode.getText(arg);if(csrfPatterns.some(pattern=>argText.toLowerCase().includes(pattern.toLowerCase()))){hasCsrfInChain=true;break}}if(!hasCsrfInChain){context.report({node,messageId:"missingCsrfProtection",data:{issue:`${methodName.toUpperCase()} route handler missing CSRF protection`,safeAlternative:`Add CSRF middleware: app.${methodName}("/path", csrf(), handler) or use app.use(csrf()) globally`},suggest:[{messageId:"addCsrfValidation",fix(fixer){return fixer.insertTextAfter(node.arguments[0],", csrf()")}}]})}}}}return{CallExpression:checkCallExpression}}});