Firebase - Firestore - Security Rules - Vibe coding - MVP
4 min lectura

Firestore MVP security: test that Bob cannot read Alice's notes

A private-notes Firestore rule and four access tests before inviting users. Covers Rules Playground, the emulator, and server SDK limitations.

Firestore MVP security: test that Bob cannot read Alice's notes

Your coding agent has wired up Firebase sign-in and a Firestore notes collection. The app works in your own account, but the important launch question is: can a second account open or change your notes? Hiding an Edit button in the UI is not an access control. Test Firestore Security Rules with two user IDs and an anonymous session before inviting users.

This walkthrough assumes one concrete path: users/{uid}/notes/{noteId}. The goal is to verify tenant isolation for an MVP you already built with Firestore.

A minimal rule for private notes

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/notes/{noteId} {
      allow read, write: if request.auth != null
                         && request.auth.uid == userId;
    }
  }
}

This compares the authenticated user's ID with the ID in the document path rather than trusting an editable field. Alice can access users/alice/notes/idea-1; Bob cannot. Paths without an applicable allow rule stay closed. Firebase documents request.auth and UID conditions in its rules guide.

Adapt the path first. If your notes live under notes/{noteId} with an ownerId field, this example does not protect them. Server SDKs also bypass Firestore Security Rules and use IAM, so this is not a replacement for server authorization. Never ship a service-account credential to the browser.

Four cases worth testing

IdentityActionPathExpected
AliceRead and createusers/alice/notes/idea-1Allow
BobRead and updateusers/alice/notes/idea-1Deny
Signed outReadusers/alice/notes/idea-1Deny
AliceReadusers/bob/notes/idea-2Deny

Test deletion too: write covers create, update, and delete. If the product supports shared notes, do not simply widen access to every signed-in user. Model collaborators and test invitation, removal, and revoked access explicitly.

For a multi-organization app, add a fifth case: two signed-in users from different workspaces. A personal two-account test will miss a query that crosses organizational boundaries. If your schema uses organizations/{orgId}, authorization needs a membership check, not merely a signed-in check. Run that case before a demo with real customer data.

Four Firestore private-note access tests: Alice allowed on her path, Bob and a signed-out session denied
Four minimum cases; adapt paths to your own database.

Fast manual check in Rules Playground

  1. In Firebase Console, open Firestore Database → Rules. Apply the example only in a test project or reviewed branch; your production project may have other collections that need their own rules.
  2. Open Rules Playground and simulate each row of the table with its exact path and user ID. The simulator checks rules in the editor, which may differ from what is deployed.
  3. Record every unexpected allow or deny, fix the rules, then rerun all four cases. A false denial can break your app as surely as a false allow can expose data.

Firebase explains this simulator in its getting-started guide. It is a quick review; automated tests protect the same scenarios when someone edits rules next month.

Repeatable local tests with the emulator

Save the rule in firestore.rules and point firebase.json at that file. Then run your test script under the emulator:

firebase emulators:exec --only firestore "npm test"

Use @firebase/rules-unit-testing to create authenticatedContext('alice'), authenticatedContext('bob'), and unauthenticatedContext(). Assert allowed operations with assertSucceeds and blocked ones with assertFails. Firebase's emulator documentation shows the setup and how to load the rules file. Clear test data between cases because the emulator keeps state while it runs.

There is a query trap: rules are not filters. A broad collection query that could return another person's notes may fail in full. Query under users/{uid}/notes using the signed-in ID and test the exact client query, not only a document read. Firebase documents this behavior in the conditions guide.

A prompt for your coding agent

Review Firestore rules for users/{uid}/notes/{noteId}.
Add tests for Alice, Bob, and a signed-out client.
Cover read, create, update, and delete.
Do not change permissions for unrelated collections.
Show me the rules diff and each test result before proposing deployment.

We have not run these tests against Joseph's Firebase project; your outcome depends on your actual paths and existing rules. If Bob or a signed-out client can read Alice's note, pause the launch. Once these cases pass, review list queries, other collections, and server-side endpoints before release. A passing rule test does not prove that every app route is secure.

Primary sources: Firebase's rule conditions, Rules Playground guide, and emulator test guide, checked November 17, 2026.

Comentarios

Aún no hay comentarios. Sé el primero.

Deja un comentario