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

Fix throttling for App Check to prevent unnecessary requests to backend #6439

Merged
merged 4 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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': minor
dwyfrequency marked this conversation as resolved.
Show resolved Hide resolved
---

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, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the test!

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
9 changes: 7 additions & 2 deletions packages/app-check/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
this._heartbeatServiceProvider!
);
} catch (e) {
if ((e as FirebaseError).code === AppCheckError.FETCH_STATUS_ERROR) {
console.log({ coder: (e as FirebaseError).code });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this left over from debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Good catch. Removing it now

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 +170,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