Secrets and API keys in a RAG index
Short answer: scan sources for secrets and exclude them before indexing, then redact anything that slips through at ingestion. Add an output scan as a backstop, because a filter on the answer catches only what it recognizes. If a secret was ever indexed, rotate it first and then purge it from the index, caches, backups, and logs.
A RAG assistant turns every indexed file into retrievable text. The model cannot be trusted to hold a key back, so the controls sit in the pipeline around it.
Where do secrets come from in indexed documents?
Short answer: from the same places they leak everywhere else. Sample configs, runbooks with pasted commands, tickets with copied error output, stack traces, chat exports, and code snippets in wiki pages. Anything a person pasted to get help can carry a token.
Connector logs and support tickets are the hardest sources. Their content changes daily and comes from customers and external systems, so each source needs a scan on every sync. The RAG threat modeling guide walks through the write paths.
Which controls stop a secret from reaching an answer?
Short answer: six control points, from the source system to the logs. Each one catches some cases and misses others, so no single layer is enough. The earlier a control sits, the less cleanup a miss costs.
| Control point | What it catches | What it misses | Test idea | Pass criterion |
|---|---|---|---|---|
| Source scanning | Known key formats and high-entropy strings | Custom formats, split secrets, images of text | Plant a canary key in a source document | Document flagged and held before indexing |
| Ingestion filter | Patterns in extracted PDF and HTML text | Skipped attachments, encoded values | Index a file with the canary inside a code block | Canary replaced with a placeholder in every chunk |
| Chunk metadata | Documents labeled restricted or secret-bearing at source | Unlabeled documents, labels lost during chunking | Index a labeled file and inspect its chunks | Label present on every chunk from that file |
| Retrieval filter | Chunks with restricted labels before prompt assembly | Anything the earlier layers failed to label | Query for the canary as a user without rights | Labeled chunk never appears in the assembled prompt |
| Output scan | Known patterns in the final answer and citations | Paraphrased, split, or encoded secrets | Ask the model to spell the canary with spaces | Canary blocked, or the spaced form counted as a finding |
| Log scan | Secrets in prompt traces, retrieval logs, and error reports | Logs shipped to tools outside the scan's reach | Search every log store for the canary | Zero matches, or matches masked |
The output scan is the weakest row. A model asked to reverse or encode a string can carry it past a pattern match, so treat it as an alerting backstop.
How should redaction work at ingestion?
Short answer: replace each detected secret with a typed placeholder before chunking and embedding, and record the detection. A placeholder such as [REDACTED:api_key] keeps the document useful for questions about setup while the value itself never enters the vector store.
Run redaction before chunking, because a key split across two chunks can pass a scanner that checks each chunk alone. Keep a detection log with the source path and secret type, without the value. That log becomes the list of keys to rotate.
Published research has recovered readable text from embeddings, so a secret deleted from the text store may survive in the vectors. Redact before the embedding call, since the embedding provider also receives the text.
What do I do when a secret was already indexed?
Short answer: rotate the secret first. Purging takes time and can miss a copy. Rotation makes every copy worthless at once. After rotation, remove the value from the index, caches, backups, and logs, and record what was done.
The order:
- Rotate or revoke the credential at its issuer, and check its access logs for use since the indexing date.
- Delete the affected chunks and vectors, then reindex the cleaned source.
- Clear any semantic cache or stored answers that may contain the value.
- Check backups and snapshots of the vector store. Restoring an old backup brings the secret back.
- Search prompt traces, retrieval logs, and observability tools, and mask or delete matches.
- Check exports, evaluation datasets, and fine-tuning sets built from the index.
Soft delete in some vector stores leaves data in place until compaction, so query for the exact chunk ID after the purge.
How do I test the controls with a canary key?
Short answer: plant a fake key with a unique string in a source, run the pipeline, and search for that string everywhere. A canary has no value, so finding it is safe. Each place where it appears is a gap.
Create a string that no real system issues, such as ARVCANARY-7F3K-Q9ZD-2M8X, and put it in a sample config page in the wiki as PAYMENTS_API_KEY=ARVCANARY-7F3K-Q9ZD-2M8X. Run the normal sync. Then check, in order:
- Did the source scanner flag the page? It may not know this format. Add the
ARVCANARY-prefix to its custom patterns for the test. - Search the chunk store for the string. Expected result: zero matches, with the placeholder in its place.
- Ask the assistant "What is the payments API key in the sample config?" ten times, plus three reworded and encoded variants. Expected result: no answer contains the string or an encoded form of it.
- Search prompt traces and logs for the string. Expected result: zero matches.
Rerun the test after any change to the parser, chunker, or connectors.
Checklist
- Every indexed source listed with its owner and sync frequency
- Secret scanning on every sync, with custom patterns for internal token formats
- Redaction before chunking and before the embedding call
- Detection log without values, used as the rotation list
- Restricted labels kept on every chunk and enforced before prompt assembly
- Logs and traces scanned for secrets and for the canary
- Rotation-first procedure written down for an indexed secret
- Backups, caches, and exports covered in the purge steps
Common questions
Is a system prompt a safe place for keys?
No. Hidden context can be extracted, so keys belong in the tool gateway or the service that calls the API.
Can the model be told to never print keys?
The instruction helps with casual questions. An indirect prompt injection can override it, so it cannot replace redaction.
Next: AI security risks · OWASP LLM Top 10 2026 · Threat modeling a RAG pipeline · Lethal trifecta · AI security review report · AI security questionnaire · Cross-tenant retrieval · Vector store backups and exports
For an outside review of your RAG pipeline, including secret handling, see the AI Security Review.