Skip to content
mentoor

Topic 3 of 5 · 5 min read

Cross-user leaks in RAG and memory features

Goal: Find the missing permission check that lets a retrieval or memory feature show one user another user's data.

An AI feature that searches or remembers must check who is asking at the moment it fetches data, because the model cannot be trusted to hold anyone back.

Check yourself · 1 of 3

A RAG assistant shows employees documents from folders they cannot open. What is the most likely cause?

Choose one answer

Why it matters

Many AI products use retrieval-augmented generation, or RAG. The system searches a pile of documents, pastes the best matches into the prompt, and the model answers from them. Others add memory, so the assistant remembers facts from earlier chats. Both features pull text from a shared store. If that store is searched without asking "may this user see this?", the assistant will happily read one customer's contract to another. It is not hacking. The assistant was simply given the text.

The model is the wrong place for the check. Once a document is in the prompt, the model can quote it, summarise it or leak it through a clever question.

How to find the missing check

Follow the data from store to answer and look for the point where identity should filter results.

  1. Index time. When documents are added, is the owner or access group saved with each one?
  2. Query time. Does every search include a filter for the current user's tenant or permissions, applied by the code and not by the model?
  3. Caches. Is an answer cache keyed by the question only? Then user B asking the same question gets user A's answer.
  4. Memory. Is memory saved per user id, or into one pool that every user reads?
  5. Shared indexes. In a multi-tenant vector store, are tenants separated by a namespace or filter that cannot be skipped?

Before and after

Before: The bot searches all 20,000 company documents and shows the top 5. A junior employee asks about pay bands and gets a draft from the HR folder.

After: Each document carries an access list. The search adds a filter for the caller's groups, so the HR folder never reaches the prompt for that employee.

A short code branch for engineers:

# Weak: no identity in the query
hits = vector_db.search(query_vector, top_k=5)

# Better: the filter is applied by code, from the session
hits = vector_db.search(
    query_vector,
    top_k=5,
    filter={"tenant_id": session.tenant_id, "allowed_groups": session.groups},
)

What goes wrong

  • Permissions copied once. Access was right at index time, but a document later became private and the index still serves it.
  • Cache without identity. The response cache key is the question text only.
  • Memory shared by mistake. A "remember my preferences" feature stores everything under one key.
  • The filter is a prompt. "Only use documents the user is allowed to see" is an instruction, not a filter.

When the filter lives in code, a tricked model has nothing forbidden to reveal.

Examples

The shared answer cache

A bot cached answers by question text. One customer asked 'What is my balance?' and the next customer who typed the same words saw the first customer's figure. Fix: include the user id in the cache key, or do not cache personal answers.

The HR folder in the index

A company chat assistant indexed the whole shared drive, including HR. Staff could ask for salary tables. Fix: store folder permissions with each chunk and filter every search by the caller's groups.

Key points

  • Retrieval and memory must filter by the caller's identity in code before text reaches the prompt.
  • Store the owner or access group with every document at index time, and refresh it when permissions change.
  • A response cache keyed only by the question can serve one user's answer to another.
  • Memory features need a per-user key, never one shared pool.
  • A prompt sentence asking the model to respect permissions is not an access control.

Common mistakes

  • Indexing everything in one store without recording who may read each document.
  • Keying a cache or a memory by the question or topic alone.
  • Relying on the model to refuse to show documents it was given.
  • Checking permissions once at index time and never when they change.

Check yourself · 2 of 3

Where should 'may this user see this document?' be checked in a RAG system?

Choose one answer

Check yourself · 3 of 3

A bot caches answers keyed by the question text. What is the risk?

Choose one answer

Practice items, not counted towards your mastery.

Start here