Firebase allows developers to launch applications at lightspeed. However, leaving default security rules unchecked exposes your datastores to data theft and manipulation. Securing Firestore documents requires understanding granular request matching.
1. Restrict Global Writes
Never leave your rules as allow read, write: if true;. Always restrict write options to authenticated administrators by verifying request.auth != null. This ensures only logged-in managers with administrative credentials can modify your content.
2. Granular Collection Paths
Write explicit rules matching specific collections rather than wildcards. For example, allow public read access on portfolios but restrict careers or contact submissions strictly to authenticated operations. Keeping paths isolated limits exposure.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /blogs/{blog} {
allow read: if true;
allow write: if request.auth != null;
}
}
}
3. Data Schema Validation
Verify incoming payloads directly inside rules. Ensure new documents contain required fields and match primitive types: request.resource.data.title is string. This prevents database corruption from malicious API scripts or typos during manual edits.