eslint-plugin-lambda-security 1.2.2 → 1.2.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.
@@ -19,7 +19,7 @@ const DANGEROUS_EVENT_PROPERTIES = [
19
19
  'rawQueryString',
20
20
  ];
21
21
  // Property accesses that indicate validation
22
- const VALIDATION_INDICATORS = [
22
+ const VALIDATION_INDICATORS = new Set([
23
23
  // Middy validators
24
24
  'validator',
25
25
  'httpJsonBodyParser',
@@ -32,13 +32,16 @@ const VALIDATION_INDICATORS = [
32
32
  'safeParseAsync',
33
33
  'assert', // superstruct.assert()
34
34
  'is', // superstruct.is()
35
- ];
35
+ ]);
36
36
  exports.noUnvalidatedEventBody = (0, eslint_devkit_1.createRule)({
37
37
  name: 'no-unvalidated-event-body',
38
38
  meta: {
39
39
  type: 'problem',
40
40
  docs: {
41
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/no-unvalidated-event-body.md',
41
42
  description: 'Detects Lambda handlers using event body/params without validation',
43
+ cwe: 'CWE-20',
44
+ cvss: 8,
42
45
  },
43
46
  hasSuggestions: true,
44
47
  messages: {
@@ -89,15 +92,15 @@ exports.noUnvalidatedEventBody = (0, eslint_devkit_1.createRule)({
89
92
  ],
90
93
  create(context, [options = {}]) {
91
94
  const { allowInTests = true, additionalProperties = [] } = options;
92
- const filename = context.filename || context.getFilename();
95
+ const filename = context.filename;
93
96
  const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
94
97
  if (allowInTests && isTestFile) {
95
98
  return {};
96
99
  }
97
- const dangerousProperties = [
100
+ const dangerousProperties = new Set([
98
101
  ...DANGEROUS_EVENT_PROPERTIES,
99
102
  ...additionalProperties,
100
- ];
103
+ ]);
101
104
  // Track which variables have been validated
102
105
  const validatedVariables = new Set();
103
106
  // Track which event parameters we're monitoring
@@ -113,13 +116,13 @@ exports.noUnvalidatedEventBody = (0, eslint_devkit_1.createRule)({
113
116
  if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
114
117
  const property = node.callee.property;
115
118
  if (property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
116
- VALIDATION_INDICATORS.includes(property.name)) {
119
+ VALIDATION_INDICATORS.has(property.name)) {
117
120
  return true;
118
121
  }
119
122
  }
120
123
  // Check for direct function calls like validate(x)
121
124
  if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
122
- if (VALIDATION_INDICATORS.includes(node.callee.name)) {
125
+ if (VALIDATION_INDICATORS.has(node.callee.name)) {
123
126
  return true;
124
127
  }
125
128
  }
@@ -133,7 +136,7 @@ exports.noUnvalidatedEventBody = (0, eslint_devkit_1.createRule)({
133
136
  if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
134
137
  eventParameters.has(node.object.name) &&
135
138
  node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
136
- dangerousProperties.includes(node.property.name)) {
139
+ dangerousProperties.has(node.property.name)) {
137
140
  return {
138
141
  eventName: node.object.name,
139
142
  property: node.property.name,
@@ -158,16 +161,106 @@ exports.noUnvalidatedEventBody = (0, eslint_devkit_1.createRule)({
158
161
  }
159
162
  return false;
160
163
  }
164
+ // Determine whether THIS file looks like Lambda code at all. We're
165
+ // strict: a function parameter is only treated as a Lambda event if
166
+ // (a) it's named `event` AND its sibling is `context` (the AWS
167
+ // signature `(event, context)` or `(event, context, callback)`),
168
+ // or (b) the file imports `aws-lambda` types / `@aws-sdk/*` /
169
+ // `@middy/*`. This eliminates the audit-reported FP class where
170
+ // Express handlers (`function handler(req, res)` or
171
+ // `(request, response)`) were misclassified as Lambda. The previous
172
+ // allowlist of `['event', 'evt', 'e', 'request', 'req']` was too
173
+ // loose — see benchmarks/AUDIT_PATTERNS.md §2.4.
174
+ const sourceCode = context.sourceCode;
175
+ const fileImportsLambda = sourceCode.ast.body.some((n) => {
176
+ if (n.type !== eslint_devkit_1.AST_NODE_TYPES.ImportDeclaration)
177
+ return false;
178
+ const src = String(n.source.value);
179
+ return (src === 'aws-lambda' ||
180
+ src.startsWith('@aws-sdk/') ||
181
+ src.startsWith('@middy/') ||
182
+ src === '@aws-cdk/aws-lambda');
183
+ });
184
+ // Detect the `export const handler = ...`, `exports.handler = ...`,
185
+ // and `module.exports.handler = ...` Lambda export conventions. A
186
+ // function attached to one of these is a Lambda handler regardless
187
+ // of whether it has the (event, context) signature or just a single
188
+ // `event` parameter — that's the AWS Lambda convention.
189
+ function isExportedAsHandler(node) {
190
+ // function handler() {} / export function handler() {}
191
+ if (node.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration &&
192
+ node.id?.name === 'handler') {
193
+ return true;
194
+ }
195
+ const parent = node.parent;
196
+ if (!parent)
197
+ return false;
198
+ // const handler = (...) / export const handler = (...)
199
+ if (parent.type === eslint_devkit_1.AST_NODE_TYPES.VariableDeclarator &&
200
+ parent.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
201
+ parent.id.name === 'handler') {
202
+ return true;
203
+ }
204
+ // exports.handler = (...) / module.exports.handler = (...)
205
+ if (parent.type === eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression) {
206
+ const left = parent.left;
207
+ if (left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
208
+ left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
209
+ left.property.name === 'handler') {
210
+ return true;
211
+ }
212
+ }
213
+ return false;
214
+ }
215
+ function isLambdaSignature(params, node) {
216
+ if (params.length < 1)
217
+ return false;
218
+ const first = params[0];
219
+ if (first.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
220
+ return false;
221
+ const second = params[1];
222
+ const hasContextSibling = second?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
223
+ (second.name === 'context' || second.name === 'callback');
224
+ // Discriminator: 2-arg `(req, res)` / `(request, response)` is
225
+ // Express, not Lambda. Reject explicitly so we never misclassify.
226
+ const looksLikeExpress = second?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
227
+ ((first.name === 'req' && second.name === 'res') ||
228
+ (first.name === 'request' && second.name === 'response'));
229
+ if (looksLikeExpress)
230
+ return false;
231
+ const isEventName = first.name === 'event' || first.name === 'evt';
232
+ // Path 1: classic AWS signature (event, context, ...).
233
+ if (isEventName && hasContextSibling)
234
+ return true;
235
+ // Path 2: file imports Lambda-related modules — single-arg
236
+ // `event`/`evt` is enough.
237
+ if (isEventName && fileImportsLambda)
238
+ return true;
239
+ // Path 3: function is exported under the conventional `handler`
240
+ // name (AWS Lambda runtime invokes by that name). The Lambda
241
+ // export convention narrows the param-name allowlist to common
242
+ // AWS-event identifiers — Express's 2-arg form is already
243
+ // excluded by `looksLikeExpress` above.
244
+ if (isExportedAsHandler(node)) {
245
+ const allowedHandlerParam = ['event', 'evt', 'e', 'request', 'req'];
246
+ if (allowedHandlerParam.includes(first.name))
247
+ return true;
248
+ }
249
+ return false;
250
+ }
161
251
  return {
162
- // Track arrow function/function parameters named 'event'
252
+ // Track arrow function/function parameters that match a Lambda
253
+ // handler signature. Three valid paths (see isLambdaSignature):
254
+ // (a) (event, context, ...) — classic AWS shape
255
+ // (b) single-arg `event` AND file imports aws-lambda / @aws-sdk
256
+ // (c) function exported as `handler` (AWS Lambda export convention)
257
+ // Express handlers never satisfy any of these.
163
258
  'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
164
- for (const param of node.params) {
165
- if (param.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
166
- // Common Lambda handler parameter names
167
- if (['event', 'evt', 'e', 'request', 'req'].includes(param.name)) {
168
- eventParameters.add(param.name);
169
- }
170
- }
259
+ if (!isLambdaSignature(node.params, node))
260
+ return;
261
+ const first = node.params[0];
262
+ if (first.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
263
+ eventParameters.add(first.name);
171
264
  }
172
265
  },
173
266
  // Check for Middy with validation middleware
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.noUserControlledRequests = void 0;
9
9
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
10
  // HTTP client function/method names
11
- const HTTP_CLIENT_METHODS = [
11
+ const HTTP_CLIENT_METHODS = new Set([
12
12
  // Built-in
13
13
  'fetch',
14
14
  // Node.js http module
@@ -26,11 +26,11 @@ const HTTP_CLIENT_METHODS = [
26
26
  'nodeFetch',
27
27
  // superagent
28
28
  'superagent',
29
- ];
29
+ ]);
30
30
  // HTTP client objects with methods
31
- const HTTP_CLIENT_OBJECTS = ['http', 'https', 'axios', 'got', 'request'];
31
+ const HTTP_CLIENT_OBJECTS = new Set(['http', 'https', 'axios', 'got', 'request']);
32
32
  // Event properties containing user input
33
- const USER_INPUT_PROPERTIES = [
33
+ const USER_INPUT_PROPERTIES = new Set([
34
34
  'body',
35
35
  'queryStringParameters',
36
36
  'pathParameters',
@@ -38,15 +38,18 @@ const USER_INPUT_PROPERTIES = [
38
38
  'headers',
39
39
  'rawPath',
40
40
  'rawQueryString',
41
- ];
41
+ ]);
42
42
  // Lambda event parameter names
43
- const EVENT_PARAM_NAMES = ['event', 'evt', 'e', 'request', 'req'];
43
+ const EVENT_PARAM_NAMES = new Set(['event', 'evt', 'e', 'request', 'req']);
44
44
  exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
45
45
  name: 'no-user-controlled-requests',
46
46
  meta: {
47
47
  type: 'problem',
48
48
  docs: {
49
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/no-user-controlled-requests.md',
49
50
  description: 'Detects HTTP requests with user-controlled URLs (SSRF vulnerability)',
51
+ cwe: 'CWE-918',
52
+ cvss: 9.1,
50
53
  },
51
54
  hasSuggestions: true,
52
55
  messages: {
@@ -90,7 +93,7 @@ exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
90
93
  ],
91
94
  create(context, [options = {}]) {
92
95
  const { allowInTests = true } = options;
93
- const filename = context.filename || context.getFilename();
96
+ const filename = context.filename;
94
97
  const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
95
98
  if (allowInTests && isTestFile) {
96
99
  return {};
@@ -113,7 +116,7 @@ exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
113
116
  if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
114
117
  isEventParameter(node.object.name) &&
115
118
  node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
116
- USER_INPUT_PROPERTIES.includes(node.property.name)) {
119
+ USER_INPUT_PROPERTIES.has(node.property.name)) {
117
120
  return `event.${node.property.name}`;
118
121
  }
119
122
  // event.queryStringParameters.url, event.body.targetUrl, etc.
@@ -121,7 +124,7 @@ exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
121
124
  node.object.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
122
125
  isEventParameter(node.object.object.name) &&
123
126
  node.object.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
124
- USER_INPUT_PROPERTIES.includes(node.object.property.name)) {
127
+ USER_INPUT_PROPERTIES.has(node.object.property.name)) {
125
128
  return `event.${node.object.property.name}.${node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
126
129
  ? node.property.name
127
130
  : '[...]'}`;
@@ -169,13 +172,13 @@ exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
169
172
  function isHttpClientCall(node) {
170
173
  // fetch(url), axios(url), got(url)
171
174
  if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
172
- HTTP_CLIENT_METHODS.includes(node.callee.name)) {
175
+ HTTP_CLIENT_METHODS.has(node.callee.name)) {
173
176
  return true;
174
177
  }
175
178
  // axios.get(url), http.request(url), etc.
176
179
  if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
177
180
  node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
178
- HTTP_CLIENT_OBJECTS.includes(node.callee.object.name)) {
181
+ HTTP_CLIENT_OBJECTS.has(node.callee.object.name)) {
179
182
  return true;
180
183
  }
181
184
  // axios.create().get(url) - chained methods
@@ -217,7 +220,7 @@ exports.noUserControlledRequests = (0, eslint_devkit_1.createRule)({
217
220
  'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
218
221
  for (const param of node.params) {
219
222
  if (param.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
220
- if (EVENT_PARAM_NAMES.includes(param.name)) {
223
+ if (EVENT_PARAM_NAMES.has(param.name)) {
221
224
  eventParameters.add(param.name);
222
225
  }
223
226
  }
@@ -8,11 +8,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.requireTimeoutHandling = void 0;
9
9
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
10
  // Lambda context parameter names
11
- const CONTEXT_PARAM_NAMES = ['context', 'ctx', 'lambdaContext'];
11
+ const CONTEXT_PARAM_NAMES = new Set(['context', 'ctx', 'lambdaContext']);
12
12
  // Event parameter names (to identify Lambda handlers)
13
- const EVENT_PARAM_NAMES = ['event', 'evt', 'e', 'request', 'req'];
13
+ const EVENT_PARAM_NAMES = new Set(['event', 'evt', 'e', 'request', 'req']);
14
14
  // External call patterns that could timeout
15
- const EXTERNAL_CALL_PATTERNS = [
15
+ const EXTERNAL_CALL_PATTERNS = new Set([
16
16
  // HTTP clients
17
17
  'fetch',
18
18
  'axios',
@@ -28,13 +28,16 @@ const EXTERNAL_CALL_PATTERNS = [
28
28
  // Other
29
29
  'promise',
30
30
  'wait',
31
- ];
31
+ ]);
32
32
  exports.requireTimeoutHandling = (0, eslint_devkit_1.createRule)({
33
33
  name: 'require-timeout-handling',
34
34
  meta: {
35
35
  type: 'suggestion',
36
36
  docs: {
37
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-lambda-security/docs/rules/require-timeout-handling.md',
37
38
  description: 'Warns when Lambda handlers with external calls lack timeout handling',
39
+ cwe: 'CWE-400',
40
+ cvss: 6,
38
41
  },
39
42
  hasSuggestions: true,
40
43
  messages: {
@@ -73,7 +76,7 @@ exports.requireTimeoutHandling = (0, eslint_devkit_1.createRule)({
73
76
  defaultOptions: [{ allowInTests: true }],
74
77
  create(context, [options = {}]) {
75
78
  const { allowInTests = true } = options;
76
- const filename = context.filename || context.getFilename();
79
+ const filename = context.filename;
77
80
  const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
78
81
  if (allowInTests && isTestFile) {
79
82
  return {};
@@ -87,9 +90,9 @@ exports.requireTimeoutHandling = (0, eslint_devkit_1.createRule)({
87
90
  // Check if this looks like a Lambda handler (event, context)
88
91
  const params = node.params;
89
92
  const hasEvent = params.some((p) => p.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
90
- EVENT_PARAM_NAMES.includes(p.name));
93
+ EVENT_PARAM_NAMES.has(p.name));
91
94
  const hasContext = params.some((p) => p.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
92
- CONTEXT_PARAM_NAMES.includes(p.name));
95
+ CONTEXT_PARAM_NAMES.has(p.name));
93
96
  if (hasEvent) {
94
97
  currentHandlerNode = node;
95
98
  hasTimeoutCheck = false;
@@ -119,7 +122,13 @@ exports.requireTimeoutHandling = (0, eslint_devkit_1.createRule)({
119
122
  'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
120
123
  enterFunction(node);
121
124
  },
122
- 'ArrowFunctionExpression:exit, FunctionExpression:exit, FunctionDeclaration:exit'(node) {
125
+ 'ArrowFunctionExpression:exit'(node) {
126
+ exitFunction(node);
127
+ },
128
+ 'FunctionExpression:exit'(node) {
129
+ exitFunction(node);
130
+ },
131
+ 'FunctionDeclaration:exit'(node) {
123
132
  exitFunction(node);
124
133
  },
125
134
  // Detect getRemainingTimeInMillis() call
@@ -146,14 +155,14 @@ exports.requireTimeoutHandling = (0, eslint_devkit_1.createRule)({
146
155
  return;
147
156
  // Check for external call patterns
148
157
  if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
149
- if (EXTERNAL_CALL_PATTERNS.includes(node.callee.name)) {
158
+ if (EXTERNAL_CALL_PATTERNS.has(node.callee.name)) {
150
159
  hasExternalCalls = true;
151
160
  }
152
161
  }
153
162
  if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
154
163
  const property = node.callee.property;
155
164
  if (property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
156
- EXTERNAL_CALL_PATTERNS.includes(property.name)) {
165
+ EXTERNAL_CALL_PATTERNS.has(property.name)) {
157
166
  hasExternalCalls = true;
158
167
  }
159
168
  }