Firestore add security rule to allow reads only from mobile app [duplicate] - google-cloud-firestore

This question already has answers here:
Is there any way to give everyone access to firestore database, but only via app?
(1 answer)
How can I set Rules in Firebase so that only my app can write on my database firestore?
(1 answer)
Closed 2 years ago.
I am making a mobile app with Firestore and React Native.
There is no authentication system and upon starting the app, the user download a collection from Firestore.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
}
}
}
As of right now, the rule allows everyone to read from the database. Is it possible/advisable to limit the traffic to only our mobile app?

It's not possible to use security rules to limit access to a certain app. If your rules allow read access without requiring a sign-in using Firebase Authentication, then anyone with an internet connection can perform reads.
Minimally, you could require anonymous authentication. But that still would not stop someone from creating an account and using that to read everything without going through the app.

Related

Flutter app in production is not writing data to firebase anymore

I have a Flutter app in production, he was working well in debug mode. I could write data to firebase without any issue. When released to production, I also could read and write to firebase. Suddenly, the app is not behaving well anymore, when I post data to firebase, it is not writing any data at all but when I modified data directly from firebase, it gets updated in the app. Anybody can help me with this issue?
This might be because of the temporary Cloud Firestore rules you have set up in the beginning.
Go to Firebase -> Firestore Database -> Rules;
in here you should be able to see the restrictions to who can write or read from your Firestore databse, or until when your users can write/read to your database.
If you just want authenticated users to Read&Write whenever they want you can use this sample;
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
If you want to create a custom rule you can check the Cloud Firestore Security documentation.

Insecure Firestore rules and App Check - the right way to go for unauthenticated database access? [duplicate]

This question already has answers here:
What is the purpose of Firebase AppCheck?
(3 answers)
Does Firebase App Check discard the need for implementing Security Rules?
(1 answer)
Closed 6 months ago.
I have built an app (Android/iOS) using Flutter that allows its users to configure the app to receive a daily notification. Users can also submit a textfield. There is no requirement for users to register and authenticate.
I am using Firestore to store data from the app.
I have architected the app so that the Firestore rules allow any access:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
To prevent anyone accessing my app's Firestore instance, I have implemented App Check, and Enforcement is enabled.
I have begun to get messages from Google '[Firebase] Your Cloud Firestore database has insecure rules'.
Can anyone advise:
Should I ignore the warnings from Google?
Must I implement user registration and authentiation when it's not necessary for the app?
Should I architect my app in a different way?
Should I do something else?
Thanks,
Luke
Should I ignore the warnings from Google?
Ignoring the warning is not recommended as it may cause security issues.
If you're allowing anyone to access database, then they can take advantage and modify, or delete your data. And your database remain unsecured
2.Must I implement user registration and authentiation when it's not necessary for the app?
If it is not necessary for authenticating user for your application then you can apply some validation rules to restrict certain database nodes or you can permit role based access. In this way you can reduce security issues.
#samthecodingman has given excellent details about number of ways to tighten up database to prevent security issues in similar thread

Firebase Auth User roles [duplicate]

This question already has an answer here:
Is there any function in User Authentication for User Roles?
(1 answer)
Closed 1 year ago.
How can I add roles to users to let them see specific views in my app?
EX: They have admin, it shows something | They have user, it is normal | They have unassigned, it shows a different page.
I need help with first, making the groups through firebase, but also implementing it into my app.
I've searched far and wide but can't seem to find a tutorial for swiftUI. Anything appreciated.
Use Firebase Security Rules, It allows you to allow or disallow access to your database, It's very easy, and part of Firebase:
As per Firebase:
Firebase Security Rules stand between your data and malicious users. You can write simple or complex rules that protect your app's data to the level of granularity that your specific app requires. Firebase Security Rules leverage extensible, flexible configuration languages to define what data your users can access for Realtime Database, Cloud Firestore, and Cloud Storage. Firebase Realtime Database Rules leverage JSON in rule definitions, while Cloud Firestore Security Rules and Firebase Security Rules for Cloud Storage leverage a unique language built to accommodate more complex rules-specific structures.
Here's an example for Firestore:
service <<name>> {
// Specify your resource path.
match <<path>> {
// Allow the request if the following conditions are true.
allow read, write: if <<condition>>
}
}
... but for Realtime Database, It's JSON based:
{
"rules": {
"<<path>>": {
// Allow the request if the condition for each method is true.
".read": <<condition>>,
".write": <<condition>>
}
}
}
Read more on https://firebase.google.com/docs/rules

