Day 1 of 1460 minExploit ArchitectHands-on lab

Day 1: RAG Security: Orientation and a Working RAG Pipeline — Build, Trace and Review

Taught by Mr Elite Updated

By the end of today you can

  • Explain the course structure, daily learning outcomes and practical mission format.
  • Describe the main stages of a retrieval-augmented generation (RAG) pipeline.
  • Set up a minimal local Python environment and run a small RAG demonstration.
  • Explain the roles of document ingestion, chunking, embeddings, vector storage, retrieval and generation.
  • Identify trust boundaries and locations where untrusted data can enter the pipeline.

Warm-up

Answer from memory first, then tap to check. Recalling yesterday is what makes it stick.

Does a RAG application usually train or fine-tune its language model every time a user uploads a document?

No. In a typical RAG design, the document is processed and indexed separately. Relevant chunks are retrieved later and supplied as context to the model.

If a vector database returns a document chunk for a question, does that automatically mean the user is allowed to read it?

No. Similarity search measures relevance, not authorisation. The application must enforce access control and tenant boundaries independently.

Welcome to the RAG Security course. Over the next lessons, we will study how retrieval-augmented generation systems collect information, turn it into searchable representations, retrieve relevant material and pass that material to a language model. We will also examine what can go wrong when documents, users, embedding services, vector databases, retrieval logic and model instructions are not separated carefully.

Today is an orientation day, but we are not going to spend the whole session talking about diagrams. You will build a small local pipeline, follow one document through each stage and mark the points where untrusted data enters. The goal is not to create a production-ready chatbot. It is to understand the moving parts well enough to ask good security questions about each one.

A RAG system does not normally retrain the model every time a document is uploaded. Instead, it retrieves relevant pieces of external information at question time and includes them in the model's context. This can make answers more useful and current, but it creates new trust boundaries. A document may be useful evidence, but it should not automatically become an instruction. A vector match may be relevant, but it does not prove that the requesting user is authorised to see the result.

Course orientation — outcomes, daily practice and missions

This course is designed to move from understanding a RAG pipeline to reviewing its security properties. Each day should give you one concept, one practical exercise and a mission that produces evidence of learning.

A typical day has four parts:

  • Learn the conceptunderstand the component, trust boundary or attack class.
  • Inspect the designidentify what data enters, where it goes and which component makes security decisions.
  • Practise in a labuse a controlled local or purpose-built environment with synthetic data.
  • Complete the missionproduce a small deliverable such as a diagram, test table, configuration review or short report.

Your missions are deliberately practical. A mission is not just “read about vector databases.” It asks you to demonstrate something: draw the data flow, identify untrusted inputs, show a retrieval result, compare expected and actual behaviour, or propose a defensive control. Keep your work in a course notebook so you can build on it in later lessons.

The course will use a consistent assessment mindset:

  • Scope the application and identify what is in and out of bounds.
  • Map components, data flows, identities and trust boundaries.
  • Form a testable security hypothesis.
  • Use synthetic data and repeatable steps.
  • Record the evidence and avoid overstating what it proves.
  • Recommend controls that are enforced by application code and infrastructure, not merely requested from the model.

For this first day, your deliverable is intentionally small: a working local pipeline or a clearly documented setup result, a data-flow sketch and a list of untrusted input points. If your computer cannot run Docker or install a vector-store package today, use the lightweight in-memory fallback below and document the limitation. The security learning objective is to understand the flow, not to spend the entire hour debugging a local environment.

Trust boundary
A point where data or control crosses between components with different levels of trust.
RAG
Retrieval-augmented generation: retrieve relevant external content and provide it to a model as context.
Mission
A short practical task with a defined brief, steps and deliverable.
Synthetic data
Artificial test content created for the exercise, not copied from real users or customers.

Quick check

What is the main purpose of the daily mission?

Set up a minimal local Python and vector-store environment

You can build a small RAG demonstration with Python and a local vector store. For a first session, keep the setup simple. The example below uses Python, a lightweight embedding model from scikit-learn and an in-memory nearest-neighbour search. This avoids requiring a Docker service or an external API key. It is a teaching pipeline, not a production vector database.

Recommended environment:

  • Python 3.10 or later
  • A virtual environment for isolated dependencies
  • A code editor or terminal
  • OptionalDocker Desktop or Docker Engine for later exercises
  • Optionala local vector database such as Chroma or Qdrant for expanded practice

Create a project folder and virtual environment:

bash
mkdir rag-security-day1
cd rag-security-day1
python -m venv .venv

Activate it:

bash
# Windows PowerShell
.venv\Scripts\Activate.ps1

# macOS / Linux
source .venv/bin/activate

Install the minimal dependencies:

bash
python -m pip install --upgrade pip
pip install scikit-learn numpy

