Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to expose Firebase apiKey to the public?

The Firebase Web-App guide states I should put the given apiKey in my Html to initialize Firebase:

// TODO: Replace with your project's customized code snippet <script src="https://www.gstatic.com/firebasejs/3.0.2/firebase.js"></script> <script>   // Initialize Firebase   var config = {     apiKey: '<your-api-key>',     authDomain: '<your-auth-domain>',     databaseURL: '<your-database-url>',     storageBucket: '<your-storage-bucket>'   };   firebase.initializeApp(config); </script> 

By doing so, the apiKey is exposed to every visitor.

What is the purpose of that key and is it really meant to be public?

like image 381
farmio Avatar asked May 27 '16 11:05

farmio


People also ask

Are Firebase projects public?

The content of the Firebase config file or object is considered public, including the app's platform-specific ID (Apple bundle ID or Android package name) and the Firebase project-specific values, like the API Key, project ID, Realtime Database URL, and Cloud Storage bucket name.

How do I get API key in Firebase?

Finding your API keys Firebase Apple Apps — Find an app's auto-matched API key in the Firebase config file, GoogleService-Info. plist , in the API_KEY field. Firebase Android Apps — Find an app's auto-matched API key in the Firebase config file, google-services.

Where can I find my Firebase API key and Authdomain?

You can find it in one of the following ways: Click Authentication and at the upper right side in that page, click Web setup. Otherwise Go to your project overview, click on '+ Add app' button, which you can find next to your project name.

How do I find my Firebase secret key?

In the Firebase console, open Settings > Service Accounts. Click Generate New Private Key, then confirm by clicking Generate Key.


1 Answers

The apiKey in this configuration snippet just identifies your Firebase project on the Google servers. It is not a security risk for someone to know it. In fact, it is necessary for them to know it, in order for them to interact with your Firebase project. This same configuration data is also included in every iOS and Android app that uses Firebase as its backend.

In that sense it is very similar to the database URL that identifies the back-end database associated with your project in the same snippet: https://<app-id>.firebaseio.com. See this question on why this is not a security risk: How to restrict Firebase data modification?, including the use of Firebase's server side security rules to ensure only authorized users can access the backend services.

If you want to learn how to secure all data access to your Firebase backend services is authorized, read up on the documentation on Firebase security rules. These rules control access to file storage and database access, and are enforced on the Firebase servers. So no matter if it's your code, or somebody else's code that uses you configuration data, it can only do what the security rules allow it to do.

For another explanation of what Firebase uses these values for, and for which of them you can set quotas, see the Firebase documentation on using and managing API keys.


If you'd like to reduce the risk of committing this configuration data to version control, consider using the SDK auto-configuration of Firebase Hosting. While the keys will still end up in the browser in the same format, they won't be hard-coded into your code anymore with that.


Update (May 2021): Thanks to the new feature called Firebase App Check, it is now actually possible to limit access to the backend services in your Firebase project to only those coming from iOS, Android and Web apps that are registered in that specific project.

You'll typically want to combine this with the user authentication based security described above, so that you have another shield against abusive users that do use your app.

By combining App Check with security rules you have both broad protection against abuse, and fine gained control over what data each user can access, while still allowing direct access to the database from your client-side application code.

like image 100
Frank van Puffelen Avatar answered Nov 14 '22 13:11

Frank van Puffelen