Skip to content

Commit

Permalink
Browser Extension Check for Analytics Module (#3555)
Browse files Browse the repository at this point in the history
* added browser extension check to analytics module; prevent initialization if under browser extension context

* removed unnecessary imports

* Create spicy-masks-sort.md

* changed error message

* updated changeset

* updated changeset
  • Loading branch information
XuechunHou committed Aug 5, 2020
1 parent 7f9b3d9 commit 2a0d254
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/spicy-masks-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/firebase": patch
"@firebase/analytics": patch
---

Added Browser Extension check for Firebase Analytics. analytics.isSupported() will now return Promise<false> for extension environments.
15 changes: 11 additions & 4 deletions packages/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import { ERROR_FACTORY, AnalyticsError } from './src/errors';
import {
isIndexedDBAvailable,
validateIndexedDBOpenable,
areCookiesEnabled
areCookiesEnabled,
isBrowserExtension
} from '@firebase/util';
import { name, version } from './package.json';

Expand Down Expand Up @@ -109,19 +110,25 @@ declare module '@firebase/app-types' {
}

/**
* this is a public static method provided to users that wraps three different checks:
* this is a public static method provided to users that wraps four different checks:
*
* 1. check if it's not a browser extension environment.
* 1. check if cookie is enabled in current browser.
* 2. check if IndexedDB is supported by the browser environment.
* 3. check if the current browser context is valid for using IndexedDB.
* 3. check if IndexedDB is supported by the browser environment.
* 4. check if the current browser context is valid for using IndexedDB.
*
*/
async function isSupported(): Promise<boolean> {
if (isBrowserExtension()) {
return false;
}
if (!areCookiesEnabled()) {
return false;
}
if (!isIndexedDBAvailable()) {
return false;
}

try {
const isDBOpenable: boolean = await validateIndexedDBOpenable();
return isDBOpenable;
Expand Down
7 changes: 5 additions & 2 deletions packages/analytics/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const enum AnalyticsError {
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed',
INDEXED_DB_UNSUPPORTED = 'indexedDB-unsupported',
INVALID_INDEXED_DB_CONTEXT = 'invalid-indexedDB-context',
COOKIES_NOT_ENABLED = 'cookies-not-enabled'
COOKIES_NOT_ENABLED = 'cookies-not-enabled',
INVALID_ANALYTICS_CONTEXT = 'invalid-analytics-context'
}

const ERRORS: ErrorMap<AnalyticsError> = {
Expand All @@ -50,7 +51,9 @@ const ERRORS: ErrorMap<AnalyticsError> = {
'Wrap initialization of analytics in analytics.isSupported() ' +
'to prevent initialization in unsupported environments',
[AnalyticsError.COOKIES_NOT_ENABLED]:
'Cookies are not enabled in this browser environment. Analytics requires cookies to be enabled.'
'Cookies are not enabled in this browser environment. Analytics requires cookies to be enabled.',
[AnalyticsError.INVALID_ANALYTICS_CONTEXT]:
'Firebase Analytics is not supported in browser extensions.'
};

interface ErrorParams {
Expand Down
6 changes: 5 additions & 1 deletion packages/analytics/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import { FirebaseInstallations } from '@firebase/installations-types';
import {
isIndexedDBAvailable,
validateIndexedDBOpenable,
areCookiesEnabled
areCookiesEnabled,
isBrowserExtension
} from '@firebase/util';

/**
Expand Down Expand Up @@ -122,6 +123,9 @@ export function factory(
app: FirebaseApp,
installations: FirebaseInstallations
): FirebaseAnalytics {
if (isBrowserExtension()) {
throw ERROR_FACTORY.create(AnalyticsError.INVALID_ANALYTICS_CONTEXT);
}
if (!areCookiesEnabled()) {
throw ERROR_FACTORY.create(AnalyticsError.COOKIES_NOT_ENABLED);
}
Expand Down

0 comments on commit 2a0d254

Please sign in to comment.