esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

How Do You Enforce Access Control in RAG?

Check who is asking before the search ranks anything, label every chunk with its permissions, and keep the model out of the decision. Explained for your AI interview.

In short

Work out who is asking on your own server, then apply their permissions as a filter inside the search query, on every request. Label each chunk with who may read it, and keep the model out of the decision about who sees what.

11 min readAdvancedCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

Access control in RAG means the search only returns chunks the person asking may read. I take the user's identity from the server's login, never from the question. I turn it into a filter and apply it inside the search query, on every request, before anything is ranked. Each chunk carries a permission label from when it was indexed. And the model never decides who sees what, because text the model reads can steer it.

What is access control in RAG, in plain words?

RAG means your app searches your own documents first, then hands the best matches to a language model, which writes the answer from them. Access control is the rule that decides which matches a particular person may see.

Picture a records clerk at a hospital. A nurse asks for a patient file. The clerk checks the nurse's badge before opening the cabinet, and only then pulls the file. RAG needs that same order: check who is asking, then search.

Take an insurer, say with around 40,000 claim files and two regional claims teams. An adjuster on the north team should never see a medical note from a claim that belongs to another team, even when that note matches their question best. A search that ignores the team would return it happily.

Key words

Chunk
A small piece of a document, stored and searched on its own.
Permission label
A tag on each chunk that says which people or groups may read it.
Identity
Who the logged-in user is, and which teams or groups they belong to.
Filter
A condition the search must obey, such as only chunks labelled for the north team.
Index
The searchable store where the chunks sit, each with its label.

Where does the permission check have to go?

Inside the search, before anything is ranked, on every query. The search should only ever see the chunks the user may read. If the search reads everything and the app hides results afterwards, the leak has already happened before the hiding step.

  • “similarity search frequently runs across the full index before access control is applied at the application layer”

    OWASP, LLM09:2026

  • “Enforce tenant scoping inside the index query, not as a post-retrieval filter, and validate it server-side.”

    OWASP, LLM09:2026

Filter before ranking, or after

Right: filter first

Apply permission filter

inside the search query

Rank allowed chunks

only what the user may read

Wrong: rank, then hide

Rank every chunk

includes files the user must not see

Hide what is not allowed

may leave too few results

Full, safe answer

two allowed chunks

Short answer

one chunk, and a leak

  1. 1The filter is applied inside the search, before ranking.
  2. 2Only allowed chunks are ranked, so the answer is full.
  3. 3Every chunk is ranked first, including files the user cannot see.
  4. 4Hiding afterwards can leave too few results, and the search has read them.
The top path applies the permission filter inside the search, so only allowed chunks are ranked and the answer is full. The bottom path ranks everything first and hides results later, so the answer can run short, and the search has already read files the user must not see.

OWASP's 2026 list gives this risk its own entry, LLM09, Vector and Embedding Weaknesses. It also says RAG is the most familiar case of it. The fix is to scope the search itself, so the question about access never comes second.

How do you store permissions and apply them?

  1. 1

    Get the identity from the login

    Your login system says who is asking and which teams they belong to. The question text never sets this.

  2. 2

    Label every chunk when it is indexed

    Each chunk keeps its own label. A confidential paragraph inside a public file gets its own label, not the file's.

  3. 3

    Build the filter in code

    Turn the user's teams into the filter on your server, and add it to every search. No teams means no results.

  4. 4

    Search with the filter inside the query

    The index applies the filter during the search, the way OWASP asks (inside the index query), so hidden chunks are not returned. Check that your engine filters during the search, not after it.

  5. 5

    Re-index when permissions change

    When a file's permissions change, update the labels on its chunks. If you push your own chunks, re-index the affected ones, because Microsoft's guide says custom or push ingestion requires re-indexing the affected documents. Delete a file's chunks when the file is deleted.

  • “Your application must authenticate users, obtain trusted principal and grant values, apply filters to every query, and prevent bypasses.”

    Microsoft, Azure AI Search guide

  • “Unresolved group memberships don't grant access.”

    Microsoft, Azure AI Search guide

Labels travel with each chunk

Claim file 91

a north team's claim file

Chunks, each labelled

Roof notes

label: north claims team

Medical notes

label: medical team, not north

Search index

keeps each chunk with its label

  1. 1The claim file is split into chunks at indexing.
  2. 2The medical paragraph gets its own, stricter label.
  3. 3The roof chunk goes in with the north team label.
  4. 4The medical chunk goes in with the medical team label.
