DEV Community

Cover image for One Forum Post Poisoned My RAG Chatbot's Knowledge Base
Bug Bounty & Beyond
Bug Bounty & Beyond

Posted on

One Forum Post Poisoned My RAG Chatbot's Knowledge Base

HelpBot answers customers using RAG. It searches a vector store built from the official help docs and the community forum, then answers from the top result. New forum answers are embedded automatically.

An attacker posts one "answer" on the forum, stuffed with the words people use when they're locked out (password, reset, forgot, account locked). It ranks above the real help article. Its actual content tells customers to email their password and OTP to the attacker.

No prompt injection is needed. The search does exactly what it was built to do, on poisoned data.

Why it worked

  1. Untrusted content went straight into the store, next to the official docs.
  2. Ranking was similarity only, so keyword stuffing wins.
  3. Nothing checked the answer before the customer saw it.

(OWASP LLM08 Vector and Embedding Weaknesses, LLM04 Data and Model Poisoning.)

The fix, in code

def ingest(doc, state):
    # FIX 1: screen untrusted content before it's embedded: keyword stuffing and off-domain contacts are quarantined.
    words = re.findall(r"[a-z]+", doc["text"].lower())
    top = Counter(words).most_common(1)[0][1] / max(len(words), 1)
    contacts = [c for c in re.findall(r"[\w.-]+@[\w.-]+|https?://\S+", doc["text"]) if not c.rstrip(".").endswith(OFFICIAL)]
    status = "quarantined" if top > 0.12 or contacts else "indexed"
    state["store"].append(dict(doc, status=status))
    return status


def search_help(args, state):
    # FIX 2: provenance. Account and security questions are answered only from official docs.
    if SECURITY.search(args["query"]):
        return search(state, args["query"], allowed=lambda d: d["source"] == "official help docs")
    return search(state, args["query"])
Enter fullscreen mode Exit fullscreen mode

A final output check blocks any reply that asks for a password or OTP, or points off-domain. Same question, run again: the post is quarantined, and the official answer comes back.

Run the lab yourself

git clone https://github.com/usinha79/hack-it-then-fix-it
cd hack-it-then-fix-it/ep18-vector-db-poisoning
pip install -r requirements.txt

python vulnerable/app.py   # http://localhost:5000, ask "How do I reset my password?"
python post_attack.py      # the attacker's forum post, then ask again
python fixed/app.py        # repeat everything
Enter fullscreen mode Exit fullscreen mode

./attack.sh runs all three steps. The embeddings are simple word-count vectors, so you can see exactly why the post ranks first. Everything is fake. Full lab: github.com/usinha79/hack-it-then-fix-it

Take-home checklist

  • Record where every chunk came from, and filter retrieval by trust for sensitive topics.
  • Screen user-generated content before it's embedded, and re-scan the store regularly.
  • Monitor what gets retrieved for high-risk queries.
  • Block replies that ask users for secrets or send them off-domain.

Catch up on Module 2

This episode is part of Module 3 of Hack It, Then Fix It. New to the series? Module 2 covers seven real AI agent attacks, each one hacked and then fixed in code, back to back:


Hack It, Then Fix It is a series where I build a small AI app with a real flaw, break it, fix it in code, and run the same attack again to prove the fix holds. Everything shown uses fake data on my own machine. Only test systems you own or are authorised to test.

Top comments (0)