Google firebase's firestore rules block key creation/edits - firebase

I have the following firestore rule to allow user access to only their record. It works fine...
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
}
}
Now under the users collection, each document contains a field/key name "isAuthenticated" which is set to true from the server side backend using service account persmission.
How can I setup the rules to make sure even the authenticated user cannot update that particular key?

To disallow all regular users from creating documents:
allow create: if false;
But note that someone accessing the database with the Admin SDK will still be able to create documents, since they access it with administrative privileges.
**Update*: to prevent the user from updating any fields:
allow update: if false;
To disallow updating a specific field:
allow update: if !("isAuthenticated" in request.writeFields);
See:
Firebase Firestore prevent client side creation of fields in a document
Allow update on single field in firestore
How to check if there is only one specific field who is updated with Firestore rules?

Related

Firestore security rules without auth

I stored my portfolio app data using Firestore and firebase storage.
There are no user inputs or registration in my app, it's a simple portfolio to show my works.
I want any user to be able to read the data coming from my firestore and firebase storage.
My current rules:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
}
}
}
The problem i have with this rules is that attackers can fluid my app with requests.
I even got warning email says
"Because your project does not have strong security rules, anyone can access your entire database. Attackers can read all of your data, and they can drive up your bill."
I do not have any sensitive data stored but i want to prevent additional charges from google.
How can i set my Firestore security rules to enable any users to read without auth but prevent attacks?
Firebase Security Rules don't like when a database point is left open, while this is normally done with Security Rules and Auth, You can define the readability of the document based on a value from inside the document.
In this example, the document has a bool value for Public
service cloud.firestore {
match /databases/{database}/documents {
// Allow the user to read data if the document has the 'visibility'
// field set to 'public'
match /{document=**} {
allow read: if resource.data.public == true;
}
}
}

How do I secure my firestore for anonymous access of product data

I am using FireBase / Firestore for my website - I have products displayed on the website which are stored in the database. - Currently, I am having my rules set up as
match /{document=**} {
allow read: if true
allow write,create,update, delete: if request.auth.uid !=null
}
I am getting now mails from Firebase warning me about my insecure rules:
[Firebase] Your Cloud Firestore database has insecure rules
We've detected the following issue(s) with your security rules:
any user can read your entire database
Because your project does not have strong security rules, anyone can access your entire database. Attackers can read all of your data, and they can drive up your bill.
How do I secure my database properly but allow my website to read product data withour requiring the visitor to sign in?
The email is warning you that anyone can read any document in your entire database because of match /{document=**}. You should avoid using this global wildcard entirely, since it can lead to unexpected security issues. You should instead call out each individual collection with specific access for that collection. Minimally, it will look more like this:
match /collection1/{document=**} {
allow read: if true
allow write,create,update, delete: if request.auth.uid !=null
}
match /collection2/{document=**} {
allow read: if true
allow write,create,update, delete: if request.auth.uid !=null
}
Whether or not this form is "proper" for your app is not clear. Your rules need to encode the specific permissions for your app. Every app is going to be different, and your rules need to be tailored to your security requirements. You are effectively writing application logic into the rules, so treat it just like any other code.

Angular app not able to access Firestore database?

I have an angular app with appropriate firebaseConfig that has the apiKey correctly configured. When the app is initialized it is supposed to go to the corresponding Firestore and collect the email addresses there and store them for later user authentication. The problem is that the app is unable to do this unless there is a user logged in. This is confusing because I thought that the app could access the Firestore database without having someone already authenticated. The only workaround I have been able to come up with is to set the Firestore rules to allow global read access. That way, when the app starts, it is guaranteed to have access to the database and emails therein.
What am I missing here?
If your security rules are like this:
// 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;
}
}
}
Then only if the user is authenticated they can access the data. You can change your rules to the following:
// 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;
}
match /{collectionName}/{docId} {
allow read: if collectionName == 'emailCollection';
}
}
}
This way if the user is authenticated then they can access all the documents, and if collection name is equal to emailCollection then a none authenticated user can access it.
https://firebase.google.com/docs/firestore/security/rules-structure#overlapping_match_statements
You're not missing anything. If you want users to be able to query Firestore collections without being authenticated ahead of time, that collection is going to need full public read access. It's not possible to write security rules that limit access to the database to a particular app or web site.

Firebase storage rules based on custom parameters

How can I grant read-write access based on custom parameters, for example,
When a user is registered with my app, a document is created and the document id is used to create a folder in firebase storage(so that I can ensure the uniqueness if every folder in firebase storage) The rule I am setting is a read permission for everyone who is authenticated and the write access is only for the user who created it and the admin only. Here is the rule I presently have in firebase storage.
service firebase.storage {
match /b/{bucket}/o {
match /{userId}/{allPaths=**} {
allow read: if request.auth != null;
allow write:if request.auth.uid == userId || userId == asdfasfasdf
}
}
}
Is this approach correct or correct me if I am doing it wrong. I have gone through the documentation and it is a bit hard for me to understand.

What is the correct rule in firebase that allows any user to save an image and any authenticated user read others users avatars?

I am using react-native with react-native-fetch-blob and its pollyfills to upload an image to firebase storage. I could not follow the docs and after many tries I decided to ask how can set the proper firebase storage rule to avoid this message:
Firebase Storage: User does not have permission to access 'avatars/dTRYaMBELiNySUoml7h7JrNyZsg2
The dTRYaMBELiNySUoml7h7JrNyZsg2 is the uid, 'cause I organized my tree as gs://_______.appspot.com/avatars is the root of all my users avatars, so with any user UID I can retrieve its avatar.
What's is the correct firebase storage rule to allow a user upload its image and allow any authenticated users to read these avatars?
Something like:
service firebase.storage {
match /b/{bucket}/o {
match /avatars/{userId} {
allow read: if request.auth != null;
allow write: if request.auth.uid == userId;
}
}
}
Per: https://firebase.google.com/docs/storage/security/user-security

Resources