I am using Firebase Realtime Database for chat functionality in my app. Now we are ready to launch our app so we should fix this issue. xxxx-xxxx-4458' has insecure rules. In official documentation and other places i have found only solution where we need to use firebase auth for validation, But our main database and login process works on our own server and we are using firebase realtime chat as only for chat purpose, so we are not using any firebase authentications so we are still not able to fix issues.
We've detected the following issue(s) with your security rules:
any user can read your entire database
any user can write to your entire database
So Is there is any other way to secure our database without using firebase authentications.
Our Firebase Implementations.
We are using our own server for all the user login,sessions and user data. User login and validate is perform by our own server. That's why we don't use firebase for any other app functions than Real time chat.
As we are not using firebase auth for user validation. It's not possible by us to secure realtime database. User login,registration,sessions,validations all perform by our own server and after validations from our own server then user can start sending message with realtime database.
Our current rules
{
"rules": {
".read": true,
".write": true
}
}
Question:
Are we already secured from outsider attack(non-app user). If no then how we can make our database secure in our scenario?.
Update: That's How my database arranged.
Now on successful login on our server, I am generating JWT and using it as
mAuth.signInWithCustomToken(mCustomToken)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success
} else {
// If sign in fails
Toast.makeText(CustomAuthActivity.this, "Authentication failed.",
}
}
});
I don't understand where/how to validate that token on firebase Auth. Please put insight on it.
With your current rules anyone in the world can wipe your database with a single API call. They can also read the entire database with a single API call. Neither of those are likely use-cases that your app requires, so I'd say your app is currently not secure.
If you want to properly secure access based on the user identity, you can inform Firebase Authentication of the profile of the user in your own identity system by implementing custom authentication. Once you have that implemented, the auth variable in your security rule will contain the information from your own user system, and you can then use that to secure access to the data.
Even if you know nothing about the user though, you can already secure your app better than what you currently have, by writing rules that fit your use-cases.
For example, since you have a chat app, you likely have a list of chat messages, and users append new messages to this list. Instead of saying that everyone can do whatever they want to the root of your database, you can only allow them to post chat messages with something like:
{
"rules": {
"chat": {
".read": true,
"$message_id": {
".write": true
}
}
}
}
So now, users can only read the /chat node, and they can only write specific nodes under it. Just this simple change already rules out a whole lot of abuse scenarios.
One step better, would be to validate that the chat messages are of the structure that you expect. For example, if the message have a user name, timestamp, and a text message, that could be:
{
"rules": {
"chat": {
".read": true,
"$message_id": {
".write": true,
".validate": "newData.hasChildren('name', 'timestamp', 'text')"
}
}
}
}
At this point you should note that these rules reflect some of your application code, which is normal: your security rules should only allow what your application code needs, and nothing more. This is known as the principle of least privilege, and is a common security practice.
Finally, you should probably also consider using Firebase App Check which can also prevent a lot of abuse by folks who are not using your application to access the database. Note that this is not a guarantee though, so you'll want to combine App Check for broad protection with security rules for fine-grained control.
Some more resources:
Firebase email saying my realtime database has insecure rules
Firebase says that my rules are insecure, why?
Issue with my Firebase realtime data base security rules
Firebase Rules Write Permissions
Related
Using firebase rules as:
{
"rules": {
".read": true,
".write": true
}
}
means that everyone inside my application can read/write the firebase resource or means everyone including any request not necessary comming from my application can read/write?
With these rules, I can:
Get your entire database with a single URL, which you actually ship in application source code. I don't even need to use your app for this, I can just do https://<yourdatabaseURL>/.json and get it all.
Wipe your entire database with a single line of code, from a tool as simple as the JavaScript console of my browser.
So yeah, it's pretty much as insecure as all the reports make it out to be.
Since you have to include the URL in your app in order to be able to access database, leaving the rules like this is just asking for problems.
You should secure your database by using Firebase App Check to make it harder to access the database outside of your application, and then implement proper security rules to have fine-grained access control.
Ideally you should:
Start with the exact opposite rules, that deny all access, then
Implement the first small use-case of your application in code.
Watch it get rejected by the security rules.
Change your rules to allow only that one use-case, and nothing else.
Go on to the next use-case.
This is known as the principle of least privilege and is key to protecting the data.
I recommend also checking out these other questions on the same (really broad) topic:
Is it safe to expose Firebase apiKey to the public?
Does Firebase App Check discard the need for implementing Security Rules?
Firebase Permission Denied
Firebase email saying my realtime database has insecure rules
I have used Firebase Firestore for database storage. I had a scenario in which i need to manage users login and logout using phone number and OTP. and as Firebase authentication manages authentication using email id or if you want to use it with phone number every time you need to verify it using OTP which was not feasible for my application so, I created my own process of authentication in which I am using OTP authentication once and I store users data to the collection and then after when users login I directly check it from collection.
It is perfectly working now but as I am facing issues while changing security rules as I want to restrict user in reading database. But I don't know how to write it as I have found many solution till now but that all were having auth in request but as I am not using auth I may not have auth in request and could find it null.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write : if true;
}
}
}
I have given read and write access to all the collection so, anyone having key can access db but I want to restrict it using security rules and I have kind of found a way of restricting user when creating or updating as I will be getting resource when requesting for set or update but in case of reading or getting data it might not have resources as i am not passing any data . So please help me if you know how can I write such rules. Thanks In Advance...
To allow only users that are authentication with Firebase access to data, you rules would be:
allow read, write: if request.auth.uid != null;
It isn't possible to do the same without Firebase Authentication, as the only way to pass data about the user into security rules is through Firebase Authentication.
If you have your own authentication mechanism, you can implement that as a custom provider within Firebase Authentication. In this approach you use server-side code to generate a Firebase token based on the information you have about the user, and the information from that token then becomes available in the security rules under auth.token.
Since I want to fetch data without authentication for my app, my security rules look like this:
{
"rules": {
".read": true,
".write": false
}
}
But Firebase said if someone has this URL he can fetch my data. How could I save my data. My app doesn't require authentication. How could I resist other, or what configuration I can set so only my app can fetch it.
In simple words,
No one should allow to fetch my app data(firestore) without my app. Where my app doesn't require authentication.
What you're asking for isn't possible. These things are all exactly the same:
Accessing data via client SDK without authentication
Accessing data with the REST API without an authentication token
Accessing data in any way without using the app itself
If you want to restrict data to your app only, you will need some form of authentication provided by Firebase Authentication.
You have to use firebase Anonymous Authentication, This authentication doesn't require user input like email, password, SSO, etc. You can just set the code inside a button to Get Started, So firebase will automatically create a userID for each user.
Let's say i have a scenario where i can use user.getUid() to use it as a tag in Firebase Database for every new user authenticated. Of course this would bind the database with the current user.
But what if someone gets to know this id, then he can use this id to fetch the data using my Firebase url.
Accessing Firebase can be secured using Firebase Database Rules.
You can allow only the users who are authenticated to access your firebase,
use :
".read": "auth.uid != null"
read about Database Rules in Firebase
Say I'm building a chat room app using Firebase. In my client code, I make direct writes to the production database (i.e, the chat content, etc.)
How can I ensure that clients do not have DIRECT access to my database? If someone decompiles my apk, and changes the table name where I make access, then they'll be able to write to any of my databases.
I need clarification on how Firebase is secure without providing a middle layer of security between the client and server (by not letting the client access the Database directly).
You have to set permissive rules for your paths in Firebase, i.e.
{
"rules": {
"users": {
// users is readable by anyone
".read": true,
// users is writable by anyone
".write": true
}
}
"important_table": {
// important_table is readable by anyone
".read": true,
// important_table is NOT writable by anyone
".write": false
}
}
}
More: https://firebase.google.com/docs/database/security/