Prompt
How do I make permissions-aware search work?
Latest observation
Permissions-aware search usually means: users only see search results for content they’re allowed to access. The exact implementation depends on your stack, but the core pattern is the same.
1) Store permissions in a queryable form
You need each document/item to carry access-control metadata such as:
- owner
- tenant/org/project
- roles allowed
- users allowed
- groups allowed
- visibility level
Example document fields:
{
"id": "doc_123",
"title": "Q4 roadmap",
"content": "...",
"tenant_id": "acme",
"allowed_user_ids": ["u1", "u7"],
"allowed_group_ids": ["g3"],
"allowed_roles": ["admin", "manager"]
}
2) Filter at search time, not after
The safest approach is to apply permission filters in the search query itself so unauthorized docs never come back.
Example logic:
- user can see docs where:
tenant_id = user's tenant- AND one of:
allowed_user_ids contains user.idallowed_group_ids intersects user.groupsallowed_roles intersects user.rolespublic = true
3) Precompute access if needed
If permissions are complex, you may want to precompute an “access index”:
- per document: list of principals that can access it
- principals = users, groups, roles, orgs
This makes search filters simpler and faster.
4) Keep search and auth in sync
Whenever permissions change, you must update the search index immediately or via a reliable pipeline.
Examples:
- document shared with a group → reindex doc
- user removed from group → no reindex needed if permissions are checked dynamically at query time against current group membership, but if ACLs are denormalized into the index, reindex may be required
5) Prefer deny-safe defaults
If permission data is missing or stale, default to not showing the document.
6) Common implementation patterns
Pattern A: Query-time ACL filtering
Best for:
- moderate ACL complexity
- search engines with filtering support
Pros:
- simpler
- secure
- no post-filter leakage
Cons:
- can be expensive with huge ACL lists
Pattern B: Index documents by visibility buckets
Example:
- public
- tenant-private
- role-based
- user-specific
Pros:
- fast
Cons:
- less flexible
Pattern C: Post-filtering after search
Not recommended as the only control, because:
- search engine may rank/count unauthorized docs
- pagination can leak info
- performance suffers
If you do it, use it only as a backup, not the primary security boundary.
7) Watch out for these pitfalls
- Leaking via counts/snippets: even showing total hits can reveal sensitive info
- Caching: cache must be permission-aware
- Facet leakage: category counts can expose restricted data
- Pagination drift: if you filter after search, page results become inconsistent
- Tenant isolation: always include tenant/org boundary if applicable
8) A simple rule of thumb
For each search request:
- identify the user
- determine their principals:
- user ID
- groups
- roles
- tenant/org
- add permission filters to the search query
- return only documents matching both relevance and ACLs
9) Example pseudo-query
SELECT *
FROM docs
WHERE tenant_id = :tenant
AND (
public = true
OR :user_id = ANY(allowed_user_ids)
OR allowed_group_ids && :group_ids
OR allowed_roles && :roles
)
ORDER BY relevance DESC
LIMIT 20;
If you tell me what search system you’re using — for example Elasticsearch/OpenSearch, Postgres full-text search, Algolia, Meilisearch, or Vespa — I can show you the exact way to implement permissions-aware search there.