If you prefer to explore a containerised vector store later, Docker can run services such as Qdrant locally. For today's exercise, the in-memory implementation is sufficient to show the stages. Do not expose a local database port to the public internet. Use localhost binding and a disposable dataset.

Create a file named `rag_demo.py` and use the example in the next section. The script builds a tiny corpus, converts text into TF-IDF vectors, calculates similarity and prints the best matching chunk. TF-IDF is not a modern neural embedding model, but it is a useful local stand-in for understanding vectorisation and retrieval mechanics.

Virtual environment
An isolated Python dependency environment for one project.
Embedding
A numeric representation of text used to compare semantic or lexical similarity.
Vector store
A system that stores vectors and associated metadata for later retrieval.
Localhost
The local machine address; binding services to localhost reduces network exposure.

Follow the RAG pipeline — ingest, embed, store, retrieve and generate

Let's trace one small document through the pipeline. A typical RAG flow has five broad stages:

1. Ingest: accept a document from an approved source and extract its text and metadata. 2. Chunk: split long text into smaller passages so retrieval can return focused context. 3. Embed: convert each chunk into a numeric vector. 4. Store and retrieve: save vectors with metadata, then find chunks relevant to a user's query. 5. Generate: pass the retrieved chunks and the user's question to the language model to produce an answer.

The following minimal example uses a local TF-IDF vectoriser and cosine similarity. It demonstrates the pipeline without calling an external model or vector database. The final “generation” step is represented by printing the retrieved context and a simple answer template. In a later lesson, you can connect a local language model if your machine supports it.

python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

documents = [
    {
        "id": "doc-001",
        "source": "training-handbook",
        "text": "The training lab is available from 9 AM to 5 PM."
    },
    {
        "id": "doc-002",
        "source": "security-guide",
        "text": "Vector search compares numeric representations of text."
    },
    {
        "id": "doc-003",
        "source": "course-notes",
        "text": "RAG retrieves relevant passages and supplies them as model context."
    }
]

# Ingest and chunk: this tiny example treats each short record as one chunk.
chunks = [item["text"] for item in documents]

# Embed: TF-IDF creates numeric vectors from the chunks.
vectorizer = TfidfVectorizer()
document_vectors = vectorizer.fit_transform(chunks)

# Store: vectors and metadata remain associated in memory.
vector_store = {
    item["id"]: {
        "vector": document_vectors[index],
        "source": item["source"],
        "text": item["text"]
    }
    for index, item in enumerate(documents)
}

# Retrieve: find the chunk most similar to the query.
query = "How does RAG provide information to a model?"
query_vector = vectorizer.transform([query])
scores = cosine_similarity(query_vector, document_vectors).flatten()
best_index = scores.argmax()
best_chunk = chunks[best_index]

# Generate: placeholder for a model call in this orientation exercise.
print("Retrieved context:", best_chunk)
print("Draft answer: The system retrieves a relevant passage and uses it as context.")

CAPTION: This example is deliberately small. It shows the sequence and metadata association, but it does not implement production chunking, durable storage, access control or a real language-model call.

Notice that the vector store keeps metadata beside the vector. In a real application, metadata may include a document ID, tenant ID, access-control labels, source, timestamp and retention information. Those fields must be validated and protected. If the application retrieves a chunk without applying the user's permissions, a relevant match can become an unauthorised disclosure.

Also notice that the query is transformed using the same vectoriser fitted on the document chunks. In a neural embedding system, document and query embeddings must be produced using compatible model and configuration choices. Version changes can affect retrieval quality, so record the embedding model and index version in a real assessment.

Ingestion
The process of accepting, parsing and preparing source content.
Chunking
Splitting a document into smaller passages for indexing and retrieval.
Embedding
Converting text into a numeric representation.
Similarity search
Ranking stored vectors by a mathematical measure of closeness to a query vector.
Metadata filter
A condition used to restrict retrieval to records matching attributes such as tenant or access label.
Grounding
Supplying relevant source material to help the model answer a question.

Quick check

What does vector similarity tell you?

Mark every place untrusted data can enter the RAG flow

The security review begins by identifying inputs. A RAG pipeline has more than one. The obvious one is the user's question, but documents and metadata can be just as important.

Common untrusted or externally influenced inputs include:

  • Uploaded documentsfile text, filenames, document properties and embedded content.
  • Connected sourcesweb pages, shared drives, ticketing systems, wikis and external APIs.
  • User queriesdirect questions, filters, requested document IDs and conversation history.
  • Document metadatatenant labels, owner fields, access tags, source URLs and timestamps.
  • Parsing and chunking outputextracted text, OCR output, table contents and chunk boundaries.
  • Embedding requeststext sent to an embedding model or service, plus model and version selection.
  • Retrieval parameterstop-k values, filters, namespace or collection names and reranking inputs.
  • Retrieved passagescontent that may contain inaccurate statements or instruction-like text.
  • Model contextthe assembled prompt, conversation history and retrieved chunks.
  • Generated outputtext later rendered in a browser, stored, passed to another model or used as a tool argument.