One claim file is split into chunks when it is indexed. Each chunk keeps its own label, so the medical paragraph stays with the medical team even though it sits in a north file.

Here is the logic in a small program. It uses made-up claims and a stand-in search, with no network, and it runs the same question for the north team in both orders.

Python
# A simplified claims search: six chunks, each with the team allowed to read it.
CHUNKS = [
    {"id": 1, "team": "north", "text": "water damage kitchen claim paid in full"},
    {"id": 2, "team": "north", "text": "water damage roof claim under review"},
    {"id": 3, "team": "south", "text": "water damage flat medical notes claim"},
    {"id": 4, "team": "south", "text": "fire damage shop claim paid in part"},
    {"id": 5, "team": "north", "text": "water damage garage appeal claim open"},
    {"id": 6, "team": "south", "text": "water damage shop fraud check claim"},
]

def score(question, chunk):
    return len(set(question.split()) & set(chunk["text"].split()))

def search_filter_first(question, team, k=2):
    # The permission filter runs inside the search: only this team's chunks are ranked.
    allowed = [c for c in CHUNKS if c["team"] == team]
    ranked = sorted(allowed, key=lambda c: -score(question, c))
    return [c["id"] for c in ranked[:k]]

def search_filter_last(question, team, k=2):
    # Wrong order: rank everything first, then hide what the user may not see.
    ranked = sorted(CHUNKS, key=lambda c: -score(question, c))
    return [c["id"] for c in ranked[:k] if c["team"] == team]

question = "water damage medical claim"
print("filter first, north:", search_filter_first(question, "north"))
print("filter last, north: ", search_filter_last(question, "north"))
print("filter first, south:", search_filter_first(question, "south"))

Output

filter first, north: [1, 2]
filter last, north:  [1]
filter first, south: [3, 6]

Tip

Asked how you would build it? Say the order out loud: identity from the login, a label on each chunk, the filter inside the query, and no results when the identity is missing.

What does a weak design still leak?

Even with a filter in place, four leaks come up again and again. Name them in the interview, and say what you would change.

  • Counts and speed

    OWASP warns that an attacker can infer the existence, topic, and approximate volume of other customers' or teams' documents from result counts, score distributions, and timing, without ever seeing the documents.

  • Raw scores

    Returning the match score each chunk got (its similarity score) lets someone test whether a document is in the index. OWASP's advice is to keep those scores on the server.

  • Backups and copies

    Stored embeddings (the number lists that stand for each chunk's meaning) can be inverted to recover source text, so a vector backup needs the same care as the documents it came from.

  • A scope from the browser

    If the browser sends the team name, anyone can change it. OWASP is blunt: a client-supplied scope is a suggestion, not a control.

How would you stop a sales rep seeing legal's notes?

A sales rep asks the bot about a supplier contract's price. The best matching chunk is a note from the legal team, marked for legal only.

  • Without layers

    The note reaches the model

    The search ranks it first, and the prompt only asks the model to keep legal notes private. The model may still quote it.

  • With layers

    The note never leaves the index

    The rep's teams become a filter built in code. The legal note is filtered out inside the query, and the model answers from the sales sheet.

What does the interviewer want to hear?

A strong answer covers

  • Identity comes from the server's login, never from the question.
  • A permission label on every chunk, set when it is indexed.
  • The filter sits inside the search query, applied on every request.
  • Fail closed: no identity, no chunks.
  • Re-index on permission changes, and delete with the source.
  • Watch for leaks through counts, scores and backups.

Common traps

  • Trap"Tell the model to only use documents the user may see."

    BetterThe model is not a guard. Put the filter in code, inside the search query.

  • Trap"Filtering after the search works the same way."

    BetterIt does not. Hidden files still get ranked, and the results can run out.

  • Trap"The vector database handles security for us."

    BetterMicrosoft's guide says the security filter is just a string with no authentication behind it. Your server must check the login and build the filter.

Can you spot the safe search call?

Spot the safe call

Which search call builds the team check from the logged-in user and applies it inside the search?

Quick check

A staff bot for an insurer keeps showing one team's notes to another. What is the first fix?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q375Design permission-aware search over an internal wiki.Coming soon
  2. Q354What are the multi-tenant concerns?Coming soon
  3. Q148What are vector and embedding security weaknesses?13 min
  4. Q201What is prompt injection?12 min
  5. Q133What are common RAG failure modes?11 min

Learn it in depth

Last updated on 26 November 2026

Building a RAG bot over private company files?

I design and build AI products and agents, including the search and permission layer behind them. Book a short call and tell me who needs to see what.