Topic 1 of 4 · 4 min read
Permission-aware retrieval for every user
Goal: Design query-time access filtering for a multi-tenant index and find the path in a given design where one user can retrieve another's data.
Filter by permission inside the retrieval query, before any text reaches the model; a prompt that says "do not reveal other tenants' data" is not an access control.
Check yourself · 1 of 3
A multi-tenant RAG app retrieves chunks for every question. Where should the rule "users see only their own tenant's documents" be enforced?
Why it matters
A RAG index is a search engine over everything you ingested. If the HR handbook, the board minutes and every customer's contracts sit in one vector store, then a nearest-neighbour search returns whatever is closest to the question, whoever asks. Once a chunk is in the prompt, the model can quote it, summarise it or be tricked into repeating it. OWASP lists missing access control in vector stores as a core weakness (LLM08:2025).
How to do it
- Store the access rule with every chunk. At ingestion, copy the source's permissions into metadata:
tenant_id, plusallowed_groupsorallowed_users, and the source document id. - Take identity from the session, never from the request body or the prompt. The user id and tenant come from your auth layer, after the token is verified.
- Apply the filter inside the vector query, as a pre-filter, so forbidden chunks are never scored or returned. Filtering after retrieval (post-filter) can return fewer than k results and still leaks through scores and counts.
- Re-check at the source when it matters. Permissions change. A chunk indexed last month may belong to a document the user lost access to yesterday, so sync ACL changes quickly or confirm with the source system before showing text.
- Apply the same filter to every path: the keyword search in a hybrid setup, the reranker's candidate pool, caches and "similar questions" features.
Before and after
# Before: the model is asked to behave
chunks = index.search(embed(question), top_k=8)
prompt = f"Only answer from documents the user may see. User: {user.id}\n{chunks}\n{question}"
# After: the database enforces it
chunks = index.search(
embed(question),
top_k=8,
filter={
"tenant_id": session.tenant_id,
"allowed_groups": {"$in": session.group_ids},
},
)
In the first version the model sees every tenant's text and is trusted to keep quiet. In the second, other tenants' text never leaves the store.
What goes wrong
- Filter value from the client. If
tenant_idarrives in the JSON body, any user can type another tenant's id. - A shared cache. A semantic cache keyed only on the question returns tenant A's answer to tenant B.
- Stale ACLs. Permissions copied once at ingestion stay open after someone is removed from a group.
- Unfiltered side doors. The main query is filtered, but a "related documents" endpoint or the BM25 index is not.
Examples
The tenant id in the body
app.post("/ask", async (req, res) => {
const { question, tenantId } = req.body;
const hits = await index.search(question, { filter: { tenant_id: tenantId } });
res.json(await answer(question, hits));
});
The filter exists, but tenantId is chosen by the caller. Replace it with req.session.tenantId, set after authentication.
The answer cache
A support bot caches answers by question text to save cost. Customer A asks "What is our refund limit?" and the answer, built from A's private contract, is cached. Customer B asks the same sentence and gets A's figure. Key the cache on tenant and permission set, or do not cache answers built from private chunks.
Key points
- Access control belongs in the retrieval query, not in a prompt instruction to the model.
- Store tenant and ACL metadata on every chunk at ingestion and keep it in sync with the source.
- Take the user's identity from the verified session, never from the request body or the question.
- Pre-filter inside the vector query; post-filtering can leak through scores, counts and thin results.
- Apply the same filter to keyword search, rerankers, caches and every other path to the index.
Common mistakes
- Telling the model in the system prompt to hide documents the user should not see.
- Filtering results after retrieval, so forbidden chunks are still scored and sometimes shown.
- Reading the tenant or user id from a field the client controls.
- Securing the vector query but forgetting the keyword index, the cache or a related-items endpoint.
Check yourself · 2 of 3
Which value should the tenant filter of a retrieval query use?
Check yourself · 3 of 3
A bot filters by tenant, but its answer cache is keyed only on the question text. What can happen?
Practice items, not counted towards your mastery.
Start here
- LLM08:2025 Vector and Embedding Weaknesses (OWASP GenAI) (opens in a new tab)DocsFree
Names access-control gaps, data poisoning and embedding inversion as the core risks of vector stores.
- PoisonedRAG: Knowledge Corruption Attacks to Retrieval-Augmented Generation (opens in a new tab)PaperFree
The research behind planting a few texts in a knowledge base to steer answers for chosen questions.
- Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection (opens in a new tab)PaperFree
The paper that named indirect prompt injection and showed it against retrieval-based apps.
More for this topic
- The lethal trifecta for AI agents (Simon Willison) (opens in a new tab)ArticleFree
A short rule for when an app that reads untrusted text can leak private data.