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,220 @@
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.noMissingAuthorizationCheck = void 0;
9
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
+ // Property accesses that indicate authorization checks
11
+ const AUTHORIZATION_CHECK_PATTERNS = [
12
+ // API Gateway authorizer
13
+ 'requestContext.authorizer',
14
+ 'requestContext.identity',
15
+ // JWT/Token validation
16
+ 'Authorization',
17
+ 'authorization',
18
+ // Common auth checks
19
+ 'claims',
20
+ 'principalId',
21
+ 'isAdmin',
22
+ 'isAuthenticated',
23
+ 'userId',
24
+ 'sub',
25
+ 'scope',
26
+ 'permissions',
27
+ ];
28
+ // Sensitive operations that require authorization
29
+ const SENSITIVE_OPERATIONS = [
30
+ // Database operations
31
+ 'query',
32
+ 'put',
33
+ 'delete',
34
+ 'update',
35
+ 'insert',
36
+ 'remove',
37
+ 'create',
38
+ // AWS SDK operations
39
+ 'send',
40
+ 'invoke',
41
+ 'putObject',
42
+ 'deleteObject',
43
+ 'getObject',
44
+ 'putItem',
45
+ 'deleteItem',
46
+ 'updateItem',
47
+ 'getItem',
48
+ ];
49
+ // Event parameter names
50
+ const EVENT_PARAM_NAMES = ['event', 'evt', 'e', 'request', 'req'];
51
+ exports.noMissingAuthorizationCheck = (0, eslint_devkit_1.createRule)({
52
+ name: 'no-missing-authorization-check',
53
+ meta: {
54
+ type: 'problem',
55
+ docs: {
56
+ description: 'Detects Lambda handlers without authorization checks',
57
+ },
58
+ hasSuggestions: true,
59
+ messages: {
60
+ missingAuthCheck: (0, eslint_devkit_1.formatLLMMessage)({
61
+ icon: eslint_devkit_1.MessageIcons.SECURITY,
62
+ issueName: 'Missing Authorization Check',
63
+ cwe: 'CWE-862',
64
+ cvss: 7.5,
65
+ description: 'Lambda handler performs {{operation}} operation without authorization check. Users may access resources without proper permissions.',
66
+ severity: 'HIGH',
67
+ fix: "Add authorization check: const claims = event.requestContext.authorizer?.claims; if (!claims?.sub) { return { statusCode: 401 } }",
68
+ documentationLink: 'https://cwe.mitre.org/data/definitions/862.html',
69
+ }),
70
+ addAuthCheck: (0, eslint_devkit_1.formatLLMMessage)({
71
+ icon: eslint_devkit_1.MessageIcons.INFO,
72
+ issueName: 'Add Authorization Check',
73
+ description: 'Check event.requestContext.authorizer for API Gateway or implement custom JWT validation',
74
+ severity: 'LOW',
75
+ fix: "const userId = event.requestContext.authorizer?.claims?.sub; if (!userId) throw new Error('Unauthorized');",
76
+ documentationLink: 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html',
77
+ }),
78
+ },
79
+ schema: [
80
+ {
81
+ type: 'object',
82
+ properties: {
83
+ allowInTests: {
84
+ type: 'boolean',
85
+ default: true,
86
+ description: 'Allow missing auth in test files',
87
+ },
88
+ handlerPatterns: {
89
+ type: 'array',
90
+ items: { type: 'string' },
91
+ description: 'Handler file patterns to check',
92
+ },
93
+ },
94
+ additionalProperties: false,
95
+ },
96
+ ],
97
+ },
98
+ defaultOptions: [
99
+ {
100
+ allowInTests: true,
101
+ handlerPatterns: [],
102
+ },
103
+ ],
104
+ create(context, [options = {}]) {
105
+ const { allowInTests = true } = options;
106
+ const filename = context.filename || context.getFilename();
107
+ const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
108
+ if (allowInTests && isTestFile) {
109
+ return {};
110
+ }
111
+ // Track state within each handler function
112
+ let currentFunctionNode = null;
113
+ let hasAuthCheck = false;
114
+ let hasEventParameter = false;
115
+ const sensitiveOperations = [];
116
+ /**
117
+ * Reset state when entering a new function
118
+ */
119
+ function enterFunction(node) {
120
+ // Check if this function has an event parameter
121
+ const hasEvent = node.params.some((param) => param.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
122
+ EVENT_PARAM_NAMES.includes(param.name));
123
+ if (hasEvent) {
124
+ currentFunctionNode = node;
125
+ hasAuthCheck = false;
126
+ hasEventParameter = true;
127
+ sensitiveOperations.length = 0;
128
+ }
129
+ }
130
+ /**
131
+ * Check and report on function exit
132
+ */
133
+ function exitFunction(node) {
134
+ if (currentFunctionNode !== node)
135
+ return;
136
+ // Report if we have sensitive operations without auth check
137
+ if (hasEventParameter && !hasAuthCheck) {
138
+ for (const { node: opNode, operation } of sensitiveOperations) {
139
+ context.report({
140
+ node: opNode,
141
+ messageId: 'missingAuthCheck',
142
+ data: { operation },
143
+ suggest: [
144
+ {
145
+ messageId: 'addAuthCheck',
146
+ fix: () => null,
147
+ },
148
+ ],
149
+ });
150
+ }
151
+ }
152
+ currentFunctionNode = null;
153
+ hasEventParameter = false;
154
+ }
155
+ /**
156
+ * Check if a member expression chain indicates auth check
157
+ */
158
+ function isAuthorizationAccess(node) {
159
+ const sourceCode = context.sourceCode || context.getSourceCode();
160
+ const text = sourceCode.getText(node);
161
+ return AUTHORIZATION_CHECK_PATTERNS.some((pattern) => text.includes(pattern) ||
162
+ text.toLowerCase().includes(pattern.toLowerCase()));
163
+ }
164
+ /**
165
+ * Check if call is a sensitive operation
166
+ */
167
+ function getSensitiveOperationName(node) {
168
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
169
+ const property = node.callee.property;
170
+ if (property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
171
+ if (SENSITIVE_OPERATIONS.includes(property.name)) {
172
+ return property.name;
173
+ }
174
+ }
175
+ }
176
+ return null;
177
+ }
178
+ return {
179
+ 'ArrowFunctionExpression, FunctionExpression, FunctionDeclaration'(node) {
180
+ enterFunction(node);
181
+ },
182
+ 'ArrowFunctionExpression:exit, FunctionExpression:exit, FunctionDeclaration:exit'(node) {
183
+ exitFunction(node);
184
+ },
185
+ // Detect authorization checks
186
+ MemberExpression(node) {
187
+ if (!currentFunctionNode)
188
+ return;
189
+ if (isAuthorizationAccess(node)) {
190
+ hasAuthCheck = true;
191
+ }
192
+ },
193
+ // Track sensitive operations
194
+ CallExpression(node) {
195
+ if (!currentFunctionNode)
196
+ return;
197
+ const operation = getSensitiveOperationName(node);
198
+ if (operation) {
199
+ sensitiveOperations.push({ node, operation });
200
+ }
201
+ },
202
+ // Detect early returns (401/403) as auth checks
203
+ ReturnStatement(node) {
204
+ if (!currentFunctionNode)
205
+ return;
206
+ if (node.argument?.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
207
+ for (const prop of node.argument.properties) {
208
+ if (prop.type === eslint_devkit_1.AST_NODE_TYPES.Property &&
209
+ prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
210
+ prop.key.name === 'statusCode' &&
211
+ prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
212
+ (prop.value.value === 401 || prop.value.value === 403)) {
213
+ hasAuthCheck = true;
214
+ }
215
+ }
216
+ }
217
+ },
218
+ };
219
+ },
220
+ });
@@ -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: no-overly-permissive-iam-policy
8
+ * Detects IAM policies with overly broad permissions
9
+ * CWE-732: Incorrect Permission Assignment for Critical Resource
10
+ *
11
+ * @see https://cwe.mitre.org/data/definitions/732.html
12
+ * @see https://owasp.org/www-project-serverless-top-10/
13
+ */
14
+ import type { TSESLint } from '@interlace/eslint-devkit';
15
+ type MessageIds = 'permissivePolicy' | 'useLeastPrivilege';
16
+ export interface Options {
17
+ /** Allow in test files. Default: true */
18
+ allowInTests?: boolean;
19
+ }
20
+ type RuleOptions = [Options?];
21
+ export declare const noOverlyPermissiveIamPolicy: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
22
+ export {};
@@ -0,0 +1,139 @@
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.noOverlyPermissiveIamPolicy = void 0;
9
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
+ // IAM policy property names
11
+ const POLICY_PROPERTIES = ['Resource', 'Action', 'Principal'];
12
+ // Wildcards that indicate overly permissive
13
+ const DANGEROUS_WILDCARDS = ['*', 'arn:aws:*', 'arn:*:*:*:*:*'];
14
+ exports.noOverlyPermissiveIamPolicy = (0, eslint_devkit_1.createRule)({
15
+ name: 'no-overly-permissive-iam-policy',
16
+ meta: {
17
+ type: 'problem',
18
+ docs: {
19
+ description: 'Detects IAM policies with overly broad permissions (wildcards)',
20
+ },
21
+ hasSuggestions: true,
22
+ messages: {
23
+ permissivePolicy: (0, eslint_devkit_1.formatLLMMessage)({
24
+ icon: eslint_devkit_1.MessageIcons.SECURITY,
25
+ issueName: 'Overly Permissive IAM Policy',
26
+ cwe: 'CWE-732',
27
+ cvss: 6.5,
28
+ description: 'IAM policy has wildcard {{property}}: "{{value}}". Violates principle of least privilege.',
29
+ severity: 'HIGH',
30
+ fix: 'Restrict {{property}} to specific resources/actions. Example: "arn:aws:s3:::my-bucket/*" instead of "*"',
31
+ documentationLink: 'https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege',
32
+ }),
33
+ useLeastPrivilege: (0, eslint_devkit_1.formatLLMMessage)({
34
+ icon: eslint_devkit_1.MessageIcons.INFO,
35
+ issueName: 'Apply Least Privilege',
36
+ description: 'Restrict permissions to specific resources and actions',
37
+ severity: 'LOW',
38
+ fix: 'Replace "*" with specific ARNs and action names',
39
+ documentationLink: 'https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html',
40
+ }),
41
+ },
42
+ schema: [
43
+ {
44
+ type: 'object',
45
+ properties: {
46
+ allowInTests: {
47
+ type: 'boolean',
48
+ default: true,
49
+ },
50
+ },
51
+ additionalProperties: false,
52
+ },
53
+ ],
54
+ },
55
+ defaultOptions: [{ allowInTests: true }],
56
+ create(context, [options = {}]) {
57
+ const { allowInTests = true } = options;
58
+ const filename = context.filename || context.getFilename();
59
+ const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
60
+ if (allowInTests && isTestFile) {
61
+ return {};
62
+ }
63
+ /**
64
+ * Check if a value is a dangerous wildcard
65
+ */
66
+ function isDangerousWildcard(value) {
67
+ return (DANGEROUS_WILDCARDS.includes(value) ||
68
+ (value.endsWith('*') && value.includes(':*:')));
69
+ }
70
+ /**
71
+ * Check property values for wildcards
72
+ */
73
+ function checkPolicyProperty(node, propertyName) {
74
+ const value = node.value;
75
+ // Check literal string
76
+ if (value.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof value.value === 'string') {
77
+ if (isDangerousWildcard(value.value)) {
78
+ context.report({
79
+ node,
80
+ messageId: 'permissivePolicy',
81
+ data: {
82
+ property: propertyName,
83
+ value: value.value,
84
+ },
85
+ suggest: [
86
+ {
87
+ messageId: 'useLeastPrivilege',
88
+ fix: () => null,
89
+ },
90
+ ],
91
+ });
92
+ }
93
+ }
94
+ // Check array of values
95
+ if (value.type === eslint_devkit_1.AST_NODE_TYPES.ArrayExpression) {
96
+ for (const element of value.elements) {
97
+ if (element?.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
98
+ typeof element.value === 'string') {
99
+ if (isDangerousWildcard(element.value)) {
100
+ context.report({
101
+ node: element,
102
+ messageId: 'permissivePolicy',
103
+ data: {
104
+ property: propertyName,
105
+ value: element.value,
106
+ },
107
+ suggest: [
108
+ {
109
+ messageId: 'useLeastPrivilege',
110
+ fix: () => null,
111
+ },
112
+ ],
113
+ });
114
+ }
115
+ }
116
+ }
117
+ }
118
+ }
119
+ return {
120
+ Property(node) {
121
+ // Check for IAM policy properties
122
+ if (node.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
123
+ const keyName = node.key.name;
124
+ if (POLICY_PROPERTIES.includes(keyName)) {
125
+ checkPolicyProperty(node, keyName);
126
+ }
127
+ }
128
+ // Also check string key properties
129
+ if (node.key.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
130
+ typeof node.key.value === 'string') {
131
+ const keyName = node.key.value;
132
+ if (POLICY_PROPERTIES.includes(keyName)) {
133
+ checkPolicyProperty(node, keyName);
134
+ }
135
+ }
136
+ },
137
+ };
138
+ },
139
+ });
@@ -1,5 +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: no-permissive-cors-middy
8
+ * Detects @middy/http-cors middleware with permissive origin configuration
9
+ * CWE-942: Permissive Cross-domain Policy with Untrusted Domains
10
+ *
11
+ * @see https://cwe.mitre.org/data/definitions/942.html
12
+ * @see https://middy.js.org/docs/middlewares/http-cors
13
+ */
14
+ import type { TSESLint } from '@interlace/eslint-devkit';
15
+ type MessageIds = 'permissiveCors' | 'useSpecificOrigins';
1
16
  export interface Options {
2
17
  /** Allow in test files. Default: true */
3
18
  allowInTests?: boolean;
4
19
  }
