What is the basic structure of a Firebase security rule?
A Firebase security rule has a service block, inside which there are match blocks defining paths, and inside those, allow statements that specify read or write permissions.
Click to reveal answer
beginner
What does the match keyword do in Firebase rules?
The match keyword defines a path in your database or storage where rules apply. It scopes the rules to specific data locations.
Click to reveal answer
beginner
Explain the purpose of allow read: if condition; in Firebase rules.
This statement allows read access to data only if the specified condition evaluates to true. It controls who can read data.
Click to reveal answer
intermediate
How do you specify multiple permissions in one rule?
You can list multiple permissions separated by commas, like allow read, write: if condition;, to apply the same condition to both read and write.
Click to reveal answer
intermediate
What is the role of variables in Firebase rule paths?
Variables in paths, like <code>{userId}</code>, act as placeholders that match any value. They let you write flexible rules for dynamic data locations.
Click to reveal answer
What keyword defines the data path in Firebase security rules?
Apath
Ballow
Cservice
Dmatch
✗ Incorrect
The match keyword defines the path where rules apply.
Which statement correctly allows both read and write if a condition is true?
Aallow read, write: if condition;
Ballow read write if condition;
Callow read or write: if condition;
Dallow read if condition; allow write if condition;
✗ Incorrect
You list multiple permissions separated by commas in one allow statement.
What does {userId} represent in a Firebase rule path?
AA variable placeholder
BA fixed folder name
CA function call
DA permission type
✗ Incorrect
Curly braces define variables that match any value in that path segment.
Where do you write Firebase security rules for Firestore?
Firestore rules go inside the service cloud.firestore block.
What happens if no rule matches a request in Firebase security rules?
AThe request is queued
BAccess is allowed by default
CAccess is denied by default
DThe request is logged but allowed
✗ Incorrect
Firebase denies access by default if no rule matches.
Describe the main parts of a Firebase security rule and their roles.
Think about how you specify where and when access is allowed.
You got /4 concepts.
Explain how variables in rule paths help manage permissions dynamically.
Imagine you want to write one rule for many users.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a match block in Firebase security rules?
easy
A. To create new collections automatically
B. To define user roles and permissions
C. To write data to the database
D. To specify which data paths the rules apply to
Solution
Step 1: Understand the role of match blocks
The match block is used to specify the path in the database where the rules apply.
Step 2: Differentiate from other rule parts
Other parts like allow control access, but match targets the data location.
Final Answer:
To specify which data paths the rules apply to -> Option D
Quick Check:
match = data path selector [OK]
Hint: Remember: match points to data location [OK]
Common Mistakes:
Confusing match with allow
Thinking match defines user roles
Assuming match writes data
2. Which of the following is the correct syntax to allow read access to all users in a Firebase rule?
easy
A. allow read { true }
B. allow read: if true;
C. allow read = true;
D. allow read if (true)
Solution
Step 1: Recall correct allow syntax
The correct syntax uses a colon and condition: allow read: if true;
Step 2: Check other options for syntax errors
Options A, B, and D use incorrect symbols or missing keywords.
Final Answer:
allow read: if true; -> Option B
Quick Check:
Correct allow syntax uses colon and if [OK]
Hint: Use colon and 'if' for conditions in allow [OK]
Common Mistakes:
Using '=' instead of ':'
Omitting 'if' keyword
Using braces instead of semicolon
3. Given the rule snippet:
match /posts/{postId} {
allow read: if request.auth != null;
allow write: if request.auth.uid == resource.data.authorId;
}
What happens if a logged-in user tries to write to a post they did NOT create?
medium
A. Write is denied because user is not the author
B. Write is allowed because user is logged in
C. Write is allowed only if postId matches user ID
D. Write is denied because read access is restricted
Solution
Step 1: Analyze write condition
Write is allowed only if request.auth.uid == resource.data.authorId, meaning user must be the author.
Step 2: Apply condition to user not author
If user is logged in but not the author, condition fails, so write is denied.
Final Answer:
Write is denied because user is not the author -> Option A
Quick Check:
Write allowed only for author = A [OK]
Hint: Check if user ID matches author ID for write [OK]
Common Mistakes:
Assuming any logged-in user can write
Confusing read and write conditions
Ignoring resource data in condition
4. Identify the error in this Firebase rule snippet:
match /users/{userId} {
allow read if request.auth.uid == userId;
allow write: if request.auth.uid == userId;
}
medium
A. Missing colon after 'allow read'
B. Incorrect variable name 'userId'
C. Missing semicolon after 'allow write' condition
D. Using '==' instead of '=' in conditions
Solution
Step 1: Check syntax for 'allow' statements
Each allow must have a colon before the condition, e.g., allow read: if ...;
Step 2: Identify missing colon
The first line misses colon after allow read, causing syntax error.
Final Answer:
Missing colon after 'allow read' -> Option A
Quick Check:
Colon required after allow keyword [OK]
Hint: Always put colon after allow before condition [OK]
Common Mistakes:
Omitting colon after allow
Confusing '=' and '=='
Forgetting semicolon at line end
5. You want to write a rule that allows users to read their own profile data but only write if their profile contains a non-empty 'name' field. Which rule snippet correctly enforces this?
hard
A. match /profiles/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.resource.data.name != "";
}
B. match /profiles/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId || request.resource.data.name != "";
}
C. match /profiles/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId && request.resource.data.name != "";
}
D. match /profiles/{userId} {
allow read: if true;
allow write: if request.auth.uid == userId && request.resource.data.name == "";
}
Solution
Step 1: Understand read condition
Users can read only their own profile: request.auth.uid == userId.
Step 2: Understand write condition
Write allowed only if user owns profile AND 'name' field is not empty: request.auth.uid == userId && request.resource.data.name != "".
Step 3: Check options for correct logic
match /profiles/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId && request.resource.data.name != "";
} matches both conditions correctly; others use wrong logic or allow too much.
Final Answer:
match /profiles/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId && request.resource.data.name != "";
} -> Option C
Quick Check:
Use AND for ownership and non-empty name [OK]
Hint: Use AND (&&) to combine ownership and data checks [OK]
Common Mistakes:
Using OR (||) instead of AND (&&)
Allowing write without ownership check
Checking for empty string incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.