Skip to content

Commit

Permalink
Skip auth check when emulating task queue function. (#1154)
Browse files Browse the repository at this point in the history
Fixes #1146
  • Loading branch information
taeold committed Jun 22, 2022
1 parent 76e0afd commit 3737898
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Adds RTDB Triggers for v2 functions (#1127)
- Fixes bug where emulated task queue function required auth header (#1154)
22 changes: 21 additions & 1 deletion spec/common/providers/tasks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof mockRequest> {
return mockRequest(data, contentType, context);
}
Expand Down Expand Up @@ -239,4 +239,24 @@ 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;
});
});
25 changes: 13 additions & 12 deletions src/common/providers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,21 @@ export function onDispatchHandler<Req = any>(
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) {
Expand Down

0 comments on commit 3737898

Please sign in to comment.