Firebase/Firestore - database has insecure rules?

I have a SwiftUI application, which uses Firebase as a back end, and my rules are something like this:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone on the internet to view, edit, and delete
// all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// your app will lose access to your Firestore database
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 10, 28);
}
}
}
I understand that these rules allow anyone to read and write to the database. However, as long as they are only using the API provided to the them in the application, how is this insecure? For instance, I could understand the danger, if, say, someone took the xcode project from my laptop and created a button that deleted all users in the database. But, no one will have access to this code.
I do want users to be able to read and write to/from the database, so I was just wondering if these rules are insecure, and, if so why? Like what is an example of how a hacker with malicious intent could exploit these rules to gain unauthorized access to user information and/or somehow modify the database in a way that the API provided in my application does not allow?
Thank you.
as long as they are only using the API provided to the them in the application
This is precisely the problem.
Your app contains all the configuration needed to connect to the database (and other resources in your Firebase project). A malicious user can take this configuration data, and call the API themselves - thus bypassing any of your client-side logic.
While you can configure Firebase App Check to help only allow access to the database is coming from your code, that is no guarantee and somebody else's might still use their code with your configuration data.
That's why it's crucial that you also encode your business logic in your security rules. Say that your application code only allows the user to delete their own account from the database, you'll then also want to encode that logic in your security rules so that they're enforced on the server. This is a variation of what the Firebase documentation on securing your database describes as content-owner only access.

Security of Firestore Database - SwiftUI

I have a very simple Firestore database for a game I creating. I have a User collection which has documents, each of which specifies a user's username, their email, and their high score.
I would like everyone to be able to read the high score and username's of everyone in the database, since I have a list that lists every user's high score and username.
However, I would only like individuals to be able to write the database (i.e. submit their own high score), if they are logged in.
Thus, I have the following security rules:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
match /Users/{document=**} {
allow read: if true;
}
}
}
My question is, is this secure? I guess this means that technically a user with malicious intent could read the emails of every user. Is there a way to prevent this by somehow specify that only everyone should be able to read the highscore and username properties of each User document?
Also, this set up does prevent malicious users from writing to the database, correct (edit here: I guess this doesn't - I'm looking into this now by reading the docs here https://firebase.google.com/docs/firestore/security/rules-structure)
I'm not new to Firebase but I am new to it's security rules, since I haven't pushed an application to production before and would like to make sure I don't leave anything vulnerable, so any feedback/guidance here is appreciated.
As I explained on your previous question, anyone can take the configuration data from your app, and call the Firebase APIs with that data.
is this secure?
Only you can determine whether your rules are correct for your use-case.
These rules allow anyone to read all documents in the /Users collection. In addition they allow any authenticated user to read and write all documents. If that is the use-case you want to support, the these are the correct rules for you.
So if you've enabled the anonymous authentication provider in Firebase Authentication, anyone can take you configuration data, write a minimal web page and call firebase.auth().signInAnonymously() and then read all user data with firebase.firestore().collection("Users").get().
Securing your database is going to be hard to learn in this question and answer style. I instead recommend you:
Read the Firebase guide on security rules.
Doug's video introduction to security rules.
Watch the series Getting to know Cloud Firestore, and pay special attention to the episode on security rules.
The video Unit testing security rules with the Firebase Emulator Suite and its follow up Intermediate topics in Firebase Security Rules.
The pro-series episode How to build a secure app in Firebase.
The article Patterns for security with Firebase: combine rules with Cloud Functions for more flexibility