Skip to content

Commit d74d3f9

Browse files
authored
fix(NODE-5260): AWS Lambda metadata detection logic is too permissive (#3663)
1 parent 64dc577 commit d74d3f9

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

src/cmap/handshake/client_metadata.ts

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ describe('Handshake Prose Tests', function () {
7070
['AWS_EXECUTION_ENV', 'AWS_Lambda_java8'],
7171
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'big']
7272
]
73+
},
74+
{
75+
expectedProvider: undefined,
76+
context: '8. Invalid - AWS_EXECUTION_ENV does not start with "AWS_Lambda_"',
77+
env: [['AWS_EXECUTION_ENV', 'EC2']]
7378
}
7479
];
7580

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,63 @@ describe('client metadata module', () => {
3838
});
3939

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

74+
context('when AWS_EXECUTION_ENV starts with "AWS_Lambda_"', () => {
75+
before(() => {
76+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_correctStartString';
77+
});
78+
after(() => {
79+
delete process.env.AWS_EXECUTION_ENV;
80+
});
81+
it('indicates the runtime is aws lambda', () => {
82+
expect(getFAASEnv()?.get('name')).to.equal('aws.lambda');
83+
});
84+
});
85+
86+
context('when AWS_EXECUTION_ENV does not start with "AWS_Lambda_"', () => {
87+
before(() => {
88+
process.env.AWS_EXECUTION_ENV = 'AWS_LambdaIncorrectStartString';
89+
});
90+
after(() => {
91+
delete process.env.AWS_EXECUTION_ENV;
92+
});
93+
it('returns null', () => {
94+
expect(getFAASEnv()).to.be.null;
95+
});
96+
});
97+
6398
context('when there is no FAAS provider data in the env', () => {
6499
it('returns null', () => {
65100
expect(getFAASEnv()).to.be.null;
@@ -70,9 +105,9 @@ describe('client metadata module', () => {
70105
context('unrelated environments', () => {
71106
before(() => {
72107
// aws
73-
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
108+
process.env.AWS_EXECUTION_ENV = 'AWS_Lambda_non_empty_string';
74109
// azure
75-
process.env.FUNCTIONS_WORKER_RUNTIME = 'non-empty-string';
110+
process.env.FUNCTIONS_WORKER_RUNTIME = 'non_empty_string';
76111
});
77112
after(() => {
78113
delete process.env.AWS_EXECUTION_ENV;
@@ -86,10 +121,10 @@ describe('client metadata module', () => {
86121
context('vercel and aws which share env variables', () => {
87122
before(() => {
88123
// vercel
89-
process.env.VERCEL = 'non-empty-string';
124+
process.env.VERCEL = 'non_empty_string';
90125
// aws
91-
process.env.AWS_EXECUTION_ENV = 'non-empty-string';
92-
process.env.AWS_LAMBDA_RUNTIME_API = 'non-empty-string';
126+
process.env.AWS_EXECUTION_ENV = 'non_empty_string';
127+
process.env.AWS_LAMBDA_RUNTIME_API = 'non_empty_string';
93128
});
94129
after(() => {
95130
delete process.env.VERCEL;
@@ -384,15 +419,15 @@ describe('client metadata module', () => {
384419
aws: [
385420
{
386421
context: 'no additional metadata',
387-
env: [['AWS_EXECUTION_ENV', 'non-empty string']],
422+
env: [['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string']],
388423
outcome: {
389424
name: 'aws.lambda'
390425
}
391426
},
392427
{
393428
context: 'AWS_REGION provided',
394429
env: [
395-
['AWS_EXECUTION_ENV', 'non-empty string'],
430+
['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string'],
396431
['AWS_REGION', 'non-null']
397432
],
398433
outcome: {
@@ -403,7 +438,7 @@ describe('client metadata module', () => {
403438
{
404439
context: 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE provided',
405440
env: [
406-
['AWS_EXECUTION_ENV', 'non-empty string'],
441+
['AWS_EXECUTION_ENV', 'AWS_Lambda_non_empty_string'],
407442
['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', '3']
408443
],
409444
outcome: {
@@ -507,7 +542,7 @@ describe('client metadata module', () => {
507542

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

@@ -526,7 +561,7 @@ describe('client metadata module', () => {
526561
context('when faas region is too large', () => {
527562
beforeEach('1. Omit fields from `env` except `env.name`.', () => {
528563
sinon.stub(process, 'env').get(() => ({
529-
AWS_EXECUTION_ENV: 'iLoveJavaScript',
564+
AWS_EXECUTION_ENV: 'AWS_Lambda_iLoveJavaScript',
530565
AWS_REGION: 'a'.repeat(512)
531566
}));
532567
});
@@ -543,7 +578,7 @@ describe('client metadata module', () => {
543578
context('release too large', () => {
544579
beforeEach('2. Omit fields from `os` except `os.type`.', () => {
545580
sinon.stub(process, 'env').get(() => ({
546-
AWS_EXECUTION_ENV: 'iLoveJavaScript',
581+
AWS_EXECUTION_ENV: 'AWS_Lambda_iLoveJavaScript',
547582
AWS_REGION: 'abc'
548583
}));
549584
sinon.stub(os, 'release').returns('a'.repeat(512));

0 commit comments

Comments
 (0)