I am really concerned about the security of my data which I would be storing in Firestore, I want to know If someone somehow can extract google-services.json file from my android app or use some other tools to access my Firestore database. Is it possible If yes how can I prevent it?
If your app uses Firebase services and makes use of google-services.json, then yes, anyone can extract that data.
The issue for app developers isn't how to lock down that data (impossible), the issue is how to protect database and storage data to only those users who should be able to access it. The only solution for this is to use Firebase Authentication to verify the identity of people accessing your app, and use security rules to determine who should be able to read and write data.
There are currently no alternatives to this. If you don't require users to sign in using Firebase Auth, and your security rules allow universal access to data, then anyone on the internet who knows the name of your project will be able to access that data. Again, there are no exceptions to this. If your data is readable and writable to the world, then you will need to accept the potential billing consequences for this.
Again, learn about security rules for the products you use: Realtime Database, Firestore, and Cloud Storage.
If security rules are not feasible for your requirements, then you will need to set up a backend service that you securely control, and route all client access through that backend. You will want to make clients pass a Firebase Authentication token to your endpoint so it can validate access using the Firebase Admin SDK.
You should setup Cloud Firestore Security Rules to limit access to the data. Here is an example:
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Take a look at Get started with Cloud Firestore Security Rules for more details.
Related
I know this is a duplicate question but none of them helped me
I use Firestore for storing little amount of data... which will be deleted after some minutes which contains no personal information. I have my own authentication system for logging users in. The users have read and write permission in the Firestore. The problem is application is in production and I keep getting Firebase warning mail that 'Your Cloud Firestore database has insecure rules'.
i have published Firebase rule as
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
I have gone through many reference and I figured it out that for secure data we have to you it's own authentication. or is there any solution for it..?
If you are not using Firebase Authentication, then there is no solution for secure per-user access. If you have a different auth system, you will have to integrate Firebase into that using a custom auth provider.
If you have to allow users to read-write with security rules. You can use the cloud function. Cloud function has administrative access. It bypassing the security rules. You have to create Rest API for Firestore read-write operation. Cloud functions doest not require user authentication.
This question already has answers here:
How to make firebase storage only available to users of the app
(2 answers)
Closed 3 years ago.
I need to access read/write images from firebase storage. There is no authentication facility. How can I restrict the data management through app only.
Existing rules to provide public access is
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
There is no way to limit access to the files through the Firebase SDK to your application only. That approach simply doesn't work in a cloud-based environment.
Also see:
Restrict access to Firebase storage so only my app can access it
How to allow only my app to access firebase without a login?
Locking down Firebase DB access to specific apps
How to make firebase storage only available to users of the app (which I just now found, and of which your question is a duplicate)
While these are usually about other Firebase products, the same logic applies to Storage.
You will need to describe in your security rules what can be done to the files in Cloud Storage through the Firebase API, and then (if needed) who can do those things by using Firebase Authentication too.
Note that using Firebase Authentication doesn't necessarily require the user to provide credentials, as Firebase provides an option to sign in anonymously. This simply gives each user a secure, unique ID, which you could for example use to allow users only access to files they uploaded themselves (and many other use-cases).
As per docs
Cloud Storage stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud
Although firebase directly doesn't support that but google cloud has the option to restrict API to specific application package name signatures only. I tried for MAP sdk, but theoretically storage should work also.
I am creating Android application with Firestore. My app does not require authentication. Is there any security rule to allows everyone read & write to firestore, but only via my app?
I have tried to find some rules, but each of them based on authentication.
Thank you for your help!
No, you can't limit access to your Cloud Firestore only to your application.
Since your application needs to know all the details that are needed to access the database, a malicious user can take those details and replicate them with code of their own.
To properly secure access to your database, you'll have to use Firebase's security rules. These are enforced on the server, so can't be by-passed by a malicious user. The logic here is that as long as the interactions with the database follow the rules you've set up, it doesn't really matter who wrote the code.
Also see:
How to enable access of firestore data to my nativescript app only?
Why is it okay to allow writes into Firebase from the client side?
Is it safe to use Firestore and its features via client only?
I have an android app which is using Firestore as database. I need to make it secure but I am not using authentication service as it is not required.
I was thinking if I can sent a unique ID from android app which can be recognised in Firestore rules and only users(people using my app) can read or write to Firestore.
Below is my security rules -
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow readwrite ;
}
}
}
I expect my Firestore database to be safe from non-users.
I was thinking if I can sent a unique ID
You can't pass arbitrary values to security rules. To send that securely and get it into the security rules, you will have to pass it into the user's authentication token as a custom claim.
But even if you do that, what will keep a malicious user from finding that same secret values (it must be available in your app after all), and passing it into the security rules in the same way.
Coming to think of it, anonymous authentication and custom claims may be your solution. Use anonymous authentication on the client to generate a UID for the user, then (on a trusted environment, such as your development machine, a server you control, or Cloud Functions) give that user a custom claim, and check for that custom claim in your security rules.
I'm creating app with Firestore database only for Android platform. I want to restrict access to my Firestore database from Rest API.
Is there any way?
There is no way to do this. Firestore does not care where access comes from. It could come from any client SDK, from any server SDK, or the REST API.
The only way to control access from a client SDK is to use security rules along with Firebase Authentication to limit access based on the user's identity. Server SDK access is controlled using service accounts that are granted permission to read and write the database (and they bypass security rules altogether).