Skip to content

Commit b9209dc

Browse files
authored
fix(app-check): Prevent redundant exchangeToken calls in debug mode (#9187)
1 parent a4897a6 commit b9209dc

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/app-check': patch
3+
---
4+
5+
Prevent redundant exchangeToken calls in debug mode

packages/app-check/src/internal-api.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,31 @@ describe('internal api', () => {
630630
expect(token).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
631631
});
632632

633+
it('exchanges debug token only once if debug mode with no cached token', async () => {
634+
const exchangeTokenStub: SinonStub = stub(
635+
client,
636+
'exchangeToken'
637+
).returns(Promise.resolve(fakeRecaptchaAppCheckToken));
638+
const debugState = getDebugState();
639+
debugState.enabled = true;
640+
debugState.token = new Deferred();
641+
debugState.token.resolve('my-debug-token');
642+
const appCheck = initializeAppCheck(app, {
643+
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
644+
});
645+
const appCheckService = appCheck as AppCheckService;
646+
const [token1, token2] = await Promise.all([
647+
getToken(appCheckService),
648+
getToken(appCheckService)
649+
]);
650+
expect(exchangeTokenStub.args[0][0].body['debug_token']).to.equal(
651+
'my-debug-token'
652+
);
653+
expect(token1).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
654+
expect(token2).to.deep.equal({ token: fakeRecaptchaAppCheckToken.token });
655+
expect(exchangeTokenStub).to.be.calledOnce;
656+
});
657+
633658
it('throttles for a period less than 1d on 503', async () => {
634659
// More detailed check of exponential backoff in providers.test.ts
635660
const appCheck = initializeAppCheck(app, {

packages/app-check/src/internal-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ export async function getToken(
118118
*/
119119
if (isDebugMode()) {
120120
try {
121+
const debugToken = await getDebugToken();
121122
// Avoid making another call to the exchange endpoint if one is in flight.
122123
if (!state.exchangeTokenPromise) {
123124
state.exchangeTokenPromise = exchangeToken(
124-
getExchangeDebugTokenRequest(app, await getDebugToken()),
125+
getExchangeDebugTokenRequest(app, debugToken),
125126
appCheck.heartbeatServiceProvider
126127
).finally(() => {
127128
// Clear promise when settled - either resolved or rejected.

0 commit comments

Comments
 (0)