eslint-plugin-lambda-security 1.0.4 → 1.1.0

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 +54 -35
  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,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 {};
@@ -0,0 +1,170 @@
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.noUnboundedBatchProcessing = void 0;
9
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
+ // Event sources with Records array
11
+ const BATCH_SOURCE_PROPERTIES = [
12
+ 'Records', // SQS, SNS, DynamoDB Streams, Kinesis, S3
13
+ 'records', // Some custom structures
14
+ 'items', // Custom batches
15
+ 'messages', // Custom message batches
16
+ ];
17
+ // Event parameter names
18
+ const EVENT_PARAM_NAMES = ['event', 'evt', 'e', 'request', 'req'];
19
+ exports.noUnboundedBatchProcessing = (0, eslint_devkit_1.createRule)({
20
+ name: 'no-unbounded-batch-processing',
21
+ meta: {
22
+ type: 'problem',
23
+ docs: {
24
+ description: 'Detects processing batch records without size validation',
25
+ },
26
+ hasSuggestions: true,
27
+ messages: {
28
+ unboundedBatch: (0, eslint_devkit_1.formatLLMMessage)({
29
+ icon: eslint_devkit_1.MessageIcons.WARNING,
30
+ issueName: 'Unbounded Batch Processing',
31
+ cwe: 'CWE-770',
32
+ cvss: 5.5,
33
+ description: 'Processing {{source}} without size check. Large batches may cause timeout or memory exhaustion.',
34
+ severity: 'MEDIUM',
35
+ fix: 'Validate batch size before processing: if (event.Records.length > MAX_BATCH) throw new Error("Batch too large")',
36
+ documentationLink: 'https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html',
37
+ }),
38
+ addBatchLimit: (0, eslint_devkit_1.formatLLMMessage)({
39
+ icon: eslint_devkit_1.MessageIcons.INFO,
40
+ issueName: 'Add Batch Size Limit',
41
+ description: 'Add a maximum batch size check before processing',
42
+ severity: 'LOW',
43
+ fix: 'const MAX_BATCH_SIZE = 10; const records = event.Records.slice(0, MAX_BATCH_SIZE);',
44
+ documentationLink: 'https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html',
45
+ }),
46
+ },
47
+ schema: [
48
+ {
49
+ type: 'object',
50
+ properties: {
51
+ allowInTests: {
52
+ type: 'boolean',
53
+ default: true,
54
+ },
55
+ maxBatchSize: {
56
+ type: 'number',
57
+ default: 100,
58
+ },
59
+ },
60
+ additionalProperties: false,
61
+ },
62
+ ],
63
+ },
64
+ defaultOptions: [{ allowInTests: true, maxBatchSize: 100 }],
65
+ create(context, [options = {}]) {
66
+ const { allowInTests = true } = options;
67
+ const filename = context.filename || context.getFilename();
68
+ const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
69
+ if (allowInTests && isTestFile) {
70
+ return {};
71
+ }
72
+ // Track handler functions
73
+ let currentHandlerNode = null;
74
+ let hasBatchSizeCheck = false;
75
+ const batchAccessNodes = [];
76
+ const eventParamName = [];
77
+ function enterFunction(node) {
78
+ const hasEvent = node.params.some((p) => {
79
+ if (p.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
80
+ EVENT_PARAM_NAMES.includes(p.name)) {
81
+ eventParamName.push(p.name);
82
+ return true;
83
+ }
84
+ return false;
85
+ });
86
+ if (hasEvent) {
87
+ currentHandlerNode = node;
88
+ hasBatchSizeCheck = false;
89
+ batchAccessNodes.length = 0;
90
+ }
91
+ }
92
+ function exitFunction(node) {
93
+ if (currentHandlerNode !== node)
94
+ return;
95
+ // Report if batch is processed without size check
96
+ if (!hasBatchSizeCheck) {
97
+ for (const { node: batchNode, source } of batchAccessNodes) {
98
+ context.report({
99
+ node: batchNode,
100
+ messageId: 'unboundedBatch',
101
+ data: { source },
102
+ suggest: [
103
+ {
104
+ messageId: 'addBatchLimit',
105
+ fix: () => null,
106
+ },
107
+ ],
108
+ });
109
+ }
110
+ }
111
+ currentHandlerNode = null;
112
+ eventParamName.length = 0;
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 event.Records access
122
+ MemberExpression(node) {
123
+ if (!currentHandlerNode)
124
+ return;
125
+ // event.Records
126
+ if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
127
+ eventParamName.includes(node.object.name) &&
128
+ node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
129
+ BATCH_SOURCE_PROPERTIES.includes(node.property.name)) {
130
+ batchAccessNodes.push({
131
+ node,
132
+ source: `${node.object.name}.${node.property.name}`,
133
+ });
134
+ }
135
+ // Check for .length access (indicates size check)
136
+ if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
137
+ node.property.name === 'length') {
138
+ // Check if this is Records.length
139
+ if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
140
+ node.object.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
141
+ BATCH_SOURCE_PROPERTIES.includes(node.object.property.name)) {
142
+ hasBatchSizeCheck = true;
143
+ }
144
+ }
145
+ },
146
+ // Detect slice/splice (limiting)
147
+ CallExpression(node) {
148
+ if (!currentHandlerNode)
149
+ return;
150
+ if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
151
+ node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
152
+ ['slice', 'splice', 'take', 'chunk'].includes(node.callee.property.name)) {
153
+ hasBatchSizeCheck = true;
154
+ }
155
+ },
156
+ // Detect size comparisons
157
+ BinaryExpression(node) {
158
+ if (!currentHandlerNode)
159
+ return;
160
+ if (['<', '<=', '>', '>='].includes(node.operator)) {
161
+ const sourceCode = context.sourceCode || context.getSourceCode();
162
+ const text = sourceCode.getText(node);
163
+ if (/\.length|Records|records/.test(text)) {
164
+ hasBatchSizeCheck = true;
165
+ }
166
+ }
167
+ },
168
+ };
169
+ },
170
+ });
@@ -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-unvalidated-event-body
8
+ * Detects Lambda handlers using event body/params without validation
9
+ * CWE-20: Improper Input Validation
10
+ *
11
+ * @see https://cwe.mitre.org/data/definitions/20.html
12
+ * @see https://owasp.org/www-project-serverless-top-10/
13
+ */
14
+ import type { TSESLint } from '@interlace/eslint-devkit';
15
+ type MessageIds = 'unvalidatedInput' | 'useValidation';
16
+ export interface Options {
17
+ /** Allow in test files. Default: true */
18
+ allowInTests?: boolean;
19
+ /** Additional event properties to check. Default: [] */
20
+ additionalProperties?: string[];
21
+ }
22
+ type RuleOptions = [Options?];
23
+ export declare const noUnvalidatedEventBody: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
24
+ export {};