📖 Scenario: You are setting up Firebase security rules for a simple app. You want to test these rules locally using the Firebase Emulator to make sure your data is safe before deploying.
🎯 Goal: Build a Firebase security rules file and test it using the Firebase Emulator. You will create a basic rule, configure the emulator, write a test case, and complete the setup to run the emulator.
📋 What You'll Learn
Create a Firebase security rules file named firestore.rules with a simple read rule
Add an emulator configuration in firebase.json to enable Firestore emulator
Write a test script in test.rules.js to check the read rule using the emulator
Complete the firebase.json file with the emulator port and rules path
💡 Why This Matters
🌍 Real World
Testing Firebase security rules locally helps prevent data leaks and unauthorized access before deploying to production.
💼 Career
Firebase developers and cloud engineers use emulators to safely test security rules and app behavior during development.
Progress0 / 4 steps
1
Create basic Firestore security rules
Create a file named firestore.rules with these exact contents: rules_version = '2'; and service cloud.firestore { match /databases/{database}/documents { match /test/{doc} { allow read: if true; } } }
Firebase
Hint
Start with the rules version, then define the service and match paths exactly as shown.
2
Configure Firestore emulator in firebase.json
Add a firestore section inside emulators in firebase.json with port set to 8080 and rules set to firestore.rules
Firebase
Hint
Inside the emulators object, add the firestore key with the port and rules file path.
3
Write a test script to check read access
Create a file named test.rules.js and write code that imports @firebase/rules-unit-testing, initializes a test app with project ID demo-project and Firestore emulator at port 8080, then attempts to read from test/doc1 collection using assertSucceeds
Firebase
Hint
Use initializeTestEnvironment with the project ID and emulator port, then test reading the document with assertSucceeds.
4
Complete firebase.json with emulator settings
Add the emulators section to firebase.json with firestore emulator on port 8080 and rules set to firestore.rules, and add ui emulator on port 4000
Firebase
Hint
Add the UI emulator settings inside the emulators object with enabled true and port 4000.
Practice
(1/5)
1. What is the main purpose of using the Firebase emulator when testing Firestore security rules?
easy
A. To safely test rules without affecting live data
B. To deploy rules directly to production
C. To speed up Firestore queries in production
D. To backup Firestore data automatically
Solution
Step 1: Understand the role of the emulator
The Firebase emulator creates a safe environment that mimics Firestore without touching live data.
Step 2: Identify the benefit of safe testing
This allows developers to test security rules without risking data loss or exposure in production.
Final Answer:
To safely test rules without affecting live data -> Option A
Quick Check:
Emulator = Safe testing environment [OK]
Hint: Emulator means safe testing, no live data risk [OK]
Common Mistakes:
Thinking emulator deploys rules to production
Confusing emulator with data backup tool
Assuming emulator speeds up production queries
2. Which command correctly starts only the Firestore emulator?
easy
A. firebase start firestore
B. firebase emulators:start --only firestore
C. firebase emulators:run firestore
D. firebase firestore:start
Solution
Step 1: Recall the correct Firebase CLI command
The command to start emulators selectively uses 'firebase emulators:start --only' followed by the service name.
Step 2: Match the command to Firestore emulator
For Firestore only, the command is 'firebase emulators:start --only firestore'.
Final Answer:
firebase emulators:start --only firestore -> Option B
Hint: Use --only with service name to start specific emulator [OK]
Common Mistakes:
Using 'firebase start firestore' which is invalid
Confusing 'run' with 'start' in command
Omitting '--only' flag
3. Given this Firestore security rule snippet tested in the emulator:
service cloud.firestore {
match /databases/{database}/documents {
match /messages/{msgId} {
allow read: if request.auth != null;
allow write: if false;
}
}
}
What will happen if an unauthenticated user tries to read a message?
medium
A. Read is denied
B. Write is allowed
C. Read is allowed
D. Write is denied
Solution
Step 1: Analyze the read rule condition
The rule allows read only if request.auth is not null, meaning user must be authenticated.
Step 2: Check the user's authentication status
The user is unauthenticated, so request.auth is null, failing the condition.
Final Answer:
Read is denied -> Option A
Quick Check:
Unauthenticated read = denied [OK]
Hint: Read allowed only if user is authenticated [OK]
Common Mistakes:
Assuming read is allowed without authentication
Confusing read and write permissions
Ignoring the request.auth check
4. You wrote this rule in the emulator but all writes are failing:
allow write: if request.auth.uid == userId;
What is the likely error causing the failure?
medium
A. request.auth.uid is always null in emulator
B. Emulator does not support write rules
C. Write rules cannot use '==' operator
D. The variable 'userId' is not defined in the rule context
Solution
Step 1: Check the rule variable usage
The rule compares request.auth.uid to userId, but userId is not defined or extracted from the document path or data.
Step 2: Understand rule variable scope
Variables like userId must be defined in the path or request data; otherwise, the condition fails.
Final Answer:
The variable 'userId' is not defined in the rule context -> Option D
Quick Check:
Undefined variable causes rule failure [OK]
Hint: Ensure all variables in rules are properly defined [OK]
Common Mistakes:
Assuming emulator nullifies request.auth.uid
Thinking '==' is invalid in rules
Believing emulator ignores write rules
5. You want to test that only users can write their own profile document in the emulator. The document path is /users/{userId}. Which rule correctly enforces this and can be tested in the emulator?
hard
A. allow write: if true;
B. allow write: if request.auth.uid != userId;
C. allow write: if request.auth.uid == userId;
D. allow write: if request.auth != null;
Solution
Step 1: Match userId from document path
In rules, userId is a variable from the path /users/{userId} and can be used in conditions.
Step 2: Enforce write only if authenticated user matches userId
The condition 'request.auth.uid == userId' ensures only the user can write their own profile.
Final Answer:
allow write: if request.auth.uid == userId; -> Option C
Quick Check:
User writes own doc = request.auth.uid == userId [OK]
Hint: Match auth uid to path userId for user-specific writes [OK]
Common Mistakes:
Using '!=' instead of '==' causing wrong access
Allowing any authenticated user to write all profiles
Setting rule to always true, no security
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.