Skip to content

Commit f36d627

Browse files
authored
Fix throttling for App Check to prevent unnecessary requests to backend (#6439)
* Switch check for FirebaseError code to use includes * Add changeset * Update changeset to a patch * Remove debugging console.log
1 parent 39f4635 commit f36d627

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

.changeset/cyan-sheep-battle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/app-check': patch
3+
---
4+
5+
Fix logic to trigger app check throttling

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ import * as util from './util';
4141
import { logger } from './logger';
4242
import { getState, clearState, setState, getDebugState } from './state';
4343
import { AppCheckTokenListener } from './public-types';
44-
import { Deferred, FirebaseError } from '@firebase/util';
44+
import { Deferred } from '@firebase/util';
4545
import { ReCaptchaEnterpriseProvider, ReCaptchaV3Provider } from './providers';
4646
import { AppCheckService } from './factory';
4747
import { ListenerType } from './types';
48-
import { AppCheckError } from './errors';
48+
import { AppCheckError, ERROR_FACTORY } from './errors';
4949

5050
const fakeRecaptchaToken = 'fake-recaptcha-token';
5151
const fakeRecaptchaAppCheckToken = {
@@ -396,11 +396,9 @@ describe('internal api', () => {
396396
const warnStub = stub(logger, 'warn');
397397
stub(client, 'exchangeToken').returns(
398398
Promise.reject(
399-
new FirebaseError(
400-
AppCheckError.FETCH_STATUS_ERROR,
401-
'test error msg',
402-
{ httpStatus: 503 }
403-
)
399+
ERROR_FACTORY.create(AppCheckError.FETCH_STATUS_ERROR, {
400+
httpStatus: 503
401+
})
404402
)
405403
);
406404

@@ -424,11 +422,9 @@ describe('internal api', () => {
424422
const warnStub = stub(logger, 'warn');
425423
stub(client, 'exchangeToken').returns(
426424
Promise.reject(
427-
new FirebaseError(
428-
AppCheckError.FETCH_STATUS_ERROR,
429-
'test error msg',
430-
{ httpStatus: 403 }
431-
)
425+
ERROR_FACTORY.create(AppCheckError.FETCH_STATUS_ERROR, {
426+
httpStatus: 403
427+
})
432428
)
433429
);
434430

packages/app-check/src/providers.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
8080
this._heartbeatServiceProvider!
8181
);
8282
} catch (e) {
83-
if ((e as FirebaseError).code === AppCheckError.FETCH_STATUS_ERROR) {
83+
if (
84+
(e as FirebaseError).code?.includes(AppCheckError.FETCH_STATUS_ERROR)
85+
) {
8486
this._throttleData = setBackoff(
8587
Number((e as FirebaseError).customData?.httpStatus),
8688
this._throttleData
@@ -167,7 +169,9 @@ export class ReCaptchaEnterpriseProvider implements AppCheckProvider {
167169
this._heartbeatServiceProvider!
168170
);
169171
} catch (e) {
170-
if ((e as FirebaseError).code === AppCheckError.FETCH_STATUS_ERROR) {
172+
if (
173+
(e as FirebaseError).code?.includes(AppCheckError.FETCH_STATUS_ERROR)
174+
) {
171175
this._throttleData = setBackoff(
172176
Number((e as FirebaseError).customData?.httpStatus),
173177
this._throttleData

0 commit comments

Comments
 (0)