Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip auth check when emulating task queue function. #1154

Merged
merged 4 commits into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -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
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
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