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.
- package/LICENSE +21 -0
- package/README.md +54 -35
- package/package.json +3 -12
- package/src/index.d.ts +58 -20
- package/src/index.js +70 -3
- package/src/rules/no-env-logging/index.d.ts +17 -1
- package/src/rules/no-env-logging/index.js +5 -0
- package/src/rules/no-error-swallowing/index.d.ts +24 -0
- package/src/rules/no-error-swallowing/index.js +197 -0
- package/src/rules/no-exposed-error-details/index.d.ts +22 -0
- package/src/rules/no-exposed-error-details/index.js +161 -0
- package/src/rules/no-hardcoded-credentials-sdk/index.d.ts +18 -1
- package/src/rules/no-hardcoded-credentials-sdk/index.js +5 -0
- package/src/rules/no-missing-authorization-check/index.d.ts +24 -0
- package/src/rules/no-missing-authorization-check/index.js +220 -0
- package/src/rules/no-overly-permissive-iam-policy/index.d.ts +22 -0
- package/src/rules/no-overly-permissive-iam-policy/index.js +139 -0
- package/src/rules/no-permissive-cors-middy/index.d.ts +18 -1
- package/src/rules/no-permissive-cors-middy/index.js +5 -0
- package/src/rules/no-permissive-cors-response/index.d.ts +18 -1
- package/src/rules/no-permissive-cors-response/index.js +5 -0
- package/src/rules/no-secrets-in-env/index.d.ts +18 -1
- package/src/rules/no-secrets-in-env/index.js +5 -0
- package/src/rules/no-unbounded-batch-processing/index.d.ts +24 -0
- package/src/rules/no-unbounded-batch-processing/index.js +170 -0
- package/src/rules/no-unvalidated-event-body/index.d.ts +24 -0
- package/src/rules/no-unvalidated-event-body/index.js +270 -0
- package/src/rules/no-user-controlled-requests/index.d.ts +22 -0
- package/src/rules/no-user-controlled-requests/index.js +267 -0
- package/src/rules/require-timeout-handling/index.d.ts +22 -0
- package/src/rules/require-timeout-handling/index.js +171 -0
|
@@ -0,0 +1,197 @@
|
|
|
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.noErrorSwallowing = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
exports.noErrorSwallowing = (0, eslint_devkit_1.createRule)({
|
|
11
|
+
name: 'no-error-swallowing',
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'problem',
|
|
14
|
+
docs: {
|
|
15
|
+
description: 'Detects empty catch blocks and missing error logging in Lambda handlers',
|
|
16
|
+
},
|
|
17
|
+
hasSuggestions: true,
|
|
18
|
+
messages: {
|
|
19
|
+
emptyCatchBlock: (0, eslint_devkit_1.formatLLMMessage)({
|
|
20
|
+
icon: eslint_devkit_1.MessageIcons.WARNING,
|
|
21
|
+
issueName: 'Error Swallowing',
|
|
22
|
+
cwe: 'CWE-390',
|
|
23
|
+
cvss: 5.0,
|
|
24
|
+
description: 'Catch block swallows error without logging. Security incidents may go undetected.',
|
|
25
|
+
severity: 'MEDIUM',
|
|
26
|
+
fix: 'Log the error with context: console.error("Operation failed", { error, awsRequestId: context.awsRequestId })',
|
|
27
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/390.html',
|
|
28
|
+
}),
|
|
29
|
+
addErrorLogging: (0, eslint_devkit_1.formatLLMMessage)({
|
|
30
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
31
|
+
issueName: 'Add Error Logging',
|
|
32
|
+
description: 'Log errors with context for security monitoring',
|
|
33
|
+
severity: 'LOW',
|
|
34
|
+
fix: 'console.error("Error:", error); // or use structured logger',
|
|
35
|
+
documentationLink: 'https://docs.aws.amazon.com/lambda/latest/dg/nodejs-logging.html',
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
schema: [
|
|
39
|
+
{
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
allowInTests: {
|
|
43
|
+
type: 'boolean',
|
|
44
|
+
default: true,
|
|
45
|
+
},
|
|
46
|
+
allowWithComment: {
|
|
47
|
+
type: 'boolean',
|
|
48
|
+
default: true,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
additionalProperties: false,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
defaultOptions: [{ allowInTests: true, allowWithComment: true }],
|
|
56
|
+
create(context, [options = {}]) {
|
|
57
|
+
const { allowInTests = true, allowWithComment = 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 catch block has any logging
|
|
65
|
+
*/
|
|
66
|
+
function hasLogging(block) {
|
|
67
|
+
let found = false;
|
|
68
|
+
function checkNode(node) {
|
|
69
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
70
|
+
const callee = node.callee;
|
|
71
|
+
// console.log, console.error, console.warn
|
|
72
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
73
|
+
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
74
|
+
callee.object.name === 'console') {
|
|
75
|
+
found = true;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
// logger.error, logger.warn, etc.
|
|
79
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
80
|
+
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
81
|
+
['logger', 'log', 'winston', 'pino', 'bunyan'].includes(callee.object.name)) {
|
|
82
|
+
found = true;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// Direct function call that includes 'log' or 'error'
|
|
86
|
+
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
87
|
+
/log|error|warn/i.test(callee.name)) {
|
|
88
|
+
found = true;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Recursively check child nodes
|
|
93
|
+
for (const key of Object.keys(node)) {
|
|
94
|
+
const child = node[key];
|
|
95
|
+
if (child && typeof child === 'object' && 'type' in child) {
|
|
96
|
+
checkNode(child);
|
|
97
|
+
}
|
|
98
|
+
else if (Array.isArray(child)) {
|
|
99
|
+
for (const item of child) {
|
|
100
|
+
// Use Object.prototype.hasOwnProperty to avoid 'in' operator narrowing issues
|
|
101
|
+
if (item !== null &&
|
|
102
|
+
item !== undefined &&
|
|
103
|
+
typeof item === 'object' &&
|
|
104
|
+
Object.prototype.hasOwnProperty.call(item, 'type')) {
|
|
105
|
+
checkNode(item);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
for (const stmt of block.body) {
|
|
112
|
+
checkNode(stmt);
|
|
113
|
+
}
|
|
114
|
+
return found;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Check if block has comments indicating intentional swallowing
|
|
118
|
+
*/
|
|
119
|
+
function hasIntentionalComment(node) {
|
|
120
|
+
if (!allowWithComment)
|
|
121
|
+
return false;
|
|
122
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
123
|
+
const comments = sourceCode.getCommentsInside(node.body);
|
|
124
|
+
return comments.some((comment) => /intentional|expected|ignore|suppress|handled|silent/i.test(comment.value));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Check if catch has throw/rethrow
|
|
128
|
+
*/
|
|
129
|
+
function hasThrow(block) {
|
|
130
|
+
return block.body.some((stmt) => stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Check if catch has return (might be valid in some contexts)
|
|
134
|
+
*/
|
|
135
|
+
function hasReturn(block) {
|
|
136
|
+
return block.body.some((stmt) => stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement);
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
CatchClause(node) {
|
|
140
|
+
const body = node.body;
|
|
141
|
+
// Empty catch block
|
|
142
|
+
if (body.body.length === 0) {
|
|
143
|
+
if (!hasIntentionalComment(node)) {
|
|
144
|
+
context.report({
|
|
145
|
+
node,
|
|
146
|
+
messageId: 'emptyCatchBlock',
|
|
147
|
+
suggest: [
|
|
148
|
+
{
|
|
149
|
+
messageId: 'addErrorLogging',
|
|
150
|
+
fix: (fixer) => {
|
|
151
|
+
const errorParam = node.param?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
|
|
152
|
+
? node.param.name
|
|
153
|
+
: 'error';
|
|
154
|
+
return fixer.replaceText(body, `{ console.error('Error:', ${errorParam}); }`);
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
// Has throw - OK, error is propagated
|
|
163
|
+
if (hasThrow(body)) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
// Check if there's logging
|
|
167
|
+
if (!hasLogging(body)) {
|
|
168
|
+
// If there's a return with error context, that's also OK
|
|
169
|
+
if (hasReturn(body)) {
|
|
170
|
+
// Check if return includes error info
|
|
171
|
+
const returnStmt = body.body.find((s) => s.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement);
|
|
172
|
+
if (returnStmt?.argument) {
|
|
173
|
+
// Check if return includes status 500 or error body
|
|
174
|
+
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
175
|
+
const returnText = sourceCode.getText(returnStmt.argument);
|
|
176
|
+
if (/500|error|fail/i.test(returnText)) {
|
|
177
|
+
return; // Acceptable error handling
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (!hasIntentionalComment(node)) {
|
|
182
|
+
context.report({
|
|
183
|
+
node,
|
|
184
|
+
messageId: 'emptyCatchBlock',
|
|
185
|
+
suggest: [
|
|
186
|
+
{
|
|
187
|
+
messageId: 'addErrorLogging',
|
|
188
|
+
fix: () => null,
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
});
|
|
@@ -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-exposed-error-details
|
|
8
|
+
* Detects Lambda handlers exposing internal error details
|
|
9
|
+
* CWE-209: Generation of Error Message Containing Sensitive Information
|
|
10
|
+
*
|
|
11
|
+
* @see https://cwe.mitre.org/data/definitions/209.html
|
|
12
|
+
* @see https://owasp.org/www-project-serverless-top-10/
|
|
13
|
+
*/
|
|
14
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
15
|
+
type MessageIds = 'exposedErrorDetails' | 'sanitizeError';
|
|
16
|
+
export interface Options {
|
|
17
|
+
/** Allow in test files. Default: true */
|
|
18
|
+
allowInTests?: boolean;
|
|
19
|
+
}
|
|
20
|
+
type RuleOptions = [Options?];
|
|
21
|
+
export declare const noExposedErrorDetails: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,161 @@
|
|
|
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.noExposedErrorDetails = void 0;
|
|
9
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
+
// Properties that expose internal details
|
|
11
|
+
const SENSITIVE_ERROR_PROPERTIES = [
|
|
12
|
+
'stack',
|
|
13
|
+
'stackTrace',
|
|
14
|
+
'trace',
|
|
15
|
+
'cause',
|
|
16
|
+
'__dirname',
|
|
17
|
+
'__filename',
|
|
18
|
+
'path',
|
|
19
|
+
'hostname',
|
|
20
|
+
'config',
|
|
21
|
+
'env',
|
|
22
|
+
];
|
|
23
|
+
exports.noExposedErrorDetails = (0, eslint_devkit_1.createRule)({
|
|
24
|
+
name: 'no-exposed-error-details',
|
|
25
|
+
meta: {
|
|
26
|
+
type: 'problem',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Detects Lambda handlers exposing internal error details in responses',
|
|
29
|
+
},
|
|
30
|
+
hasSuggestions: true,
|
|
31
|
+
messages: {
|
|
32
|
+
exposedErrorDetails: (0, eslint_devkit_1.formatLLMMessage)({
|
|
33
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
34
|
+
issueName: 'Exposed Error Details',
|
|
35
|
+
cwe: 'CWE-209',
|
|
36
|
+
cvss: 4.3,
|
|
37
|
+
description: 'Response exposes {{property}} which may leak internal paths, configs, or stack traces.',
|
|
38
|
+
severity: 'MEDIUM',
|
|
39
|
+
fix: 'Return generic error message. Log detailed errors server-side: console.error(error)',
|
|
40
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/209.html',
|
|
41
|
+
}),
|
|
42
|
+
sanitizeError: (0, eslint_devkit_1.formatLLMMessage)({
|
|
43
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
44
|
+
issueName: 'Sanitize Error Response',
|
|
45
|
+
description: 'Return a generic error message without internal details',
|
|
46
|
+
severity: 'LOW',
|
|
47
|
+
fix: 'return { statusCode: 500, body: JSON.stringify({ message: "Internal server error" }) }',
|
|
48
|
+
documentationLink: 'https://owasp.org/www-community/Improper_Error_Handling',
|
|
49
|
+
}),
|
|
50
|
+
},
|
|
51
|
+
schema: [
|
|
52
|
+
{
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
allowInTests: {
|
|
56
|
+
type: 'boolean',
|
|
57
|
+
default: true,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
additionalProperties: false,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
defaultOptions: [{ allowInTests: true }],
|
|
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 if we're inside a return statement that looks like API response
|
|
73
|
+
let insideApiResponse = false;
|
|
74
|
+
/**
|
|
75
|
+
* Check if this return looks like an API Gateway response
|
|
76
|
+
*/
|
|
77
|
+
function isApiResponse(node) {
|
|
78
|
+
return node.properties.some((prop) => prop.type === eslint_devkit_1.AST_NODE_TYPES.Property &&
|
|
79
|
+
prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
80
|
+
['statusCode', 'body', 'headers'].includes(prop.key.name));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check if member expression accesses sensitive property
|
|
84
|
+
*/
|
|
85
|
+
function getSensitiveProperty(node) {
|
|
86
|
+
if (node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
87
|
+
if (SENSITIVE_ERROR_PROPERTIES.includes(node.property.name)) {
|
|
88
|
+
return node.property.name;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
ReturnStatement(node) {
|
|
95
|
+
if (node.argument?.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression &&
|
|
96
|
+
isApiResponse(node.argument)) {
|
|
97
|
+
insideApiResponse = true;
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
'ReturnStatement:exit'() {
|
|
101
|
+
insideApiResponse = false;
|
|
102
|
+
},
|
|
103
|
+
MemberExpression(node) {
|
|
104
|
+
if (!insideApiResponse)
|
|
105
|
+
return;
|
|
106
|
+
const sensitiveProperty = getSensitiveProperty(node);
|
|
107
|
+
if (sensitiveProperty) {
|
|
108
|
+
// Check if this is in a body property or response
|
|
109
|
+
const parent = node.parent;
|
|
110
|
+
// error.stack in response
|
|
111
|
+
if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.Property ||
|
|
112
|
+
parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
113
|
+
context.report({
|
|
114
|
+
node,
|
|
115
|
+
messageId: 'exposedErrorDetails',
|
|
116
|
+
data: {
|
|
117
|
+
property: sensitiveProperty,
|
|
118
|
+
},
|
|
119
|
+
suggest: [
|
|
120
|
+
{
|
|
121
|
+
messageId: 'sanitizeError',
|
|
122
|
+
fix: () => null,
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
// JSON.stringify(error) - stringifying entire error object
|
|
130
|
+
CallExpression(node) {
|
|
131
|
+
if (!insideApiResponse)
|
|
132
|
+
return;
|
|
133
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
134
|
+
node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
135
|
+
node.callee.object.name === 'JSON' &&
|
|
136
|
+
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
137
|
+
node.callee.property.name === 'stringify') {
|
|
138
|
+
const arg = node.arguments[0];
|
|
139
|
+
if (arg?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
140
|
+
// Check if it looks like an error variable
|
|
141
|
+
if (/error|err|e|exception/i.test(arg.name)) {
|
|
142
|
+
context.report({
|
|
143
|
+
node,
|
|
144
|
+
messageId: 'exposedErrorDetails',
|
|
145
|
+
data: {
|
|
146
|
+
property: 'error object',
|
|
147
|
+
},
|
|
148
|
+
suggest: [
|
|
149
|
+
{
|
|
150
|
+
messageId: 'sanitizeError',
|
|
151
|
+
fix: () => null,
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
});
|
|
@@ -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-hardcoded-credentials-sdk
|
|
8
|
+
* Detects hardcoded AWS credentials in SDK client 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/sdk-for-javascript/v3/developer-guide/loading-browser-credentials-browser.html
|
|
13
|
+
*/
|
|
14
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
15
|
+
type MessageIds = 'hardcodedCredentials' | 'useCredentialProvider';
|
|
1
16
|
export interface Options {
|
|
2
17
|
/** Allow in test files. Default: true */
|
|
3
18
|
allowInTests?: boolean;
|
|
4
19
|
}
|
|
5
|
-
|
|
20
|
+
type RuleOptions = [Options?];
|
|
21
|
+
export declare const noHardcodedCredentialsSdk: 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.noHardcodedCredentialsSdk = 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-missing-authorization-check
|
|
8
|
+
* Detects Lambda handlers without authorization checks
|
|
9
|
+
* CWE-862: Missing Authorization
|
|
10
|
+
*
|
|
11
|
+
* @see https://cwe.mitre.org/data/definitions/862.html
|
|
12
|
+
* @see https://owasp.org/www-project-serverless-top-10/
|
|
13
|
+
*/
|
|
14
|
+
import type { TSESLint } from '@interlace/eslint-devkit';
|
|
15
|
+
type MessageIds = 'missingAuthCheck' | 'addAuthCheck';
|
|
16
|
+
export interface Options {
|
|
17
|
+
/** Allow in test files. Default: true */
|
|
18
|
+
allowInTests?: boolean;
|
|
19
|
+
/** Handler file patterns to check. Default: common Lambda patterns */
|
|
20
|
+
handlerPatterns?: string[];
|
|
21
|
+
}
|
|
22
|
+
type RuleOptions = [Options?];
|
|
23
|
+
export declare const noMissingAuthorizationCheck: TSESLint.RuleModule<MessageIds, RuleOptions, unknown, TSESLint.RuleListener>;
|
|
24
|
+
export {};
|
|
@@ -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 {};
|