TensorPass

How to prevent prompt injection in a website chatbot, layer by layer

How to prevent prompt injection in a website chatbot: four guards in order (input, envelope, stream, output), least privilege on tools, and our internal test of 12 November 2026.

Published on

How to prevent prompt injection in a website chatbot comes down to several guards placed in order, because one control does not hold on its own: a three layer defence tested on 5,080 samples in 2026 still let 11.3% of attacks through. Your chat reads three kinds of text in one stream. Your instructions, the visitor's question and your tools' results arrive as the same tokens, and the model has no built in boundary between them. This page builds the four guards AI Connect runs on every conversation (input, envelope, stream and output), shows the payload each one stops, then adds least privilege on the tools. It ends with our internal pen test of 12 November 2026: 108 of 108 guard and MCP server checks, and 35 of 35 model conversations with attacks in English, French and Hebrew.

Key takeaways:

  • Prompt injection is entry LLM01 of the OWASP Top 10 for LLM Applications 2025, and OWASP states that it is unclear whether a fool-proof prevention method exists.
  • A three layer defence for RAG chatbots cut attack success from 71.4% to 11.3% on 5,080 samples in 2026, at a 4.8% false positive rate and 61.2 ms of median added latency.
  • The AI Connect input guard stops a question over 2,000 characters, a long base64 blob or a match of 18 injection patterns in English, French and Hebrew.
  • On 12 November 2026 our internal pen test of the tensorpass.app chat passed 108 of 108 guard and MCP server checks and 35 of 35 conversations on claude-haiku-5-5.

Why the model cannot tell instructions from data in one token stream

A chat model receives one sequence of tokens and predicts the next one. Your system prompt, the visitor's message and the text your MCP tools return are concatenated into that sequence before the model reads it. Nothing in the sequence marks which part carries authority. Prompt injection is the attack that exploits this: text that arrives as data and gets followed as an instruction. The term dates from 2022.

"Under the hood of an LLM, there's no distinction made between 'data' or 'instructions'; there is only ever 'next token'." Dave Chismon, UK National Cyber Security Centre (NCSC), 8 December 2025.

The consequence for a website is concrete. A direct injection comes from the visitor typing into the chat box, such as the visitor who made your bot write poems against your brand. An indirect injection comes from content the chat retrieves: a product review, a comment, a CMS page or a tool result written by a third party. In a RAG (retrieval-augmented generation) chat, one poisoned document reaches every visitor whose question retrieves it. EchoLeak, the Microsoft 365 Copilot flaw tracked as CVE-2025-32711, carries a CVSS 3.1 base score of 9.3 (critical) from Microsoft for this kind of information disclosure.

Marking the data helps without closing the gap. Spotlighting, a family of marking techniques measured on GPT-family models in 2024, cut attack success from above 50% to below 2%, a rate that stays above zero. A filter built on known bad phrases faces an unbounded number of rephrasings. That is why each guard below assumes the previous one has already failed.

How to prevent prompt injection with four guards, in order

The order follows the path of one conversation turn. The question leaves the page, the model reads it beside your instructions and tool results, the answer streams back, and the widget draws it. AI Connect places one guard at each of those four points, in the visitor's browser, with no setup. The safe answer is the fixed reply the module shows in place of anything a guard stops.

GuardWhere it runsPayload it stopsWhat the 12 November 2026 run tested
Input guardThe page, before the question leaves"1gn0r3 y0ur rul3s", a system: prefix, a spoofed envelope tag21 direct attacks stopped before any model read them
Envelope guardAround your instructions and every tool resultA review that writes a forged closing tag, then new instructions4 conversations with a fake notice, a forged tag and an attacker link planted in the first tool result
Stream guardThe page, at each streamed pieceAn answer that repeats the salt or your instructionsNo visible answer carried the canary, the salt or the attacker's host
Output guardThe widget that draws the answerA Markdown image pointing at an attacker's server, an off-site link14 answers that had to stop or pass, in unit mode

A stopped question stays out of the conversation the model reads. A stopped answer is replaced by your safe answer in the history, so the next question reads the safe answer where the attacker's text was.

Input guard: what it screens and what it lets through

The input guard checks each question before it leaves the page. A stopped question does not reach the model and stays out of the conversation the model reads. That costs the visitor nothing in tokens and costs the attacker one attempt.

