Firestore Rules Generator
Build Firestore security rules and emulator tests from your data model, and check the rules you already have.
- Free
- No sign-up
- Your input stays in your browser
Using a template replaces the current plan. Your plan is saved in this browser only.
Who can do what
- Yes any document
- Own only documents they own
Click a cell to cycle it. get reads one document, list runs queries. Signed-in user includes everyone with a role. Set how ownership works on each collection below.
Roles
Anyone and Signed-in user are built in. Add the roles your app gives people.
Collections and fields
Paths can be nested, such as users/{userId}/notes. Listed fields become checks on every write.
Checks
Your rules
Import a plan JSON
Paste your rules or try an example. You’ll see who can read and write each path, and what to fix, with the line number and a fix for each problem.
These checks read the rules alone. They can’t see your data, your queries, or server code using the Admin SDK, which skips rules. Test changes in the emulator before you deploy.
Who can do what
Audit · Paid
Want the whole setup checked?
A review of your code, auth or AI setup, with clear findings on what to fix first.
Starts with a free 30-minute intro call, or email hello@nestedbit.com.
What Firestore security rules do
Your web or mobile app talks to Cloud Firestore directly, with a configuration that ships inside the app for anyone to read. Security rules are the only thing that decides which of those requests succeed. They run on Google’s servers for every read and write from a client SDK, and anything they don’t allow is denied.
Server code that uses the Admin SDK skips rules entirely, so a Cloud Function or your own backend can still manage every document. That makes rules a lock on the front door, not on the building: keep secrets and admin-only data where only server code reads it.
How to generate Firestore rules
- Start from a template close to your app: a personal app, a public blog, a team SaaS with organizations, or chat rooms.
- List your collections, including nested ones such as
users/{userId}/notes, and say how each document’s owner is found: an owner field, the document ID, or a user ID in the path. - Fill in the grid: who may get, list, create, update and delete, on any document or only their own.
- List the fields each collection holds, with their types, which are required, which can’t change, and which hold the server’s time.
- Read the checks, then download
firestore.rulesand the emulator tests, run the tests, and deploy.
Already built your roles in the RBAC Matrix Builder? Open this tool in the same browser and use your matrix as the starting point.
Firestore rules examples
Each user’s own data
The safest shape keeps a user’s data under their user ID, so one comparison covers every method, including queries.
match /users/{userId}/notes/{noteId} {
allow get, list, create, update, delete: if request.auth != null
&& request.auth.uid == userId;
}
Public reads, authors write
On create there’s no stored document yet, so check the new data. On update, check the stored owner, and list the fields an update may change, so an author can’t hand the post to someone else or change fields your app treats as fixed.
match /posts/{postId} {
allow get, list: if true;
allow create: if request.auth != null
&& request.resource.data.authorId == request.auth.uid;
allow update: if request.auth != null
&& resource.data.authorId == request.auth.uid
&& request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['title', 'body', 'updatedAt']);
allow delete: if request.auth != null
&& resource.data.authorId == request.auth.uid;
}
Roles from custom claims
Custom claims are set by server code and travel in the user’s ID token, so checking them costs no reads. A user picks up a changed claim the next time their token refreshes. You can inspect what a token carries with the JWT decoder.
// Set from server code: getAuth().setCustomUserClaims(uid, { role: 'editor' })
function hasRole(roles) {
return request.auth != null && request.auth.token.get('role', '') in roles;
}
match /posts/{postId} {
allow delete: if hasRole(['editor', 'admin']);
}
Members of an organization
For multi-tenant apps, keep data under the organization and each member’s role in a document only server code can write. Each check costs one document read.
function memberRole(orgId, roles) {
return request.auth != null
&& get(/databases/$(database)/documents/organizations/$(orgId)/members/$(request.auth.uid))
.data.role in roles;
}
match /organizations/{orgId}/projects/{projectId} {
allow get, list: if memberRole(orgId, ['admin', 'member', 'viewer']);
allow delete: if memberRole(orgId, ['admin']);
}
Checking the fields that are written
Rules can also check what’s written: which fields are allowed, their types and sizes, and that timestamps come from the
server. Without this, a client can add any field it likes, including one your code trusts, such as role.
function validCreate() {
let data = request.resource.data;
return data.keys().hasAll(['title', 'authorId', 'createdAt'])
&& data.keys().hasOnly(['title', 'body', 'authorId', 'createdAt'])
&& data.title is string && data.title.size() <= 200
&& (!('body' in data) || (data.body is string && data.body.size() <= 50000))
&& data.createdAt == request.time;
}
Test mode, and why your app stopped working
A database created in test mode starts with rules like these, open to anyone until a date about a month away:
match /{document=**} {
allow read, write: if request.time < timestamp.date(2026, 12, 1);
}
Until that date, anyone with your project ID can read, change and delete everything without signing in. After it, every request is denied, which is often the first sign. Moving the date forward reopens the database. Replace the rule with ones that check who is asking, collection by collection. Paste your rules into “Check my rules” to see whether the date has passed and what each path allows.
Mistakes the checker finds
- Open access:
if true, or an allow with no condition, on writes or on every collection. - Any signed-in user:
request.auth != nullon its own. With open sign-up, or anonymous sign-in, that’s anyone. - Recursive wildcards: a
{document=**}rule also applies to every collection below it, and rules that match the same document are combined, so the widest one wins. - Takeovers: an update rule that checks the owner in the new data instead of the stored data.
- Self-promotion: roles read with
get()from a document the same rules let users write. - Unchecked writes: no field list, no types, no size limits, and owners who can change any field.
- Email checks without
email_verified, and custom claims compared with the string'true'. - Missing
rules_version = '2'when collection group queries or recursive wildcards rely on it. - Cloud Storage uploads with no size or content type limit.
Rules are not filters
Firestore doesn’t drop the documents a user can’t read from a query: it refuses the whole query unless every document
it could return is allowed. A rule that lets users read their own posts needs a query that asks only for their posts,
such as where('authorId', '==', uid). If a list works in the emulator but fails in your app, compare the
query with the rule.
Test rules in the emulator
The generator also writes a test file with a test for each person, method and collection: a visitor, a signed-in user, each role and, for multi-tenant apps, someone from another organization. It checks the field rules too, such as refusing unknown fields and made-up timestamps. It runs on the Firebase Local Emulator Suite with Node’s built-in test runner:
npm install --save-dev @firebase/rules-unit-testing firebase
firebase emulators:exec --only firestore "node --test firestore.rules.test.mjs"
Keep the tests next to your rules and run them before every deploy, so a later change can’t quietly open something up.
Questions
Is it safe to paste my rules here?
Yes. Rules you paste are checked in your browser, and aren’t sent to NestedBit or anyone else, or stored. Rules hold no data or keys. The plan you build in “Generate rules” is saved in this browser only, so it’s there when you come back.
What are Firestore test mode rules?
The rules a database starts with in test mode: anyone can read and write everything until a date about a month after it was created, and nothing works after that date. Don’t move the date forward. Replace the rule with ones that check who is asking, collection by collection.
Is request.auth != null secure enough?
Only for data every user may see or change. It lets in anyone with an account, and with open sign-up or anonymous sign-in, that’s anyone. Check that the document belongs to the user, or that they hold a role.
Why do my rules allow more than the rule I wrote?
When several match statements cover a document, access is allowed if any of them allows it. A broad {document=**} rule, often left over from test mode, overrides every stricter rule below it. The checker lists each path with the widest access that applies to it.
Should roles come from custom claims or a document?
Custom claims are set from server code, travel in the ID token and cost no reads, but a change only reaches the user when their token refreshes. A roles document changes at once and costs one read per check. Either way, users must never be able to write their own role, which the checker looks for.
How do I test Firestore security rules?
Run them in the Firebase Local Emulator Suite. The generator writes a test file for @firebase/rules-unit-testing with a test for each person, method and collection, plus the field checks, and the command to run it with Node’s built-in test runner.
Does it check Cloud Storage rules?
Yes. Paste storage.rules into “Check my rules” and it checks them the same way, plus uploads with no size or file type limit. The generator writes Firestore rules only.
Do security rules apply to the Admin SDK?
No. Server code using the Admin SDK or a service account skips rules and is controlled by IAM instead. Rules only govern requests from the web and mobile SDKs.