Consider the following Firebase Realtime Database security rule snippet:
{
"rules": {
".read": "auth != null && auth.uid == 'user123'"
}
}What happens when a user with UID 'user456' tries to read data?
Check the condition that compares the user's UID.
The rule allows read access only if the user is authenticated and their UID equals 'user123'. Since 'user456' does not match, the read is denied.
Given this Firestore security rule snippet:
service cloud.firestore {
match /databases/{database}/documents {
match /posts/{postId} {
allow write: if request.auth != null && request.auth.uid == resource.data.ownerId;
}
}
}What is the effect of this rule on write operations?
Look at the condition comparing request.auth.uid and resource.data.ownerId.
The rule allows write only if the user is authenticated and their UID matches the ownerId field of the post document, ensuring only owners can write.
You want to design Firebase Realtime Database rules where:
- Admins can read and write all data.
- Regular users can only read and write their own data.
Which rule snippet correctly implements this?
Consider how to check admin role and user identity in the same condition.
Option B allows read and write if the user is admin or if the user is accessing their own data. Other options either restrict admin access or allow too much access.
Which Firestore security rule snippet prevents users from changing the 'ownerId' field of a document during an update?
service cloud.firestore {
match /databases/{database}/documents {
match /items/{itemId} {
allow update: if request.auth != null && request.auth.uid == resource.data.ownerId && request.resource.data.ownerId == resource.data.ownerId;
}
}
}Look at the condition comparing request.resource.data.ownerId and resource.data.ownerId.
The rule ensures the user is authenticated and owner, and that the ownerId field remains unchanged during update, preventing unauthorized ownership changes.
You have a Firebase Realtime Database with many users. You want to optimize security rules to minimize rule evaluation time while ensuring:
- Users can read and write only their own data.
- Admins can read and write all data.
Which rule structure is best for performance and security?
Consider how to minimize rule checks and allow admin override efficiently.
Option C scopes user data under 'users/$uid' with per-user rules and adds top-level admin rules for all data, optimizing evaluation by limiting checks and allowing admin access.
