Firebase Security Rules Testing Guide: Firestore and Storage Validation Before Release
Security RulesFirestoreCloud StorageTestingAccess Control

Firebase Security Rules Testing Guide: Firestore and Storage Validation Before Release

FFirebase Live Editorial Team
2026-12-01
7 min read

A practical Firebase Security Rules testing workflow for Firestore and Storage, covering authentication, ownership, roles, queries, validation, and release che…

Testing Firebase Security Rules before deployment helps you find unauthorized reads, writes, and file uploads while the fix is still local and reviewable. This guide provides a repeatable workflow for Firestore and Cloud Storage, including scenario-based test cases, emulator commands, authentication states, and a release checklist.

Overview

Firebase Security Rules are authorization boundaries, not merely input filters. A complete test plan should verify both who can access a resource and what that user is allowed to do with it. For Firestore, that includes reads, creates, updates, deletes, collection queries, document ownership, and role-based access. For Cloud Storage, it also includes path ownership, file size, content type, and metadata validation where those conditions are part of your rules.

The safest default workflow is to test against local emulators before deploying rules to a shared or production project:

  1. Describe the data paths, storage paths, and access policy.
  2. Create representative users and authentication claims for each role.
  3. Start the Firestore and Storage emulators.
  4. Seed only the data required for the test cases.
  5. Run tests for allowed and denied operations.
  6. Review query behavior, validation rules, and edge cases.
  7. Deploy rules only after the test suite passes and the change has been reviewed.

The Firebase Emulator Suite is useful for this workflow because it allows rules to be exercised without modifying production data. For a repeatable team process, keep emulator configuration, seed data, and test code in the repository. A separate local-development workflow can help with data seeding and team resets; see the Firebase Emulator Suite workflow guide.

Do not use the Admin SDK as a substitute for client-side rules tests. Admin SDK requests generally operate with administrative privileges and therefore do not represent the access path used by a browser or mobile client.

Checklist by scenario

Unauthenticated access

  • Attempt to read each collection or document that should be private.
  • Attempt to create, update, and delete documents without a signed-in user.
  • Attempt to list a collection when only individual document access is intended.
  • For Storage, try reading and uploading to protected paths without authentication.
  • Confirm that public resources are public intentionally, rather than because a broad wildcard permits them.

Authenticated ownership

  • Sign in as the document owner and verify the permitted read or write succeeds.
  • Sign in as a different user and verify access to the owner’s document is denied.
  • Test create operations where the new document’s user identifier must match the authenticated user.
  • Test updates that attempt to change immutable ownership fields.
  • Test deletes separately; delete permission should not be assumed from read or update permission.

Roles and custom claims

  • Create test identities for every role used by the application, such as member, moderator, and administrator.
  • Verify that role checks deny users with no claim, an unexpected claim value, or a stale role.
  • Test both role-specific document access and role-specific collection queries.
  • Include a case where a user tries to add or modify their own role field.
  • When roles are stored in custom claims, account for token refresh behavior in the test setup.

Keep authorization logic in the rules and treat client-provided role fields as untrusted input. A custom-claims workflow should also define how claims are assigned, refreshed, and revoked; the Firebase Custom Claims guide provides a useful companion checklist.

Validation and partial updates

  • Try a valid document with all required fields and expected data types.
  • Omit each required field one at a time.
  • Supply an unexpected type, such as a string where a timestamp or number is expected.
  • Add an unapproved field to test whether the rules reject unknown data.
  • Attempt to change protected fields during an update.
  • Test both complete writes and partial updates; their rule expressions may evaluate differently.

Firestore queries

  • Test a query that includes the constraints required by the rules.
  • Test the same query without the ownership or tenant filter.
  • Test ordering, limits, and pagination patterns used by the application.
  • Verify that a query cannot return a mixture of permitted and forbidden documents.
  • Test collection group queries separately if your application uses them.

Firestore evaluates whether a query could return unauthorized documents; it does not simply remove forbidden results after the query runs. This makes query tests essential. Document reads may pass while a collection query fails because the query does not constrain the result set sufficiently. For related collection and index decisions, see the Firestore data modeling guide.

