Prompt
I'm building a multi-tenant app and need search that respects row-level permissions
Latest observation
For a multi-tenant app, the safest pattern is:
- Store tenant and permission scope in the data model
- Apply authorization filters at query time
- Make search index documents carry the same access metadata
- Enforce permissions again on the application side before returning results
Recommended design
1) Data model
At minimum, every row/document should include:
tenant_idowner_idorcreated_by- visibility flags or ACL references, such as:
publicteam_idsrole_ids- explicit
user_ids - resource-specific grants
Example:
{
"id": "doc_123",
"tenant_id": "tenant_a",
"title": "Quarterly Plan",
"content": "…",
"visibility": "restricted",
"owner_id": "user_42",
"allowed_user_ids": ["user_42", "user_77"],
"allowed_role_ids": ["manager"]
}
2) Search architecture
If you’re using an external search engine like Elasticsearch/OpenSearch/Meilisearch/Typesense, index the document plus access metadata. Then query with both:
- the search terms
- permission filters for the current user
Example filter logic:
tenant_id == current_tenant- AND one of:
public == trueowner_id == current_userallowed_user_ids contains current_userallowed_role_ids intersects user_roles
3) Avoid post-filtering only
Do not search first and then filter results in memory as your only control. That can cause:
- leaking result counts
- pagination bugs
- timing side channels
- accidental exposure if someone forgets to filter
Use pre-filtering in the search query whenever possible.
4) Database-backed search
If search is in SQL, use row-level predicates directly:
SELECT *
FROM documents
WHERE tenant_id = :tenant_id
AND (
visibility = 'public'
OR owner_id = :user_id
OR EXISTS (
SELECT 1
FROM document_acl acl
WHERE acl.document_id = documents.id
AND acl.user_id = :user_id
)
)
AND to_tsvector('english', title || ' ' || content) @@ plainto_tsquery(:q)
ORDER BY created_at DESC
LIMIT 20;
5) Use database RLS if available
If your database supports row-level security (for example PostgreSQL), it’s a strong choice for defense in depth.
- Enable RLS on tenant-owned tables
- Set session variables for current user/tenant
- Let the database enforce access automatically
This helps prevent accidental bypass through ad hoc queries.
6) Multi-tenant isolation
For stronger isolation, consider one of these approaches:
- Shared table + tenant_id filter: simplest, most common
- Schema per tenant: more isolation, more operational overhead
- Database per tenant: strongest isolation, highest overhead
Most apps start with shared tables and strict RLS.
7) Search index security
If using external search, remember the index is a copy of data. Treat it as sensitive:
- never index fields you wouldn’t want exposed
- secure the search service itself
- rotate credentials
- restrict direct client access if permission logic is server-side
- if clients query search directly, use scoped tokens or signed filters
Practical pattern
A solid implementation usually looks like:
- API authenticates user
- API loads tenant context and user permissions
- API builds a search query with mandatory ACL filters
- Search engine returns only permitted hits
- API re-validates authorization before responding
- Logs and metrics avoid exposing sensitive result content
If you want the simplest robust answer
Use:
tenant_idon every row- PostgreSQL RLS for base authorization
- a search index that stores
tenant_idand ACL metadata - server-side query building that always adds permission filters
If you want, I can give you:
- a PostgreSQL RLS example,
- an Elasticsearch/OpenSearch permission-filtering example, or
- a full schema design for document-level ACLs.