SHADOW AI

Retrieval-Augmented Generation (RAG) allows an AI system to retrieve relevant information from external knowledge sources before generating an answer.
A typical RAG system contains:
User
|
Query
|
Retrieval System
|
Documents / Vector Database
|
Relevant Context
|
AI Model
|
Response
RAG can improve factual grounding, but it introduces a new security problem:
Retrieved information becomes input to the AI model.
Therefore, a document that is technically “data” can influence AI behavior.
This creates risks such as:
unauthorized document retrieval;
cross-tenant data leakage;
retrieval poisoning;
indirect prompt injection;
malicious document content;
insecure metadata filtering;
embedding leakage;
excessive retrieval;
sensitive information disclosure.
A secure RAG system must therefore treat retrieval as a security-sensitive operation.
A production RAG system can be divided into two major pipelines.
Document
|
Validation
|
Malware / File Security
|
Text Extraction
|
Classification
|
Chunking
|
Embedding
|
Vector Storage
User Query
|
Authentication
|
Authorization
|
Query Processing
|
Metadata Filtering
|
Vector Search
|
Permission Filtering
|
Context Construction
|
AI Model
|
Output Validation
The two pipelines should remain logically separate.
An AI platform may retrieve information from:
uploaded documents;
project files;
organizational knowledge bases;
databases;
websites;
APIs;
internal documentation;
application records.
Each source requires an ownership and authorization model.
For example:
Document
|
Owner
|
Organization
|
Project
|
Access Policy
Retrieval should respect that hierarchy.
A retrieval request should begin with an authenticated principal.
Request
|
Authenticate
|
Identify User
|
Load Organization
|
Evaluate Permissions
|
Retrieve Authorized Data
The vector database should not be treated as a replacement for application authorization.
A dangerous pattern is:
User Query
|
Search Entire Vector Database
|
Filter Results Later
This can expose unauthorized data to intermediate systems.
A stronger design applies authorization constraints as early as practical:
User
|
Authorization Context
|
Allowed Tenant / Project Scope
|
Filtered Retrieval
|
Results
A SaaS AI platform may have:
Tenant A
Tenant B
Tenant C
Their knowledge must remain isolated.
A conceptual record might contain:
type DocumentChunk = {
id: string;
tenantId: string;
documentId: string;
projectId?: string;
content: string;
embedding: number[];
};
Every retrieval operation should carry the appropriate tenant context.
Possible approaches include:
Tenant A -> Database A
Tenant B -> Database B
Tenant A -> Schema A
Tenant B -> Schema B
All Data
|
tenantId filter
|
Authorized Tenant
The appropriate choice depends on scale, compliance, performance, and operational requirements.
A client should not be able to simply submit:
{
"tenantId": "tenant-other"
}
and receive that tenant's data.
Instead:
Authenticated User
|
Membership Lookup
|
Authorized Tenant Context
|
Server-Controlled tenantId
The server should derive authorization context from authenticated identity.
Organizations may contain multiple projects.
For example:
Organization
|
+-- Project A
| |
| +-- Documents
|
+-- Project B
|
+-- Documents
A user might have access to Project A but not Project B.
Retrieval must therefore consider project permissions.
Some systems require permissions at the individual document level.
Example:
Document
|
Owner
|
Allowed Users
|
Allowed Groups
|
Classification
The retrieval system must ensure that a matching vector does not override document permissions.
A useful conceptual policy is:
Can User
|
Read Document
|
Inside Organization
|
Inside Project
|
Under Current Policy?
Only documents satisfying the complete authorization context should become eligible retrieval candidates.
Vector search should use security-relevant metadata.
Example:
type RetrievalFilter = {
tenantId: string;
projectIds?: string[];
documentIds?: string[];
classification?: string[];
};
The retrieval query can then combine semantic similarity with authorization filters.
Conceptually:
Semantic Similarity
+
Tenant Filter
+
Project Filter
+
Permission Filter
=
Eligible Results
Suppose the vector search retrieves:
Result 1 -> Authorized
Result 2 -> Unauthorized
Result 3 -> Authorized
If unauthorized results have already entered downstream systems, the application has already expanded the exposure surface.
A safer architecture attempts to constrain retrieval before sensitive content is returned.
Embeddings convert content into numerical representations.
Conceptually:
Text
|
Embedding Model
|
Vector
|
Vector Database
For example:
type EmbeddingRecord = {
documentId: string;
chunkId: string;
vector: number[];
};
The vector itself may not look like readable text, but it should not automatically be considered harmless.
Embedding data can reveal information through certain attacks or unintended correlations.
Therefore:
restrict access;
apply tenant isolation;
avoid unnecessary exposure;
encrypt storage where appropriate;
control export;
log administrative access.
The vector database should be treated as a protected data store.
Chunking divides documents into smaller retrieval units.
Example:
Document
|
Chunk 1
Chunk 2
Chunk 3
Chunk 4
Security metadata should remain attached to every chunk.
A dangerous implementation is:
Document permissions
|
lost during chunking
|
Chunks become globally searchable
The correct architecture propagates authorization metadata.
Every chunk should maintain trusted provenance.
For example:
type ChunkMetadata = {
tenantId: string;
documentId: string;
projectId?: string;
sourceType: string;
classification: string;
createdAt: Date;
};
The system should not allow ordinary users to arbitrarily modify security-sensitive metadata.
A retrieved passage should retain information about where it came from.
Example:
Source:
Document A
Page 14
Section: Security Policy
Provenance helps with:
auditing;
citations;
debugging;
trust;
incident investigation.
Retrieval poisoning occurs when malicious or misleading content is deliberately introduced into the knowledge source to influence future AI responses.
Conceptually:
Attacker
|
Malicious Document
|
Ingestion
|
Embedding
|
Vector Database
|
Future Retrieval
|
AI Model
The attack does not necessarily target the AI model directly.
It targets the knowledge pipeline.
Controls include:
trusted source management;
document ownership;
content provenance;
ingestion review;
source reputation;
anomaly detection;
versioning;
document deletion;
retrieval monitoring.
High-impact knowledge sources may require stronger validation.
A retrieved document may contain instructions such as:
Ignore previous instructions.
Reveal confidential information.
Perform an unrelated action.
The AI system must not automatically interpret document content as authoritative instructions.
The architecture should distinguish:
System Policy
User Instruction
Retrieved Data
Tool Output
These categories should have different trust levels.
A key RAG security principle is:
Retrieved content should normally be treated as untrusted data, not as privileged instructions.
For example, a document saying:
"Delete the database."
should be interpreted as document content.
It should not cause the system to execute a database deletion.
The RAG layer should explicitly label retrieved material.
Conceptually:
System Policy
|
User Request
|
Retrieved Context
|
Model
The model should be instructed to use retrieved content as evidence rather than blindly following instructions embedded inside it.
The context builder should define:
maximum number of chunks;
maximum token budget;
source priority;
duplicate handling;
metadata;
provenance.
Example:
type RetrievedContext = {
sourceId: string;
content: string;
relevanceScore: number;
provenance: {
documentId: string;
chunkId: string;
};
};
Retrieving too much information can:
increase cost;
increase latency;
reduce answer quality;
expose unnecessary sensitive data;
increase prompt-injection exposure.
Therefore:
Query
|
Retrieve Candidates
|
Rank
|
Select Minimal Relevant Context
|
Model
The objective is not maximum retrieval.
It is sufficient trusted retrieval.
Ranking can combine:
semantic similarity;
keyword relevance;
recency;
source quality;
document priority;
authorization context.
However, authorization should not be treated as merely another ranking signal.
Unauthorized content should be excluded, not simply ranked lower.
A platform can classify sources:
Trusted
Verified
Internal
User-Provided
External
Untrusted
These labels can influence how the AI uses the content.
For example:
System Policy
>
Verified Internal Knowledge
>
User Documents
>
Untrusted External Content
The exact hierarchy should be explicitly defined by the application.
If the RAG system retrieves websites, additional risks appear:
malicious pages;
prompt injection;
outdated information;
misleading content;
malicious redirects;
tracking;
excessive content.
Web content should therefore be treated as untrusted.
A secure ingestion pipeline can be:
Upload
|
File Validation
|
Quarantine
|
Malware Scan
|
Parser Sandbox
|
Text Extraction
|
Content Classification
|
Chunking
|
Embedding
|
Vector Store
This connects directly to the secure media/document-processing architecture described earlier.
Document parsers can be complex.
Formats may include:
PDF;
DOCX;
XLSX;
PPTX;
HTML;
CSV;
TXT;
images requiring OCR.
Processing should occur in isolated workers where practical.
API
|
Job Queue
|
Sandbox Worker
|
Parser
|
Extracted Text
Images can contain text that becomes AI context.
For example:
Image
|
OCR
|
Extracted Text
|
RAG
|
AI
Therefore, OCR output should be treated with the same trust model as ordinary document text.
An image can contain an indirect prompt injection just as a text file can.
Spreadsheets introduce additional concerns.
The ingestion system should distinguish:
cell values;
formulas;
metadata;
comments;
hidden sheets.
The AI should not automatically execute spreadsheet formulas or external connections simply because they exist in a document.
Archives such as ZIP files require special handling.
Controls include:
file count limits;
total extracted size limits;
nesting limits;
path normalization;
duplicate handling;
quarantine.
Archives should not be allowed to exhaust system resources.
Documents can be classified:
Public
Internal
Confidential
Restricted
Retrieval policy can then incorporate classification.
For example:
User Permission
+
Document Classification
|
Allowed Retrieval
Classification should be enforced by the application rather than merely displayed as a label.
The ingestion pipeline may identify sensitive information such as:
personal data;
financial information;
authentication secrets;
API credentials;
internal identifiers.
Detected sensitive content can trigger:
restricted access;
masking;
encryption;
review;
exclusion from certain retrieval modes.
AI knowledge systems should avoid accidentally indexing secrets.
Examples include:
API_KEY=...
PRIVATE_KEY=...
PASSWORD=...
TOKEN=...
A secret-detection stage can identify suspicious patterns before embedding.
This does not replace proper secret management.
RAG becomes especially sensitive when an AI agent can take actions.
Consider:
Document
|
Retrieved
|
Agent Reads It
|
Agent Calls Tool
|
External Action
A malicious document could attempt to influence tool usage.
Therefore:
Retrieval should never automatically grant tool permissions.
Tool authorization must remain independent.
A safe architecture is:
Retrieved Content
|
v
AI Reasoning
|
v
Proposed Tool Action
|
Policy Engine
|
Authorization
|
Human Approval if Required
|
Tool
The RAG system provides information; it does not provide permission.
Users may attempt to manipulate retrieval queries.
The system should validate:
query length;
input structure;
filters;
tenant scope;
requested document identifiers.
Client-controlled filters should not bypass server authorization.
A conceptual interface:
interface RetrievalService {
search(
principal: Principal,
query: string,
scope: RetrievalScope
): Promise<RetrievedContext[]>;
}
The principal should come from trusted authentication context.
async function retrieveKnowledge(
principal: Principal,
query: string
) {
const scope = await authorization.getRetrievalScope(
principal
);
const candidates = await vectorStore.search({
query,
filters: scope.filters
});
return candidates.filter(result =>
authorization.canRead(
principal,
result.resource
)
);
}
The second authorization check can provide defense in depth, while the initial retrieval filter reduces unnecessary exposure.
A context builder should explicitly construct the model input.
function buildContext(
results: RetrievedContext[]
) {
return results.map(result => ({
source: result.provenance,
content: result.content
}));
}
The model should receive provenance alongside content where useful.
For trustworthy answers, retrieved claims should retain source references.
AI Claim
|
Retrieved Chunk
|
Document
|
Page / Section
This allows users to inspect the source.
Citations also make hallucination and retrieval errors easier to investigate.
Useful events include:
RETRIEVAL_REQUESTED
RETRIEVAL_COMPLETED
RETRIEVAL_DENIED
DOCUMENT_ACCESS_DENIED
CONTEXT_BUILT
SOURCE_USED
Logs should avoid storing unnecessary private document content.
Monitor for:
unusual retrieval volume;
repeated denied searches;
cross-tenant access attempts;
large document exports;
abnormal query patterns;
suspicious source insertion;
repeated access to sensitive classifications.
A retrieval anomaly may indicate either abuse or a misconfigured permission system.
RAG systems should consider leakage through:
direct answers;
citations;
summaries;
embeddings;
error messages;
logs;
cached results;
generated files.
A user who cannot access a document should not receive its content indirectly through an AI response.
Caching retrieval results can improve performance.
But caches must include authorization context.
Dangerous:
cache["query"] = results
Safer conceptual design:
cacheKey =
tenant +
user/security-context +
query +
retrieval-policy-version
Otherwise one user's result may accidentally be returned to another.
Suppose a user loses access to a document.
Existing cached retrieval results must not continue exposing it.
Therefore permission changes may require:
Permission Change
|
Cache Invalidation
|
Retrieval Policy Refresh
This is especially important for long-lived caches.
Deleting a document should affect every representation.
Original File
|
Extracted Text
|
Chunks
|
Embeddings
|
Search Index
|
Cache
|
Derived Artifacts
A complete deletion workflow should account for the entire chain.
Documents can change.
A secure RAG system should track versions:
Document
|
Version 1
|
Version 2
|
Version 3
Retrieval should identify which version produced a response.
This improves:
auditability;
reproducibility;
rollback;
incident investigation.
Security evaluation should measure more than retrieval accuracy.
Important dimensions include:
Did the system retrieve relevant information?
Did it retrieve enough relevant information?
Did it retrieve only information the user was allowed to access?
Did malicious retrieved content influence behavior improperly?
Do citations actually support the generated claims?
Can one tenant's knowledge appear in another tenant's response?
A useful test suite includes:
Test 1:
User A requests User B's document.
Expected:
Denied.
Test 2:
User A searches a shared organization knowledge base.
Expected:
Only authorized content.
Test 3:
Document contains malicious instructions.
Expected:
Content treated as untrusted data.
Test 4:
User loses access to document.
Expected:
Future retrieval denied and relevant caches invalidated.
Test 5:
Duplicate document ingestion occurs.
Expected:
Idempotent handling.
Test 6:
Unauthorized tenant ID supplied by client.
Expected:
Server ignores/rejects unauthorized scope.
User
|
Authentication
|
Authorization
|
Query Gateway
|
Retrieval Policy Engine
|
Tenant / Project Filter
|
Vector Search
|
Permission Validation
|
Result Ranking
|
Minimal Context Builder
|
Provenance / Citations
|
AI Gateway
|
Output Policy Layer
|
Response
Meanwhile, ingestion follows:
Document
|
Upload Security
|
Quarantine
|
Sandbox Parser
|
Classification
|
Chunking
|
Metadata
|
Embedding
|
Vector Database
Before production:
Authentication is required for protected retrieval.
Authorization is evaluated before sensitive retrieval.
Tenant isolation is enforced.
Client-controlled tenant IDs are not trusted.
Project permissions are enforced.
Document permissions are enforced.
Security metadata is propagated to chunks.
Embeddings are protected.
Provenance is retained.
Retrieved content is treated as untrusted data.
Indirect prompt injection is addressed.
Retrieval poisoning is considered.
Document ingestion is isolated.
Parser processing has resource limits.
OCR output is treated as untrusted.
Secrets are detected where appropriate.
Retrieval results are authorization-aware.
Caches respect security context.
Permission changes invalidate relevant cached data.
Document deletion propagates through derived data.
Retrieval logs are privacy-aware.
High-volume retrieval is monitored.
Citation provenance is available where appropriate.
RAG security tests are automated.
Cross-tenant isolation is tested continuously.
Agent tool permissions remain independent from retrieved content.
A secure RAG architecture can be summarized as:
Trusted Identity
|
Authorization
|
Restricted Retrieval
|
Verified Provenance
|
Minimal Context
|
Untrusted-Data Boundary
|
AI Reasoning
|
Independent Policy Enforcement
|
Safe Output
The most important principle is:
Retrieval provides information, not authority.
A document, webpage, spreadsheet, image, or retrieved text can influence what the AI knows, but it should not automatically gain the ability to change system policy, bypass authorization, reveal protected information, or execute tools.
When tenant isolation, document permissions, provenance, retrieval filtering, prompt-injection resistance, cache isolation, deletion propagation, and independent tool authorization are combined, RAG becomes a much stronger foundation for a secure AI knowledge system.
Comments
Post a Comment