eslint-plugin-lambda-security 1.0.4 → 1.1.1

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