Bird
Raised Fist0
Firebasecloud~20 mins

Rule syntax and structure in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firebase Rule Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Firebase Rule Match Statements
Which of the following Firebase security rule match statements correctly matches all documents in the 'users' collection?
Amatch /users/{userId} { allow read, write: if request.auth != null; }
Bmatch /users { allow read, write: if request.auth != null; }
Cmatch /users/{userId}/profile { allow read, write: if request.auth != null; }
Dmatch /users/{userId}/documents/{docId} { allow read, write: if request.auth != null; }
Attempts:
2 left
💡 Hint
Remember that to match documents inside a collection, you need to specify a wildcard for document IDs.
❓ Configuration
intermediate
2:00remaining
Evaluating Firebase Rule Condition Syntax
Which of the following Firebase rule conditions correctly checks if the authenticated user's ID matches the document ID being accessed?
Aallow read: if request.auth.uid == resource.data.uid;
Ballow read: if request.auth.uid == resource.data.userId;
Callow read: if request.auth.uid == request.resource.id;
Dallow read: if request.auth.uid == resource.id;
Attempts:
2 left
💡 Hint
The document's data field that stores the user ID must be referenced correctly.
❓ Architecture
advanced
3:00remaining
Designing Firebase Rules for Nested Collections
You have a Firestore database with a collection 'projects' and each project has a subcollection 'tasks'. Which rule structure correctly allows authenticated users to read and write only their own projects and tasks?
Amatch /projects/{projectId} { allow read, write: if request.auth.uid == resource.data.ownerId; } match /projects/{projectId}/tasks/{taskId} { allow read, write: if request.auth.uid == resource.data.ownerId; }
B} ;dIrenwo.atad.ecruoser == diu.htua.tseuqer fi :etirw ,daer wolla { }dIksat{/sksat/}dItcejorp{/stcejorp/ hctam } ;dIrenwo.atad.ecruoser == diu.htua.tseuqer fi :etirw ,daer wolla { }dItcejorp{/stcejorp/ hctam
Cmatch /projects/{projectId} { allow read, write: if request.auth.uid == resource.data.ownerId; } match /projects/{projectId}/tasks/{taskId} { allow read, write: if request.auth.uid == resource.data.assigneeId; }
Dmatch /projects/{projectId} { allow read, write: if request.auth.uid == resource.data.ownerId; } match /projects/{projectId}/tasks/{taskId} { allow read, write: if request.auth.uid == resource.data.taskOwnerId; }
Attempts:
2 left
💡 Hint
Tasks may be assigned to different users than the project owner.
❓ security
advanced
2:00remaining
Identifying Security Risks in Firebase Rules
Consider the following Firebase rule snippet:

match /posts/{postId} { allow read: if true; allow write: if request.auth != null; }

What is the main security risk with this rule?
Firebase
match /posts/{postId} { allow read: if true; allow write: if request.auth != null; }
AOnly authenticated users can read and write posts, so no risk exists.
BAnyone can read all posts, but only authenticated users can write, which may allow unauthorized edits.
CNo one can read posts because read is set to true incorrectly.
DThe rule syntax is invalid and will cause a deployment error.
Attempts:
2 left
💡 Hint
Check who can read and who can write with these conditions.
❓ service_behavior
expert
3:00remaining
Predicting Firebase Rule Evaluation Outcome
Given the following Firebase rule snippet:

match /documents/{docId} { allow read: if request.time < timestamp.date(2023, 1, 1); allow write: if request.auth.uid == resource.data.ownerId; }

If a user tries to read a document on February 1, 2023, what will happen?
Firebase
match /documents/{docId} { allow read: if request.time < timestamp.date(2023, 1, 1); allow write: if request.auth.uid == resource.data.ownerId; }
AThe write request will be allowed if the user is the owner, regardless of the date.
BThe read request will be allowed because the user is authenticated.
CThe read request will cause a syntax error due to incorrect timestamp usage.
DThe read request will be denied because the current time is after January 1, 2023.
Attempts:
2 left
💡 Hint
Focus on the read condition and the date comparison.

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

  1. Step 1: Understand the role of match blocks

    The match block is used to specify the path in the database where the rules apply.
  2. Step 2: Differentiate from other rule parts

    Other parts like allow control access, but match targets the data location.
  3. Final Answer:

    To specify which data paths the rules apply to -> Option D
  4. 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

  1. Step 1: Recall correct allow syntax

    The correct syntax uses a colon and condition: allow read: if true;
  2. Step 2: Check other options for syntax errors

    Options A, B, and D use incorrect symbols or missing keywords.
  3. Final Answer:

    allow read: if true; -> Option B
  4. 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

  1. Step 1: Analyze write condition

    Write is allowed only if request.auth.uid == resource.data.authorId, meaning user must be the author.
  2. Step 2: Apply condition to user not author

    If user is logged in but not the author, condition fails, so write is denied.
  3. Final Answer:

    Write is denied because user is not the author -> Option A
  4. 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

  1. Step 1: Check syntax for 'allow' statements

    Each allow must have a colon before the condition, e.g., allow read: if ...;
  2. Step 2: Identify missing colon

    The first line misses colon after allow read, causing syntax error.
  3. Final Answer:

    Missing colon after 'allow read' -> Option A
  4. 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

  1. Step 1: Understand read condition

    Users can read only their own profile: request.auth.uid == userId.
  2. 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 != "".
  3. 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.
  4. 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
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard