← Learn / Notebooks

AI securityTechnicalStudy guide

RAG Document Access Control

Follow authenticated identity into retrieval policy and compare filtering before and after a top-k cut.

Before you start: Basic RAG retrieval and the idea of users, groups and document permissions.

AI-assisted study notebook · Published 14 Nov 2026. Source-support checks and synthetic examples are recorded; automated review is not independent expert fact verification.

In [1]: Architecture

1. Architecture and scope

RAG brings retrieved text into a generator’s context. For a collection with restricted documents, access policy therefore matters before text crosses into that context. A document’s relevance to a question does not give the caller permission to read it. The diagram separates identity, policy, retrieval and context assembly. [1] [2]

Microsoft’s security-filter pattern carries principal identifiers in indexed fields and applies a query filter. Its documentation explicitly says those strings do not perform authentication or authorization. Elastic separately documents document-level security enforced through roles. These are different mechanisms; neither removes the need to understand the actual deployment’s policy boundary. [2] [3]

Scope. A local synthetic document collection. No real identities, permissions or protected documents are queried.

Workflow and scope notes
  1. Authenticate caller. The application establishes the caller and trusted group membership. A user-entered team name must not become authoritative policy.
  2. Resolve policy. The service derives access rules from trusted authorization state. Policy version and revocation behavior need explicit handling outside this toy.
  3. Retrieve within policy. Select candidates within the allowed set before ranking in this experiment. The implementation must ensure the retrieval filter cannot be omitted or changed by the caller.
  4. Assemble context. Only approved document text crosses into model context. Caches, citations and follow-up reads need the same caller-aware boundary.
In [2]: Experiment

2. Access filtering and top-k order

Four fictional documents have fixed relevance scores and ACLs. Select a trusted principal and k. Compare filtering the candidate set before top-k with taking the global top-k and then removing unauthorized hits.

Access filtering and top-k order

Original synthetic experiment · Change an input to update the figure.

D1: Team A, score .98. D2: Team B, .94. D3: Public, .85. D4: Public, .70. ACLs and identity are trusted fixed inputs. Both displayed final contexts exclude disallowed documents; post-filtering can underfill the result set. No timing or attack is simulated.

Access matrix and context selectionFilter first: D3, D4 · Trim after top-k: empty. Both final selections contain only allowed IDs in this toy.DOCUMENTSCOREACLALLOWED NOWD1.98team-aNoD2.94team-bNoD3.85publicYesD4.70publicYesFilter → top-kD3 · D4Top-k → filterEmpty after trimming
Filter first: D3, D4 · Trim after top-k: empty
Calculation record
{
  "inputs": {
    "principal": "public",
    "k": "2"
  },
  "before": [
    "D3",
    "D4"
  ],
  "after": []
}
CaseResultInterpretation
Public caller, k=2, filter firstD3 · D4Two allowed documents are selected from the allowed candidate set.
Public caller, k=2, top-k firstEmpty after trimmingD1 and D2 occupied the cutoff and are then removed.
Send global hits to the model firstOutside this permitted workflowFiltering the displayed answer later would not undo that context disclosure.
In [3]: Interpretation

3. Interpretation and limitations

For the public caller at k=2, the order of filtering and truncation changes coverage: filtering first returns D3 and D4; global top-k followed by trimming returns none. That is a property of the declared list. It is not a claim that every database executes filters or approximate search in the same order.

A filter expression alone does not establish who the caller is. In the trusted application, the identity-to-policy mapping must be controlled by the server. The lab’s principal selector is an educational input; exposing such a selector as authority in a real endpoint would invalidate the assumed boundary. [2]

Editorial review questions: how do ACL changes reach the index, how are stale cache entries invalidated, and are passage previews and direct source reads checked too? The lesson proposes these checks; it does not prove that a particular cache or index enforces them. [2] [3]

Practice exercise

Use Team A with k=2. Trace which document is removed by post-filtering. Then list every copy of passage text in a hypothetical RAG system that would need a consistent access decision.

In [4]: Python

4. Worked Python example

The core calculation below uses only the Python standard library. The full download adds the verification assertions and input fixtures. It runs locally on synthetic data; it does not load a model or contact a service.

lab.py · Core example · Python 3.13
DOCS = [
    ("D1", 0.98, "team-a"), ("D2", 0.94, "team-b"),
    ("D3", 0.85, "public"), ("D4", 0.70, "public"),
]


def contexts(principal, k):
    # A trusted application supplies the principal; not client authority.
    allowed = lambda d: d[2] == "public" or d[2] == principal
    ranked = sorted(DOCS, key=lambda d: -d[1])
    before = [d[0] for d in ranked if allowed(d)][:k]
    after = [d[0] for d in ranked[:k] if allowed(d)]
    return {"before": before, "after": after}

Download full Python example and checks ↓

Run: python -I lab.py. Recorded run: Python 3.13; 9 fixture states plus scenario assertions passed. The JSON output is a reproducibility record, not a real-system benchmark.

In [5]: Knowledge check

5. Questions and explanations

Predict before opening the explanation. Answers refer to the stated scope and synthetic inputs.

Q1 · BoundaryDoes a high relevance score grant access?
Does a high relevance score grant access?
Answer and explanation

Answer: No. Relevance and permission are different properties.

Q2 · OrderingFor the public caller at k=2, what does filter-before-top-k return here?
For the public caller at k=2, what does filter-before-top-k return here?
Answer and explanation

Answer: D3 and D4. D1 and D2 are excluded before truncation; the allowed set contains D3 and D4.

Q3 · IdentityWho should derive the authoritative filter in the stated architecture?
Who should derive the authoritative filter in the stated architecture?
Answer and explanation

Answer: Trusted application policy. A principal string in a filter does not authenticate the caller. [2]

Q4 · ContextCan filtering an answer undo restricted text already sent in model context?
Can filtering an answer undo restricted text already sent in model context?
Answer and explanation

Answer: No. The earlier context disclosure has already happened in that hypothetical workflow.

In [6]: Sources

References and source links

  1. Kamsetty, A. (2021). Retrieval Augmented Generation with Huggingface Transformers and Ray.https://huggingface.co/blog/ray-rag
  2. Microsoft. (Documentation accessed 2026). Security Filter Pattern - Azure AI Search | Microsoft Learn.https://learn.microsoft.com/en-us/azure/search/search-security-trimming-for-azure-search
  3. Elastic maintainers. (Documentation accessed 2026). Controlling access at the document and field level | Elastic Docs.https://www.elastic.co/docs/deploy-manage/users-roles/cluster-or-deployment-auth/controlling-access-at-document-field-level
Source checks and provenance

Primary publisher pages and relevant passages checked on 13 Nov 2026. Source support is not independent expert fact verification. Main documentation can change; retrieved-body hashes, dates, version labels and saved revision IDs are in the audit download.

[1] Retrieval Augmented Generation with Huggingface Transformers and Ray

Evidence: What is Retrieval Augmented Generation?. Architecture context from the saved public original; no dated training instructions are reused.

Public ResearchNode original 46f3ffc4bbd079e05a8ded5ad882e391
Revision e53ba23d35dfe41db4dce1ab971ee509. Captured 2026-11-06.

[2] Security Filter Pattern - Azure AI Search | Microsoft Learn

Evidence: About the security filter pattern. Filtering simulates result trimming; the application still needs trusted authentication and authorization.

[3] Controlling access at the document and field level | Elastic Docs

Evidence: Document level security. An actual product-level control with documented limitations; not equivalent to every vector filter.

Claim-to-evidence ledger ↓ · Review report ↓