eslint-plugin-lambda-security 0.0.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.
- package/AGENTS.md +106 -0
- package/CHANGELOG.md +42 -0
- package/README.md +355 -0
- package/package.json +99 -0
- package/src/index.d.ts +65 -0
- package/src/index.js +58 -0
- package/src/index.js.map +1 -0
- package/src/rules/no-env-logging/index.d.ts +5 -0
- package/src/rules/no-env-logging/index.js +129 -0
- package/src/rules/no-env-logging/index.js.map +1 -0
- package/src/rules/no-hardcoded-credentials-sdk/index.d.ts +5 -0
- package/src/rules/no-hardcoded-credentials-sdk/index.js +226 -0
- package/src/rules/no-hardcoded-credentials-sdk/index.js.map +1 -0
- package/src/rules/no-permissive-cors-middy/index.d.ts +5 -0
- package/src/rules/no-permissive-cors-middy/index.js +134 -0
- package/src/rules/no-permissive-cors-middy/index.js.map +1 -0
- package/src/rules/no-permissive-cors-response/index.d.ts +7 -0
- package/src/rules/no-permissive-cors-response/index.js +144 -0
- package/src/rules/no-permissive-cors-response/index.js.map +1 -0
- package/src/rules/no-secrets-in-env/index.d.ts +7 -0
- package/src/rules/no-secrets-in-env/index.js +154 -0
- package/src/rules/no-secrets-in-env/index.js.map +1 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* eslint-plugin-lambda-security
|
|
4
|
+
* Security-focused ESLint rules for AWS Lambda and Middy applications
|
|
5
|
+
*
|
|
6
|
+
* Detects insecure API Gateway responses, missing security headers,
|
|
7
|
+
* CORS misconfigurations, input validation issues, and more.
|
|
8
|
+
*/
|
|
9
|
+
// P0 Critical Rules
|
|
10
|
+
const no_hardcoded_credentials_sdk_1 = require("./rules/no-hardcoded-credentials-sdk");
|
|
11
|
+
const no_permissive_cors_response_1 = require("./rules/no-permissive-cors-response");
|
|
12
|
+
const no_permissive_cors_middy_1 = require("./rules/no-permissive-cors-middy");
|
|
13
|
+
const no_secrets_in_env_1 = require("./rules/no-secrets-in-env");
|
|
14
|
+
const no_env_logging_1 = require("./rules/no-env-logging");
|
|
15
|
+
const rules = {
|
|
16
|
+
'no-hardcoded-credentials-sdk': no_hardcoded_credentials_sdk_1.noHardcodedCredentialsSdk,
|
|
17
|
+
'no-permissive-cors-response': no_permissive_cors_response_1.noPermissiveCorsResponse,
|
|
18
|
+
'no-permissive-cors-middy': no_permissive_cors_middy_1.noPermissiveCorsMidly,
|
|
19
|
+
'no-secrets-in-env': no_secrets_in_env_1.noSecretsInEnv,
|
|
20
|
+
'no-env-logging': no_env_logging_1.noEnvLogging,
|
|
21
|
+
};
|
|
22
|
+
const configs = {
|
|
23
|
+
recommended: {
|
|
24
|
+
name: 'lambda-security/recommended',
|
|
25
|
+
plugins: {
|
|
26
|
+
'lambda-security': { rules },
|
|
27
|
+
},
|
|
28
|
+
rules: {
|
|
29
|
+
'lambda-security/no-hardcoded-credentials-sdk': 'error',
|
|
30
|
+
'lambda-security/no-permissive-cors-response': 'error',
|
|
31
|
+
'lambda-security/no-permissive-cors-middy': 'error',
|
|
32
|
+
'lambda-security/no-secrets-in-env': 'error',
|
|
33
|
+
'lambda-security/no-env-logging': 'warn',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
strict: {
|
|
37
|
+
name: 'lambda-security/strict',
|
|
38
|
+
plugins: {
|
|
39
|
+
'lambda-security': { rules },
|
|
40
|
+
},
|
|
41
|
+
rules: {
|
|
42
|
+
'lambda-security/no-hardcoded-credentials-sdk': 'error',
|
|
43
|
+
'lambda-security/no-permissive-cors-response': 'error',
|
|
44
|
+
'lambda-security/no-permissive-cors-middy': 'error',
|
|
45
|
+
'lambda-security/no-secrets-in-env': 'error',
|
|
46
|
+
'lambda-security/no-env-logging': 'error',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
module.exports = {
|
|
51
|
+
rules,
|
|
52
|
+
configs,
|
|
53
|
+
meta: {
|
|
54
|
+
name: 'eslint-plugin-lambda-security',
|
|
55
|
+
version: '0.0.1',
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/eslint-plugin-lambda-security/src/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;AAEH,oBAAoB;AACpB,uFAAiF;AACjF,qFAA+E;AAC/E,+EAAyE;AACzE,iEAA2D;AAC3D,2DAAsD;AAEtD,MAAM,KAAK,GAAG;IACZ,8BAA8B,EAAE,wDAAyB;IACzD,6BAA6B,EAAE,sDAAwB;IACvD,0BAA0B,EAAE,gDAAqB;IACjD,mBAAmB,EAAE,kCAAc;IACnC,gBAAgB,EAAE,6BAAY;CAC/B,CAAC;AAEF,MAAM,OAAO,GAAG;IACd,WAAW,EAAE;QACX,IAAI,EAAE,6BAA6B;QACnC,OAAO,EAAE;YACP,iBAAiB,EAAE,EAAE,KAAK,EAAE;SAC7B;QACD,KAAK,EAAE;YACL,8CAA8C,EAAE,OAAO;YACvD,6CAA6C,EAAE,OAAO;YACtD,0CAA0C,EAAE,OAAO;YACnD,mCAAmC,EAAE,OAAO;YAC5C,gCAAgC,EAAE,MAAM;SACzC;KACF;IACD,MAAM,EAAE;QACN,IAAI,EAAE,wBAAwB;QAC9B,OAAO,EAAE;YACP,iBAAiB,EAAE,EAAE,KAAK,EAAE;SAC7B;QACD,KAAK,EAAE;YACL,8CAA8C,EAAE,OAAO;YACvD,6CAA6C,EAAE,OAAO;YACtD,0CAA0C,EAAE,OAAO;YACnD,mCAAmC,EAAE,OAAO;YAC5C,gCAAgC,EAAE,OAAO;SAC1C;KACF;CACF,CAAC;AAEF,iBAAS;IACP,KAAK;IACL,OAAO;IACP,IAAI,EAAE;QACJ,IAAI,EAAE,+BAA+B;QACrC,OAAO,EAAE,OAAO;KACjB;CACF,CAAC"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noEnvLogging = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
// Logging function names
|
|
6
|
+
const LOGGING_FUNCTIONS = ['log', 'info', 'warn', 'error', 'debug', 'trace'];
|
|
7
|
+
exports.noEnvLogging = (0, eslint_devkit_1.createRule)({
|
|
8
|
+
name: 'no-env-logging',
|
|
9
|
+
meta: {
|
|
10
|
+
type: 'problem',
|
|
11
|
+
docs: {
|
|
12
|
+
description: 'Detects logging of process.env which may expose secrets',
|
|
13
|
+
},
|
|
14
|
+
hasSuggestions: true,
|
|
15
|
+
messages: {
|
|
16
|
+
envLogging: (0, eslint_devkit_1.formatLLMMessage)({
|
|
17
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
18
|
+
issueName: 'Environment Variable Logging',
|
|
19
|
+
cwe: 'CWE-532',
|
|
20
|
+
description: 'Logging process.env may expose secrets (API keys, passwords, tokens)',
|
|
21
|
+
severity: 'HIGH',
|
|
22
|
+
fix: 'Remove process.env from log statements or log specific non-sensitive values',
|
|
23
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/532.html',
|
|
24
|
+
}),
|
|
25
|
+
removeEnvLogging: (0, eslint_devkit_1.formatLLMMessage)({
|
|
26
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
27
|
+
issueName: 'Remove Env Logging',
|
|
28
|
+
description: 'Remove environment variable logging',
|
|
29
|
+
severity: 'LOW',
|
|
30
|
+
fix: 'Log specific values instead: console.log("Region:", process.env.AWS_REGION)',
|
|
31
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/532.html',
|
|
32
|
+
}),
|
|
33
|
+
},
|
|
34
|
+
schema: [
|
|
35
|
+
{
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
allowInTests: { type: 'boolean', default: true },
|
|
39
|
+
},
|
|
40
|
+
additionalProperties: false,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
defaultOptions: [{ allowInTests: true }],
|
|
45
|
+
create(context, [options = {}]) {
|
|
46
|
+
const { allowInTests = true } = options;
|
|
47
|
+
const filename = context.filename || context.getFilename();
|
|
48
|
+
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
49
|
+
if (allowInTests && isTestFile) {
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
const sourceCode = context.sourceCode;
|
|
53
|
+
/**
|
|
54
|
+
* Check if a node is process.env or process.env access
|
|
55
|
+
*/
|
|
56
|
+
function isProcessEnv(node) {
|
|
57
|
+
// Direct process.env
|
|
58
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
59
|
+
node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
60
|
+
node.object.name === 'process' &&
|
|
61
|
+
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
62
|
+
node.property.name === 'env') {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Check if a node contains process.env
|
|
69
|
+
*/
|
|
70
|
+
function containsProcessEnv(node) {
|
|
71
|
+
const text = sourceCode.getText(node);
|
|
72
|
+
return text.includes('process.env');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Check if this is a console/logger call
|
|
76
|
+
*/
|
|
77
|
+
function isLoggingCall(node) {
|
|
78
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
79
|
+
const obj = node.callee.object;
|
|
80
|
+
const prop = node.callee.property;
|
|
81
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
82
|
+
return false;
|
|
83
|
+
const methodName = prop.name;
|
|
84
|
+
if (!LOGGING_FUNCTIONS.includes(methodName))
|
|
85
|
+
return false;
|
|
86
|
+
// console.log, console.info, etc.
|
|
87
|
+
if (obj.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && obj.name === 'console') {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
// logger.log, log.info, etc.
|
|
91
|
+
if (obj.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && /log(ger)?/i.test(obj.name)) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
CallExpression(node) {
|
|
99
|
+
if (!isLoggingCall(node))
|
|
100
|
+
return;
|
|
101
|
+
// Check each argument for process.env
|
|
102
|
+
for (const arg of node.arguments) {
|
|
103
|
+
// Direct process.env logging
|
|
104
|
+
if (isProcessEnv(arg)) {
|
|
105
|
+
context.report({
|
|
106
|
+
node: arg,
|
|
107
|
+
messageId: 'envLogging',
|
|
108
|
+
suggest: [{ messageId: 'removeEnvLogging', fix: () => null }],
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
// process.env inside template literal or string concatenation
|
|
112
|
+
else if (containsProcessEnv(arg)) {
|
|
113
|
+
// Don't flag if it's just a specific env var access like process.env.AWS_REGION
|
|
114
|
+
const text = sourceCode.getText(arg);
|
|
115
|
+
// Flag if it's the entire process.env object
|
|
116
|
+
if (/process\.env\s*[,)\]]/.test(text) || /JSON\.stringify\s*\(\s*process\.env\s*\)/.test(text)) {
|
|
117
|
+
context.report({
|
|
118
|
+
node: arg,
|
|
119
|
+
messageId: 'envLogging',
|
|
120
|
+
suggest: [{ messageId: 'removeEnvLogging', fix: () => null }],
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-lambda-security/src/rules/no-env-logging/index.ts"],"names":[],"mappings":";;;AAQA,4DAAsG;AAWtG,yBAAyB;AACzB,MAAM,iBAAiB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAEhE,QAAA,YAAY,GAAG,IAAA,0BAAU,EAA0B;IAC9D,IAAI,EAAE,gBAAgB;IACtB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,yDAAyD;SACvE;QACD,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,UAAU,EAAE,IAAA,gCAAgB,EAAC;gBAC3B,IAAI,EAAE,4BAAY,CAAC,QAAQ;gBAC3B,SAAS,EAAE,8BAA8B;gBACzC,GAAG,EAAE,SAAS;gBACd,WAAW,EAAE,sEAAsE;gBACnF,QAAQ,EAAE,MAAM;gBAChB,GAAG,EAAE,6EAA6E;gBAClF,iBAAiB,EAAE,iDAAiD;aACrE,CAAC;YACF,gBAAgB,EAAE,IAAA,gCAAgB,EAAC;gBACjC,IAAI,EAAE,4BAAY,CAAC,IAAI;gBACvB,SAAS,EAAE,oBAAoB;gBAC/B,WAAW,EAAE,qCAAqC;gBAClD,QAAQ,EAAE,KAAK;gBACf,GAAG,EAAE,6EAA6E;gBAClF,iBAAiB,EAAE,iDAAiD;aACrE,CAAC;SACH;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,YAAY,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE;iBACjD;gBACD,oBAAoB,EAAE,KAAK;aAC5B;SACF;KACF;IACD,cAAc,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;IACxC,MAAM,CAAC,OAAsD,EAAE,CAAC,OAAO,GAAG,EAAE,C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I,CAAC,IAAI,0CAA0C,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;4BAChG,OAAO,CAAC,MAAM,CAAC;gCACb,IAAI,EAAE,GAAG;gCACT,SAAS,EAAE,YAAY;gCACvB,OAAO,EAAE,CAAC,EAAE,SAAS,EAAE,kBAAkB,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC;6BAC9D,CAAC,CAAC;wBACL,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noHardcodedCredentialsSdk = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
// Patterns that indicate AWS access keys
|
|
6
|
+
const AWS_ACCESS_KEY_PATTERNS = [
|
|
7
|
+
/^AKIA[0-9A-Z]{16}$/, // IAM user access key
|
|
8
|
+
/^ASIA[0-9A-Z]{16}$/, // STS temporary access key
|
|
9
|
+
/^AIDA[0-9A-Z]{16}$/, // IAM user ID
|
|
10
|
+
];
|
|
11
|
+
// Property names that indicate credentials
|
|
12
|
+
const CREDENTIAL_PROPERTY_NAMES = [
|
|
13
|
+
'accessKeyId',
|
|
14
|
+
'secretAccessKey',
|
|
15
|
+
'sessionToken',
|
|
16
|
+
'awsAccessKeyId',
|
|
17
|
+
'awsSecretAccessKey',
|
|
18
|
+
];
|
|
19
|
+
// AWS SDK v3 client names
|
|
20
|
+
const AWS_SDK_V3_CLIENTS = [
|
|
21
|
+
'S3Client',
|
|
22
|
+
'DynamoDBClient',
|
|
23
|
+
'LambdaClient',
|
|
24
|
+
'SQSClient',
|
|
25
|
+
'SNSClient',
|
|
26
|
+
'SecretsManagerClient',
|
|
27
|
+
'KMSClient',
|
|
28
|
+
'STSClient',
|
|
29
|
+
'IAMClient',
|
|
30
|
+
'EC2Client',
|
|
31
|
+
'ECSClient',
|
|
32
|
+
'CloudWatchClient',
|
|
33
|
+
'CloudWatchLogsClient',
|
|
34
|
+
'APIGatewayClient',
|
|
35
|
+
'CognitoIdentityClient',
|
|
36
|
+
'CognitoIdentityProviderClient',
|
|
37
|
+
'EventBridgeClient',
|
|
38
|
+
'StepFunctionsClient',
|
|
39
|
+
'RDSClient',
|
|
40
|
+
'ElastiCacheClient',
|
|
41
|
+
];
|
|
42
|
+
exports.noHardcodedCredentialsSdk = (0, eslint_devkit_1.createRule)({
|
|
43
|
+
name: 'no-hardcoded-credentials-sdk',
|
|
44
|
+
meta: {
|
|
45
|
+
type: 'problem',
|
|
46
|
+
docs: {
|
|
47
|
+
description: 'Detects hardcoded AWS credentials in SDK client configurations',
|
|
48
|
+
},
|
|
49
|
+
hasSuggestions: true,
|
|
50
|
+
messages: {
|
|
51
|
+
hardcodedCredentials: (0, eslint_devkit_1.formatLLMMessage)({
|
|
52
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
53
|
+
issueName: 'Hardcoded AWS Credentials',
|
|
54
|
+
cwe: 'CWE-798',
|
|
55
|
+
description: 'Hardcoded AWS credentials detected in {{clientName}} config: {{property}}',
|
|
56
|
+
severity: 'CRITICAL',
|
|
57
|
+
fix: 'Use credential provider chain: new {{clientName}}({ credentials: fromNodeProviderChain() })',
|
|
58
|
+
documentationLink: 'https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html',
|
|
59
|
+
}),
|
|
60
|
+
useCredentialProvider: (0, eslint_devkit_1.formatLLMMessage)({
|
|
61
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
62
|
+
issueName: 'Use Credential Provider',
|
|
63
|
+
description: 'Use AWS credential provider chain instead of hardcoded credentials',
|
|
64
|
+
severity: 'LOW',
|
|
65
|
+
fix: 'import { fromNodeProviderChain } from "@aws-sdk/credential-providers"',
|
|
66
|
+
documentationLink: 'https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html',
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
schema: [
|
|
70
|
+
{
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
allowInTests: {
|
|
74
|
+
type: 'boolean',
|
|
75
|
+
default: true,
|
|
76
|
+
description: 'Allow hardcoded credentials in test files',
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
additionalProperties: false,
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
defaultOptions: [
|
|
84
|
+
{
|
|
85
|
+
allowInTests: true,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
create(context, [options = {}]) {
|
|
89
|
+
const { allowInTests = true } = options;
|
|
90
|
+
const filename = context.filename || context.getFilename();
|
|
91
|
+
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
92
|
+
if (allowInTests && isTestFile) {
|
|
93
|
+
return {};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Check if a value looks like an AWS access key
|
|
97
|
+
*/
|
|
98
|
+
function looksLikeAwsAccessKey(value) {
|
|
99
|
+
return AWS_ACCESS_KEY_PATTERNS.some(pattern => pattern.test(value));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Check if a property name is credential-related
|
|
103
|
+
*/
|
|
104
|
+
function isCredentialProperty(name) {
|
|
105
|
+
return CREDENTIAL_PROPERTY_NAMES.some(credProp => name.toLowerCase() === credProp.toLowerCase());
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get the AWS client name from the call expression
|
|
109
|
+
* Only match known AWS SDK clients or names ending with 'Client' that look like AWS
|
|
110
|
+
*/
|
|
111
|
+
function getAwsClientName(node) {
|
|
112
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
113
|
+
const name = node.callee.name;
|
|
114
|
+
// Known AWS SDK v3 clients
|
|
115
|
+
if (AWS_SDK_V3_CLIENTS.includes(name)) {
|
|
116
|
+
return name;
|
|
117
|
+
}
|
|
118
|
+
// Only match *Client if it looks like AWS (contains common AWS service prefixes)
|
|
119
|
+
if (name.endsWith('Client') && /^(S3|DynamoDB|Lambda|SQS|SNS|Secrets|KMS|STS|IAM|EC2|ECS|CloudWatch|APIGateway|Cognito|EventBridge|StepFunctions|RDS|ElastiCache|SSM|Athena|Glue|Kinesis|Firehose|Rekognition|Textract|Comprehend|Polly|Lex|Bedrock|My|Custom|AWS)/i.test(name)) {
|
|
120
|
+
return name;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Check ObjectExpression for credential properties with hardcoded values
|
|
127
|
+
*/
|
|
128
|
+
function checkCredentialsObject(obj, clientName) {
|
|
129
|
+
for (const prop of obj.properties) {
|
|
130
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
131
|
+
continue;
|
|
132
|
+
// Check for credentials property
|
|
133
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
134
|
+
prop.key.name === 'credentials' &&
|
|
135
|
+
prop.value.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
136
|
+
// Check nested credentials object
|
|
137
|
+
checkCredentialsInner(prop.value, clientName);
|
|
138
|
+
}
|
|
139
|
+
// Also check top-level credential properties (older pattern)
|
|
140
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
141
|
+
isCredentialProperty(prop.key.name)) {
|
|
142
|
+
if (prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof prop.value.value === 'string') {
|
|
143
|
+
reportHardcodedCredential(prop, clientName, prop.key.name);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Check inner credentials object
|
|
150
|
+
*/
|
|
151
|
+
function checkCredentialsInner(obj, clientName) {
|
|
152
|
+
for (const prop of obj.properties) {
|
|
153
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
154
|
+
continue;
|
|
155
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && isCredentialProperty(prop.key.name)) {
|
|
156
|
+
// Check for literal string values
|
|
157
|
+
if (prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof prop.value.value === 'string') {
|
|
158
|
+
const value = prop.value.value;
|
|
159
|
+
const keyName = prop.key.name.toLowerCase();
|
|
160
|
+
// For accessKeyId, check if it looks like an actual AWS key (AKIA/ASIA pattern)
|
|
161
|
+
if (keyName === 'accesskeyid') {
|
|
162
|
+
if (looksLikeAwsAccessKey(value)) {
|
|
163
|
+
reportHardcodedCredential(prop, clientName, prop.key.name);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// For secretAccessKey, require minimum length (AWS secrets are 40+ chars)
|
|
167
|
+
else if (keyName === 'secretaccesskey') {
|
|
168
|
+
if (value.length >= 20) {
|
|
169
|
+
reportHardcodedCredential(prop, clientName, prop.key.name);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// For sessionToken, any non-short value is suspicious
|
|
173
|
+
else if (keyName === 'sessiontoken') {
|
|
174
|
+
if (value.length >= 15) {
|
|
175
|
+
reportHardcodedCredential(prop, clientName, prop.key.name);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Check for template literals (always flag - dynamic construction is suspicious)
|
|
180
|
+
else if (prop.value.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
|
|
181
|
+
reportHardcodedCredential(prop, clientName, prop.key.name);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Report hardcoded credential
|
|
188
|
+
*/
|
|
189
|
+
function reportHardcodedCredential(node, clientName, propertyName) {
|
|
190
|
+
context.report({
|
|
191
|
+
node,
|
|
192
|
+
messageId: 'hardcodedCredentials',
|
|
193
|
+
data: {
|
|
194
|
+
clientName,
|
|
195
|
+
property: propertyName,
|
|
196
|
+
},
|
|
197
|
+
suggest: [
|
|
198
|
+
{
|
|
199
|
+
messageId: 'useCredentialProvider',
|
|
200
|
+
fix: () => null, // Complex fix - just provide suggestion
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
NewExpression(node) {
|
|
207
|
+
const clientName = getAwsClientName(node);
|
|
208
|
+
if (!clientName)
|
|
209
|
+
return;
|
|
210
|
+
// Check constructor arguments
|
|
211
|
+
if (node.arguments.length > 0 && node.arguments[0].type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
212
|
+
checkCredentialsObject(node.arguments[0], clientName);
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
// Also check for { credentials: { accessKeyId: ... } } patterns in variable declarations
|
|
216
|
+
VariableDeclarator(node) {
|
|
217
|
+
if (node.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
218
|
+
node.id.name.toLowerCase().includes('credential') &&
|
|
219
|
+
node.init?.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
220
|
+
checkCredentialsInner(node.init, 'AWSClient');
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-lambda-security/src/rules/no-hardcoded-credentials-sdk/index.ts"],"names":[],"mappings":";;;AASA,4DAAsG;AAWtG,yCAAyC;AACzC,MAAM,uBAAuB,GAAG;IAC9B,oBAAoB,EAAG,sBAAsB;IAC7C,oBAAoB,EAAG,2BAA2B;IAClD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C;oBACD,qBAAqB,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;gBAChD,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noPermissiveCorsMidly = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
exports.noPermissiveCorsMidly = (0, eslint_devkit_1.createRule)({
|
|
6
|
+
name: 'no-permissive-cors-middy',
|
|
7
|
+
meta: {
|
|
8
|
+
type: 'problem',
|
|
9
|
+
docs: {
|
|
10
|
+
description: 'Detects @middy/http-cors middleware with permissive origin (*)',
|
|
11
|
+
},
|
|
12
|
+
hasSuggestions: true,
|
|
13
|
+
messages: {
|
|
14
|
+
permissiveCors: (0, eslint_devkit_1.formatLLMMessage)({
|
|
15
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
16
|
+
issueName: 'Permissive CORS in Middy',
|
|
17
|
+
cwe: 'CWE-942',
|
|
18
|
+
description: 'Wildcard CORS origin in @middy/http-cors allows any domain',
|
|
19
|
+
severity: 'HIGH',
|
|
20
|
+
fix: 'Use specific origins: httpCors({ origins: ["https://your-domain.com"] })',
|
|
21
|
+
documentationLink: 'https://middy.js.org/docs/middlewares/http-cors',
|
|
22
|
+
}),
|
|
23
|
+
useSpecificOrigins: (0, eslint_devkit_1.formatLLMMessage)({
|
|
24
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
25
|
+
issueName: 'Use Specific Origins',
|
|
26
|
+
description: 'Configure allowed origins in httpCors middleware',
|
|
27
|
+
severity: 'LOW',
|
|
28
|
+
fix: 'httpCors({ origins: ["https://allowed-domain.com"] })',
|
|
29
|
+
documentationLink: 'https://middy.js.org/docs/middlewares/http-cors',
|
|
30
|
+
}),
|
|
31
|
+
},
|
|
32
|
+
schema: [
|
|
33
|
+
{
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
allowInTests: {
|
|
37
|
+
type: 'boolean',
|
|
38
|
+
default: true,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
defaultOptions: [{ allowInTests: true }],
|
|
46
|
+
create(context, [options = {}]) {
|
|
47
|
+
const { allowInTests = true } = options;
|
|
48
|
+
const filename = context.filename || context.getFilename();
|
|
49
|
+
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
50
|
+
if (allowInTests && isTestFile) {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Check if a call is to httpCors/cors middleware
|
|
55
|
+
*/
|
|
56
|
+
function isCorsMiddlewareCall(node) {
|
|
57
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
58
|
+
const name = node.callee.name.toLowerCase();
|
|
59
|
+
return name === 'httpcors' || name === 'cors';
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check cors options for permissive origin
|
|
65
|
+
*/
|
|
66
|
+
function checkCorsOptions(node, optionsObj) {
|
|
67
|
+
for (const prop of optionsObj.properties) {
|
|
68
|
+
if (prop.type !== eslint_devkit_1.AST_NODE_TYPES.Property)
|
|
69
|
+
continue;
|
|
70
|
+
// Check for 'origin' or 'origins' property
|
|
71
|
+
if (prop.key.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
72
|
+
const keyName = prop.key.name.toLowerCase();
|
|
73
|
+
if (keyName === 'origin' || keyName === 'origins') {
|
|
74
|
+
// Check for wildcard string
|
|
75
|
+
if (prop.value.type === eslint_devkit_1.AST_NODE_TYPES.Literal && prop.value.value === '*') {
|
|
76
|
+
context.report({
|
|
77
|
+
node: prop,
|
|
78
|
+
messageId: 'permissiveCors',
|
|
79
|
+
suggest: [
|
|
80
|
+
{
|
|
81
|
+
messageId: 'useSpecificOrigins',
|
|
82
|
+
fix: () => null,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
// Check for array with wildcard
|
|
88
|
+
else if (prop.value.type === eslint_devkit_1.AST_NODE_TYPES.ArrayExpression) {
|
|
89
|
+
for (const element of prop.value.elements) {
|
|
90
|
+
if (element?.type === eslint_devkit_1.AST_NODE_TYPES.Literal && element.value === '*') {
|
|
91
|
+
context.report({
|
|
92
|
+
node: element,
|
|
93
|
+
messageId: 'permissiveCors',
|
|
94
|
+
suggest: [
|
|
95
|
+
{
|
|
96
|
+
messageId: 'useSpecificOrigins',
|
|
97
|
+
fix: () => null,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
CallExpression(node) {
|
|
110
|
+
if (!isCorsMiddlewareCall(node))
|
|
111
|
+
return;
|
|
112
|
+
// httpCors() with no arguments defaults to permissive
|
|
113
|
+
if (node.arguments.length === 0) {
|
|
114
|
+
context.report({
|
|
115
|
+
node,
|
|
116
|
+
messageId: 'permissiveCors',
|
|
117
|
+
suggest: [
|
|
118
|
+
{
|
|
119
|
+
messageId: 'useSpecificOrigins',
|
|
120
|
+
fix: () => null,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
// Check options object
|
|
127
|
+
if (node.arguments[0]?.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression) {
|
|
128
|
+
checkCorsOptions(node, node.arguments[0]);
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-lambda-security/src/rules/no-permissive-cors-middy/index.ts"],"names":[],"mappings":";;;AASA,4DAAsG;AAWzF,QAAA,qBAAqB,GAAG,IAAA,0BAAU,EAA0B;IACvE,IAAI,EAAE,0BAA0B;IAChC,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,gEAAgE;SAC9E;QACD,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,cAAc,EAAE,IAAA,gCAAgB,EAAC;gBAC/B,IAAI,EAAE,4BAAY,CAAC,QAAQ;gBAC3B,SAAS,EAAE,0BAA0B;gBACrC,GAAG,EAAE,SAAS;gBACd,WAAW,EAAE,4DAA4D;gBACzE,QAAQ,EAAE,MAAM;gBAChB,GAAG,EAAE,0EAA0E;gBAC/E,iBAAiB,EAAE,iDAAiD;aACrE,CAAC;YACF,kBAAkB,EAAE,IAAA,gCAAgB,EAAC;gBACnC,IAAI,EAAE,4BAAY,CAAC,IAAI;gBACvB,SAAS,EAAE,sBAAsB;gBACjC,WAAW,EAAE,kDAAkD;gBAC/D,QAAQ,EAAE,KAAK;gBACf,GAAG,EAAE,uDAAuD;gBAC5D,iBAAiB,EAAE,iDAAiD;aACrE,CAAC;SACH;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,YAAY,EAAE;wBACZ,IAAI,EAAE,SAAS;wBACf,OAAO,EAAE,IAAI;qBACd;iBACF;gBACD,oBAAoB,EAAE,KAAK;aAC5B;SACF;KACF;IACD,cAAc,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;IACxC,MAAM,CAAC,OAAsD,EAAE,CAAC,OAAO,GAAG,EAAE,CAAC;QAC3E,MAAM,EAAE,YAAY,GAAG,IAAI,EAAE,G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gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;gBAC5C,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
/** Allow in test files. Default: true */
|
|
3
|
+
allowInTests?: boolean;
|
|
4
|
+
/** Allowed origins (patterns). Default: [] */
|
|
5
|
+
allowedOrigins?: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare const noPermissiveCorsResponse: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
|