How to ensure data security in Firebase? - android

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/

Related

How insecure is firebase rule: read and write = true

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

Firebase Realtime Database xxxx-xxxx-4458' has insecure rules

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

Fetch firestore data for a specific app without authentication

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.

Can an offline user carry out database transactions specific to their uid in Firebase Realtime Database?

The situation:
User starts the app with no internet connection. I attempt to authenticate them anonymously in my launch activity and perform a database write to the reference '/users/$uid', in a database with this rule structure:
{
"rules": {
"users": {
"$user_id": {
".write": "$user_id === auth.uid",
".read": "$user_id === auth.uid"
}
}
}
}
So, if this situation occurs and the user then comes online will the queued database transaction actually be executed - or do I need to manually re-authenticate and retry the database transaction?
I'm not sure if the uid for anonymous authentication will be generated if the user is offline, so I don't think the database reference can be resolved.
Reference links:
https://firebase.google.com/docs/auth/android/anonymous-auth
https://firebase.google.com/docs/database/android/offline-capabilities
I would save the data in sharedpreferences or local DB (depends on the type of data you want to save).
After that either start background service and try to connect to your realtime database every 10mins or you want for the user next time they open the app and check if theres any authentication need to be checked.
For the background service i would recommend AlarmManger.
So, after testing this, I've found that:
The uid cannot be relied upon between restarts if the user never had connectivity in the first place
Database transactions which are performed locally - because you have setPersistence(true) - will be queued and synced with remote, regardless of whether the user has connectivity or not, but not if the DatabaseReference used contains a reference to the current user's uid, because this cannot be relied upon between restarts (such as in the scenario described in the question).
To get around this, I'm using local storage by means of SharedPreferences for the most basic data and waiting for connection before instantiating a DatabaseReference which references the user's uid.

How to integrate Firebase authentication service with it's database?

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

Categories

Resources