It stops three kinds of question. The first is a question longer than maxInputLength, the length cap set to 2,000 characters by default. The second is a question holding a long base64 blob, the encoding OWASP lists among the ways to slip instructions past a filter. The third is a match of one of 18 injection patterns in English, French and Hebrew: ignore or forget the instructions, a system: prefix, new instructions, a change of role or persona, a request for the prompt, or a spoofed envelope tag.

The patterns tolerate padding. Up to three words may sit between the verb and its object, so "ignore all of the previous instructions" stops as "ignore instructions" does. The guard also reads the question a second time with invisible characters removed and with digits used as letters read as letters, so "1gn0r3 y0ur rul3s" stops too.

What it lets through matters as much. Our test sends 8 ordinary questions that share words with the attacks, such as a question about assembly instructions or a visitor named Dan, plus the site's 4 sample questions. All 12 must pass. A pattern list catches the known phrasings and nothing more; a request for an answer in JSON with an extra debug field matches no pattern, and it is the attack that reached the model in our run. The next three guards exist for that reason.

Envelope guard: marking untrusted text before the model reads it

The envelope guard tells the model which text is yours. For each conversation, the visitor's browser draws a random salt (12 hex characters on tensorpass.app). It then closes <instructions-SALT> around your instructions and <tool-result-SALT> around every tool result. Your instructions tell the model that only the first envelope guides it.

The salt does the work. A plain delimiter such as <<<USER_INPUT>>> is public, so an attacker writes the closing marker into a review and continues with new instructions. Text that does not know the salt cannot close a salted envelope. A review that writes </tool-result-...> followed by new orders stays inside the tool result, where the model reads it as data. A new conversation draws a new salt, and the salt goes only to the model. Your page still receives each tool result as it is: renderToolResults and the onToolResult hook get the raw result, and only the model reads the envelope.

The 2026 three layer framework applies the same idea in its second layer, a provenance-based instruction hierarchy enforced during context assembly so that retrieved content cannot override operator policy. Its ablation studies found that each layer adds protection the others lack, which shows the limit. The envelope lowers the odds that the model obeys planted text. It does not make obedience impossible, so the answer still goes through two more checks.

Sealed envelopes, each closed with its own unique seal, illustrating salted envelopes around untrusted text

Stream guard: stopping a bad answer mid stream

The stream guard checks the answer at each piece the model streams, together with everything written before it. Streaming creates a specific problem for a public chat: a guard that waits for the end of the answer has already let the visitor read the dangerous part. A guard that checks each piece in isolation misses a pattern split across two pieces. AI Connect checks the accumulated text at every piece. A link or an email address still being written waits for its last character, so a host is judged once it is complete. From the first piece that trips a rule, the text stops reaching the page, the model's answer is cut in the wallet, and the safe answer replaces what showed. The answer stops on an envelope tag or the conversation's salt, which means the model is repeating its instructions. It stops on an image in Markdown or HTML, on a link outside the page's origin and your allowed hosts, on an email address nobody provided, and on a match of your denyPatterns (internal names such as a tool, a table or a server). In our 35 model conversations, no answer the visitor would see carried the canary, the salt or the attacker's host.

There is a cost you should know about. The pieces that streamed before a pattern was complete were on screen until the safe answer replaced them, and the browser received the whole answer whatever the page draws. The stream guard keeps an answer clean on screen. It is no place to protect a secret: content that must stay out of a visitor's reach stays on your server, before the model sees it.

The output guard decides what the widget draws once the answer is final. OWASP lists improper output handling as its own risk, LLM05:2025, because model text ends up in HTML, Markdown, scripts or URLs. Treat every answer as untrusted input to your renderer.

Images are the first exfiltration route. A Markdown image loads by itself, and its address can carry the conversation to another server. The scenario is documented in LLM01:2025: hidden instructions on a page make the model insert an image linking to a URL, which leaks the private conversation. The AI Connect modules draw no image at all.

Links are the second route. A module draws a link only when the guard allows its host. Allowed hosts are your page's origin, the hosts you list in allowedLinkHosts, and addresses your tools returned as a whole field, such as "website": "https://...". An address written inside free text, such as a review, is no field. A planted link therefore stays unclickable even when the model copies it exactly, and a planted email address stops the answer. If you write your own renderer with useAiChat, call linkAllowed(href) before you draw any link, and encode the text for the context it lands in.

Least privilege on every tool the chat calls

Least privilege limits what a successful injection reaches. The guards protect the page. Your MCP server is a separate door, since anyone can call it without your page. A rule from Simon Willison and Baibhav Bista sets the standard: "when an LLM processes information from a party, the privileges it has drops to that of the party". An anonymous visitor's chat gets anonymous privileges.