5
- export declare const noPermissiveCorsMidly: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
20
+ type RuleOptions = [Options?];
21
+ export declare const noPermissiveCorsMidly: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
22
+ export {};
@@ -1,4 +1,9 @@
1
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
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
8
  exports.noPermissiveCorsMidly = void 0;
4
9
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -1,7 +1,24 @@
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: no-permissive-cors-response
8
+ * Detects `Access-Control-Allow-Origin: '*'` in Lambda response headers
9
+ * CWE-942: Permissive Cross-domain Policy with Untrusted Domains
10
+ *
11
+ * @see https://cwe.mitre.org/data/definitions/942.html
12
+ * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html
13
+ */
14
+ import type { TSESLint } from '@interlace/eslint-devkit';
15
+ type MessageIds = 'permissiveCors' | 'useSpecificOrigin';
1
16
  export interface Options {
2
17
  /** Allow in test files. Default: true */
3
18
  allowInTests?: boolean;
4
19
  /** Allowed origins (patterns). Default: [] */
5
20
  allowedOrigins?: string[];
6
21
  }
7
- export declare const noPermissiveCorsResponse: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
22
+ type RuleOptions = [Options?];
23
+ export declare const noPermissiveCorsResponse: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
24
+ export {};
@@ -1,4 +1,9 @@
1
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
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
8
  exports.noPermissiveCorsResponse = void 0;
