eslint-plugin-lambda-security 1.0.2 → 1.0.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.
@@ -0,0 +1,263 @@
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
+ // HTTP client function/method names
6
+ const HTTP_CLIENT_METHODS = [
7
+ // Built-in
8
+ 'fetch',
9
+ // Node.js http module
10
+ 'request',
11
+ 'get',
12
+ 'post',
13
+ 'put',
14
+ 'delete',
15
+ 'patch',
16
+ // Axios
17
+ 'axios',
18
+ // got
19
+ 'got',
20
+ // node-fetch
21
+ 'nodeFetch',
22
+ // superagent
23
+ 'superagent',
24
+ ];
25
+ // HTTP client objects with methods
26
+ const HTTP_CLIENT_OBJECTS = ['http', 'https', 'axios', 'got', 'request'];
27
+ // Event properties containing user input
28
+ const USER_INPUT_PROPERTIES = [
29
+ 'body',
30
+ 'queryStringParameters',
31
+ 'pathParameters',
32
+ 'multiValueQueryStringParameters',
33
+ 'headers',
34
+ 'rawPath',
35
+ 'rawQueryString',
36
+ ];
37
+ // Lambda event parameter names
38
+ const EVENT_PARAM_NAMES = ['event', 'evt', 'e', 'request', 'req'];
39
+ exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
40
+ name: 'no-user-controlled-requests',
41
+ meta: {
42
+ type: 'problem',
43
+ docs: {
44
+ description: 'Detects HTTP requests with user-controlled URLs (SSRF vulnerability)',
45
+ },
46
+ hasSuggestions: true,
47
+ messages: {
48
+ ssrfVulnerability: (0, eslint_devkit_1.formatLLMMessage)({
49
+ icon: eslint_devkit_1.MessageIcons.SECURITY,
50
+ issueName: 'Server-Side Request Forgery (SSRF)',
51
+ cwe: 'CWE-918',
52
+ owasp: 'SAS-8',
53
+ cvss: 9.1,
54
+ description: 'HTTP request URL contains user-controlled input from {{source}}. Attackers can access internal services or exfiltrate data.',
55
+ severity: 'CRITICAL',
56
+ fix: 'Validate URL against allowlist before making request. Never use user input directly in URLs.',
57
+ documentationLink: 'https://owasp.org/www-community/attacks/Server_Side_Request_Forgery',
58
+ }),
59
+ useAllowlist: (0, eslint_devkit_1.formatLLMMessage)({
60
+ icon: eslint_devkit_1.MessageIcons.INFO,
61
+ issueName: 'Use URL Allowlist',
62
+ description: 'Implement an allowlist for permitted URL domains/hosts',
63
+ severity: 'LOW',
64
+ fix: "const ALLOWED_HOSTS = ['api.trusted.com']; if (!ALLOWED_HOSTS.includes(new URL(url).host)) throw new Error('Invalid URL')",
65
+ documentationLink: 'https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html',
66
+ }),
67
+ },
68
+ schema: [
69
+ {
70
+ type: 'object',
71
+ properties: {
72
+ allowInTests: {
73
+ type: 'boolean',
74
+ default: true,
75
+ description: 'Allow user-controlled URLs in test files',
76
+ },
77
+ },
78
+ additionalProperties: false,
79
+ },
80
+ ],
81
+ },
82
+ defaultOptions: [
83
+ {
84
+ allowInTests: true,
85
+ },
86
+ ],
87
+ create(context, [options = {}]) {
88
+ const { allowInTests = true } = options;
89
+ const filename = context.filename || context.getFilename();
90
+ const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
91
+ if (allowInTests && isTestFile) {
92
+ return {};
93
+ }
94
+ // Track event parameters
95
+ const eventParameters = new Set();
96
+ // Track variables that contain user input
97
+ const taintedVariables = new Map(); // variable name -> source
98
+ /**
99
+ * Check if an identifier is an event parameter
100
+ */
101
+ function isEventParameter(name) {
102
+ return eventParameters.has(name);
103
+ }
104
+ /**
105
+ * Check if a member expression accesses user input from event
106
+ */
107
+ function getEventInputSource(node) {
108
+ // event.body, event.queryStringParameters, etc.
109
+ if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
110
+ isEventParameter(node.object.name) &&
111
+ node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
112
+ USER_INPUT_PROPERTIES.includes(node.property.name)) {
113
+ return `event.${node.property.name}`;
114
+ }
115
+ // event.queryStringParameters.url, event.body.targetUrl, etc.
116
+ if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
117
+ node.object.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
118
+ isEventParameter(node.object.object.name) &&
119
+ node.object.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
120
+ USER_INPUT_PROPERTIES.includes(node.object.property.name)) {
121
+ return `event.${node.object.property.name}.${node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
122
+ ? node.property.name
123
+ : '[...]'}`;
124
+ }
125
+ return null;
126
+ }
127
+ /**
128
+ * Get taint source from an expression
129
+ */
130
+ function getTaintSource(node) {
131
+ // Direct event property access
132
+ if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
133
+ const source = getEventInputSource(node);
134
+ if (source)
135
+ return source;
136
+ }
137
+ // Variable that was previously tainted
138
+ if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
139
+ const source = taintedVariables.get(node.name);
140
+ if (source)
141
+ return source;
142
+ }
143
+ // Template literal with tainted content
144
+ if (node.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
145
+ for (const expr of node.expressions) {
146
+ const source = getTaintSource(expr);
147
+ if (source)
148
+ return source;
149
+ }
150
+ }
151
+ // Binary expression (string concatenation)
152
+ if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
153
+ const leftSource = getTaintSource(node.left);
154
+ if (leftSource)
155
+ return leftSource;
156
+ const rightSource = getTaintSource(node.right);
157
+ if (rightSource)
158
+ return rightSource;
159
+ }
160
+ return null;
161
+ }
162
+ /**
163
+ * Check if this is an HTTP client call
164
+ */
165
+ function isHttpClientCall(node) {
166
+ // fetch(url), axios(url), got(url)
167
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
168
+ HTTP_CLIENT_METHODS.includes(node.callee.name)) {
169
+ return true;
170
+ }
171
+ // axios.get(url), http.request(url), etc.
172
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
173
+ node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
174
+ HTTP_CLIENT_OBJECTS.includes(node.callee.object.name)) {
175
+ return true;
176
+ }
177
+ // axios.create().get(url) - chained methods
178
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
179
+ node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
180
+ ['get', 'post', 'put', 'delete', 'patch', 'request'].includes(node.callee.property.name)) {
181
+ return true;
182
+ }
183
+ return false;
184
+ }
185
+ /**
186
+ * Check if URL argument is user-controlled
187
+ */
188
+ function checkUrlArgument(node) {
189
+ if (node.arguments.length === 0)
190
+ return null;
191
+ const urlArg = node.arguments[0];
192
+ const source = getTaintSource(urlArg);
193
+ if (source) {
194
+ return { tainted: true, source };
195
+ }
196
+ // Check object config { url: taintedVar }
197
+ if (urlArg.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
198
+ for (const prop of urlArg.properties) {
199
+ if (prop.type === eslint_devkit_1.AST_NODE_TYPES.Property &&
200
+ prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
201
+ ['url', 'uri', 'href', 'baseURL'].includes(prop.key.name)) {
202
+ const propSource = getTaintSource(prop.value);
203
+ if (propSource) {
204
+ return { tainted: true, source: propSource };
205
+ }
206
+ }
207
+ }
208
+ }
209
+ return null;
210
+ }
211
+ return {
212
+ // Track event parameters in handlers
213
+ 'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
214
+ for (const param of node.params) {
215
+ if (param.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
216
+ if (EVENT_PARAM_NAMES.includes(param.name)) {
217
+ eventParameters.add(param.name);
218
+ }
219
+ }
220
+ }
221
+ },
222
+ // Track variable assignments that contain user input
223
+ VariableDeclarator(node) {
224
+ if (node.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.init) {
225
+ const source = getTaintSource(node.init);
226
+ if (source) {
227
+ taintedVariables.set(node.id.name, source);
228
+ }
229
+ }
230
+ },
231
+ // Track reassignments
232
+ AssignmentExpression(node) {
233
+ if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
234
+ const source = getTaintSource(node.right);
235
+ if (source) {
236
+ taintedVariables.set(node.left.name, source);
237
+ }
238
+ }
239
+ },
240
+ // Check HTTP client calls for tainted URLs
241
+ CallExpression(node) {
242
+ if (!isHttpClientCall(node))
243
+ return;
244
+ const result = checkUrlArgument(node);
245
+ if (result?.tainted) {
246
+ context.report({
247
+ node,
248
+ messageId: 'ssrfVulnerability',
249
+ data: {
250
+ source: result.source,
251
+ },
252
+ suggest: [
253
+ {
254
+ messageId: 'useAllowlist',
255
+ fix: () => null, // Complex fix requiring allowlist setup
256
+ },
257
+ ],
258
+ });
259
+ }
260
+ },
261
+ };
262
+ },
263
+ });
@@ -0,0 +1,5 @@
1
+ export interface Options {
2
+ /** Allow in test files. Default: true */
3
+ allowInTests?: boolean;
4
+ }
5
+ export declare const requireTimeoutHandling: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireTimeoutHandling = void 0;
4
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
+ // Lambda context parameter names
6
+ const CONTEXT_PARAM_NAMES = ['context', 'ctx', 'lambdaContext'];
7
+ // Event parameter names (to identify Lambda handlers)
8
+ const EVENT_PARAM_NAMES = ['event', 'evt', 'e', 'request', 'req'];
9
+ // External call patterns that could timeout
10
+ const EXTERNAL_CALL_PATTERNS = [
11
+ // HTTP clients
12
+ 'fetch',
13
+ 'axios',
14
+ 'got',
15
+ 'request',
16
+ // AWS SDK
17
+ 'send',
18
+ 'invoke',
19
+ // Database
20
+ 'query',
21
+ 'execute',
22
+ 'connect',
23
+ // Other
24
+ 'promise',
25
+ 'wait',
26
+ ];
27
+ exports.requireTimeoutHandling = (0, eslint_devkit_1.createRule)({
28
+ name: 'require-timeout-handling',
29
+ meta: {
30
+ type: 'suggestion',
31
+ docs: {
32
+ description: 'Warns when Lambda handlers with external calls lack timeout handling',
33
+ },
34
+ hasSuggestions: true,
35
+ messages: {
36
+ missingTimeoutHandling: (0, eslint_devkit_1.formatLLMMessage)({
37
+ icon: eslint_devkit_1.MessageIcons.WARNING,
38
+ issueName: 'Missing Timeout Handling',
39
+ cwe: 'CWE-400',
40
+ owasp: 'SAS-7',
41
+ cvss: 6.0,
42
+ description: 'Lambda handler makes external calls without timeout handling. Function may timeout without cleanup.',
43
+ severity: 'MEDIUM',
44
+ fix: 'Use context.getRemainingTimeInMillis() to check available time before external calls, or use AbortController',
45
+ documentationLink: 'https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html',
46
+ }),
47
+ addTimeoutCheck: (0, eslint_devkit_1.formatLLMMessage)({
48
+ icon: eslint_devkit_1.MessageIcons.INFO,
49
+ issueName: 'Add Timeout Check',
50
+ description: 'Check remaining time before making external calls to prevent timeouts',
51
+ severity: 'LOW',
52
+ fix: "const remainingTime = context.getRemainingTimeInMillis(); if (remainingTime < 5000) { return { statusCode: 503 } }",
53
+ documentationLink: 'https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html',
54
+ }),
55
+ },
56
+ schema: [
57
+ {
58
+ type: 'object',
59
+ properties: {
60
+ allowInTests: {
61
+ type: 'boolean',
62
+ default: true,
63
+ },
64
+ },
65
+ additionalProperties: false,
66
+ },
67
+ ],
68
+ },
69
+ defaultOptions: [{ allowInTests: true }],
70
+ create(context, [options = {}]) {
71
+ const { allowInTests = true } = options;
72
+ const filename = context.filename || context.getFilename();
73
+ const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
74
+ if (allowInTests && isTestFile) {
75
+ return {};
76
+ }
77
+ // Track handler functions
78
+ let currentHandlerNode = null;
79
+ let hasTimeoutCheck = false;
80
+ let hasExternalCalls = false;
81
+ let hasContextParam = false;
82
+ function enterFunction(node) {
83
+ // Check if this looks like a Lambda handler (event, context)
84
+ const params = node.params;
85
+ const hasEvent = params.some((p) => p.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
86
+ EVENT_PARAM_NAMES.includes(p.name));
87
+ const hasContext = params.some((p) => p.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
88
+ CONTEXT_PARAM_NAMES.includes(p.name));
89
+ if (hasEvent) {
90
+ currentHandlerNode = node;
91
+ hasTimeoutCheck = false;
92
+ hasExternalCalls = false;
93
+ hasContextParam = hasContext;
94
+ }
95
+ }
96
+ function exitFunction(node) {
97
+ if (currentHandlerNode !== node)
98
+ return;
99
+ // Report if handler has external calls but no timeout handling and has context param
100
+ if (hasExternalCalls && !hasTimeoutCheck && hasContextParam) {
101
+ context.report({
102
+ node,
103
+ messageId: 'missingTimeoutHandling',
104
+ suggest: [
105
+ {
106
+ messageId: 'addTimeoutCheck',
107
+ fix: () => null,
108
+ },
109
+ ],
110
+ });
111
+ }
112
+ currentHandlerNode = null;
113
+ }
114
+ return {
115
+ 'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
116
+ enterFunction(node);
117
+ },
118
+ 'ArrowFunctionExpression:exit, FunctionExpression:exit, FunctionDeclaration:exit'(node) {
119
+ exitFunction(node);
120
+ },
121
+ // Detect getRemainingTimeInMillis() call
122
+ MemberExpression(node) {
123
+ if (!currentHandlerNode)
124
+ return;
125
+ if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
126
+ node.property.name === 'getRemainingTimeInMillis') {
127
+ hasTimeoutCheck = true;
128
+ }
129
+ },
130
+ // Detect AbortController usage
131
+ NewExpression(node) {
132
+ if (!currentHandlerNode)
133
+ return;
134
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
135
+ node.callee.name === 'AbortController') {
136
+ hasTimeoutCheck = true;
137
+ }
138
+ },
139
+ // Detect external calls
140
+ CallExpression(node) {
141
+ if (!currentHandlerNode)
142
+ return;
143
+ // Check for external call patterns
144
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
145
+ if (EXTERNAL_CALL_PATTERNS.includes(node.callee.name)) {
146
+ hasExternalCalls = true;
147
+ }
148
+ }
149
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
150
+ const property = node.callee.property;
151
+ if (property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
152
+ EXTERNAL_CALL_PATTERNS.includes(property.name)) {
153
+ hasExternalCalls = true;
154
+ }
155
+ }
156
+ // Check for Promise.race (timeout pattern)
157
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
158
+ node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
159
+ node.callee.object.name === 'Promise' &&
160
+ node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
161
+ node.callee.property.name === 'race') {
162
+ hasTimeoutCheck = true;
163
+ }
164
+ },
165
+ };
166
+ },
167
+ });