eslint-plugin-secure-coding 3.4.2 → 3.4.4

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 (32) hide show
  1. package/package.json +2 -3
  2. package/src/index.js +1 -134
  3. package/src/oxlint.js +1 -3
  4. package/src/rules/detect-non-literal-regexp/index.js +1 -329
  5. package/src/rules/detect-object-injection/index.js +1 -691
  6. package/src/rules/detect-weak-password-validation/index.js +1 -54
  7. package/src/rules/no-directive-injection/index.js +1 -416
  8. package/src/rules/no-electron-security-issues/index.js +1 -385
  9. package/src/rules/no-format-string-injection/index.js +1 -565
  10. package/src/rules/no-graphql-injection/index.js +1 -612
  11. package/src/rules/no-hardcoded-credentials/index.js +1 -658
  12. package/src/rules/no-hardcoded-session-tokens/index.js +1 -56
  13. package/src/rules/no-improper-sanitization/index.js +1 -391
  14. package/src/rules/no-improper-type-validation/index.js +1 -422
  15. package/src/rules/no-insecure-comparison/index.js +1 -249
  16. package/src/rules/no-ldap-injection/index.js +1 -403
  17. package/src/rules/no-missing-authentication/index.js +1 -282
  18. package/src/rules/no-pii-in-logs/index.js +1 -63
  19. package/src/rules/no-privilege-escalation/index.js +1 -282
  20. package/src/rules/no-redos-vulnerable-regex/index.js +0 -0
  21. package/src/rules/no-sensitive-data-exposure/index.js +1 -249
  22. package/src/rules/no-template-injection/index.js +1 -84
  23. package/src/rules/no-unchecked-loop-condition/index.js +1 -541
  24. package/src/rules/no-unlimited-resource-allocation/index.js +1 -554
  25. package/src/rules/no-unsafe-deserialization/index.js +1 -497
  26. package/src/rules/no-unsafe-regex-construction/index.js +1 -275
  27. package/src/rules/no-weak-password-recovery/index.js +1 -382
  28. package/src/rules/no-xpath-injection/index.js +1 -431
  29. package/src/rules/no-xxe-injection/index.js +1 -230
  30. package/src/rules/require-backend-authorization/index.js +1 -58
  31. package/src/rules/require-secure-defaults/index.js +1 -41
  32. package/src/types/index.js +1 -2
