diff --git a/src/app.tsx b/src/app.tsx index 4f3e2e8e..c47d500c 100755 --- a/src/app.tsx +++ b/src/app.tsx @@ -41,6 +41,9 @@ import { redactBreadcrumb, redactEvent } from './util/sentryRedaction'; Sentry.init({ dsn: "https://8a606d82aa68850021fbfac2ffda30b5@o4509293310967808.ingest.de.sentry.io/4509293314113617", + // The dev server (`npm start`, also used by the Cypress runs in CI) + // must not report: its errors would eat the reserved Sentry volume + enabled: !__DEV__, // Our DPA rules out personal data in Sentry. Sentry v11 collects user // info (incl. IP address), cookies, headers and bodies by default, so // switch every data collection category off explicitly. diff --git a/src/modules/settings/watchSettingWhileAdmin.spec.ts b/src/modules/settings/watchSettingWhileAdmin.spec.ts index fa5da7ca..74615e0d 100644 --- a/src/modules/settings/watchSettingWhileAdmin.spec.ts +++ b/src/modules/settings/watchSettingWhileAdmin.spec.ts @@ -1,6 +1,6 @@ import {runSaga, stdChannel} from 'redux-saga'; import {onValue} from 'firebase/database'; -import firebase from '../../util/firebase'; +import firebase, {isSignedIn} from '../../util/firebase'; import {error} from '../../util/log'; import {FIREBASE_AUTHENTICATION_EVENT} from '../auth/actions'; import {watchSettingWhileAdmin} from './watchSettingWhileAdmin'; @@ -23,6 +23,11 @@ const authEvent = (authData: any) => ({ const adminLogin = () => authEvent({uid: 'admin-uid', admin: true}); const userLogin = () => authEvent({uid: 'user-uid', admin: false}); const logout = () => authEvent(null); +// Shaped like the errors Firebase passes to a cancelled listener +const permissionDenied = () => Object.assign( + new Error(`permission_denied at ${PATH}: Client doesn't have permission to access the desired data.`), + {code: 'PERMISSION_DENIED'} +); describe('modules', () => { describe('settings', () => { @@ -42,6 +47,7 @@ describe('modules', () => { beforeEach(() => { jest.clearAllMocks(); (firebase as jest.Mock).mockImplementation((path: string) => ({path})); + (isSignedIn as jest.Mock).mockReturnValue(true); unsubscribe = jest.fn(); (onValue as jest.Mock).mockReturnValue(unsubscribe); @@ -129,7 +135,7 @@ describe('modules', () => { it('logs and allows a new subscription when Firebase cancels the listener', () => { emit(adminLogin()); - const denied = new Error('permission_denied'); + const denied = permissionDenied(); lastListener().onCancel(denied); expect(error).toHaveBeenCalledWith(`Listener for ${PATH} was cancelled`, denied); @@ -137,6 +143,26 @@ describe('modules', () => { emit(adminLogin()); expect(onValue).toHaveBeenCalledTimes(2); }); + + it('does not log a permission_denied cancel after the sign-out', () => { + emit(adminLogin()); + (isSignedIn as jest.Mock).mockReturnValue(false); + lastListener().onCancel(permissionDenied()); + + expect(error).not.toHaveBeenCalled(); + + emit(adminLogin()); + expect(onValue).toHaveBeenCalledTimes(2); + }); + + it('logs other cancels after the sign-out', () => { + emit(adminLogin()); + (isSignedIn as jest.Mock).mockReturnValue(false); + const unavailable = Object.assign(new Error('unavailable'), {code: 'UNAVAILABLE'}); + lastListener().onCancel(unavailable); + + expect(error).toHaveBeenCalledWith(`Listener for ${PATH} was cancelled`, unavailable); + }); }); }); }); diff --git a/src/modules/settings/watchSettingWhileAdmin.ts b/src/modules/settings/watchSettingWhileAdmin.ts index feae9f75..dd398f70 100644 --- a/src/modules/settings/watchSettingWhileAdmin.ts +++ b/src/modules/settings/watchSettingWhileAdmin.ts @@ -2,11 +2,18 @@ import {END, eventChannel, Task} from 'redux-saga'; import {cancel, fork, put, take, takeEvery} from 'redux-saga/effects'; import {onValue} from 'firebase/database'; import {FIREBASE_AUTHENTICATION_EVENT} from '../auth/actions'; -import firebase from '../../util/firebase'; +import firebase, {isSignedIn} from '../../util/firebase'; import {error} from '../../util/log'; type LoadedActionCreator = (value: any) => { type: string }; +// Signing out makes Firebase cancel the admin-only listeners with +// permission_denied, often before the sign-out reaches the sagas that +// would remove them. Such a cancel is expected and not worth reporting. +function isDeniedAfterSignOut(e: unknown) { + return (e as any)?.code === 'PERMISSION_DENIED' && !isSignedIn(); +} + // Emits `loaded(value)` for every change at `path`. Closing the channel // removes the Firebase listener. If Firebase cancels the listener (e.g. // permission denied), the error is logged and the channel ends. @@ -15,7 +22,9 @@ export function createSettingChannel(path: string, loaded: LoadedActionCreator) firebase(path), snapshot => emit(loaded(snapshot.val())), e => { - error(`Listener for ${path} was cancelled`, e); + if (!isDeniedAfterSignOut(e)) { + error(`Listener for ${path} was cancelled`, e); + } emit(END); } )); diff --git a/src/util/firebase.spec.ts b/src/util/firebase.spec.ts index f6fce084..e86caca5 100644 --- a/src/util/firebase.spec.ts +++ b/src/util/firebase.spec.ts @@ -45,6 +45,7 @@ import firebaseDefault, { unauth, loadValue, getIdToken, + isSignedIn, } from './firebase'; describe('util/firebase', () => { @@ -307,4 +308,15 @@ describe('util/firebase', () => { expect(result).toBe(mockToken); }); }); + + describe('isSignedIn', () => { + it('returns true when a user is signed in', () => { + expect(isSignedIn()).toBe(true); + }); + + it('returns false when nobody is signed in', () => { + mockAuth.currentUser = null; + expect(isSignedIn()).toBe(false); + }); + }); }); diff --git a/src/util/firebase.ts b/src/util/firebase.ts index e8d5a8df..f65225fb 100644 --- a/src/util/firebase.ts +++ b/src/util/firebase.ts @@ -114,6 +114,11 @@ export function getIdToken() { return getAuth().currentUser!.getIdToken(); } +export function isSignedIn() { + initialize(); + return !!getAuth().currentUser; +} + export default firebase; if ((window as any).Cypress) {