Sub topic 14
Enforcing permissions in search results
This chapter assumes you know how filters work in a search query (see the chapter on query design) and how index updates flow from the source of truth (see the chapter on syncing data). It covers how to keep users from seeing documents they are not allowed to read.
Why permissions are a search problem
A search service returns matches from its own copy of the data. It does not know your application's access rules unless you give them to it. If the index holds every document and the query ignores permissions, a user can read titles and snippets of documents they could never open. Counts and facets can leak information too, such as "12 results in Legal".
Do not filter after the search
A common first attempt is to run the query, then drop the results the user cannot see. This breaks in three ways:
- Pages come back short. A page of 10 hits might shrink to 3 after filtering.
- Total counts and facet buckets still include hidden documents.
- Deep pagination gets slow, because you must fetch and check many hits to fill one page.
Apply the permission check inside the search query, so the engine only ranks and counts documents the user may see.
Store access data on the document
Add a field with the principals (users or groups) allowed to read each document. Map it as an exact-match field.
PUT /documents
{
"mappings": {
"properties": {
"title": { "type": "text" },
"body": { "type": "text" },
"allowed_groups": { "type": "keyword" },
"owner_id": { "type": "keyword" }
}
}
}At query time, look up the caller's groups from your auth system. Never take them from the request body. Then add them as a filter.
GET /documents/_search
{
"query": {
"bool": {
"must": [ { "match": { "body": "quarterly budget" } } ],
"filter": [
{ "bool": {
"should": [
{ "terms": { "allowed_groups": ["finance", "all-staff"] } },
{ "term": { "owner_id": "user-17" } }
],
"minimum_should_match": 1
} }
]
}
},
"size": 10
}The filter clause does not change relevance scores, and the engine can cache it. Facets and counts computed on this query only cover permitted documents.
Keep permissions in sync
Permission data is part of what you sync. When a document is shared, or a user leaves a group, the index must change too. Two cases behave differently:
- Changing a document's own access list means reindexing that one document. This is cheap.
- Changing group membership is cheap if you filter on group names, because the user's group list is read at query time. It is expensive if you copied each user's ID into every document.
Prefer indexing groups or roles over individual users where your model allows it.
Because of eventual consistency, a revoked permission can stay visible in the index for a short time. For sensitive data, add a second check. After the search returns IDs, verify access to each hit against the source of truth before showing it. Treat this as a safety net, not a replacement for the query filter.
Common mistakes
- Putting the filter in the
mustclause when you meant a filter. This works but affects scoring. - Mapping
allowed_groupsastext. Analysis can split or lowercase values, so exact matches fail. - Forgetting a default for documents with no access list. Decide whether missing means public or private, and index an explicit value.
- Leaking data through suggestions. Autocomplete queries need the same filter.
Exercise
A search page shows "Results: 48" but the user can open only 30 of those documents, because the app removes forbidden hits after the search. Name two ways to fix this and one reason the fix still needs a source-of-truth check for sensitive data.
Show answer
Add the user's groups as a filter inside the search query, so counts, facets, and pages only cover permitted documents. Also index an explicit access field (such as allowed_groups) on every document, with an agreed default for missing values. A source-of-truth check is still useful because the index is eventually consistent. A permission revoked moments ago may still be present in the index, so verifying each hit before display closes that gap.