eslint-plugin-lambda-security 1.3.2 → 1.3.3

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.
@@ -1,276 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUnvalidatedEventBody = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const DANGEROUS_EVENT_PROPERTIES = [
6
- 'body',
7
- 'queryStringParameters',
8
- 'pathParameters',
9
- 'multiValueQueryStringParameters',
10
- 'headers',
11
- 'multiValueHeaders',
12
- 'rawPath',
13
- 'rawQueryString',
14
- ];
15
- const VALIDATION_INDICATORS = new Set([
16
- 'validator',
17
- 'httpJsonBodyParser',
18
- '@middy/validator',
19
- 'parse',
20
- 'validate',
21
- 'parseAsync',
22
- 'safeParse',
23
- 'safeParseAsync',
24
- 'assert',
25
- 'is',
26
- ]);
27
- exports.noUnvalidatedEventBody = (0, eslint_devkit_1.createRule)({
28
- name: 'no-unvalidated-event-body',
29
- meta: {
30
- type: 'problem',
31
- docs: {
32
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/no-unvalidated-event-body.md',
33
- description: 'Detects Lambda handlers using event body/params without validation',
34
- cwe: 'CWE-20',
35
- cvss: 8,
36
- },
37
- hasSuggestions: true,
38
- messages: {
39
- unvalidatedInput: (0, eslint_devkit_1.formatLLMMessage)({
40
- icon: eslint_devkit_1.MessageIcons.SECURITY,
41
- issueName: 'Unvalidated Event Input',
42
- cwe: 'CWE-20',
43
- cvss: 8.0,
44
- description: 'Lambda handler uses {{property}} without validation. This can lead to injection attacks.',
45
- severity: 'HIGH',
46
- fix: 'Validate event input using Middy validator, Zod, or Joi before use',
47
- documentationLink: 'https://owasp.org/www-project-serverless-top-10/',
48
- }),
49
- useValidation: (0, eslint_devkit_1.formatLLMMessage)({
50
- icon: eslint_devkit_1.MessageIcons.INFO,
51
- issueName: 'Add Input Validation',
52
- description: 'Add input validation using Middy validator middleware or a schema library',
53
- severity: 'LOW',
54
- fix: "import { validator } from '@middy/validator'; middy(handler).use(validator({ inputSchema }))",
55
- documentationLink: 'https://middy.js.org/docs/middlewares/validator',
56
- }),
57
- },
58
- schema: [
59
- {
60
- type: 'object',
61
- properties: {
62
- allowInTests: {
63
- type: 'boolean',
64
- default: true,
65
- description: 'Allow unvalidated input in test files',
66
- },
67
- additionalProperties: {
68
- type: 'array',
69
- items: { type: 'string' },
70
- default: [],
71
- description: 'Additional event properties to check',
72
- },
73
- },
74
- additionalProperties: false,
75
- },
76
- ],
77
- },
78
- defaultOptions: [
79
- {
80
- allowInTests: true,
81
- additionalProperties: [],
82
- },
83
- ],
84
- create(context, [options = {}]) {
85
- const { allowInTests = true, additionalProperties = [] } = options;
86
- const filename = context.filename;
87
- const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
88
- if (allowInTests && isTestFile) {
89
- return {};
90
- }
91
- const dangerousProperties = new Set([
92
- ...DANGEROUS_EVENT_PROPERTIES,
93
- ...additionalProperties,
94
- ]);
95
- const validatedVariables = new Set();
96
- const eventParameters = new Set();
97
- let hasValidationMiddleware = false;
98
- function isValidationCall(node) {
99
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
100
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
101
- const property = node.callee.property;
102
- if (property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
103
- VALIDATION_INDICATORS.has(property.name)) {
104
- return true;
105
- }
106
- }
107
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
108
- if (VALIDATION_INDICATORS.has(node.callee.name)) {
109
- return true;
110
- }
111
- }
112
- }
113
- return false;
114
- }
115
- function isEventPropertyAccess(node) {
116
- if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
117
- eventParameters.has(node.object.name) &&
118
- node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
119
- dangerousProperties.has(node.property.name)) {
120
- return {
121
- eventName: node.object.name,
122
- property: node.property.name,
123
- };
124
- }
125
- return null;
126
- }
127
- function semanticParent(node) {
128
- let current = node;
129
- let parent = current.parent;
130
- while (parent &&
131
- (parent.type === eslint_devkit_1.AST_NODE_TYPES.LogicalExpression ||
132
- parent.type === eslint_devkit_1.AST_NODE_TYPES.TSAsExpression ||
133
- parent.type === eslint_devkit_1.AST_NODE_TYPES.TSNonNullExpression)) {
134
- current = parent;
135
- parent = current.parent;
136
- }
137
- return parent;
138
- }
139
- const sourceCode = context.sourceCode;
140
- const fileImportsLambda = sourceCode.ast.body.some((n) => {
141
- if (n.type !== eslint_devkit_1.AST_NODE_TYPES.ImportDeclaration)
142
- return false;
143
- const src = String(n.source.value);
144
- return (src === 'aws-lambda' ||
145
- src.startsWith('@aws-sdk/') ||
146
- src.startsWith('@middy/') ||
147
- src === '@aws-cdk/aws-lambda');
148
- });
149
- function isExportedAsHandler(node) {
150
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration &&
151
- node.id?.name === 'handler') {
152
- return true;
153
- }
154
- const parent = node.parent;
155
- if (!parent)
156
- return false;
157
- if (parent.type === eslint_devkit_1.AST_NODE_TYPES.VariableDeclarator &&
158
- parent.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
159
- parent.id.name === 'handler') {
160
- return true;
161
- }
162
- if (parent.type === eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression) {
163
- const left = parent.left;
164
- if (left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
165
- left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
166
- left.property.name === 'handler') {
167
- return true;
168
- }
169
- }
170
- return false;
171
- }
172
- function isLambdaSignature(params, node) {
173
- if (params.length < 1)
174
- return false;
175
- const first = params[0];
176
- if (first.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
177
- return false;
178
- const second = params[1];
179
- const hasContextSibling = second?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
180
- (second.name === 'context' || second.name === 'callback');
181
- const looksLikeExpress = second?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
182
- ((first.name === 'req' && second.name === 'res') ||
183
- (first.name === 'request' && second.name === 'response'));
184
- if (looksLikeExpress)
185
- return false;
186
- const isEventName = first.name === 'event' || first.name === 'evt';
187
- if (isEventName && hasContextSibling)
188
- return true;
189
- if (isEventName && fileImportsLambda)
190
- return true;
191
- if (isExportedAsHandler(node)) {
192
- const allowedHandlerParam = ['event', 'evt', 'e', 'request', 'req'];
193
- if (allowedHandlerParam.includes(first.name))
194
- return true;
195
- }
196
- return false;
197
- }
198
- return {
199
- 'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
200
- if (!isLambdaSignature(node.params, node))
201
- return;
202
- const first = node.params[0];
203
- eventParameters.add(first.name);
204
- },
205
- CallExpression(node) {
206
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
207
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
208
- node.callee.property.name === 'use') {
209
- for (const arg of node.arguments) {
210
- if (arg.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
211
- arg.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
212
- ['validator', 'httpJsonBodyParser'].includes(arg.callee.name)) {
213
- hasValidationMiddleware = true;
214
- }
215
- }
216
- }
217
- if (isValidationCall(node)) {
218
- const parent = node.parent;
219
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.VariableDeclarator &&
220
- parent.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
221
- validatedVariables.add(parent.id.name);
222
- }
223
- }
224
- },
225
- MemberExpression(node) {
226
- if (hasValidationMiddleware)
227
- return;
228
- const eventAccess = isEventPropertyAccess(node);
229
- if (!eventAccess)
230
- return;
231
- const parent = semanticParent(node);
232
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
233
- isValidationCall(parent)) {
234
- return;
235
- }
236
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression &&
237
- parent.operator === 'typeof') {
238
- return;
239
- }
240
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement ||
241
- parent?.type === eslint_devkit_1.AST_NODE_TYPES.ConditionalExpression) {
242
- return;
243
- }
244
- if (node.optional) {
245
- return;
246
- }
247
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
248
- parent.optional) {
249
- return;
250
- }
251
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.ChainExpression) {
252
- return;
253
- }
254
- if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
255
- parent.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
256
- parent.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
257
- parent.callee.object.name === 'console') {
258
- return;
259
- }
260
- context.report({
261
- node,
262
- messageId: 'unvalidatedInput',
263
- data: {
264
- property: `event.${eventAccess.property}`,
265
- },
266
- suggest: [
267
- {
268
- messageId: 'useValidation',
269
- fix: () => null,
270
- },
271
- ],
272
- });
273
- },
274
- };
275
- },
276
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnvalidatedEventBody=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DANGEROUS_EVENT_PROPERTIES=["body","queryStringParameters","pathParameters","multiValueQueryStringParameters","headers","multiValueHeaders","rawPath","rawQueryString"];const VALIDATION_INDICATORS=new Set(["validator","httpJsonBodyParser","@middy/validator","parse","validate","parseAsync","safeParse","safeParseAsync","assert","is"]);exports.noUnvalidatedEventBody=(0,eslint_devkit_1.createRule)({name:"no-unvalidated-event-body",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/no-unvalidated-event-body.md",description:"Detects Lambda handlers using event body/params without validation",cwe:"CWE-20",cvss:8},hasSuggestions:true,messages:{unvalidatedInput:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Unvalidated Event Input",cwe:"CWE-20",cvss:8,description:"Lambda handler uses {{property}} without validation. This can lead to injection attacks.",severity:"HIGH",fix:"Validate event input using Middy validator, Zod, or Joi before use",documentationLink:"https://owasp.org/www-project-serverless-top-10/"}),useValidation:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Add Input Validation",description:"Add input validation using Middy validator middleware or a schema library",severity:"LOW",fix:"import { validator } from '@middy/validator'; middy(handler).use(validator({ inputSchema }))",documentationLink:"https://middy.js.org/docs/middlewares/validator"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:true,description:"Allow unvalidated input in test files"},additionalProperties:{type:"array",items:{type:"string"},default:[],description:"Additional event properties to check"}},additionalProperties:false}]},defaultOptions:[{allowInTests:true,additionalProperties:[]}],create(context,[options={}]){const{allowInTests=true,additionalProperties=[]}=options;const filename=context.filename;const isTestFile=/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(allowInTests&&isTestFile){return{}}const dangerousProperties=new Set([...DANGEROUS_EVENT_PROPERTIES,...additionalProperties]);const validatedVariables=new Set;const eventParameters=new Set;let hasValidationMiddleware=false;function isValidationCall(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){const property=node.callee.property;if(property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&VALIDATION_INDICATORS.has(property.name)){return true}}if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){if(VALIDATION_INDICATORS.has(node.callee.name)){return true}}}return false}function isEventPropertyAccess(node){if(node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&eventParameters.has(node.object.name)&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&dangerousProperties.has(node.property.name)){return{eventName:node.object.name,property:node.property.name}}return null}function semanticParent(node){let current=node;let parent=current.parent;while(parent&&(parent.type===eslint_devkit_1.AST_NODE_TYPES.LogicalExpression||parent.type===eslint_devkit_1.AST_NODE_TYPES.TSAsExpression||parent.type===eslint_devkit_1.AST_NODE_TYPES.TSNonNullExpression)){current=parent;parent=current.parent}return parent}const sourceCode=context.sourceCode;const fileImportsLambda=sourceCode.ast.body.some(n=>{if(n.type!==eslint_devkit_1.AST_NODE_TYPES.ImportDeclaration)return false;const src=String(n.source.value);return src==="aws-lambda"||src.startsWith("@aws-sdk/")||src.startsWith("@middy/")||src==="@aws-cdk/aws-lambda"});function isExportedAsHandler(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration&&node.id?.name==="handler"){return true}const parent=node.parent;if(!parent)return false;if(parent.type===eslint_devkit_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&parent.id.name==="handler"){return true}if(parent.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression){const left=parent.left;if(left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&left.property.name==="handler"){return true}}return false}function isLambdaSignature(params,node){if(params.length<1)return false;const first=params[0];if(first.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;const second=params[1];const hasContextSibling=second?.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&(second.name==="context"||second.name==="callback");const looksLikeExpress=second?.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&(first.name==="req"&&second.name==="res"||first.name==="request"&&second.name==="response");if(looksLikeExpress)return false;const isEventName=first.name==="event"||first.name==="evt";if(isEventName&&hasContextSibling)return true;if(isEventName&&fileImportsLambda)return true;if(isExportedAsHandler(node)){const allowedHandlerParam=["event","evt","e","request","req"];if(allowedHandlerParam.includes(first.name))return true}return false}return{"ArrowFunctionExpression, FunctionExpression, FunctionDeclaration"(node){if(!isLambdaSignature(node.params,node))return;const first=node.params[0];eventParameters.add(first.name)},CallExpression(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.property.name==="use"){for(const arg of node.arguments){if(arg.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&arg.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&["validator","httpJsonBodyParser"].includes(arg.callee.name)){hasValidationMiddleware=true}}}if(isValidationCall(node)){const parent=node.parent;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){validatedVariables.add(parent.id.name)}}},MemberExpression(node){if(hasValidationMiddleware)return;const eventAccess=isEventPropertyAccess(node);if(!eventAccess)return;const parent=semanticParent(node);if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&isValidationCall(parent)){return}if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.UnaryExpression&&parent.operator==="typeof"){return}if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement||parent?.type===eslint_devkit_1.AST_NODE_TYPES.ConditionalExpression){return}if(node.optional){return}if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&parent.optional){return}if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.ChainExpression){return}if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&parent.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&parent.callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&parent.callee.object.name==="console"){return}context.report({node,messageId:"unvalidatedInput",data:{property:`event.${eventAccess.property}`},suggest:[{messageId:"useValidation",fix:()=>null}]})}}}});
@@ -1,224 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUserControlledRequests = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const HTTP_CLIENT_METHODS = new Set([
6
- 'fetch',
7
- 'request',
8
- 'get',
9
- 'post',
10
- 'put',
11
- 'delete',
12
- 'patch',
13
- 'axios',
14
- 'got',
15
- 'nodeFetch',
16
- 'superagent',
17
- ]);
18
- const HTTP_CLIENT_OBJECTS = new Set(['http', 'https', 'axios', 'got', 'request']);
19
- const USER_INPUT_PROPERTIES = new Set([
20
- 'body',
21
- 'queryStringParameters',
22
- 'pathParameters',
23
- 'multiValueQueryStringParameters',
24
- 'headers',
25
- 'rawPath',
26
- 'rawQueryString',
27
- ]);
28
- const EVENT_PARAM_NAMES = new Set(['event', 'evt', 'e', 'request', 'req']);
29
- exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
30
- name: 'no-user-controlled-requests',
31
- meta: {
32
- type: 'problem',
33
- docs: {
34
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/no-user-controlled-requests.md',
35
- description: 'Detects HTTP requests with user-controlled URLs (SSRF vulnerability)',
36
- cwe: 'CWE-918',
37
- cvss: 9.1,
38
- },
39
- hasSuggestions: true,
40
- messages: {
41
- ssrfVulnerability: (0, eslint_devkit_1.formatLLMMessage)({
42
- icon: eslint_devkit_1.MessageIcons.SECURITY,
43
- issueName: 'Server-Side Request Forgery (SSRF)',
44
- cwe: 'CWE-918',
45
- cvss: 9.1,
46
- description: 'HTTP request URL contains user-controlled input from {{source}}. Attackers can access internal services or exfiltrate data.',
47
- severity: 'CRITICAL',
48
- fix: 'Validate URL against allowlist before making request. Never use user input directly in URLs.',
49
- documentationLink: 'https://owasp.org/www-community/attacks/Server_Side_Request_Forgery',
50
- }),
51
- useAllowlist: (0, eslint_devkit_1.formatLLMMessage)({
52
- icon: eslint_devkit_1.MessageIcons.INFO,
53
- issueName: 'Use URL Allowlist',
54
- description: 'Implement an allowlist for permitted URL domains/hosts',
55
- severity: 'LOW',
56
- fix: "const ALLOWED_HOSTS = ['api.trusted.com']; if (!ALLOWED_HOSTS.includes(new URL(url).host)) throw new Error('Invalid URL')",
57
- documentationLink: 'https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html',
58
- }),
59
- },
60
- schema: [
61
- {
62
- type: 'object',
63
- properties: {
64
- allowInTests: {
65
- type: 'boolean',
66
- default: true,
67
- description: 'Allow user-controlled URLs in test files',
68
- },
69
- },
70
- additionalProperties: false,
71
- },
72
- ],
73
- },
74
- defaultOptions: [
75
- {
76
- allowInTests: true,
77
- },
78
- ],
79
- create(context, [options = {}]) {
80
- const { allowInTests = true } = options;
81
- const filename = context.filename;
82
- const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
83
- if (allowInTests && isTestFile) {
84
- return {};
85
- }
86
- const eventParameters = new Set();
87
- const taintedVariables = new Map();
88
- function isEventParameter(name) {
89
- return eventParameters.has(name);
90
- }
91
- function getEventInputSource(node) {
92
- if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
93
- isEventParameter(node.object.name) &&
94
- node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
95
- USER_INPUT_PROPERTIES.has(node.property.name)) {
96
- return `event.${node.property.name}`;
97
- }
98
- if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
99
- node.object.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
100
- isEventParameter(node.object.object.name) &&
101
- node.object.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
102
- USER_INPUT_PROPERTIES.has(node.object.property.name)) {
103
- return `event.${node.object.property.name}.${node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
104
- ? node.property.name
105
- : '[...]'}`;
106
- }
107
- return null;
108
- }
109
- function getTaintSource(node) {
110
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
111
- const source = getEventInputSource(node);
112
- if (source)
113
- return source;
114
- }
115
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
116
- const source = taintedVariables.get(node.name);
117
- if (source)
118
- return source;
119
- }
120
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
121
- for (const expr of node.expressions) {
122
- const source = getTaintSource(expr);
123
- if (source)
124
- return source;
125
- }
126
- }
127
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
128
- const leftSource = getTaintSource(node.left);
129
- if (leftSource)
130
- return leftSource;
131
- const rightSource = getTaintSource(node.right);
132
- if (rightSource)
133
- return rightSource;
134
- }
135
- return null;
136
- }
137
- function isHttpClientCall(node) {
138
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
139
- HTTP_CLIENT_METHODS.has(node.callee.name)) {
140
- return true;
141
- }
142
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
143
- node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
144
- HTTP_CLIENT_OBJECTS.has(node.callee.object.name)) {
145
- return true;
146
- }
147
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
148
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
149
- ['get', 'post', 'put', 'delete', 'patch', 'request'].includes(node.callee.property.name)) {
150
- return true;
151
- }
152
- return false;
153
- }
154
- function checkUrlArgument(node) {
155
- if (node.arguments.length === 0)
156
- return null;
157
- const urlArg = node.arguments[0];
158
- const source = getTaintSource(urlArg);
159
- if (source) {
160
- return { tainted: true, source };
161
- }
162
- if (urlArg.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
163
- for (const prop of urlArg.properties) {
164
- if (prop.type === eslint_devkit_1.AST_NODE_TYPES.Property &&
165
- prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
166
- ['url', 'uri', 'href', 'baseURL'].includes(prop.key.name)) {
167
- const propSource = getTaintSource(prop.value);
168
- if (propSource) {
169
- return { tainted: true, source: propSource };
170
- }
171
- }
172
- }
173
- }
174
- return null;
175
- }
176
- return {
177
- 'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
178
- for (const param of node.params) {
179
- if (param.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
180
- if (EVENT_PARAM_NAMES.has(param.name)) {
181
- eventParameters.add(param.name);
182
- }
183
- }
184
- }
185
- },
186
- VariableDeclarator(node) {
187
- if (node.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.init) {
188
- const source = getTaintSource(node.init);
189
- if (source) {
190
- taintedVariables.set(node.id.name, source);
191
- }
192
- }
193
- },
194
- AssignmentExpression(node) {
195
- if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
196
- const source = getTaintSource(node.right);
197
- if (source) {
198
- taintedVariables.set(node.left.name, source);
199
- }
200
- }
201
- },
202
- CallExpression(node) {
203
- if (!isHttpClientCall(node))
204
- return;
205
- const result = checkUrlArgument(node);
206
- if (result?.tainted) {
207
- context.report({
208
- node,
209
- messageId: 'ssrfVulnerability',
210
- data: {
211
- source: result.source,
212
- },
213
- suggest: [
214
- {
215
- messageId: 'useAllowlist',
216
- fix: () => null,
217
- },
218
- ],
219
- });
220
- }
221
- },
222
- };
223
- },
224
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUserControlledRequests=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const HTTP_CLIENT_METHODS=new Set(["fetch","request","get","post","put","delete","patch","axios","got","nodeFetch","superagent"]);const HTTP_CLIENT_OBJECTS=new Set(["http","https","axios","got","request"]);const USER_INPUT_PROPERTIES=new Set(["body","queryStringParameters","pathParameters","multiValueQueryStringParameters","headers","rawPath","rawQueryString"]);const EVENT_PARAM_NAMES=new Set(["event","evt","e","request","req"]);exports.noUserControlledRequests=(0,eslint_devkit_1.createRule)({name:"no-user-controlled-requests",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/no-user-controlled-requests.md",description:"Detects HTTP requests with user-controlled URLs (SSRF vulnerability)",cwe:"CWE-918",cvss:9.1},hasSuggestions:true,messages:{ssrfVulnerability:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Server-Side Request Forgery (SSRF)",cwe:"CWE-918",cvss:9.1,description:"HTTP request URL contains user-controlled input from {{source}}. Attackers can access internal services or exfiltrate data.",severity:"CRITICAL",fix:"Validate URL against allowlist before making request. Never use user input directly in URLs.",documentationLink:"https://owasp.org/www-community/attacks/Server_Side_Request_Forgery"}),useAllowlist:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use URL Allowlist",description:"Implement an allowlist for permitted URL domains/hosts",severity:"LOW",fix:"const ALLOWED_HOSTS = ['api.trusted.com']; if (!ALLOWED_HOSTS.includes(new URL(url).host)) throw new Error('Invalid URL')",documentationLink:"https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:true,description:"Allow user-controlled URLs in test files"}},additionalProperties:false}]},defaultOptions:[{allowInTests:true}],create(context,[options={}]){const{allowInTests=true}=options;const filename=context.filename;const isTestFile=/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(allowInTests&&isTestFile){return{}}const eventParameters=new Set;const taintedVariables=new Map;function isEventParameter(name){return eventParameters.has(name)}function getEventInputSource(node){if(node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&isEventParameter(node.object.name)&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&USER_INPUT_PROPERTIES.has(node.property.name)){return`event.${node.property.name}`}if(node.object.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.object.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&isEventParameter(node.object.object.name)&&node.object.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&USER_INPUT_PROPERTIES.has(node.object.property.name)){return`event.${node.object.property.name}.${node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?node.property.name:"[...]"}`}return null}function getTaintSource(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){const source=getEventInputSource(node);if(source)return source}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const source=taintedVariables.get(node.name);if(source)return source}if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){for(const expr of node.expressions){const source=getTaintSource(expr);if(source)return source}}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression){const leftSource=getTaintSource(node.left);if(leftSource)return leftSource;const rightSource=getTaintSource(node.right);if(rightSource)return rightSource}return null}function isHttpClientCall(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&HTTP_CLIENT_METHODS.has(node.callee.name)){return true}if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&HTTP_CLIENT_OBJECTS.has(node.callee.object.name)){return true}if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&["get","post","put","delete","patch","request"].includes(node.callee.property.name)){return true}return false}function checkUrlArgument(node){if(node.arguments.length===0)return null;const urlArg=node.arguments[0];const source=getTaintSource(urlArg);if(source){return{tainted:true,source}}if(urlArg.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression){for(const prop of urlArg.properties){if(prop.type===eslint_devkit_1.AST_NODE_TYPES.Property&&prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&["url","uri","href","baseURL"].includes(prop.key.name)){const propSource=getTaintSource(prop.value);if(propSource){return{tainted:true,source:propSource}}}}}return null}return{"ArrowFunctionExpression, FunctionExpression, FunctionDeclaration"(node){for(const param of node.params){if(param.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){if(EVENT_PARAM_NAMES.has(param.name)){eventParameters.add(param.name)}}}},VariableDeclarator(node){if(node.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.init){const source=getTaintSource(node.init);if(source){taintedVariables.set(node.id.name,source)}}},AssignmentExpression(node){if(node.left.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const source=getTaintSource(node.right);if(source){taintedVariables.set(node.left.name,source)}}},CallExpression(node){if(!isHttpClientCall(node))return;const result=checkUrlArgument(node);if(result?.tainted){context.report({node,messageId:"ssrfVulnerability",data:{source:result.source},suggest:[{messageId:"useAllowlist",fix:()=>null}]})}}}}});