📖 Scenario: You are building a simple app where users can upload and view their own photos securely. You want to make sure that each user can only upload and read their own files in Firebase Storage.
🎯 Goal: Create Firebase Storage security rules that allow authenticated users to read and write only their own files under their user ID folder.
📋 What You'll Learn
Create a variable to represent the user's ID in the path
Add a condition to allow read and write only if the user is authenticated
Add a condition to allow access only to files under the user's own folder
Complete the Firebase Storage rules with proper match and allow statements
💡 Why This Matters
🌍 Real World
Securing user files in Firebase Storage is essential for apps that handle personal data like photos or documents.
💼 Career
Understanding and writing Firebase Storage security rules is a key skill for cloud developers and backend engineers working with Firebase.
Progress0 / 4 steps
1
Define the user ID variable in the path
Create a match block for the path /user_uploads/{userId}/{allPaths=**} and define the variable userId to represent the user folder.
Firebase
Hint
Use match to specify the folder path with {userId} as a variable.
2
Add authentication check
Inside the match /user_uploads/{userId}/{allPaths=**} block, add the allow read, write rule with the condition request.auth != null && request.auth.uid == userId.
Firebase
Hint
Use request.auth != null to check authentication and compare request.auth.uid with userId.
3
Add read and write rules for user files
Write the allow read, write rule inside the match /user_uploads/{userId}/{allPaths=**} block that permits access only if the user is authenticated and their UID matches userId.
Firebase
Hint
This rule ensures only the authenticated user can access their own files.
4
Complete the Firebase Storage security rules
Ensure the entire Firebase Storage security rules file includes the service firebase.storage declaration, the match /b/{bucket}/o block, and the match /user_uploads/{userId}/{allPaths=**} block with the allow read, write rule as specified.
Firebase
Hint
Check that all parts of the rules file are present and correctly structured.
Practice
(1/5)
1. What is the main purpose of Firebase Storage security rules?
easy
A. To control who can read or write files in storage
B. To speed up file uploads
C. To increase storage space automatically
D. To backup files to another server
Solution
Step 1: Understand the role of security rules
Security rules define who can access files and what actions they can perform.
Step 2: Identify the main function
The main function is to control read and write permissions to protect data.
Final Answer:
To control who can read or write files in storage -> Option A
Quick Check:
Security rules = control access [OK]
Hint: Security rules control access permissions only [OK]
Common Mistakes:
Thinking rules speed up uploads
Believing rules increase storage size
Confusing rules with backup features
2. Which of the following is the correct syntax to allow only authenticated users to read files in Firebase Storage rules?
easy
A. allow read: if request.auth != null;
B. allow read: if user.authenticated == true;
C. allow read: if auth.user != null;
D. allow read: if request.user.authenticated;
Solution
Step 1: Check Firebase Storage rules syntax
The correct way to check authentication is using request.auth != null.
Step 2: Compare options
Only allow read: if request.auth != null; uses the correct Firebase syntax for authentication check.
Final Answer:
allow read: if request.auth != null; -> Option A
Quick Check:
Authentication check = request.auth != null [OK]
Hint: Use request.auth to check user authentication [OK]
Common Mistakes:
Using incorrect variable names like user.authenticated
Missing the 'request.' prefix
Using wrong boolean checks
3. Given the following Firebase Storage rule snippet, what happens when an unauthenticated user tries to read a file?
service firebase.storage {
match /b/{bucket}/o {
match /files/{fileId} {
allow read: if request.auth != null;
allow write: if false;
}
}
}
medium
A. The read request causes a syntax error
B. The read request is allowed because write is false
C. The read request is allowed because no write permission is needed
D. The read request is denied because the user is not authenticated
Solution
Step 1: Analyze the read rule condition
The rule allows read only if request.auth != null, meaning user must be authenticated.
Step 2: Consider unauthenticated user
An unauthenticated user has request.auth == null, so the condition fails and read is denied.
Final Answer:
The read request is denied because the user is not authenticated -> Option D
Quick Check:
Unauthenticated read = denied [OK]
Hint: Read allowed only if request.auth is not null [OK]
Common Mistakes:
Ignoring the read condition and assuming access
Confusing read and write permissions
Thinking false write affects read
4. Identify the error in this Firebase Storage rule that aims to allow users to write only to their own folder:
service firebase.storage {
match /b/{bucket}/o {
match /users/{userId}/{allPaths=**} {
allow write: if request.auth.uid == userId;
}
}
}
medium
A. The variable userId is not defined in the path
B. The wildcard {allPaths=**} is incorrectly used
C. The rule does not allow read access
D. The condition should use request.auth.userId instead of request.auth.uid
Solution
Step 1: Check path variables
The path correctly defines {userId} and {allPaths=**} wildcards.
Step 2: Review condition and permissions
The condition correctly compares request.auth.uid with userId for write permission.
Step 3: Identify missing read permission
The rule only allows write, no read permission is granted, which may block legitimate reads.
Final Answer:
The rule does not allow read access -> Option C
Quick Check:
Missing read permission = problem [OK]
Hint: Check if both read and write permissions are set [OK]
Common Mistakes:
Assuming write permission covers read
Confusing request.auth.uid with userId variable
Misusing wildcard syntax
5. You want to allow users to upload images only to their own folder and only if the file size is less than 2MB. Which Firebase Storage rule correctly enforces this?
service firebase.storage {
match /b/{bucket}/o {
match /users/{userId}/{fileName} {
allow write: if request.auth.uid == userId && request.resource.size < 2 * 1024 * 1024;
allow read: if request.auth.uid == userId;
}
}
}
hard
A. The file size check should use resource.size instead of request.resource.size
B. This rule correctly enforces user ownership and file size limit
C. The rule should allow write without checking user ID
D. The size limit should be checked on the existing file, not request.resource
Solution
Step 1: Verify user ownership condition
The rule checks request.auth.uid == userId to ensure users write only to their folder.
Step 2: Verify file size condition
The rule uses request.resource.size < 2 * 1024 * 1024 to limit uploads to less than 2MB.
Step 3: Confirm read permission
Read is allowed only for the authenticated user matching userId.
Final Answer:
This rule correctly enforces user ownership and file size limit -> Option B
Quick Check:
User ID and size check = correct [OK]
Hint: Use request.resource.size for new file size check [OK]
Common Mistakes:
Using resource.size instead of request.resource.size
Not checking user ID for write
Checking size on existing file instead of upload
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.