Prompt Injection Defence for Website Chatbots

You cannot prompt your way out of this. The defences that work are architectural: least privilege enforced in code, trust boundaries, output filtering, and deciding what goes into your index at all.

If your chatbot reads documents to answer questions, then whoever can influence those documents can influence your chatbot. That is the whole problem, and it does not have a complete solution.

What it has is a set of architectural defences that reduce what an attacker can achieve. Teams that understand that build systems where a successful injection is an annoyance. Teams that believe a well-written system prompt is protection build systems where it is a breach.

This sits alongside the general design question in our piece on chatbot guardrails. Here we are dealing specifically with untrusted text.

Why it cannot be fully solved

A language model receives one stream of text. Your instructions, the user’s message and any retrieved content arrive in that same stream, and the model has no reliable mechanism to treat some of it as authoritative and the rest as inert.

Conventional software does not have this problem because code and data live in different places. Here they do not, which is why this is a design constraint rather than a bug awaiting a patch.

Accept that and the engineering becomes clear. You are not preventing a model from being influenced. You are making sure that influencing it does not matter much.

Direct and indirect, and which one should worry you

Direct injection is a user typing something designed to change behaviour. Irritating, usually visible, and mostly a reputational risk: a screenshot of your assistant saying something it should not.

Indirect injection is instruction-like text sitting inside content the system retrieves. A product review, an uploaded document, a support ticket, a page on a site you crawl. The user never typed it and may not know it happened.

Indirect is the one that matters for business assistants, because retrieval is the entire architecture. Anything in your index is text the model will read as though you put it there.

Decide what goes in the index, because that is a security decision

The cheapest defence and the most overlooked.

Sort your sources by who can write to them. Your own published pages and documentation are controlled. Internal policy documents are controlled. Product reviews, user comments, uploaded files, support tickets and anything scraped from outside are not.

For anything uncontrolled, you have three options: exclude it, sanitise it before indexing, or isolate it so it is only retrieved for narrowly scoped questions and never in a context where the assistant can act.

Most teams index everything they have because more context felt better. Indexing a corpus anybody can write to, in a system that can take actions, is the arrangement that produces incidents.

Least privilege, enforced in code

This is the defence that actually works, and the word that matters is code.

Write down what the assistant is permitted to do. Then enforce it in your application rather than in the prompt. If it should only read from a particular index and only answer questions, it should not hold credentials that permit anything else. A model cannot misuse an ability it does not have.

For anything consequential, issuing a refund, changing an order, sending a message, reading another customer’s record, require a deterministic check outside the model. The model may propose; your code decides whether the request is permitted, for this user, within these limits.

A system where the model’s output directly triggers an action with no independent check is one injected document away from doing whatever that document asked.

Mark your trust boundaries

Structure what you send so the different sources are clearly separated, with retrieved content clearly delimited and labelled as reference material rather than direction.

This helps and it is not a defence. Tell the model explicitly that retrieved material is information to use, never instructions to follow, and that it must not act on directions found inside documents. That instruction reduces the success rate of naive attempts. It does not stop a determined one, and treating it as sufficient is the common mistake.

Use it as one layer among several, not as the layer.

Filter the output, not just the input

The defence people miss entirely, and the one that closes the most damaging vector.

Consider what happens if an injected instruction persuades the assistant to include a link or an image in its reply, with information from the conversation embedded in the address. When the interface renders that, a request goes out, carrying whatever was embedded, to somebody else’s server. Nobody clicked anything.

That is a data exfiltration path that exists purely in the rendering layer, and it is closed by controlling output rather than input.

Practically: do not render images from arbitrary addresses in assistant output. Restrict links to destinations you control or to a known allowlist. Escape output rather than rendering arbitrary markup. If your assistant legitimately needs to link to sources, construct those links from your own records of which document was retrieved, not from text the model produced.

Treat model output the way you treat any untrusted input to your interface, because that is what it is. The general principle is the same one in website security that protects your SEO.

Keep secrets out of the context

Anything in the prompt can be revealed. Assume your system instructions will eventually be extracted, because they will.

That is usually survivable, as long as the instructions contain no credentials, no internal endpoints, no pricing you did not intend to publish and nothing embarrassing.

Write your system prompt as though it will appear on a forum. If that thought is uncomfortable, the problem is the prompt rather than the attacker.

Sanitise what you can, carefully

For user-generated sources you must index, stripping or neutralising instruction-shaped patterns before indexing reduces the easy cases.

Be realistic about it. Pattern matching catches obvious attempts and misses paraphrase, and over-aggressive filtering damages legitimate content, since a support ticket may genuinely contain the words ignore the previous advice.

Treat sanitisation as reducing volume rather than as a boundary. The boundary is least privilege.

Log, and actually look

You cannot respond to what you cannot see. Log the conversation, which documents were retrieved, and any action that was attempted or blocked.

Then look at it. Blocked actions are the interesting signal, because a pattern of them means somebody is probing. Unusual retrieval patterns are worth noticing. So are conversations where the assistant produced output structurally unlike its normal responses.

Remember that these logs contain personal data, which means retention, access control and a stated purpose like any other record.

Test your own system

Before launch, and periodically afterwards, somebody should try to break it deliberately.

Try to make it ignore its scope. Try to extract its instructions. Place instruction-like text inside a document in your own index and see whether retrieving that document changes behaviour. Try to make it emit a link or image of your choosing. Try to make it take an action it should not.

That last test is the one that matters. If the answer is that it cannot, because the capability does not exist, your architecture is sound regardless of how persuadable the model is.

Turn every successful attempt into a permanent test case in your evaluation set, alongside the refusal cases described in our piece on evaluating retrieval quality.

What does not work

A blocklist of suspicious phrases. Paraphrase defeats it and it blocks legitimate content.

Asking the model to detect injection attempts in its own context. Sometimes useful, not a control.

Claiming the system is injection-proof. No deployment is, and the claim is the kind of thing that appears in an incident report later.

A longer system prompt with more emphatic instructions. Marginal improvement, no structural change.

Assuming it does not apply because your assistant only answers questions. If it renders output in a browser, the exfiltration vector applies.

A sensible order of work

  1. List every source in your index and mark who can write to each one.
  2. Remove or isolate anything uncontrolled that does not need to be there.
  3. Write down what the assistant may do, and enforce it in code with credentials scoped to exactly that.
  4. Put a deterministic check in front of every consequential action.
  5. Delimit and label retrieved content, and instruct that it is reference rather than direction.
  6. Restrict what the interface will render from assistant output, images and links especially.
  7. Audit your system prompt for anything you would not publish.
  8. Log retrievals and blocked actions, and review them.
  9. Run an adversarial test before launch and after every significant change.

The broader agent design question is in building AI agents that use your data safely, and the retrieval architecture in building a RAG chatbot on your own content.

If you have an assistant live and nobody has tried to break it, that is the place to start. Get in touch, or see what our AI development work covers.