Firebase Allow only the cloud functions access to read and write [duplicate] - firebase

This question already has answers here:
Cloud Firestore Security Rules allow write only from Firebase function
(3 answers)
Closed 3 years ago.
For security reasons I would only like the cloud functions to be able to access my Firestore data. Then have my app call my cloud functions for data.
I can't find any docs about locking down your database so that only your cloud functions can access them.

You just have to deny all access as follows
// Deny read/write access to all users under any conditions
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
As a matter of fact, Cloud Functions run with administrative privilege and therefore they will totally bypass the security rules.

Related

Firestore rules don't seem to work with firebase node js API function [duplicate]

This question already has answers here:
Cloud Firestore ignores rules
(1 answer)
Firestore security rules, allowing access from nodejs API
(2 answers)
Closed 6 months ago.
I have a Firebase rule for a collection but it doesn't seem to work. Currently I am disallowing writes to the doc but it doesn't seem to have any effect when I try to update it. The rules fail when the database is interacted via the firebase node js API function.
match /accounts/{accId} {
allow read: if (request.auth != null &&
((resource.data.userId == request.auth.uid) ||
(request.auth.uid == 'e6aaXxP6PsMWvuhCo6oOYqTTL8p2')));
allow write: if false;
}
If you are using the nodejs SDK initialized with a service account, that code will always bypass security rules. Security rules only apply to web and mobile applications using their platform SDKs. They do not apply to any backend SDKs. The assumption is that your code running on a secure backend is fully controlled by you and not subject to abuse.

How to modify Firebase firestore security rules for deployment [duplicate]

This question already has answers here:
Your Cloud Firestore database has insecure: any user can read your entire database
(2 answers)
Basic Firebase Rules [duplicate]
(2 answers)
Closed 10 months ago.
I’m trying to modify my security rules such that I can deploy an app to play store, I tried the following:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**}{
allow read: if true;
allow write: if request.auth != null
}
}
}
But still I received an email that says:
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
Any solutions?

Security rule to limit storage space used in Firebase Storage?

In Firestore I store the infos of space used (in Firebase Storage) and space total per user and I was wondering if I could use these variables to write a Storage rule to directly limit storage space per user there?
Is there any way to achieve that? Is it even useful?
Currently the rule I have allow users to only access their own storage folder :
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /users/{userId}/{allPaths=**} {
allow read, write, delete: if request.auth.uid == userId;
}
}
}
What you're trying to do is not immediately possible in security rules. Storage security rules cannot access data in Firestore. Similarly, Firestore security rules cannot access data in Storage. So, it's not possible to stop a Storage upload from happening based on data that exists outside that product.
The best you can do to enforce a quota is to use a product like Cloud Functions to run a Storage trigger after an upload completes, and check at that point if the upload caused the user to exceed any limits. You can then choose to delete the file if you want. You might also want to use a trigger to maintain a running total of usage per-user.

Can I lock read and write operations from Firestore of client side?

Can I lock read and write operations from Firestore of client side? I want that only Firebase Cloud Functions can read and write in Firestore.
I was connect directly from client to Firestore but I want now the client can call only with back-end ( To make some condition and security ) server for avoiding hack data
It's quite simple, you have to deny all access as follows:
// Deny read/write access to all users under any conditions
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
Since Cloud Functions interact with Firestore through the Admin SDK they completely bypass the Security Rules.

How to write security rules in cloud fire store for read if I want my users to read all news and updates from my application without sign in?

My application is a local news application that publishes the news and happenings of my village and nearby surroundings. I want people to read all the news without any authentication and only the app owner or publisher can write the news. So, in this case, I have the following rules:
allow read: if true;
allow write: if request.auth.uid =="My uid"
But when I do this I get this message in my email from Firestore:
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
You're not showing the entire rules, but if they look like this:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
allow write: if request.auth.uid =="My uid"
}
}
}
Firestore finds that to be insecure because you are applying universal read access to every single document in the entire database. This can lead to unexpected security problems.
Instead, you should call out individual collections that should have read and write access. Don't use the wildcard /{document=**}.
match /collection-name/{id} {
allow read: if true;
allow write: if request.auth.uid =="My uid"
}
I suggest reading the documentation on security rules thoroughly (not just the first page) to learn how they work.

Resources