Cloud Storage paths and metadata

  • Upload to the exact path format used by the client.
  • Try a sibling user’s path and a path with altered user identifiers.
  • Test allowed and disallowed content types.
  • Test files below, at, and above the configured size boundary.
  • Test metadata requirements, including missing or unexpected metadata.
  • Verify reads, updates, and deletes independently.

A practical coverage matrix should include rows for unauthenticated, owner, other authenticated user, and privileged role. Add columns for read, create, update, delete, query or list, validation, and boundary conditions. Mark every cell as allow, deny, or not applicable, then connect each cell to a named test.

What to double-check

Use the local emulators during development and continuous integration. A typical CLI workflow is to start only the services under test, for example:

firebase emulators:start --only firestore,storage

The exact test command depends on your language and project setup, but the important properties are consistent: tests should create isolated data, authenticate as explicit identities, assert the result of each operation, and clean up or reset state between runs. Use the rules-testing APIs appropriate to your chosen SDK or test environment, and keep emulator versions aligned with the project tooling used by the team.

For each rules change, double-check these areas:

  • Authentication state: Is the test truly unauthenticated, or is it using a leftover signed-in context?
  • Claims: Are role claims represented exactly as the rules expect? Is the test token refreshed when claims change?
  • Existing versus new data: Do update tests compare the prior document with the proposed document?
  • Tenant boundaries: Can a user alter an identifier, path segment, or query constraint to reach another tenant?
  • Field validation: Are required fields, types, allowed values, and immutable fields covered?
  • Queries: Does every production query satisfy the rule constraints and required indexes?
  • Storage metadata: Are path, size, content type, and metadata checks tested at their boundaries?
  • Deployment target: Are you deploying the intended rules files to the intended Firebase project?

Keep the rules review separate from performance testing, but connect the results. A restrictive rule can expose an inefficient query pattern, while a broad query can create both authorization and cost concerns. The Firestore cost optimization guide can be used alongside this checklist when reviewing query behavior.

Common mistakes

  • Testing only successful cases: A test suite that proves the owner can read a document says little about whether another user can read it. Every allow case should have a corresponding denial case where practical.
  • Using production data for rule experiments: Local fixtures are safer, easier to reset, and easier to review.
  • Assuming nested paths inherit the intended policy: Review each collection, subcollection, and Storage path explicitly. A rule for one path pattern may not cover another.
  • Trusting client-side checks: Hiding a button or disabling a form does not enforce authorization. Rules must reject the operation server-side.
  • Testing with the Admin SDK only: Administrative clients can bypass the conditions that ordinary users encounter.
  • Ignoring update semantics: An update that changes ownership, tenant identifiers, or role fields needs its own assertion.
  • Deploying rules without a project check: Confirm the active Firebase project and inspect the files included in the deployment command.
  • Leaving broad development rules in place: Temporary rules such as unrestricted access should be treated as a local-only condition with an explicit removal task.

When to revisit

Run this Firebase Security Rules testing checklist before every release that changes a collection, subcollection, Storage path, authentication flow, custom claim, query, or file-upload policy. Revisit it when a new user role or tenant model is introduced, when a client changes its query shape, or when emulator and rules-testing tooling changes.

For a practical release gate, require the following:

  1. Rules and test fixtures are committed together.
  2. All scenario-matrix tests pass locally and in continuous integration.
  3. At least one denial case exists for each sensitive read and write.
  4. Ownership, role access, query constraints, validation, and Storage boundaries have been reviewed.
  5. The deployment target has been verified.
  6. A post-deployment smoke test uses a non-administrative client against the intended environment.

Afterward, record any new path or permission in the matrix so the next review starts from the current system rather than memory. Treat the matrix as living security documentation: update it before seasonal planning cycles, major feature launches, and changes to development or deployment tools.

Related Topics

#Security Rules#Firestore#Cloud Storage#Testing#Access Control
F

Firebase Live Editorial Team

Technical Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.