From 150a94e88d2e14131840320e621b04dae7b8da0e Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Tue, 21 Jun 2022 12:16:38 -0700 Subject: [PATCH 1/4] Skip checking authorization header when emulating a task queue function. --- src/common/providers/tasks.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/common/providers/tasks.ts b/src/common/providers/tasks.ts index d84f7a585..bb7ee1400 100644 --- a/src/common/providers/tasks.ts +++ b/src/common/providers/tasks.ts @@ -117,20 +117,21 @@ export function onDispatchHandler( throw new https.HttpsError('invalid-argument', 'Bad Request'); } - const authHeader = req.header('Authorization') || ''; - const token = authHeader.match(/^Bearer (.*)$/)?.[1]; - // Note: this should never happen since task queue functions are guarded by IAM. - if (!token) { - throw new https.HttpsError('unauthenticated', 'Unauthenticated'); - } - // We skip authenticating the token since tq functions are guarded by IAM. - const authToken = await https.unsafeDecodeIdToken(token); - const context: TaskContext = { - auth: { + const context: TaskContext = {}; + if (!process.env.FUNCTIONS_EMULATOR) { + const authHeader = req.header('Authorization') || ''; + const token = authHeader.match(/^Bearer (.*)$/)?.[1]; + // Note: this should never happen since task queue functions are guarded by IAM. + if (!token) { + throw new https.HttpsError('unauthenticated', 'Unauthenticated'); + } + // We skip authenticating the token since tq functions are guarded by IAM. + const authToken = await https.unsafeDecodeIdToken(token); + context.auth = { uid: authToken.uid, token: authToken, - }, - }; + } + } const data: Req = https.decode(req.body.data); if (handler.length === 2) { From 50f14b38095de224778a3cf94429768a1830fe26 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Tue, 21 Jun 2022 12:24:56 -0700 Subject: [PATCH 2/4] Add test. --- spec/common/providers/tasks.spec.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/spec/common/providers/tasks.spec.ts b/spec/common/providers/tasks.spec.ts index d3711a18f..a128fd87d 100644 --- a/spec/common/providers/tasks.spec.ts +++ b/spec/common/providers/tasks.spec.ts @@ -83,7 +83,7 @@ describe('onEnqueueHandler', () => { function mockEnqueueRequest( data: unknown, contentType: string = 'application/json', - context: { authorization: string } = { authorization: 'Bearer abc' } + context: { authorization?: string } = { authorization: 'Bearer abc' } ): ReturnType { return mockRequest(data, contentType, context); } @@ -239,4 +239,25 @@ describe('onEnqueueHandler', () => { expectedStatus: 204, }); }); + + + it('should skip auth in emulated environment', async () => { + const restore = process.env.FUNCTIONS_EMULATOR; + process.env.FUNCTIONS_EMULATOR = "true"; + + await runTaskTest({ + httpRequest: mockEnqueueRequest(null, 'application/json', {}), + expectedData: null, + taskFunction: (data, context) => { + expect(context.auth).to.be.undefined; + return null; + }, + taskFunction2: (request) => { + expect(request.auth).to.be.undefined; + }, + expectedStatus: 204, + }); + + process.env.FUNCTIONS_EMULATOR = restore; + }); }); From 6f9113a20ab154d6d97928036be13046b5dce6b4 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Tue, 21 Jun 2022 12:25:41 -0700 Subject: [PATCH 3/4] Prettier. --- spec/common/providers/tasks.spec.ts | 3 +-- src/common/providers/tasks.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/common/providers/tasks.spec.ts b/spec/common/providers/tasks.spec.ts index a128fd87d..6811f0b2a 100644 --- a/spec/common/providers/tasks.spec.ts +++ b/spec/common/providers/tasks.spec.ts @@ -240,10 +240,9 @@ describe('onEnqueueHandler', () => { }); }); - it('should skip auth in emulated environment', async () => { const restore = process.env.FUNCTIONS_EMULATOR; - process.env.FUNCTIONS_EMULATOR = "true"; + process.env.FUNCTIONS_EMULATOR = 'true'; await runTaskTest({ httpRequest: mockEnqueueRequest(null, 'application/json', {}), diff --git a/src/common/providers/tasks.ts b/src/common/providers/tasks.ts index bb7ee1400..52360af48 100644 --- a/src/common/providers/tasks.ts +++ b/src/common/providers/tasks.ts @@ -130,7 +130,7 @@ export function onDispatchHandler( context.auth = { uid: authToken.uid, token: authToken, - } + }; } const data: Req = https.decode(req.body.data); From 5e9af35c2799fe23aedef4c4ce35c7a0e6780bfa Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Tue, 21 Jun 2022 12:28:03 -0700 Subject: [PATCH 4/4] Add changelog entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b174a17fe..5d516b215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,2 @@ - Adds RTDB Triggers for v2 functions (#1127) +- Fixes bug where emulated task queue function required auth header (#1154)