Skip to content

Commit

Permalink
Fix throttling for App Check to prevent unnecessary requests to backe…
Browse files Browse the repository at this point in the history
…nd (#6439)

* Switch check for FirebaseError code to use includes

* Add changeset

* Update changeset to a patch

* Remove debugging console.log
  • Loading branch information
dwyfrequency committed Jul 18, 2022
1 parent 39f4635 commit f36d627
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-sheep-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/app-check': patch
---

Fix logic to trigger app check throttling
20 changes: 8 additions & 12 deletions packages/app-check/src/internal-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ import * as util from './util';
import { logger } from './logger';
import { getState, clearState, setState, getDebugState } from './state';
import { AppCheckTokenListener } from './public-types';
import { Deferred, FirebaseError } from '@firebase/util';
import { Deferred } from '@firebase/util';
import { ReCaptchaEnterpriseProvider, ReCaptchaV3Provider } from './providers';
import { AppCheckService } from './factory';
import { ListenerType } from './types';
import { AppCheckError } from './errors';
import { AppCheckError, ERROR_FACTORY } from './errors';

const fakeRecaptchaToken = 'fake-recaptcha-token';
const fakeRecaptchaAppCheckToken = {
Expand Down Expand Up @@ -396,11 +396,9 @@ describe('internal api', () => {
const warnStub = stub(logger, 'warn');
stub(client, 'exchangeToken').returns(
Promise.reject(
new FirebaseError(
AppCheckError.FETCH_STATUS_ERROR,
'test error msg',
{ httpStatus: 503 }
)
ERROR_FACTORY.create(AppCheckError.FETCH_STATUS_ERROR, {
httpStatus: 503
})
)
);

Expand All @@ -424,11 +422,9 @@ describe('internal api', () => {
const warnStub = stub(logger, 'warn');
stub(client, 'exchangeToken').returns(
Promise.reject(
new FirebaseError(
AppCheckError.FETCH_STATUS_ERROR,
'test error msg',
{ httpStatus: 403 }
)
ERROR_FACTORY.create(AppCheckError.FETCH_STATUS_ERROR, {
httpStatus: 403
})
)
);

Expand Down
8 changes: 6 additions & 2 deletions packages/app-check/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
this._heartbeatServiceProvider!
);
} catch (e) {
if ((e as FirebaseError).code === AppCheckError.FETCH_STATUS_ERROR) {
if (
(e as FirebaseError).code?.includes(AppCheckError.FETCH_STATUS_ERROR)
) {
this._throttleData = setBackoff(
Number((e as FirebaseError).customData?.httpStatus),
this._throttleData
Expand Down Expand Up @@ -167,7 +169,9 @@ export class ReCaptchaEnterpriseProvider implements AppCheckProvider {
this._heartbeatServiceProvider!
);
} catch (e) {
if ((e as FirebaseError).code === AppCheckError.FETCH_STATUS_ERROR) {
if (
(e as FirebaseError).code?.includes(AppCheckError.FETCH_STATUS_ERROR)
) {
this._throttleData = setBackoff(
Number((e as FirebaseError).customData?.httpStatus),
this._throttleData
Expand Down

0 comments on commit f36d627

Please sign in to comment.