For each input, ask:

  • Who controls it?
  • Is it authenticated and validated?
  • Can it contain instructions, markup, malformed content or unexpected metadata?
  • Does it carry an identity or tenant label, and can the caller change that label?
  • Which component consumes it next?
  • What happens if it is oversized, malformed, misleading or outside the user's permissions?

The most important beginner lesson is that retrieval does not make content trusted. A document may have come from an internal wiki and still contain user-editable text. A vector database may be private and still store poisoned or incorrectly labelled records. A model may receive retrieved content and mistake instruction-like text for directions. Security controls should be applied at the boundary where the data enters and again where it is used.

Draw the pipeline and mark the inputs with a star.

This is a first-pass map, not a complete threat model. Later lessons will examine ingestion validation, metadata trust, vector-store isolation, retrieval filtering, indirect prompt injection and data leakage in more depth.

Your turn

What Goes in the Index?

Review a minimal local RAG ingestion and retrieval flow. Your task is to identify where source text and metadata enter, follow one synthetic document through the pipeline and produce a short trust-boundary review. The lab uses a harmless local corpus. Do not ingest confidential documents or connect external data sources.

Open the lab in a new tab

What to try

What to notice

  • Which fields came from the document, and which were assigned by the application?
  • Is the source label trusted, or could an uploader choose or modify it?
  • Does the retrieval result include enough metadata to identify its source?
  • Is access control applied before retrieved text is added to model context?
  • Does the pipeline treat retrieved text as information rather than authoritative instructions?

If it did not work

- Assuming RAG fine-tunes or retrains the model every time a document is uploaded.

- Treating a vector similarity score as proof of authorisation or trustworthiness.

- Forgetting to preserve document IDs and source metadata during chunking.

- Using different or incompatible embedding configurations for documents and queries.

- Assuming internal documents are always trustworthy.

- Trusting uploader-supplied tenant IDs, owner fields or access labels without server-side validation.

Recap

  • RAG retrieves external content and supplies relevant passages to a model as context; it does not normally retrain the model for each upload.
  • A basic pipeline includes ingestion, chunking, embedding, storage, retrieval and generation.
  • Vectors support similarity search, but similarity is not authorisation.
  • Metadata must remain associated with chunks and must be validated.
  • Untrusted data can enter through documents, queries, metadata, parsers, retrieval parameters, retrieved passages and generated output.
  • Retrieved content should be treated as data, not automatically as instructions.
  • Access control must be enforced by the application and data layer before content reaches the model.

Cheat sheet

RAG
Retrieve relevant external content and provide it to a model as context
Ingestion
Accept and parse source documents
Chunking
Split documents into passages
Embedding
Convert text into numeric vectors
Vector store
Store vectors with associated metadata
Retrieval
Find relevant chunks for a query
Generation
Produce an answer using the question and permitted retrieved context
Similarity
Relevance estimate, not an access-control decision
Metadata
Source, document ID, tenant, owner, access label and timestamp
Trust boundary
Point where data or authority crosses between components
Untrusted inputs
Documents, queries, metadata, parser output, retrieval settings, retrieved chunks and generated output
Minimum control principle
Validate inputs, enforce access control server-side and treat retrieved text as untrusted content

Before the next day

Mission: Build and Map Your First Local RAG Pipeline

Run the minimal local RAG example or complete a documented code review if your environment prevents execution. Trace one synthetic document from ingestion to retrieval, then identify where untrusted data enters and what security question belongs at each boundary.

About 30 min

You are done when: A one-page RAG data-flow diagram, a short run log or setup note, and a list of at least five untrusted input points with one security question for each.

Want more? Add a second synthetic user identity and describe where tenant or role filtering must occur. You do not need to implement multi-tenant access control today; identify the exact retrieval and data-access boundaries where it must be enforced.

Quiz

4 questions. Your score is saved with your progress.

1. What is the main purpose of retrieval in a RAG application?

2. Which sequence best describes a basic RAG pipeline?

3. A vector store returns a highly relevant chunk. What must the application still check?

4. Which is an example of untrusted data entering a RAG pipeline?

Finish the day

Mark the day complete to save your progress.

Next: Next, we will look more closely at the ingestion boundary. You will examine how documents are accepted, parsed, chunked and labelled—and why a small mistake in this stage can affect everything retrieved later. Keep today's pipeline diagram. We will add to it as the course progresses, turning it into a practical RAG security review map. Before moving on, make sure you can explain the difference between a document being relevant and a document being authorised for a particular user. That distinction will appear repeatedly throughout the course. Coming soon.

Tell someone you are doing this:WhatsAppXLinkedIn

Your notes

Private to this device. Write what you want to remember from today.