Android Firestore rules custom auth - android

We are using in out app Google sign in but also have function to "skip authentication"
For authenticated user we have the following rule
function isSignedIn() {
return request.auth != null;
}
For "skip authentication" we are not using anonymous Firebase login, using kind of local caching mechanism.
The question is the a way to add rule for "skipped users" with some hardcored obfuscated token?

It's not possible to send extra information along with a query in order to authorize the query using security rules. This is not secure, as anyone would be able to bypass the rules simply by knowing the "password" in this case. All identity-based authorization must go through a Firebase Authentication user account - there are currently no alternatives.

Related

How to restrict API key if firebase don't have authentication?

I m creating an app using firebase. As in our app, there is no sign-in concept(users don't need to sign in to the application). so now I need to secure my firebase.
As the API key is publicly exposed, but we can still restrict that API key to some domain or some IP.
These API keys are auto-created by firebase.
Let's consider a web app, I choose the HTTP address and add my domain. It means that the key is bind to the domains which I have entered in the API and Services - Credentials Section.
In the real-time Firebase security rules.
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".write": "auth!=null && auth.uid === 'my uid'"
".read": true
}
}
I just add the security for write but for app users(web, android, or ios) as there is no signing concept I set as true value.
But now the question arrives how can I make my firebase secure so that if any other app or web app using our firebase it should not return data.
It will always throw forbidden or unauthorized err but not data as that unauthorized app or domain I haven't bind in mine keys
For this, I have restricted my API key,
For browser key, authorized domains added
For android key, authorized app(SHA and package added)
For the ios key, bundled ids are added.
But the restriction is not working for the API keys.
Although when I added authentication is read also, then API restriction working perfectly (But this still causing the problem as our app has millions of users, it says "The SMS quota for this project has been exceeded. Exceeded quota for verifying password")
So how should I restrict firebase to listen from our domain and our app only?
I even try sign-in in anonymously but it is not a solution as it creates millions of anonymous users which are not correct. Also, the Anonymous account has a 100 million limit.
Also go through App Check, different providers for different platforms but it also has quota limit so didn't check
Hope I m able to explain the question.
Any suggestions would be really helpful
What you're looking for is Firebase's new App Check feature, which protects your backend resources from abuse. For web apps App Check uses reCAPTCHA v3 to reduce the chances of abuse.
But I'd still recommend that you implement Authentication and security rules too. If you don't want to require that your users enter credentials, you can use Firebase's anonymous authentication to generate a unique UID for them. You can then check for that UID in your security rules, and still ensure that each user/device only accesses the data they're authorized for.

Firebase custom authorization that allows database sharing

In Firebase, I am trying to add a way to custom sign in a user. For sake of simplicity I'll have two users for my flutter app, User 1, and User 2. User 1 decides to register an account to my app using Firebase, In this app, User 1 will be able to write data to the Realtime Database. Once User 1 is logged in, is there a way for User 1 to generate a "code", so to speak, that can allow User 2 to register by entering the it, then be able to see all the data User 1 made? In my Firebase console, all I see is various modes of sign in using social media and other platforms but no option for custom authentication. An example of code would be great but an article explaining this would also be great as I am lost on what term to lookup.
-Thank you so much and I wish you have a great rest of your day!
Firebase's custom authentication doesn't have any UI, since you're writing the code for it.
But I think here you may just be looking for something like Firebase anonymous authentication, which literally just generates a UID for the device that you run the code on. If you then log this UID and stuff it into your security rules, you have a decent starting point at what you're looking for.
{
"rules": {
".read": true,
".write": "auth.uid === 'theUidForTheUserWhoCanWrite'"
}
}
Also see:
How to only allow one admin user to write Firebase Database?
How to allow one admin user to write Firebase Database? Without changing all the other rules below
How to set one user as admin in firebase?
How to secure access for user and admin in Firebase Database?

Can someone access my Firestore database by somehow manipulating client app?

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.

What security rules should be added to Google Cloud Firestore to keep data safe where no authentication is required to access the app

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.

How to allow only my app to access firebase without a login?

I am storing my app's data on Firebase and this being my first project on Firebase is proving to be much more difficult than I thought. I have gone through the official documentation and it says that we can login a user with their email and password or use other login options like Google or Facebook etc. However I don't want user's to login to my application but only read and write data to firebase if they are using my app. Right now I am using public rules for my firebase but then anyone with a reference to my firebase URL can read and write to the database. How do I overcome this?
Thanks in advance.
Enable Anonymous sing-in provider under Sing-in Method under Authentication tab in your Firebase Console and
use firebase anonymous authentication
Nowadays you can accomplish this by enabling Firebase App Check, which only allows requests coming from your real app on a genuine device to make requests these protected services on your Firebase project:
Realtime Database
Cloud Firestore
Cloud Storage
Cloud Functions (callable functions)
You can also implement App Check in your own backend code to provide it with the same protection.
As the documentation says:
Using App Check does not guarantee the elimination of all abuse, but by integrating with App Check, you are taking an important step towards abuse protection for your backend resources.
For the above services, that'd typically mean that you'll also want to implement Firebase Authentication and then implement fine-grained access control in server-side security rules.
Firebase Real-Time DB does not allow access to Un-Authorized Users.
Making the Firebase Database Rules true for Read & Write is not the way as (Chintan Soni) said.
So a Authentication mechanism is the best way!
If you only want to use firebase database you should:
Download SDK from gradle https://firebase.google.com/docs/android/setup
In firebase console create project and add your app, download json i put it on your Android Studio Project
Add database dependiences 'com.google.firebase:firebase-database:9.2.1'
Configure rules in console. You could also disable rules (recomended only for first tests:
"rules": {
".read": true,
".write": true
}
I also recomended update Android Studio to version 2.1 or 2.2 with Firebase integration included.
Set Database rules up as follows:
{
"rules": {
".read": true,
".write": true
}
}

Categories

Resources