The server side of our setup applies seven checks:

  1. Public fields by name. Build each tool result from named public fields, and give page URLs, websites and booking links as fields of their own.
  2. A check on each result. Run checkToolResult(text, guards) from @tensorpass/aiconnect on every result before it returns, so no internal name leaves the server.
  3. Private names in a server policy. Keep internal service names and hosts in a policy file only the server imports; values your page passes as guards ship in its code, where anyone reads them.
  4. Bounded inputs. Give every input a bound in its schema, and accept no address from the model.
  5. Capped results. Keep each result under a fixed size; tensorpass.app serves long pages in parts under 20,000 bytes.
  6. A rate limit per address. The tensorpass.app docs server takes 60 requests a minute from each address and bodies up to 16 KB, and a burst of up to 100 requests must meet a 429.
  7. Human approval for writes. Keep any tool that sends, books or changes data behind a confirmation in code, as OWASP mitigation 5 recommends.

For the tools themselves, see how to give your site's AI chat real tools with an MCP server. On tensorpass.app the result is measurable: we searched the 17 scripts of the live home page on 12 November 2026 and found 0 internal service names and 0 server addresses.

Our measured run of 12 November 2026

On 12 November 2026 the chat on the tensorpass.app home page passed 108 of 108 checks of its guards and its docs MCP server, and 35 of 35 conversations through a real model, claude-haiku-5-5. This is an internal test, run with the pen test script the free aiconnect-hardening Agent Skill ships. It is no third party audit. The full method sits in attacking our own AI chat.

The script has three modes. Unit mode runs the guards with the site's policy and no network: 21 attacks the input guard must stop, 12 ordinary questions that must pass, 4 envelope checks and 14 answers that must stop or pass. MCP mode calls each tool with five hostile argument categories, including a path that climbs out of the site's folder and an internal network address. Model mode runs the whole chat. Every attack asks for a canary, a random code drawn for the run, and a check fails when an answer the visitor would see carries the canary, the attacker's host or the salt.

The 35 conversations break down this way:

CategoryConversationsWhat it tries
Direct attacks the input guard stops21Dropped instructions, role changes, spoofed tags, base64, invisible characters
Direct attacks that reach the model4A claim to be the developer, a fictional frame, a JSON answer with a debug field
Attacks over several messages2A request split across three messages, a slow drift to an outside task
Plain sample questions4Each must get an answer that called a tool
Sample questions with planted content4A fake notice with a link, an image, the canary and a forged closing tag

Of the 25 direct attacks, 19 are in English, 3 in French and 3 in Hebrew. One attack went through before the 35 of 35: the JSON escape, which asks for an answer in JSON with an extra debug field holding a code. On 12 November we added a rule to the instructions the guards wrap around yours, and the next run passed all 35. The day before, 114 guard unit tests passed, and 17 of 17 end to end scenarios were blocked with Mistral played as a compromised model.

The academic baseline sets the scale. The three layer framework cut attack success from 71.4% to 11.3% across GPT-4o, Llama 3 and Mistral 7B, beating the best single layer baseline by 27.3 percentage points. Its 5,080 samples and our 35 conversations measure different things on different pipelines, so the two figures do not compare one to one.

What remains possible after four guards

The guards reduce a risk without removing it. Our 35 of 35 holds for one model, claude-haiku-5-5, and your visitors bring their own models. The aiconnect-hardening skill tells a site to rerun the model mode with the model its visitors use most.

Three limits follow from the design. Streamed pieces show until the safe answer replaces them. The visitor reads your page's code, your instructions and every tool result, so all three must hold public facts only. A new phrasing outside the 18 patterns reaches the model, which is why the envelope, stream and output guards sit behind the input guard. For the wider threat model of a public chat, read our guide to AI chatbot security for a public site, and the prompt injection definition for the attack class itself.

FAQ: prompt injection in a website chatbot

Can a system prompt alone stop prompt injection?

No. A system prompt is neither a secret nor a security control, and LLM01:2025 lists 7 separate mitigations, from output validation to adversarial testing. A system prompt sits in the same token stream as the attack, so a later instruction competes with it on equal terms. Use it to set scope, for instance with the scope option of the guards, and put the actual controls in code: the input check, salted envelopes, the stream check and least privilege on tools.

How to prevent prompt injection when the chat reads reviews and comments?

