Skip to content

Initialize App Check debug mode in initializeAppCheck #5512

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

Merged
merged 6 commits into from
Sep 23, 2021
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
39 changes: 38 additions & 1 deletion packages/app-check/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import '../test/setup';
import { expect } from 'chai';
import { spy, stub } from 'sinon';
import { match, spy, stub } from 'sinon';
import {
setTokenAutoRefreshEnabled,
initializeAppCheck,
Expand All @@ -38,10 +38,12 @@ import * as logger from './logger';
import * as client from './client';
import * as storage from './storage';
import * as internalApi from './internal-api';
import * as indexeddb from './indexeddb';
import { deleteApp, FirebaseApp } from '@firebase/app';
import { CustomProvider, ReCaptchaV3Provider } from './providers';
import { AppCheckService } from './factory';
import { AppCheckToken } from './public-types';
import { getDebugToken } from './debug';

describe('api', () => {
let app: FirebaseApp;
Expand Down Expand Up @@ -118,6 +120,41 @@ describe('api', () => {
})
).to.equal(appCheckInstance);
});
it('starts debug mode on first call', async () => {
const fakeWrite = (): Promise<void> => Promise.resolve();
const writeToIndexedDBStub = stub(
indexeddb,
'writeDebugTokenToIndexedDB'
).callsFake(fakeWrite);
stub(indexeddb, 'readDebugTokenFromIndexedDB').callsFake(() =>
Promise.resolve(undefined)
);
const logStub = stub(logger.logger, 'warn');
const consoleStub = stub(console, 'log');
self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
});
await fakeWrite();
const token = writeToIndexedDBStub.args[0];
expect(logStub).to.not.be.called;
expect(consoleStub.args[0][0]).to.include(token);
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
});
it('warns about debug mode on second call', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

What does it test?
I think we need a test to verify initializeDebugMode is called only once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh wow, the test caught the fact that I was not setting the initialize property... thanks.

const logStub = stub(logger.logger, 'warn');
self.FIREBASE_APPCHECK_DEBUG_TOKEN = 'abcdefg';
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
});
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
});
const token = await getDebugToken();
expect(token).to.equal('abcdefg');
expect(logStub).to.be.calledWith(match('abcdefg'));
self.FIREBASE_APPCHECK_DEBUG_TOKEN = undefined;
});

it('initialize reCAPTCHA when a ReCaptchaV3Provider is provided', () => {
const initReCAPTCHAStub = stub(reCAPTCHA, 'initialize').returns(
Expand Down
20 changes: 20 additions & 0 deletions packages/app-check/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
isValid
} from './internal-api';
import { readTokenFromStorage } from './storage';
import { getDebugToken, initializeDebugMode, isDebugMode } from './debug';
import { logger } from './logger';

declare module '@firebase/component' {
interface NameServiceMapping {
Expand Down Expand Up @@ -65,6 +67,21 @@ export function initializeAppCheck(
options.isTokenAutoRefreshEnabled &&
initialOptions.provider.isEqual(options.provider)
) {
// Log a warning if `initializeAppCheck()` is called after the first time
// if this app is still in debug mode, and show the token.
// If it's the first time, `initializeDebugMode()` already logs a message.
if (isDebugMode()) {
logger.warn(
`App Check is in debug mode. To turn off debug mode, unset ` +
`the global variable FIREBASE_APPCHECK_DEBUG_TOKEN and ` +
`restart the app.`
);
// Make this a separate console statement so user will at least have the
// first message if the token promise doesn't resolve in time.
void getDebugToken().then(token =>
logger.warn(`Debug token is ${token}.`)
);
}
return existingInstance;
} else {
throw ERROR_FACTORY.create(AppCheckError.ALREADY_INITIALIZED, {
Expand All @@ -73,6 +90,9 @@ export function initializeAppCheck(
}
}

// Only read global variable on first call to `initializeAppCheck()`.
initializeDebugMode();

const appCheck = provider.initialize({ options });
_activate(app, options.provider, options.isTokenAutoRefreshEnabled);

Expand Down
2 changes: 0 additions & 2 deletions packages/app-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
} from '@firebase/component';
import { _AppCheckComponentName } from './public-types';
import { factory, internalFactory } from './factory';
import { initializeDebugMode } from './debug';
import { _AppCheckInternalComponentName } from './types';
import { name, version } from '../package.json';

Expand Down Expand Up @@ -82,4 +81,3 @@ function registerAppCheck(): void {
}

registerAppCheck();
initializeDebugMode();