Skip to content

Commit c0c3d99

Browse files
nbbeekendurran
authored andcommittedMay 26, 2023
fix(NODE-5262): AWS Lambda metadata detection logic is too permissive (#3683)

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed
 

‎src/cmap/handshake/client_metadata.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ export function getFAASEnv(): Map<string, string | Int32> | null {
176176
VERCEL_REGION = ''
177177
} = process.env;
178178

179-
const isAWSFaaS = AWS_EXECUTION_ENV.length > 0 || AWS_LAMBDA_RUNTIME_API.length > 0;
179+
const isAWSFaaS =
180+
AWS_EXECUTION_ENV.startsWith('AWS_Lambda_') || AWS_LAMBDA_RUNTIME_API.length > 0;
180181
const isAzureFaaS = FUNCTIONS_WORKER_RUNTIME.length > 0;
181182
const isGCPFaaS = K_SERVICE.length > 0 || FUNCTION_NAME.length > 0;
182183
const isVercelFaaS = VERCEL.length > 0;

‎test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe('Handshake Prose Tests', function () {
7171
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
7272
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'big']
7373
]
74+
},
75+
{
76+
expectedProvider: undefined,
77+
context: '8. Invalid - AWS_EXECUTION_ENV does not start with "AWS_Lambda_"',
78+
env: [['AWS_EXECUTION_ENV', 'EC2']]
7479
}
7580
];
7681

‎test/unit/cmap/handshake/client_metadata.test.ts

+50-15
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,63 @@ describe('client metadata module', () => {
3737
});
3838

3939
describe('getFAASEnv()', function () {
40-
const tests: Array<[string, string]> = [
41-
['AWS_EXECUTION_ENV', 'aws.lambda'],
40+
const tests: Array<[envVariable: string, provider: string]> = [
4241
['AWS_LAMBDA_RUNTIME_API', 'aws.lambda'],
4342
['FUNCTIONS_WORKER_RUNTIME', 'azure.func'],
4443
['K_SERVICE', 'gcp.func'],
4544
['FUNCTION_NAME', 'gcp.func'],
4645
['VERCEL', 'vercel']
4746
];
4847
for (const [envVariable, provider] of tests) {
49-
context(`when ${envVariable} is in the environment`, () => {
48+
context(`when ${envVariable} is set to a non-empty string`, () => {
5049
before(() => {
51-
process.env[envVariable] = 'non empty string';
50+
process.env[envVariable] = 'non_empty_string';
5251
});
5352
after(() => {
5453
delete process.env[envVariable];
5554
});
5655
it('determines the correct provider', () => {
5756
expect(getFAASEnv()?.get('name')).to.equal(provider);
5857
});
58+
59+
context(`when ${envVariable} is set to an empty string`, () => {
60+
before(() => {
61+
process.env[envVariable] = '';
62+
});
63+
after(() => {
64+
delete process.env[envVariable];
65+
});
66+
it('returns null', () => {
67+
expect(getFAASEnv()).to.be.null;
68+
});
69+
});
5970
});
6071
}
6172

73+
context('when AWS_EXECUTION_ENV starts with "AWS_Lambda_"', () => {
74+
before(() => {
75+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_correctStartString';
76+
});
77+
after(() => {
78+
delete process.env.AWS_EXECUTION_ENV;
79+
});
80+
it('indicates the runtime is aws lambda', () => {
81+
expect(getFAASEnv()?.get('name')).to.equal('aws.lambda');
82+
});
83+
});
84+
85+
context('when AWS_EXECUTION_ENV does not start with "AWS_Lambda_"', () => {
86+
before(() => {
87+
process.env.AWS_EXECUTION_ENV = 'AWS_LambdaIncorrectStartString';
88+
});
89+
after(() => {
90+
delete process.env.AWS_EXECUTION_ENV;
91+
});
92+
it('returns null', () => {
93+
expect(getFAASEnv()).to.be.null;
94+
});
95+
});
96+
6297
context('when there is no FAAS provider data in the env', () => {
6398
it('returns null', () => {
6499
expect(getFAASEnv()).to.be.null;
@@ -69,9 +104,9 @@ describe('client metadata module', () => {
69104
context('unrelated environments', () => {
70105
before(() => {
71106
// aws
72-
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
107+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_non_empty_string';
73108
// azure
74-
process.env.FUNCTIONS_WORKER_RUNTIME = 'non-empty-string';
109+
process.env.FUNCTIONS_WORKER_RUNTIME = 'non_empty_string';
75110
});
76111
after(() => {
77112
delete process.env.AWS_EXECUTION_ENV;
@@ -85,10 +120,10 @@ describe('client metadata module', () => {
85120
context('vercel and aws which share env variables', () => {
86121
before(() => {
87122
// vercel
88-
process.env.VERCEL = 'non-empty-string';
123+
process.env.VERCEL = 'non_empty_string';
89124
// aws
90-
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
91-
process.env.AWS_LAMBDA_RUNTIME_API = 'non-empty-string';
125+
process.env.AWS_EXECUTION_ENV = 'non_empty_string';
126+
process.env.AWS_LAMBDA_RUNTIME_API = 'non_empty_string';
92127
});
93128
after(() => {
94129
delete process.env.VERCEL;
@@ -383,15 +418,15 @@ describe('client metadata module', () => {
383418
aws: [
384419
{
385420
context: 'no additional metadata',
386-
env: [['AWS_EXECUTION_ENV', 'non-empty string']],
421+
env: [['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string']],
387422
outcome: {
388423
name: 'aws.lambda'
389424
}
390425
},
391426
{
392427
context: 'AWS_REGION provided',
393428
env: [
394-
['AWS_EXECUTION_ENV', 'non-empty string'],
429+
['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string'],
395430
['AWS_REGION', 'non-null']
396431
],
397432
outcome: {
@@ -402,7 +437,7 @@ describe('client metadata module', () => {
402437
{
403438
context: 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE provided',
404439
env: [
405-
['AWS_EXECUTION_ENV', 'non-empty string'],
440+
['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string'],
406441
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '3']
407442
],
408443
outcome: {
@@ -506,7 +541,7 @@ describe('client metadata module', () => {
506541

507542
context('when a numeric FAAS env variable is not numerically parsable', () => {
508543
before(() => {
509-
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
544+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_non_empty_string';
510545
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '123not numeric';
511546
});
512547

@@ -525,7 +560,7 @@ describe('client metadata module', () => {
525560
context('when faas region is too large', () => {
526561
beforeEach('1. Omit fields from `env` except `env.name`.', () => {
527562
sinon.stub(process, 'env').get(() => ({
528-
AWS_EXECUTION_ENV: 'iLoveJavaScript',
563+
AWS_EXECUTION_ENV: 'AWS_Lambda_iLoveJavaScript',
529564
AWS_REGION: 'a'.repeat(512)
530565
}));
531566
});
@@ -542,7 +577,7 @@ describe('client metadata module', () => {
542577
context('release too large', () => {
543578
beforeEach('2. Omit fields from `os` except `os.type`.', () => {
544579
sinon.stub(process, 'env').get(() => ({
545-
AWS_EXECUTION_ENV: 'iLoveJavaScript',
580+
AWS_EXECUTION_ENV: 'AWS_Lambda_iLoveJavaScript',
546581
AWS_REGION: 'abc'
547582
}));
548583
sinon.stub(os, 'release').returns('a'.repeat(512));

0 commit comments

Comments
 (0)
Please sign in to comment.