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?
I am not convinced to expose security/config keys to client. I would not call it secure, not because some one can steal all private information from first day, because someone can make excessive request, and drain your quota and make you owe to Google a lot of money.
You need to think about many concepts from restricting people not to access where they are not supposed to be, DOS attacks etc.
I would more prefer the client first will hit to your web server, there you put what ever first hand firewall, captcha , cloudflare, custom security in between the client and server, or between server and firebase and you are good to go. At least you can first stop suspect activity before it reaches to firebase. You will have much more flexibility.
I only see one good usage scenario for using client based config for internal usages. For example, you have internal domain, and you are pretty sure outsiders cannot access there, so you can setup environment like browser -> firebase type.
The apiKey essentially 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.
In that sense it is very similar to the database URL that Firebase has historically been used to identify the back-end:
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.Building on the answers of prufrofro and Frank van Puffelen here, I put together this solution that doesn't prevent scraping, but can make it harder to use your API key.
Warning: To get your data, even with this method, one can for example simply open the JS console in Chrome and type:
Only the database security rules can protect your data.
Nevertheless, I restricted my production API key use to my domain name like this:
projectname.firebaseapp.com/*
)Now the app will only work on this domain name. So I created another API Key that will be private for localhost developement.
This one is not restricted, so make sure you keep it private.
I use Webpack to build my production app. In order to make sure I don't publish this new unrestricted API key by mistake, I put it inside my
index.html
just as you would normally do with your API key. Then, inside mywebpack.production.config.js
file, I replace the key every timeindex.html
is copied to the production build, like this:By default, as mentioned by Emmanuel Campos, Firebase only whitelists
localhost
and your Firebase hosting domain.