Skip to content

Commit 3737898

Browse files
authored
Skip auth check when emulating task queue function. (#1154)
Fixes #1146
1 parent 76e0afd commit 3737898

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
- Adds RTDB Triggers for v2 functions (#1127)
2+
- Fixes bug where emulated task queue function required auth header (#1154)

spec/common/providers/tasks.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('onEnqueueHandler', () => {
8383
function mockEnqueueRequest(
8484
data: unknown,
8585
contentType: string = 'application/json',
86-
context: { authorization: string } = { authorization: 'Bearer abc' }
86+
context: { authorization?: string } = { authorization: 'Bearer abc' }
8787
): ReturnType<typeof mockRequest> {
8888
return mockRequest(data, contentType, context);
8989
}
@@ -239,4 +239,24 @@ describe('onEnqueueHandler', () => {
239239
expectedStatus: 204,
240240
});
241241
});
242+
243+
it('should skip auth in emulated environment', async () => {
244+
const restore = process.env.FUNCTIONS_EMULATOR;
245+
process.env.FUNCTIONS_EMULATOR = 'true';
246+
247+
await runTaskTest({
248+
httpRequest: mockEnqueueRequest(null, 'application/json', {}),
249+
expectedData: null,
250+
taskFunction: (data, context) => {
251+
expect(context.auth).to.be.undefined;
252+
return null;
253+
},
254+
taskFunction2: (request) => {
255+
expect(request.auth).to.be.undefined;
256+
},
257+
expectedStatus: 204,
258+
});
259+
260+
process.env.FUNCTIONS_EMULATOR = restore;
261+
});
242262
});

src/common/providers/tasks.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,21 @@ export function onDispatchHandler<Req = any>(
117117
throw new https.HttpsError('invalid-argument', 'Bad Request');
118118
}
119119

120-
const authHeader = req.header('Authorization') || '';
121-
const token = authHeader.match(/^Bearer (.*)$/)?.[1];
122-
// Note: this should never happen since task queue functions are guarded by IAM.
123-
if (!token) {
124-
throw new https.HttpsError('unauthenticated', 'Unauthenticated');
125-
}
126-
// We skip authenticating the token since tq functions are guarded by IAM.
127-
const authToken = await https.unsafeDecodeIdToken(token);
128-
const context: TaskContext = {
129-
auth: {
120+
const context: TaskContext = {};
121+
if (!process.env.FUNCTIONS_EMULATOR) {
122+
const authHeader = req.header('Authorization') || '';
123+
const token = authHeader.match(/^Bearer (.*)$/)?.[1];
124+
// Note: this should never happen since task queue functions are guarded by IAM.
125+
if (!token) {
126+
throw new https.HttpsError('unauthenticated', 'Unauthenticated');
127+
}
128+
// We skip authenticating the token since tq functions are guarded by IAM.
129+
const authToken = await https.unsafeDecodeIdToken(token);
130+
context.auth = {
130131
uid: authToken.uid,
131132
token: authToken,
132-
},
133-
};
133+
};
134+
}
134135

135136
const data: Req = https.decode(req.body.data);
136137
if (handler.length === 2) {

0 commit comments

Comments
 (0)