@@ -1,282 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noMissingAuthentication = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- const DEFAULT_AUTH_MIDDLEWARE_PATTERNS = [
7
- 'authenticate',
8
- 'auth',
9
- 'requireAuth',
10
- 'isAuthenticated',
11
- 'verifyToken',
12
- 'checkAuth',
13
- 'ensureAuthenticated',
14
- 'passport.authenticate',
15
- 'jwt',
16
- 'session',
17
- ];
18
- const DEFAULT_ROUTE_HANDLER_PATTERNS = [
19
- 'get',
20
- 'post',
21
- 'put',
22
- 'delete',
23
- 'patch',
24
- 'all',
25
- 'use',
26
- ];
27
- function isInsideAuthMiddleware(node, sourceCode, authPatterns) {
28
- let current = node;
29
- while (current) {
30
- if (current.parent && current.parent.type === 'CallExpression') {
31
- const callExpr = current.parent;
32
- const isArgument = callExpr.arguments.some((arg) => arg === current);
33
- if (!isArgument) {
34
- current = current.parent;
35
- continue;
36
- }
37
- const callee = callExpr.callee;
38
- if (callee.type === 'Identifier') {
39
- const calleeName = callee.name.toLowerCase();
40
- if (authPatterns.some(pattern => calleeName.includes(pattern.toLowerCase()))) {
41
- return true;
42
- }
43
- }
44
- if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier') {
45
- const propertyName = callee.property.name.toLowerCase();
46
- if (propertyName === 'use' || propertyName === 'all') {
47
- for (const arg of callExpr.arguments) {
48
- const argText = sourceCode.getText(arg);
49
- if (authPatterns.some(pattern => argText.toLowerCase().includes(pattern.toLowerCase()))) {
50
- return true;
51
- }
52
- }
53
- }
54
- }
55
- }
56
- if ('parent' in current && current.parent) {
57
- current = current.parent;
58
- }
59
- else {
60
- break;
61
- }
62
- }
63
- return false;
64
- }
65
- function matchesIgnorePattern(text, patterns) {
66
- return patterns.some(pattern => {
67
- try {
68
- const regex = new RegExp(pattern, 'i');
69
- return regex.test(text);
70
- }
71
- catch {
72
- return text.toLowerCase().includes(pattern.toLowerCase());
73
- }
74
- });
75
- }
76
- exports.noMissingAuthentication = (0, eslint_devkit_2.createRule)({
77
- name: 'no-missing-authentication',
78
- meta: {
79
- type: 'problem',
80
- docs: {
81
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-missing-authentication.md',
82
- description: 'Detects missing authentication checks in route handlers',
83
- cwe: 'CWE-287',
84
- cvss: 9.8,
85
- },
86
- hasSuggestions: true,
87
- messages: {
88
- missingAuthentication: (0, eslint_devkit_1.formatLLMMessage)({
89
- icon: eslint_devkit_1.MessageIcons.SECURITY,
90
- issueName: 'Missing Authentication',
91
- cwe: 'CWE-287',
92
- description: 'Route handler missing authentication check: {{route}}',
93
- severity: 'CRITICAL',
94
- fix: 'Add authentication middleware: app.{{method}}(\'{{path}}\', authenticate(), handler)',
95
- documentationLink: 'https://cwe.mitre.org/data/definitions/287.html',
96
- }),
97
- addAuthentication: (0, eslint_devkit_1.formatLLMMessage)({
98
- icon: eslint_devkit_1.MessageIcons.INFO,
99
- issueName: 'Add Authentication',
100
- description: 'Add authentication middleware before route handler',
101
- severity: 'LOW',
102
- fix: 'app.get("/path", authenticate(), handler)',
103
- documentationLink: 'https://cwe.mitre.org/data/definitions/287.html',
104
- }),
105
- },
106
- schema: [
107
- {
108
- type: 'object',
109
- properties: {
110
- allowInTests: {
111
- type: 'boolean',
112
- default: false,
113
- description: 'Allow missing authentication in test files',
114
- },
115
- authMiddlewarePatterns: {
116
- type: 'array',
117
- items: { type: 'string' },
118
- default: DEFAULT_AUTH_MIDDLEWARE_PATTERNS,
119
- description: 'Authentication middleware patterns to recognize',
120
- },
121
- routeHandlerPatterns: {
122
- type: 'array',
123
- items: { type: 'string' },
124
- default: DEFAULT_ROUTE_HANDLER_PATTERNS,
125
- description: 'Route handler patterns to check',
126
- },
127
- ignorePatterns: {
128
- type: 'array',
129
- items: { type: 'string' },
130
- default: [],
131
- description: 'Additional patterns to ignore',
132
- },
133
- testFilePattern: {
134
- type: 'string',
135
- default: '\\.(test|spec)\\.(ts|tsx|js|jsx)$',
136
- description: 'Test file pattern regex string',
137
- },
138
- },
139
- additionalProperties: false,
140
- },
141
- ],
142
- },
143
- defaultOptions: [
144
- {
145
- allowInTests: false,
146
- testFilePattern: '\\.(test|spec)\\.(ts|tsx|js|jsx)$',
147
- authMiddlewarePatterns: DEFAULT_AUTH_MIDDLEWARE_PATTERNS,
148
- routeHandlerPatterns: DEFAULT_ROUTE_HANDLER_PATTERNS,
149
- ignorePatterns: [],
150
- },
151
- ],
152
- create(context, [options = {}]) {
153
- const { allowInTests = false, testFilePattern = '\\.(test|spec)\\.(ts|tsx|js|jsx)$', authMiddlewarePatterns = DEFAULT_AUTH_MIDDLEWARE_PATTERNS, routeHandlerPatterns = DEFAULT_ROUTE_HANDLER_PATTERNS, ignorePatterns = [], } = options;
154
- const filename = context.filename;
155
- const testFileRegex = new RegExp(testFilePattern);
156
- const isTestFile = allowInTests && testFileRegex.test(filename);
157
- const sourceCode = context.sourceCode;
158
- function findVariableDeclaration(identifier) {
159
- const varName = identifier.name;
160
- let current = identifier;
161
- while (current) {
162
- if (current.type === 'Program' ||
163
- current.type === 'FunctionDeclaration' ||
164
- current.type === 'FunctionExpression' ||
165
- current.type === 'ArrowFunctionExpression') {
166
- const scopeBody = current.type === 'Program'
167
- ? current.body
168
- : (current.body.type === 'BlockStatement' ? current.body.body : []);
169
- for (const stmt of scopeBody) {
170
- if (stmt.type === 'VariableDeclaration') {
171
- for (const declarator of stmt.declarations) {
172
- if (declarator.id.type === 'Identifier' && declarator.id.name === varName) {
173
- return declarator;
174
- }
175
- }
176
- }
177
- }
178
- }
179
- if ('parent' in current && current.parent) {
180
- current = current.parent;
181
- }
182
- else {
183
- break;
184
- }
185
- }
186
- return null;
187
- }
188
- function isIdentifierFromAuthMiddleware(identifier) {
189
- const declarator = findVariableDeclaration(identifier);
190
- if (!declarator || !declarator.init) {
191
- return false;
192
- }
193
- if (declarator.init.type === 'CallExpression' && declarator.init.callee.type === 'Identifier') {
194
- const calleeName = declarator.init.callee.name.toLowerCase();
195
- return authMiddlewarePatterns.some(pattern => calleeName.includes(pattern.toLowerCase()));
196
- }
197
- return false;
198
- }
199
- function checkCallExpression(node) {
200
- if (isTestFile) {
201
- return;
202
- }
203
- if (node.callee.type === 'MemberExpression') {
204
- const property = node.callee.property;
205
- const object = node.callee.object;
206
- if (object.type !== 'Identifier') {
207
- return;
208
- }
209
- const objectName = object.name.toLowerCase();
210
- const routerLikeNames = ['app', 'router', 'server', 'route', 'express', 'fastify', 'koa', 'hapi'];
211
- const looksLikeRouter = routerLikeNames.some(name => objectName.includes(name));
212
- if (!looksLikeRouter) {
213
- return;
214
- }
215
- if (property.type === 'Identifier') {
216
- const methodName = property.name.toLowerCase();
217
- if (routeHandlerPatterns.includes(methodName)) {
218
- let routePath = 'unknown';
219
- if (node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
220
- routePath = String(node.arguments[0].value);
221
- }
222
- else if (node.arguments.length > 0) {
223
- const pathText = sourceCode.getText(node.arguments[0]);
224
- routePath = pathText;
225
- }
226
- const text = sourceCode.getText(node);
227
- const pathIgnored = matchesIgnorePattern(routePath, ignorePatterns);
228
- if (pathIgnored) {
229
- console.log('DEBUG MSG: Ignored path:', routePath);
230
- return;
231
- }
232
- if (matchesIgnorePattern(text, ignorePatterns)) {
233
- console.log('DEBUG MSG: Ignored text:', text);
234
- return;
235
- }
236
- let hasAuth = false;
237
- for (const arg of node.arguments) {
238
- const argText = sourceCode.getText(arg);
239
- if (authMiddlewarePatterns.some(pattern => argText.toLowerCase().includes(pattern.toLowerCase()))) {
240
- hasAuth = true;
241
- break;
242
- }
243
- if (arg.type === 'Identifier' && isIdentifierFromAuthMiddleware(arg)) {
244
- hasAuth = true;
245
- break;
246
- }
247
- }
248
- if (!hasAuth && node.arguments.length > 0) {
249
- const lastArg = node.arguments[node.arguments.length - 1];
250
- if (lastArg.type === 'ArrowFunctionExpression' ||
251
- lastArg.type === 'FunctionExpression') {
252
- if (isInsideAuthMiddleware(lastArg, sourceCode, authMiddlewarePatterns)) {
253
- hasAuth = true;
254
- }
255
- }
256
- }
257
- if (!hasAuth) {
258
- context.report({
259
- node: node.callee,
260
- messageId: 'missingAuthentication',
261
- data: {
262
- route: `${methodName}(${routePath})`,
263
- method: methodName,
264
- path: routePath,
265
- },
266
- suggest: [
267
- {
268
- messageId: 'addAuthentication',
269
- fix: (_fixer) => null,
270
- },
271
- ],
272
- });
273
- }
274
- }
275
- }
276
- }
277
- }
278
- return {
279
- CallExpression: checkCallExpression,
280
- };
281
- },
282
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noMissingAuthentication=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const DEFAULT_AUTH_MIDDLEWARE_PATTERNS=["authenticate","auth","requireAuth","isAuthenticated","verifyToken","checkAuth","ensureAuthenticated","passport.authenticate","jwt","session"];const DEFAULT_ROUTE_HANDLER_PATTERNS=["get","post","put","delete","patch","all","use"];function isInsideAuthMiddleware(node,sourceCode,authPatterns){let current=node;while(current){if(current.parent&&current.parent.type==="CallExpression"){const callExpr=current.parent;const isArgument=callExpr.arguments.some(arg=>arg===current);if(!isArgument){current=current.parent;continue}const callee=callExpr.callee;if(callee.type==="Identifier"){const calleeName=callee.name.toLowerCase();if(authPatterns.some(pattern=>calleeName.includes(pattern.toLowerCase()))){return true}}if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"){const propertyName=callee.property.name.toLowerCase();if(propertyName==="use"||propertyName==="all"){for(const arg of callExpr.arguments){const argText=sourceCode.getText(arg);if(authPatterns.some(pattern=>argText.toLowerCase().includes(pattern.toLowerCase()))){return true}}}}}if("parent"in current&&current.parent){current=current.parent}else{break}}return false}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.noMissingAuthentication=(0,eslint_devkit_2.createRule)({name:"no-missing-authentication",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-missing-authentication.md",description:"Detects missing authentication checks in route handlers",cwe:"CWE-287",cvss:9.8},hasSuggestions:true,messages:{missingAuthentication:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Missing Authentication",cwe:"CWE-287",description:"Route handler missing authentication check: {{route}}",severity:"CRITICAL",fix:"Add authentication middleware: app.{{method}}('{{path}}', authenticate(), handler)",documentationLink:"https://cwe.mitre.org/data/definitions/287.html"}),addAuthentication:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add Authentication",description:"Add authentication middleware before route handler",severity:"LOW",fix:'app.get("/path", authenticate(), handler)',documentationLink:"https://cwe.mitre.org/data/definitions/287.html"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow missing authentication in test files"},authMiddlewarePatterns:{type:"array",items:{type:"string"},default:DEFAULT_AUTH_MIDDLEWARE_PATTERNS,description:"Authentication middleware patterns to recognize"},routeHandlerPatterns:{type:"array",items:{type:"string"},default:DEFAULT_ROUTE_HANDLER_PATTERNS,description:"Route handler patterns to check"},ignorePatterns:{type:"array",items:{type:"string"},default:[],description:"Additional patterns to ignore"},testFilePattern:{type:"string",default:"\\.(test|spec)\\.(ts|tsx|js|jsx)$",description:"Test file pattern regex string"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,testFilePattern:"\\.(test|spec)\\.(ts|tsx|js|jsx)$",authMiddlewarePatterns:DEFAULT_AUTH_MIDDLEWARE_PATTERNS,routeHandlerPatterns:DEFAULT_ROUTE_HANDLER_PATTERNS,ignorePatterns:[]}],create(context,[options={}]){const{allowInTests=false,testFilePattern="\\.(test|spec)\\.(ts|tsx|js|jsx)$",authMiddlewarePatterns=DEFAULT_AUTH_MIDDLEWARE_PATTERNS,routeHandlerPatterns=DEFAULT_ROUTE_HANDLER_PATTERNS,ignorePatterns=[]}=options;const filename=context.filename;const testFileRegex=new RegExp(testFilePattern);const isTestFile=allowInTests&&testFileRegex.test(filename);const sourceCode=context.sourceCode;function findVariableDeclaration(identifier){const varName=identifier.name;let current=identifier;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){return declarator}}}}}if("parent"in current&&current.parent){current=current.parent}else{break}}return null}function isIdentifierFromAuthMiddleware(identifier){const declarator=findVariableDeclaration(identifier);if(!declarator||!declarator.init){return false}if(declarator.init.type==="CallExpression"&&declarator.init.callee.type==="Identifier"){const calleeName=declarator.init.callee.name.toLowerCase();return authMiddlewarePatterns.some(pattern=>calleeName.includes(pattern.toLowerCase()))}return false}function checkCallExpression(node){if(isTestFile){return}if(node.callee.type==="MemberExpression"){const property=node.callee.property;const object=node.callee.object;if(object.type!=="Identifier"){return}const objectName=object.name.toLowerCase();const routerLikeNames=["app","router","server","route","express","fastify","koa","hapi"];const looksLikeRouter=routerLikeNames.some(name=>objectName.includes(name));if(!looksLikeRouter){return}if(property.type==="Identifier"){const methodName=property.name.toLowerCase();if(routeHandlerPatterns.includes(methodName)){let routePath="unknown";if(node.arguments.length>0&&node.arguments[0].type==="Literal"){routePath=String(node.arguments[0].value)}else if(node.arguments.length>0){const pathText=sourceCode.getText(node.arguments[0]);routePath=pathText}const text=sourceCode.getText(node);const pathIgnored=matchesIgnorePattern(routePath,ignorePatterns);if(pathIgnored){console.log("DEBUG MSG: Ignored path:",routePath);return}if(matchesIgnorePattern(text,ignorePatterns)){console.log("DEBUG MSG: Ignored text:",text);return}let hasAuth=false;for(const arg of node.arguments){const argText=sourceCode.getText(arg);if(authMiddlewarePatterns.some(pattern=>argText.toLowerCase().includes(pattern.toLowerCase()))){hasAuth=true;break}if(arg.type==="Identifier"&&isIdentifierFromAuthMiddleware(arg)){hasAuth=true;break}}if(!hasAuth&&node.arguments.length>0){const lastArg=node.arguments[node.arguments.length-1];if(lastArg.type==="ArrowFunctionExpression"||lastArg.type==="FunctionExpression"){if(isInsideAuthMiddleware(lastArg,sourceCode,authMiddlewarePatterns)){hasAuth=true}}}if(!hasAuth){context.report({node:node.callee,messageId:"missingAuthentication",data:{route:`${methodName}(${routePath})`,method:methodName,path:routePath},suggest:[{messageId:"addAuthentication",fix:_fixer=>null}]})}}}}}return{CallExpression:checkCallExpression}}});
@@ -1,63 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noPiiInLogs = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- exports.noPiiInLogs = (0, eslint_devkit_1.createRule)({
6
- name: 'no-pii-in-logs',
7
- meta: {
8
- type: 'suggestion',
9
- docs: {
10
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-pii-in-logs.md',
11
- description: 'Prevent PII (email, SSN, credit cards) in console logs',
12
- cwe: 'CWE-359',
13
- cvss: 7.5,
14
- },
15
- messages: {
16
- violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
17
- icon: eslint_devkit_1.MessageIcons.SECURITY,
18
- issueName: 'violation Detected',
19
- cwe: 'CWE-359',
20
- description: 'Prevent PII (email, SSN, credit cards) in console logs detected - this is a security risk',
21
- severity: 'HIGH',
22
- fix: 'Review and apply secure practices',
23
- documentationLink: 'https://cwe.mitre.org/data/definitions/359.html',
24
- })
25
- },
26
- schema: [],
27
- },
28
- defaultOptions: [],
29
- create(context) {
30
- function report(node) {
31
- context.report({
32
- node,
33
- messageId: 'violationDetected',
34
- });
35
- }
36
- return {
37
- CallExpression(node) {
38
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
39
- node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
40
- node.callee.object.name === 'console' &&
41
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
42
- ['log', 'error', 'warn', 'info'].includes(node.callee.property.name)) {
43
- for (const arg of node.arguments) {
44
- if (arg.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
45
- arg.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
46
- const propName = arg.property.name.toLowerCase();
47
- const piiProps = ['email', 'ssn', 'password', 'creditcard', 'phone'];
48
- if (piiProps.some(p => propName.includes(p))) {
49
- report(node);
50
- }
51
- }
52
- if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof arg.value === 'string') {
53
- const text = arg.value.toLowerCase();
54
- if (text.includes('email:') || text.includes('ssn:') || text.includes('password:')) {
55
- report(node);
56
- }
57
- }
58
- }
59
- }
60
- },
61
- };
62
- },
63
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noPiiInLogs=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");exports.noPiiInLogs=(0,eslint_devkit_1.createRule)({name:"no-pii-in-logs",meta:{type:"suggestion",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-pii-in-logs.md",description:"Prevent PII (email, SSN, credit cards) in console logs",cwe:"CWE-359",cvss:7.5},messages:{violationDetected:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"violation Detected",cwe:"CWE-359",description:"Prevent PII (email, SSN, credit cards) in console logs detected - this is a security risk",severity:"HIGH",fix:"Review and apply secure practices",documentationLink:"https://cwe.mitre.org/data/definitions/359.html"})},schema:[]},defaultOptions:[],create(context){function report(node){context.report({node,messageId:"violationDetected"})}return{CallExpression(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.object.name==="console"&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&["log","error","warn","info"].includes(node.callee.property.name)){for(const arg of node.arguments){if(arg.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&arg.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const propName=arg.property.name.toLowerCase();const piiProps=["email","ssn","password","creditcard","phone"];if(piiProps.some(p=>propName.includes(p))){report(node)}}if(arg.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof arg.value==="string"){const text=arg.value.toLowerCase();if(text.includes("email:")||text.includes("ssn:")||text.includes("password:")){report(node)}}}}}}}});
@@ -1,282 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noPrivilegeEscalation = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- const DEFAULT_ROLE_CHECK_PATTERNS = [
7
- 'hasRole',
8
- 'checkRole',
9
- 'isAdmin',
10
- 'isAuthorized',
11
- 'hasPermission',
12
- 'checkPermission',
13
- 'verifyRole',
14
- 'requireRole',
15
- ];
16
- const DEFAULT_USER_INPUT_PATTERNS = [
17
- /\breq\.(body|query|params)\b/,
18
- /\brequest\.(body|query|params)\b/,
19
- /\buserInput\b/,
20
- /\binput\b/,
21
- ];
22
- function matchesIgnorePattern(text, patterns) {
23
- return patterns.some(pattern => {
24
- try {
25
- const regex = new RegExp(pattern, 'i');
26
- return regex.test(text);
27
- }
28
- catch {
29
- return text.toLowerCase().includes(pattern.toLowerCase());
30
- }
31
- });
32
- }
33
- function containsUserInput(node, sourceCode, userInputPatterns) {
34
- const text = sourceCode.getText(node);
35
- return userInputPatterns.some(pattern => pattern.test(text));
36
- }
37
- function isInsideRoleCheck(node, sourceCode, roleCheckPatterns) {
38
- let current = node;
39
- while (current) {
40
- if (current.parent && current.parent.type === 'IfStatement') {
41
- const ifStmt = current.parent;
42
- const conditionText = sourceCode.getText(ifStmt.test);
43
- if (roleCheckPatterns.some(pattern => conditionText.toLowerCase().includes(pattern.toLowerCase()))) {
44
- return true;
45
- }
46
- }
47
- if (current.parent && current.parent.type === 'ConditionalExpression') {
48
- const condExpr = current.parent;
49
- const testText = sourceCode.getText(condExpr.test);
50
- if (roleCheckPatterns.some(pattern => testText.toLowerCase().includes(pattern.toLowerCase()))) {
51
- return true;
52
- }
53
- }
54
- if (current.parent && current.parent.type === 'CallExpression') {
55
- const callExpr = current.parent;
56
- const callee = callExpr.callee;
57
- if (callee.type === 'Identifier') {
58
- const calleeName = callee.name.toLowerCase();
59
- if (roleCheckPatterns.some(pattern => calleeName.includes(pattern.toLowerCase()))) {
60
- return true;
61
- }
62
- }
63
- if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier') {
64
- const propertyName = callee.property.name.toLowerCase();
65
- if (roleCheckPatterns.some(pattern => propertyName.includes(pattern.toLowerCase()))) {
66
- return true;
67
- }
68
- }
69
- }
70
- if ('parent' in current && current.parent) {
71
- current = current.parent;
72
- }
73
- else {
74
- break;
75
- }
76
- }
77
- return false;
78
- }
79
- exports.noPrivilegeEscalation = (0, eslint_devkit_2.createRule)({
80
- name: 'no-privilege-escalation',
81
- meta: {
82
- type: 'problem',
83
- docs: {
84
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-privilege-escalation.md',
85
- description: 'Detects potential privilege escalation vulnerabilities',
86
- cwe: 'CWE-269',
87
- cvss: 8.8,
88
- },
89
- hasSuggestions: true,
90
- messages: {
91
- privilegeEscalation: (0, eslint_devkit_1.formatLLMMessage)({
92
- icon: eslint_devkit_1.MessageIcons.SECURITY,
93
- issueName: 'Privilege Escalation',
94
- cwe: 'CWE-269',
95
- description: 'Potential privilege escalation: {{issue}} - user input used without role validation',
96
- severity: 'HIGH',
97
- fix: 'Add role check before using user input: if (!hasRole(user, requiredRole)) throw new Error("Unauthorized");',
98
- documentationLink: 'https://cwe.mitre.org/data/definitions/269.html',
99
- }),
100
- addRoleCheck: (0, eslint_devkit_1.formatLLMMessage)({
101
- icon: eslint_devkit_1.MessageIcons.INFO,
102
- issueName: 'Add Role Check',
103
- description: 'Add role check before privilege operations',
104
- severity: 'LOW',
105
- fix: 'if (!hasRole(user, requiredRole)) throw new Error("Unauthorized")',
106
- documentationLink: 'https://cwe.mitre.org/data/definitions/269.html',
107
- }),
108
- },
109
- schema: [
110
- {
111
- type: 'object',
112
- properties: {
113
- allowInTests: {
114
- type: 'boolean',
115
- default: false,
116
- description: 'Allow privilege escalation patterns in test files',
117
- },
118
- testFilePattern: {
119
- type: 'string',
120
- default: '\\.(test|spec)\\.(ts|tsx|js|jsx)$',
121
- description: 'Test file pattern regex string',
122
- },
123
- roleCheckPatterns: {
124
- type: 'array',
125
- items: { type: 'string' },
126
- default: DEFAULT_ROLE_CHECK_PATTERNS,
127
- description: 'Role check patterns to recognize',
128
- },
129
- userInputPatterns: {
130
- type: 'array',
131
- items: { type: 'string' },
132
- default: [],
133
- description: 'Additional user input patterns to check (regex strings)',
134
- },
135
- ignorePatterns: {
136
- type: 'array',
137
- items: { type: 'string' },
138
- default: [],
139
- description: 'Additional patterns to ignore',
140
- },
141
- },
142
- additionalProperties: false,
143
- },
144
- ],
145
- },
146
- defaultOptions: [
147
- {
148
- allowInTests: false,
149
- testFilePattern: '\\.(test|spec)\\.(ts|tsx|js|jsx)$',
150
- roleCheckPatterns: DEFAULT_ROLE_CHECK_PATTERNS,
151
- userInputPatterns: [],
152
- ignorePatterns: [],
153
- },
154
- ],
155
- create(context, [options = {}]) {
156
- const { allowInTests = false, testFilePattern = '\\.(test|spec)\\.(ts|tsx|js|jsx)$', roleCheckPatterns = DEFAULT_ROLE_CHECK_PATTERNS, userInputPatterns: additionalUserInputPatterns = [], ignorePatterns = [], } = options;
157
- const filename = context.filename;
158
- const testFileRegex = new RegExp(testFilePattern);
159
- const isTestFile = allowInTests && testFileRegex.test(filename);
160
- const sourceCode = context.sourceCode;
161
- const userInputPatterns = [
162
- ...DEFAULT_USER_INPUT_PATTERNS,
163
- ...additionalUserInputPatterns.map(pattern => new RegExp(pattern, 'i')),
164
- ];
165
- function checkAssignmentExpression(node) {
166
- if (isTestFile) {
167
- return;
168
- }
169
- if (node.left.type === 'MemberExpression' &&
170
- node.left.property.type === 'Identifier') {
171
- const propertyName = node.left.property.name.toLowerCase();
172
- if (['role', 'permission', 'privilege', 'access', 'level'].includes(propertyName)) {
173
- const text = sourceCode.getText(node);
174
- if (matchesIgnorePattern(text, ignorePatterns)) {
175
- return;
176
- }
177
- if (containsUserInput(node.right, sourceCode, userInputPatterns)) {
178
- if (!isInsideRoleCheck(node, sourceCode, roleCheckPatterns)) {
179
- context.report({
180
- node: node,
181
- messageId: 'privilegeEscalation',
182
- data: {
183
- issue: `Role assignment from user input: ${sourceCode.getText(node.left)} = ${sourceCode.getText(node.right)}`,
184
- },
185
- suggest: [
186
- {
187
- messageId: 'addRoleCheck',
188
- fix: () => null,
189
- },
190
- ],
191
- });
192
- }
193
- }
194
- }
195
- }
196
- }
197
- function checkCallExpression(node) {
198
- if (isTestFile) {
199
- return;
200
- }
201
- const callee = node.callee;
202
- let isPrivilegeOperation = false;
203
- let operationName = '';
204
- if (callee.type === 'Identifier') {
205
- const calleeName = callee.name.toLowerCase();
206
- if (['setrole', 'grant', 'revoke', 'elevate', 'promote'].some(op => calleeName.includes(op))) {
207
- isPrivilegeOperation = true;
208
- operationName = callee.name;
209
- }
210
- }
211
- if (callee.type === 'MemberExpression' && callee.property.type === 'Identifier') {
212
- const propertyName = callee.property.name.toLowerCase();
213
- if (['setrole', 'grant', 'revoke', 'elevate', 'promote', 'updaterole'].some(op => propertyName.includes(op))) {
214
- isPrivilegeOperation = true;
215
- operationName = propertyName;
216
- }
217
- }
218
- if (isPrivilegeOperation) {
219
- const text = sourceCode.getText(node);
220
- if (matchesIgnorePattern(text, ignorePatterns)) {
221
- return;
222
- }
223
- for (const arg of node.arguments) {
224
- if (containsUserInput(arg, sourceCode, userInputPatterns)) {
225
- if (!isInsideRoleCheck(node, sourceCode, roleCheckPatterns)) {
226
- context.report({
227
- node: node,
228
- messageId: 'privilegeEscalation',
229
- data: {
230
- issue: `Privilege operation (${operationName}) with user input without role validation`,
231
- },
232
- suggest: [
233
- {
234
- messageId: 'addRoleCheck',
235
- fix: () => null,
236
- },
237
- ],
238
- });
239
- return;
240
- }
241
- }
242
- }
243
- }
244
- }
245
- function checkObjectExpression(node) {
246
- if (isTestFile)
247
- return;
248
- for (const prop of node.properties) {
249
- if (prop.type === 'Property' && prop.key.type === 'Identifier') {
250
- const keyName = prop.key.name.toLowerCase();
251
- if (['role', 'permission', 'privilege', 'access', 'level'].includes(keyName)) {
252
- const text = sourceCode.getText(prop);
253
- if (matchesIgnorePattern(text, ignorePatterns))
254
- continue;
255
- if (containsUserInput(prop.value, sourceCode, userInputPatterns)) {
256
- if (!isInsideRoleCheck(node, sourceCode, roleCheckPatterns)) {
257
- context.report({
258
- node: prop,
259
- messageId: 'privilegeEscalation',
260
- data: {
261
- issue: `Role assignment in object from user input: ${sourceCode.getText(prop)}`,
262
- },
263
- suggest: [
264
- {
265
- messageId: 'addRoleCheck',
266
- fix: () => null,
267
- },
268
- ],
269
- });
270
- }
271
- }
272
- }
273
- }
274
- }
275
- }
276
- return {
277
- AssignmentExpression: checkAssignmentExpression,
278
- CallExpression: checkCallExpression,
279
- ObjectExpression: checkObjectExpression,
280
- };
281
- },
282
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noPrivilegeEscalation=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const DEFAULT_ROLE_CHECK_PATTERNS=["hasRole","checkRole","isAdmin","isAuthorized","hasPermission","checkPermission","verifyRole","requireRole"];const DEFAULT_USER_INPUT_PATTERNS=[/\breq\.(body|query|params)\b/,/\brequest\.(body|query|params)\b/,/\buserInput\b/,/\binput\b/];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())}})}function containsUserInput(node,sourceCode,userInputPatterns){const text=sourceCode.getText(node);return userInputPatterns.some(pattern=>pattern.test(text))}function isInsideRoleCheck(node,sourceCode,roleCheckPatterns){let current=node;while(current){if(current.parent&&current.parent.type==="IfStatement"){const ifStmt=current.parent;const conditionText=sourceCode.getText(ifStmt.test);if(roleCheckPatterns.some(pattern=>conditionText.toLowerCase().includes(pattern.toLowerCase()))){return true}}if(current.parent&&current.parent.type==="ConditionalExpression"){const condExpr=current.parent;const testText=sourceCode.getText(condExpr.test);if(roleCheckPatterns.some(pattern=>testText.toLowerCase().includes(pattern.toLowerCase()))){return true}}if(current.parent&&current.parent.type==="CallExpression"){const callExpr=current.parent;const callee=callExpr.callee;if(callee.type==="Identifier"){const calleeName=callee.name.toLowerCase();if(roleCheckPatterns.some(pattern=>calleeName.includes(pattern.toLowerCase()))){return true}}if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"){const propertyName=callee.property.name.toLowerCase();if(roleCheckPatterns.some(pattern=>propertyName.includes(pattern.toLowerCase()))){return true}}}if("parent"in current&&current.parent){current=current.parent}else{break}}return false}exports.noPrivilegeEscalation=(0,eslint_devkit_2.createRule)({name:"no-privilege-escalation",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-privilege-escalation.md",description:"Detects potential privilege escalation vulnerabilities",cwe:"CWE-269",cvss:8.8},hasSuggestions:true,messages:{privilegeEscalation:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Privilege Escalation",cwe:"CWE-269",description:"Potential privilege escalation: {{issue}} - user input used without role validation",severity:"HIGH",fix:'Add role check before using user input: if (!hasRole(user, requiredRole)) throw new Error("Unauthorized");',documentationLink:"https://cwe.mitre.org/data/definitions/269.html"}),addRoleCheck:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add Role Check",description:"Add role check before privilege operations",severity:"LOW",fix:'if (!hasRole(user, requiredRole)) throw new Error("Unauthorized")',documentationLink:"https://cwe.mitre.org/data/definitions/269.html"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false,description:"Allow privilege escalation patterns in test files"},testFilePattern:{type:"string",default:"\\.(test|spec)\\.(ts|tsx|js|jsx)$",description:"Test file pattern regex string"},roleCheckPatterns:{type:"array",items:{type:"string"},default:DEFAULT_ROLE_CHECK_PATTERNS,description:"Role check patterns to recognize"},userInputPatterns:{type:"array",items:{type:"string"},default:[],description:"Additional user input patterns to check (regex strings)"},ignorePatterns:{type:"array",items:{type:"string"},default:[],description:"Additional patterns to ignore"}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,testFilePattern:"\\.(test|spec)\\.(ts|tsx|js|jsx)$",roleCheckPatterns:DEFAULT_ROLE_CHECK_PATTERNS,userInputPatterns:[],ignorePatterns:[]}],create(context,[options={}]){const{allowInTests=false,testFilePattern="\\.(test|spec)\\.(ts|tsx|js|jsx)$",roleCheckPatterns=DEFAULT_ROLE_CHECK_PATTERNS,userInputPatterns:additionalUserInputPatterns=[],ignorePatterns=[]}=options;const filename=context.filename;const testFileRegex=new RegExp(testFilePattern);const isTestFile=allowInTests&&testFileRegex.test(filename);const sourceCode=context.sourceCode;const userInputPatterns=[...DEFAULT_USER_INPUT_PATTERNS,...additionalUserInputPatterns.map(pattern=>new RegExp(pattern,"i"))];function checkAssignmentExpression(node){if(isTestFile){return}if(node.left.type==="MemberExpression"&&node.left.property.type==="Identifier"){const propertyName=node.left.property.name.toLowerCase();if(["role","permission","privilege","access","level"].includes(propertyName)){const text=sourceCode.getText(node);if(matchesIgnorePattern(text,ignorePatterns)){return}if(containsUserInput(node.right,sourceCode,userInputPatterns)){if(!isInsideRoleCheck(node,sourceCode,roleCheckPatterns)){context.report({node,messageId:"privilegeEscalation",data:{issue:`Role assignment from user input: ${sourceCode.getText(node.left)} = ${sourceCode.getText(node.right)}`},suggest:[{messageId:"addRoleCheck",fix:()=>null}]})}}}}}function checkCallExpression(node){if(isTestFile){return}const callee=node.callee;let isPrivilegeOperation=false;let operationName="";if(callee.type==="Identifier"){const calleeName=callee.name.toLowerCase();if(["setrole","grant","revoke","elevate","promote"].some(op=>calleeName.includes(op))){isPrivilegeOperation=true;operationName=callee.name}}if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"){const propertyName=callee.property.name.toLowerCase();if(["setrole","grant","revoke","elevate","promote","updaterole"].some(op=>propertyName.includes(op))){isPrivilegeOperation=true;operationName=propertyName}}if(isPrivilegeOperation){const text=sourceCode.getText(node);if(matchesIgnorePattern(text,ignorePatterns)){return}for(const arg of node.arguments){if(containsUserInput(arg,sourceCode,userInputPatterns)){if(!isInsideRoleCheck(node,sourceCode,roleCheckPatterns)){context.report({node,messageId:"privilegeEscalation",data:{issue:`Privilege operation (${operationName}) with user input without role validation`},suggest:[{messageId:"addRoleCheck",fix:()=>null}]});return}}}}}function checkObjectExpression(node){if(isTestFile)return;for(const prop of node.properties){if(prop.type==="Property"&&prop.key.type==="Identifier"){const keyName=prop.key.name.toLowerCase();if(["role","permission","privilege","access","level"].includes(keyName)){const text=sourceCode.getText(prop);if(matchesIgnorePattern(text,ignorePatterns))continue;if(containsUserInput(prop.value,sourceCode,userInputPatterns)){if(!isInsideRoleCheck(node,sourceCode,roleCheckPatterns)){context.report({node:prop,messageId:"privilegeEscalation",data:{issue:`Role assignment in object from user input: ${sourceCode.getText(prop)}`},suggest:[{messageId:"addRoleCheck",fix:()=>null}]})}}}}}}return{AssignmentExpression:checkAssignmentExpression,CallExpression:checkCallExpression,ObjectExpression:checkObjectExpression}}});