Search code examples
firebasegoogle-cloud-firestorefirefox-addonfirefox-addon-webextensions

Firebase Firestore access within Firefox browser extension content script fails


I have a browser extension that attempts to interact with a Firestore DB from within a content script. In Chrome this functions perfectly with no issues, but in Firefox it fails with a cryptic error:

@firebase/firestore: Firestore (8.8.1): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

Looking at the network tab in firefox/chrome devtools it's clear that no requests are being made to the Firestore backend which explains why this error is being thrown, but I can't figure out why this is happening.

I've been unable to identify any obvious differences between network request handling between chrome & firefox content scripts.

I should also note that when I try to access firestore from within the Firebase background page it works as expected, it's only within the content script that there are issues.

A minimal repro can be found here & the code that is being run in the content script can be found here.


Solution

  • The reason it works in Chrome but fails on Firefox is because they have different policies for how they treat network requests inside content scripts.

    In Chrome, content script requests happen in the context of the page.

    In Firefox, the requests happen in the context of the extension.

    This means that you'll need to add the sites you're making requests to in the permissions key inside manifest.json to have the requests succeed.