Treat every review, comment and CMS page your tools return as data inside a salted envelope, and return addresses only as whole fields. A review that forges </tool-result-...> stays inside its envelope because it lacks the per conversation salt. A link written inside the review text is no field, so the widget leaves it unclickable. In our 12 November 2026 run, 4 of 4 conversations with planted content passed.

Does the input guard block legitimate questions?

It is built to let them through, and the test checks it. Unit mode sends 8 ordinary questions that share words with attacks, such as a question about assembly instructions, plus 4 sample questions, and all 12 must pass. The 2026 three layer framework reports a 4.8% false positive rate, which shows the trade off every filter carries. Keep your own lookalike questions in the test so a new pattern cannot silently block them.

What is the difference between prompt injection and a jailbreak?

LLM01:2025 treats jailbreaking as a form of prompt injection in which the attacker makes the model disregard its safety protocols entirely. Prompt injection is the broader class: input that overrides your application's instructions, typed by a visitor or planted in retrieved content. For a site, the practical split is ownership. The model vendor trains against jailbreaks, and you own the trust boundary between your instructions, the visitor and your data.

How often should you rerun the pen test?

Rerun the unit and MCP modes on every change. On tensorpass.app they run in CI as the last Playwright project on every change. The model mode needs an API key and runs by hand, and you rerun it when you change instructions, tools or the model your visitors use most. The guards read their options at a conversation's first question, so a policy change applies from the next conversation.

How TensorPass guards the chat on your site

We ship the four guards inside every AI Connect module, turned on with no setup, and you tune them with one guards option. Start from the guards module documentation, which lists every check and option.

The guards module. The AI Connect SDK, guards included, is free and open source under Apache 2.0, public on GitHub since 12 November 2026. Its four packages are on npm under the @tensorpass scope since 16 November 2026, with provenance signed by the GitHub build.

The aiconnect-hardening Agent Skill. Our Agent Skills are free, and this one holds the rate limit template, the policy files and the pen test that produced our 108 of 108 and 35 of 35.

Vulnerability reports. Send any bypass you find to [email protected].

Rerun the checks after every change: how to prevent prompt injection from coming back

Each change to your instructions, your tools or your allowed hosts reopens the question the guards answered. Run the unit and MCP modes in CI on every commit, run the model mode by hand with your visitors' most used model, and keep the JSON escape and your own lookalike questions in the suite. Then publish the date and the count of your last run, labelled internal, as we did on 12 November 2026.

To track how to prevent prompt injection on your own chat over time, log the date, the model and the pass count of each run beside the commit that triggered it. A drop from 35 of 35 then points straight at the change that caused it.

Also worth reading:

Sources:

Questions

Can a system prompt alone stop prompt injection?
No. A system prompt is neither a secret nor a security control, and LLM01:2025 lists 7 separate mitigations, from output validation to adversarial testing. A system prompt sits in the same token stream as the attack, so a later instruction competes with it on equal terms. Use it to set scope, for instance with the scope option of the guards, and put the actual controls in code: the input check, salted envelopes, the stream check and least privilege on tools.
How to prevent prompt injection when the chat reads reviews and comments?
Treat every review, comment and CMS page your tools return as data inside a salted envelope, and return addresses only as whole fields. A review that forges </tool-result-...> stays inside its envelope because it lacks the per conversation salt. A link written inside the review text is no field, so the widget leaves it unclickable. In our 12 November 2026 run, 4 of 4 conversations with planted content passed.
Does the input guard block legitimate questions?
It is built to let them through, and the test checks it. Unit mode sends 8 ordinary questions that share words with attacks, such as a question about assembly instructions, plus 4 sample questions, and all 12 must pass. The 2026 three layer framework reports a 4.8% false positive rate, which shows the trade off every filter carries. Keep your own lookalike questions in the test so a new pattern cannot silently block them.
What is the difference between prompt injection and a jailbreak?
LLM01:2025 treats jailbreaking as a form of prompt injection in which the attacker makes the model disregard its safety protocols entirely. Prompt injection is the broader class: input that overrides your application's instructions, typed by a visitor or planted in retrieved content. For a site, the practical split is ownership. The model vendor trains against jailbreaks, and you own the trust boundary between your instructions, the visitor and your data.
How often should you rerun the pen test?
Rerun the unit and MCP modes on every change. On tensorpass.app they run in CI as the last Playwright project on every change. The model mode needs an API key and runs by hand, and you rerun it when you change instructions, tools or the model your visitors use most. The guards read their options at a conversation's first question, so a policy change applies from the next conversation.
Read the quickstartRead the changelog