Secure Firebase Storage While Allowing Certain Users Access - firebase

So I have a bucket of images in my Firebase Storage. Everyone's images go in there and I am concerned about security because if a user is authenticated, I think they can read and write everyone's images. This is not good.
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageId} {
allow read: if request.auth != null;
// Only allow uploads of any image file that's less than 10MB
allow write: if request.auth != null && request.resource.size < 10 * 1024 * 1024;
}
}
}
I want multiple/specific users that have access to the files to be able to access them and no one else. Also, access should be dynamic (a user can gain or lose access). I've seen others secure files specific to one user by adding a {userId} wildcard in the match statement but I want MULTIPLE users. From my research, it sounds like the only way to do this would be through custom claims. I want to make this is correct before I dive too deep into it. It looks like custom claims can only store 1000 bytes of data too so that might be a bottleneck in my case.
Thanks!

Related

Firebase Cloud Firestore security rules only allow read not write

I made a simple app for underprivileged students so that they can learn during the Pandemic. I update notes for each subject daily through the firebase console (Cloud firestore). No authentication included because the students are small, and not well versed with technology.
I have only allowed read and deleted the write options in the security rules. Last night I got this email. I have added the image copy. I just want everyone to download the app and read the data(Notes) but no one to write. Is my database safe? Can anyone write, delete or manipulate the database if they got the project id?
Soon I'm planning to buy the blaze plan but now I'm a little insecure.
My security rules are as follows:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
}
}
}
email
As stated in that email/warning message, the rules you have are not considered secure. As your rules currently stand, a malicious user could abuse your database by calling hundreds of requests to it which may lead to unexpected billing charges.
The trigger for this warning is simple - if "allow read; or "allow write; is present on the /{document=**} path, send the user the warning as these broad rules are considered a bug and should be tightened. One of the main reasons the warning exists is if you store sensitive user data like phone numbers, email addresses, billing information under a /users/someUserId document - with the current rules this is now publicly accessible and can get you in toruble with data privacy laws and regulations like GDPR. There are a number of other similar conditions that also send similar warnings like if the system detects that the default 30 days of read/write access has expired.
If your data is expected to be publicly accessible, rather than grant read access to the entire database, grant it to the specific collections that you want to be public.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// public database of cars
match /cars/{carId} {
allow read;
}
// public database of trains
match /trains/{trainId} {
allow read;
}
// only that user can read/write their own data
match /users/{userId} {
allow read: if request.auth != null && request.auth.uid == userId;
allow write: if request.auth != null && request.auth.uid == userId;
}
}
}
I recommend having a read of the fixing insecure rules documentation for more information.
You can also make use of granular rules to limit the queries that can be performed against your database such as limiting getting a list of posts to 10 at a time.
yes it is safe, and people can only read stuff
no one can write to it unless they can access the database directly
it's completely safe, no 1 can write or delete your database, but since the database is open to read to everyone, hence some notorious people might send endless requests to your database and exaust ur daily limit if u using free plan or if u using biling, ur biling cost will sky rocket.
so best is to make read permission for authenticated users only, and on app side, u can do anonymous login, so that u don't have to enforce gmail or other login on app side, and unauthenticated users can't exaust ur daily limit, check the following link for anonymous login https://firebase.google.com/docs/auth/android/anonymous-auth

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.

Firebase security rules confusion

I have a flutter app which uses pdf files and video files. I have put these files in firebase storage and I put the url of these files in database collections to use them in my app.
I do not want any email and password authentication on my app. Are my pdf and video files secure?
Can anybody access them or obtain them?
This is my rules for database:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
This is my rules for firebase storage :
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
Your rules allows anybody to read, modify or delete your files, so not, your files are not safe at all.
If you want your files to be safe, you must consider implement some kind of authentication and set the appropriate rules to only you or certain group of users be able to access your files.
You can read more about setting rules in the Firebase documentation.
If you don't want to ask your users to enter credentials, but still want a modicum of security, consider using Firebase's anonymous authentication provider. From the documentation:
You can use Firebase Authentication to create and use temporary anonymous accounts to authenticate with Firebase. These temporary anonymous accounts can be used to allow users who haven't yet signed up to your app to work with data protected by security rules. If an anonymous user decides to sign up to your app, you can link their sign-in credentials to the anonymous account so that they can continue to work with their protected data in future sessions.
Of course if you don't want to associate your files or data with a specific user, then anonymous auth is also pretty meaningless. But at that point you're indeed looking to allow pure unauthenticated public access. This may be a fine option too, as long as you realize that your project will be charged for any reads/writes by any users.
If you want any users, without identifying them or providing credentials, to be able to read the data/files, but not write any data/files of their own, you're looking for read-only rules:
allow read; if true;
allow write: if false;
Or shorter, but less explicit to read:
allow read

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.

Resources