4
9
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -1,7 +1,24 @@
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: no-secrets-in-env
8
+ * Detects secrets directly assigned to process.env or hardcoded in environment configurations
9
+ * CWE-798: Use of Hard-coded Credentials
10
+ *
11
+ * @see https://cwe.mitre.org/data/definitions/798.html
12
+ * @see https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html
13
+ */
14
+ import type { TSESLint } from '@interlace/eslint-devkit';
15
+ type MessageIds = 'secretsInEnv' | 'useSecretsManager';
1
16
  export interface Options {
2
17
  /** Allow in test files. Default: true */
3
18
  allowInTests?: boolean;
4
19
  /** Additional secret patterns to detect. Default: [] */
5
20
  additionalPatterns?: string[];
6
21
  }
7
- export declare const noSecretsInEnv: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
22
+ type RuleOptions = [Options?];
23
+ export declare const noSecretsInEnv: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
24
+ export {};
@@ -1,4 +1,9 @@
1
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
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
8
  exports.noSecretsInEnv = void 0;
4
9
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -0,0 +1,24 @@
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: no-unbounded-batch-processing
8
+ * Detects processing arrays/records without size limits
9
+ * CWE-770: Allocation of Resources Without Limits
10
+ *
11
+ * @see https://cwe.mitre.org/data/definitions/770.html
12
+ * @see https://owasp.org/www-project-serverless-top-10/
13
+ */
14
+ import type { TSESLint } from '@interlace/eslint-devkit';
15
+ type MessageIds = 'unboundedBatch' | 'addBatchLimit';
16
+ export interface Options {
17
+ /** Allow in test files. Default: true */
18
+ allowInTests?: boolean;
19
+ /** Maximum allowed batch size before warning. Default: 100 */
20
+ maxBatchSize?: number;
21
+ }
22
+ type RuleOptions = [Options?];
23
+ export declare const noUnboundedBatchProcessing: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
24
+ export {};