Prompt
How do I make sure search only returns documents the user can access?
Latest observation
Nov 14, 2026
OpenAI APIWeb search: off
To make search only return documents a user can access, you generally want security trimming: enforce permissions at query time and ideally also at index time.
Best practice
-
Store access control metadata on each document
- Example fields:
ownerId,tenantId,allowedGroupIds,isPublic,acl - Keep it normalized and easy to filter on.
- Example fields:
-
Filter search results by the user’s permissions
- When a user searches, include a permission filter in the query:
tenantId = currentTenant- AND (
isPublic = trueORownerId = currentUserORallowedGroupIds intersects userGroups)
- This ensures unauthorized docs never appear in results.
- When a user searches, include a permission filter in the query:
-
Apply authorization before returning details
- Even if search filters results, re-check permissions when fetching the full document or rendering a preview.
- Never trust the search result alone.
-
Use ACL-aware indexing if needed
- For systems with complex permissions, index a compact representation of access rules:
- direct user IDs
- group IDs
- roles
- Then filter using those fields.
- For systems with complex permissions, index a compact representation of access rules:
-
Prefer tenant-level partitioning when possible
- If your app is multi-tenant, separate indexes or partitions by tenant to reduce risk of cross-tenant leakage.
-
Avoid relying only on client-side filtering
- Don’t return all results and hide unauthorized ones in the UI. The backend must enforce access control.
Example pattern
If a user belongs to groups ["g1", "g3"], search query might become:
WHERE tenantId = 't1'
AND (
isPublic = true
OR ownerId = 'u123'
OR allowedGroupIds IN ('g1', 'g3')
)
Extra safety
- Log and audit search access.
- Make sure snippets/previews don’t leak sensitive content.
- Cache permission sets carefully so they don’t become stale.
- If permissions change often, update the index or use real-time filtering.
If you tell me what search system you’re using (Elasticsearch, SQL full-text search, Meilisearch, Algolia, etc.), I can show the exact way to implement it.