Skip to content

Commit 2a0d254

Browse files
authored
Browser Extension Check for Analytics Module (#3555)
* 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
1 parent 7f9b3d9 commit 2a0d254

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

.changeset/spicy-masks-sort.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@firebase/firebase": patch
3+
"@firebase/analytics": patch
4+
---
5+
6+
Added Browser Extension check for Firebase Analytics. analytics.isSupported() will now return Promise<false> for extension environments.

packages/analytics/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import { ERROR_FACTORY, AnalyticsError } from './src/errors';
3535
import {
3636
isIndexedDBAvailable,
3737
validateIndexedDBOpenable,
38-
areCookiesEnabled
38+
areCookiesEnabled,
39+
isBrowserExtension
3940
} from '@firebase/util';
4041
import { name, version } from './package.json';
4142

@@ -109,19 +110,25 @@ declare module '@firebase/app-types' {
109110
}
110111

111112
/**
112-
* this is a public static method provided to users that wraps three different checks:
113+
* this is a public static method provided to users that wraps four different checks:
113114
*
115+
* 1. check if it's not a browser extension environment.
114116
* 1. check if cookie is enabled in current browser.
115-
* 2. check if IndexedDB is supported by the browser environment.
116-
* 3. check if the current browser context is valid for using IndexedDB.
117+
* 3. check if IndexedDB is supported by the browser environment.
118+
* 4. check if the current browser context is valid for using IndexedDB.
119+
*
117120
*/
118121
async function isSupported(): Promise<boolean> {
122+
if (isBrowserExtension()) {
123+
return false;
124+
}
119125
if (!areCookiesEnabled()) {
120126
return false;
121127
}
122128
if (!isIndexedDBAvailable()) {
123129
return false;
124130
}
131+
125132
try {
126133
const isDBOpenable: boolean = await validateIndexedDBOpenable();
127134
return isDBOpenable;

packages/analytics/src/errors.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const enum AnalyticsError {
2525
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed',
2626
INDEXED_DB_UNSUPPORTED = 'indexedDB-unsupported',
2727
INVALID_INDEXED_DB_CONTEXT = 'invalid-indexedDB-context',
28-
COOKIES_NOT_ENABLED = 'cookies-not-enabled'
28+
COOKIES_NOT_ENABLED = 'cookies-not-enabled',
29+
INVALID_ANALYTICS_CONTEXT = 'invalid-analytics-context'
2930
}
3031

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

5659
interface ErrorParams {

packages/analytics/src/factory.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import { FirebaseInstallations } from '@firebase/installations-types';
4141
import {
4242
isIndexedDBAvailable,
4343
validateIndexedDBOpenable,
44-
areCookiesEnabled
44+
areCookiesEnabled,
45+
isBrowserExtension
4546
} from '@firebase/util';
4647

4748
/**
@@ -122,6 +123,9 @@ export function factory(
122123
app: FirebaseApp,
123124
installations: FirebaseInstallations
124125
): FirebaseAnalytics {
126+
if (isBrowserExtension()) {
127+
throw ERROR_FACTORY.create(AnalyticsError.INVALID_ANALYTICS_CONTEXT);
128+
}
125129
if (!areCookiesEnabled()) {
126130
throw ERROR_FACTORY.create(AnalyticsError.COOKIES_NOT_ENABLED);
127131
}

0 commit comments

Comments
 (0)