Prompt
How do I build permissions-aware search for private content?
Latest observation
To build permissions-aware search for private content, the core rule is:
A user should only retrieve documents they’re already allowed to access.
That means search must enforce authorization at query time, not just after results are returned.
1) Start with an authorization model
Define who can see what in a structured way, for example:
- User
- Group / role / team
- Resource
- Permission:
read,write,admin, etc.
Common patterns:
- ACLs: each document stores allowed users/groups
- RBAC: roles grant access to sets of documents
- ABAC: policies based on attributes like department, project, classification
- Hybrid: most real systems use this
2) Represent access in search-friendly form
Your search index needs enough metadata to filter results.
Typical document fields:
document_idtitle,body,tagsowner_idallowed_user_idsallowed_group_idsvisibility(public,private,internal)tenant_idif multi-tenant
Example searchable document:
{
"id": "doc_123",
"title": "Quarterly plan",
"body": "…",
"allowed_user_ids": ["u1", "u9"],
"allowed_group_ids": ["g2", "g5"],
"tenant_id": "t1"
}
3) Filter before ranking if possible
When a user searches, first compute the set of permissions they have, such as:
- user ID
- groups they belong to
- roles/attributes
- tenant or org scope
Then issue the search query with permission filters:
tenant_id = current_tenantallowed_user_ids contains current_userallowed_group_ids intersects current_groups- or
visibility = public
This prevents unauthorized docs from ever entering the result set.
4) Prefer “security trimming” in the search backend
If your search engine supports it, use security trimming:
- Elasticsearch/OpenSearch: filter clauses
- PostgreSQL full-text:
WHEREclauses - Solr: filter queries
- Dedicated permission-aware search systems
Example logic:
SELECT *
FROM documents
WHERE tenant_id = :tenant
AND (
visibility = 'public'
OR owner_id = :user
OR allowed_user_ids @> ARRAY[:user]
OR allowed_group_ids && :user_groups
)
AND to_tsvector(title || ' ' || body) @@ plainto_tsquery(:query);
5) Be careful with post-filtering
A common mistake is:
- Search returns top 100 docs
- Application removes unauthorized ones
This can cause:
- information leakage via snippets/counts
- incomplete ranking because unauthorized docs affected scoring
- empty pages even though accessible docs exist
- timing/side-channel risks
If you must post-filter, fetch a larger candidate set and re-rank after filtering, but this is still weaker than backend filtering.
6) Handle snippets and facets securely
Search results expose more than titles.
Protect:
- snippets
- highlighted text
- facet counts
- autocomplete suggestions
- “did you mean”
- result totals
These should all respect permissions, or they can leak private content existence.
7) Index permissions carefully
You have two main design choices:
A. Store ACLs on each document
Good for:
- smaller/medium systems
- straightforward filtering
Downside:
- large ACL arrays can bloat index size
- frequent permission changes require reindexing
B. Use document classification + external permission lookup
Good for:
- large ACLs
- more dynamic policies
Downside:
- query-time joins can be expensive
- harder to make fast and consistent
A common compromise:
- index coarse access tags in search
- keep fine-grained checks in an authorization service
- verify on result access, not just in search
8) Support multi-tenant isolation
If you have tenants/orgs, always include tenant scoping:
- separate index per tenant, or
- shared index with mandatory
tenant_idfilter
Separate indices simplify isolation, but shared indices are easier operationally. In either case, tenant checks should be mandatory.
9) Consider query-time authorization vs indexing-time authorization
Query-time auth
Check permissions when searching.
- more flexible
- accurate for changing permissions
- usually preferred
Index-time auth
Precompute visibility lists or per-user indices.
- fast search
- but expensive to maintain
- hard with frequent permission changes
Per-user indexes are usually not scalable except in small systems.
10) Add defense in depth
Even if search filters results:
- re-check access when opening a document
- log denied access attempts
- rate-limit suspicious queries
- audit permission changes
- test for leakage in snippets and counts
11) Example architecture
A practical setup:
- Auth service stores users, groups, policies.
- Search index stores content plus coarse access metadata.
- On search request:
- authenticate user
- fetch user groups/attributes
- build permission filter
- query search engine with filter
- When a result is opened:
- verify permission again in the app/service layer
12) Good implementation patterns by system
Elasticsearch / OpenSearch
Use a bool query with must for text and filter for permissions.
PostgreSQL
Use full-text search plus WHERE permission clauses.
Vector search / semantic search
Important: vector similarity alone is not permission-aware. You must filter candidate vectors by permissions, or use a permission-aware retrieval pipeline:
- retrieve authorized candidates only
- then embed/rank among them
13) Common pitfalls
- forgetting autocomplete/search suggestions
- leaking existence through result counts
- not filtering snippets/highlights
- storing permissions only in the app layer
- failing to update index when access changes
- using post-filtering on a small candidate set
- not scoping by tenant
14) Minimal rule of thumb
If you want a simple and safe design:
- Store
tenant_id - Store
allowed_user_idsandallowed_group_idsor a visibility label - Query with mandatory permission filters
- Re-check access before serving the document
- Make snippets/facets permission-aware
If you want, I can also show:
- a concrete Elasticsearch query example,
- a PostgreSQL design, or
- a vector search permission-aware architecture.