Charitha Reddysecurity engineering
← All writing

Access control belongs in the retrieval layer, not the prompt

The first working version of my RAG chatbot answered an intern's question with two open high-severity pentest findings, including the vulnerable endpoint. The fix wasn't a better prompt. It was making sure the model never sees what the user isn't allowed to see.

The leak

The knowledge base mixes public security guides with confidential internal documents: an incident response playbook and a pentest report for a fictional company. Version one had no notion of users. Asked "What are the open high severity pentest findings?", it answered with both findings, an IDOR in the invoice API and a SQL injection in a report export, sourced from pentest_findings_2026.md [confidential].

A subtler problem showed up in an unrelated question. Asked "What is the capital of France?", the bot correctly said it didn't know, but the sources it had retrieved were two confidential documents and one public one. Semantic search returns the nearest chunks, relevant or not, so confidential text was being placed in the model's context for questions that had nothing to do with it.

Why the prompt can't be the control

The tempting fix is one more line in the system prompt: "Don't reveal confidential information to interns." That puts the access decision in the hands of a component that can be argued with. Prompt injection exists precisely because models can be talked out of their instructions, and a decision that can be talked out of isn't an access control.

The model can't leak what it never sees. So the access decision has to happen before retrieval results reach the prompt, in code, where it can't be negotiated.

The fix: filter at the database

Every chunk is tagged with an access level at ingestion, based on the folder it came from. Retrieval then adds one filter to the vector search:

ROLE_PERMISSIONS = {
    "intern":        ["public"],
    "security_team": ["public", "confidential"],
}

allowed = allowed_access_levels(role)   # unknown role -> PermissionError
results = collection.query(
    query_embeddings=[query_vector],
    n_results=k,
    where={"access": {"$in": allowed}},
)

After the change, the same intern question returns "I don't know. The provided context does not mention open high severity pentest findings", with only public sources. The security team role still gets the full answer.

Details that make it hold up

Taking it to production

This demo hard-codes two users with no passwords. A real deployment would take the role from a verified identity token, such as an OIDC ID token from SSO. Document-level labels would likely grow into attribute-based rules (team, project, data classification). And the whole scheme depends on correct labels at ingestion: if a confidential file lands in the public folder, the filter will faithfully serve it. Ingestion is now part of the security boundary, so it deserves its own reviews and audit logging.

The general principle carries well beyond chatbots: decide what an AI system may access in deterministic code, and treat the model as an untrusted component working inside those limits.

Code, tests and full resultssecure-rag-chatbot on GitHub ↗

More writing