Back to Blog
Published:
Last Updated:
Fresh Content

Zero-Trust Multi-Tenant Firestore: Middleware, Claims, and 60+ Wildcard Permissions

9 min read
1,700 words
high priority
Muhammad Mudassir

Muhammad Mudassir

Founder & CEO, Cognilium AI

Zero-Trust Multi-Tenant Firestore: Middleware, Claims, and 60+ Wildcard Permissions — Cognilium AI

TL;DR

Hard tenant isolation on Firestore is not a query-pattern choice — it is a middleware layer, an immutable claim source, and a permission model with wildcards.

Hard tenant isolation on Firestore is not a query-pattern choice — it is a middleware layer, an immutable claim source, and a permission model with wildcards. The architecture that makes cross-tenant data leakage structurally impossible.
multi-tenant SaaSFirestore securitytenant isolationFirebase custom claimsRBACwildcard permissionszero-trust architecture

Hard multi-tenancy on Firestore — where tenant A literally cannot see tenant B's data — is not a query-pattern. You cannot "remember to filter by org_id" your way to security. It has to be a middleware layer, an immutable claim source, and a permission model that expands cleanly without granting more than intended. The architecture below is what shipped.

TenantContextMiddleware

Every API request hits a middleware that does three things: reads the immutable Firebase custom claim, looks up organizations/{orgId}/permissions in Firestore, and attaches a request-scoped context to the request object. Every downstream operation reads from request.context — never from the raw token, never from the URL, never from a header the client controls.

  • Custom claim: set at signup by an admin SDK action; immutable until rotated
  • Permissions doc: editable by org owners; read-only at request time
  • Context: {orgId, userId, roles[], permissions[]} — all derived, none client-supplied

Firestore queries: scoped at the path layer

Every collection lives under organizations/{orgId}/. There is no top-level documents collection — only organizations/{orgId}/documents. A query that omits the orgId path segment fails at dispatch. The system has no way to "accidentally" query across tenants because the path itself enforces scope.

Permissions: 60+ wildcard-driven

Permission strings: "{resource}:{action}:{scope}". Examples: "documents:read:org" (read documents in own org), "documents:*:org" (any action on documents), "billing:read:platform" (read billing across platform — admin-only). 5 platform/org roles map to permission sets; assignment is per-user.

  • Wildcard expansion at permission-check time, not at storage. "documents:*:org" expands to read+write+delete+list when checked.
  • Scope is the third segment — "org" stays in the user's tenant; "platform" cross-tenant; "self" only the user's own resources.
  • 60+ raw permissions; 5 roles bundle them; assignment is per-user with override support.

Secrets: backend-only, never serialized

Tenant secrets (API keys, tokens, OAuth refresh tokens) live in a backend-only collection. Firestore security rules deny all client reads. Only the backend service account can read them, only at the moment of use. Secrets never travel to the frontend, never appear in any response payload, never get logged.

What can still go wrong

  • A developer forgets the middleware on a new endpoint — the query fails because of path scoping, but error messages might leak existence info. Mitigation: 401 on auth failure, generic 404 on missing resource, never 403.
  • A client manipulates a URL parameter to access another org's resource — middleware reads the immutable claim, ignores the URL, scopes the query correctly.
  • A wildcard expansion grants more than intended — quarterly permission audit + a test suite that asserts no wildcard expands to a cross-scope grant.
  • Custom claim rotation gap — there is a 1-hour window where the old claim is still valid. Mitigation: rotate-and-revoke is a two-step process documented in runbooks.

Numbers from production

  • 60+ permissions, 5 roles, 15 Firestore composite indexes
  • 7 alert policies (latency, error rate, auth failures, permission denials, cross-tenant attempts)
  • 8 Cloud Scheduler jobs for periodic audits and maintenance
  • P95 endpoint latency: 5s alert; permission-check latency: 5-10ms (cached)

What this architecture is and is not

It is hard tenant isolation, defense in depth, and a permission model that survives growth. It is not a complete zero-trust architecture (which also requires mTLS between services, secrets management beyond Firestore, audit logging, and incident-response runbooks). The architecture above is the part that lives in the application code; the rest belongs in infrastructure.

Share this article

Muhammad Mudassir

Muhammad Mudassir

Founder & CEO, Cognilium AI | 10+ years

Mudassir Marwat is the Founder & CEO of Cognilium AI. He has shipped 100+ production AI systems acro...

Founder & CEO of Cognilium AI; 100+ production AI systems shipped; multi-cloud AI architecture (AWSGCPAzure); built and operated 4 production AI products
Agentic AIRAG → GraphRAG retrievalVoice AIMulti-Agent Orchestration

Frequently Asked Questions

Find answers to common questions about the topics covered in this article.

Still have questions?

Get in touch with our team for personalized assistance.

Contact Us

Related Articles

Continue exploring related topics and insights from our content library.

The 8-Stage Document Intelligence Pipeline
11 min
1
Muhammad Mudassir
May 5, 2026

The 8-Stage Document Intelligence Pipeline

Parse, classify, evidence-map, extract, validate, score, graph, cross-document-link. The eight-stage pipeline that turns unstructured legal/financial PDFs into validated structured data with mislink detection at the end.

words
Read Article
Gemini-Driven Entity Disambiguation With Post-Creation Mislink Detection
7 min
2
Muhammad Mudassir
May 5, 2026

Gemini-Driven Entity Disambiguation With Post-Creation Mislink Detection

Auto-merging "Acme Corp" with "Acme Corporation" is the easy half. The hard half is catching the merges that should not have happened — a re-check pass after creation that flags 3% of merges as suspect.

words
Read Article
Smart Category Routing for Contract Review
6 min
3
Muhammad Mudassir
May 5, 2026

Smart Category Routing for Contract Review

A focused application of the LLMOps routing pattern to legal contract analysis — the analyst-selection logic that ships fewer clauses to fewer agents and finishes a 3,300-call review in 154 seconds.

words
Read Article

Explore More Insights

Discover more expert articles on AI, engineering, and technology trends.