A RAG chatbot answers from whatever the vector search hands it. Point it at your company docs, and when an intern asks "How much does a senior engineer get paid?" it can quote the HR salary bands word for word.
The model did nothing wrong. Retrieval gave it a document the intern was never allowed to read. You can't prompt your way out of that: the fix has to happen before the text reaches the LLM.
Here's a worked example with real output. I ran every query below this morning on a fresh install, and the full script is in a recipe you can run yourself.
The setup: eight chunks, four sensitivity levels
Every chunk stores its text, its embedding, and acl_group: the group allowed to read it.
CREATE TABLE IF NOT EXISTS rbac_kb (
chunk_id INTEGER PRIMARY KEY,
doc_ref TEXT,
acl_group TEXT, -- who may read this chunk
content TEXT,
embedding VECTOR(384)
);
-
Handbook pages for
all_staff(core hours, career levels, "ask HR about your pay") - An engineering wiki page for
engineering -
2026 salary bands and HR case notes for
hr_managers - A Q4 board deck for
finance_exec
The embedding model runs inside the database, so indexing is one statement:
UPDATE rbac_kb SET embedding = EMBED(content);
Step 1: watch it leak
This is what most RAG tutorials do: rank by similarity and take the top 3.
SELECT chunk_id, doc_ref, acl_group,
COSINE_SIMILARITY(embedding, EMBED('How much does a senior engineer get paid?')) AS relevance
FROM rbac_kb
ORDER BY relevance DESC
LIMIT 3;
| chunk_id | doc_ref | acl_group | relevance |
|---|---|---|---|
| 5 | Comp Bands 2026 | hr_managers | 0.635 |
| 6 | Comp Bands 2026 | hr_managers | 0.516 |
| 2 | Handbook §5 | all_staff | 0.392 |
The two most relevant chunks are exactly the ones the intern shouldn't see. Feed them to the LLM with a careful "answer ONLY from these sources" prompt, and the model does what it was told:
A Senior Software Engineer (L5) is paid between $182,000 to $228,000 base salary in 2026.
The prompt was fine. The retrieval was the leak.
Step 2: model users and nested groups
Real orgs don't grant access user by user. Priya is in engineering, which is inside all_staff. Marcus is in hr_managers, inside hr, inside all_staff. One table holds both kinds of edge:
CREATE TABLE IF NOT EXISTS rbac_membership (
member TEXT, -- a user or a group
group_name TEXT
);
INSERT INTO rbac_membership (member, group_name) VALUES
('priya','engineering'),
('marcus','hr_managers'),
('dana','finance_exec'),
('engineering','all_staff'),
('hr_managers','hr'),
('hr','all_staff'),
('finance_exec','finance'),
('finance','all_staff');
Step 3: resolve effective access with one recursive query
A recursive CTE walks the hierarchy and stores each user's effective groups in rbac_access, the table every retrieval joins against:
INSERT INTO rbac_access (user_id, grp)
WITH RECURSIVE reach(user_id, grp) AS (
SELECT member, group_name FROM rbac_membership WHERE member IN ('priya','marcus','dana')
UNION
SELECT r.user_id, m.group_name FROM rbac_membership m JOIN reach r ON m.member = r.grp
)
SELECT user_id, grp FROM reach;
| user_id | can_read |
|---|---|
| dana | all_staff, finance, finance_exec |
| marcus | all_staff, hr, hr_managers |
| priya | all_staff, engineering |
Step 4: the same search, one JOIN added
SELECT k.chunk_id, k.doc_ref, k.acl_group,
COSINE_SIMILARITY(k.embedding, EMBED('How much does a senior engineer get paid?')) AS relevance
FROM rbac_kb k
JOIN rbac_access a ON k.acl_group = a.grp
WHERE a.user_id = 'priya'
ORDER BY relevance DESC
LIMIT 3;
Priya's top 3: Handbook §5 (0.392), Handbook §7 (0.263), Eng Wiki: Releases (0.124). No salary bands. Run it as marcus and he still gets both Comp Bands chunks first, because he's allowed to.
The filter sits in the same query that ranks by meaning, so nothing the LLM receives was ever off-limits. Your app passes the signed-in user's id; the model never sees the filter, so no prompt can talk its way around it.
Step 5: each user gets their own answer
Build one context per user from their permitted top 3, then call GENERATE() once per row (full SQL in the recipe). Same question, same prompt, on my run:
- Priya: "I cannot share that information and point to your manager or the HR helpdesk at hr@acme.example."
- Marcus: "According to the Comp Bands 2026 document, the salary range for a Senior Software Engineer (L5) is $182,000 to $228,000 annually..."
Priya's refusal isn't a guardrail prompt. The model simply never had the number, and the handbook chunk it did have tells it where to send her.
Step 6: the audit log is one INSERT
Security teams ask "who saw the salary bands?" Log every chunk you served from the rows you already retrieved:
INSERT INTO rbac_audit (user_id, question, chunk_id, doc_ref, logged_at)
SELECT user_id, 'How much does a senior engineer get paid?', chunk_id, doc_ref, CURRENT_TIMESTAMP
FROM rbac_top;
Result: Marcus was served Comp Bands 2026 twice, Priya never. And "who can read this document?" is the same join turned around:
SELECT DISTINCT a.user_id AS can_read_comp_bands
FROM rbac_access a
JOIN rbac_kb k ON k.acl_group = a.grp
WHERE k.doc_ref = 'Comp Bands 2026';
-- marcus
Step 7: revoke access without re-embedding
Marcus moves from HR management to a general HR role. Change one membership row, refresh rbac_access, and his next search returns only handbook pages (§5, §7, §2). The embeddings weren't touched: permissions live next to the vectors, not baked into a per-team copy of the index.
Why this design holds up
- Label every chunk. The access label travels with the evidence through chunking and re-indexing.
-
Resolve groups once.
WITH RECURSIVEflattens any nesting depth into a plain lookup table. -
Filter in retrieval, not in the prompt. A
JOINcan't be jailbroken. - Audit from the same rows. What you logged is exactly what the model saw.
- Revocation is a row change. No re-index, no per-tenant vector store.
Run it yourself
Most RAG stacks spread this across a vector store, a permissions service, an LLM gateway and a log pipeline, with sync jobs in between. I ran all of it in one engine, SynapCores: documents, embeddings (all-minilm, in-database), the group hierarchy, GENERATE() with a local model, and the audit table.
curl -fsSL https://get.synapcores.com | sh
# or
docker pull synapcores/community:v2.5.2-ce
It's the free, self-hosted Community Edition. The complete script, with the dataset, all ten steps, and cleanup, is in the recipe: RAG role-based access control (about 12 minutes). If you'd rather not install anything yet, there's a browser Playground.
Related: RAG with citations and the graph version of group inheritance, RBAC effective permissions in Cypher.
Where does your RAG stack enforce document permissions today: in the vector store's metadata filter, in the app, or not yet?
Top comments (0)