Prompt injection: untrusted text in the prompt

Unit 2 of 5 in Securing LLM applications.

You will be able to explain direct and indirect prompt injection, what input screening can and cannot do, and how to test a defence honestly.

Lesson

Why it exists, and what it can do

The OWASP cheat sheet defines prompt injection as a vulnerability that lets someone manipulate an application's behaviour by putting input in front of the model that changes its intended output. It lists the impacts: bypassing safety controls and content filters, unauthorized data access, leaking the internal instructions, unauthorized actions through connected tools and APIs, and manipulation that persists across sessions.

The impact depends on what the application can reach. A chatbot that can only print text has a small blast radius. The same weakness in an assistant that can read mail, call an API or change files is serious, which is why the later units spend so much time on what the model is allowed to do.

Direct and indirect injection

Direct injection is explicit instructions in the user's own message. Indirect injection is instructions hidden in content the application fetches for the user. The cheat sheet's examples of such content are code comments and documentation that coding assistants read, commit messages and merge request descriptions, issue descriptions and reviews, web pages and documents the model fetches, and email content and attachments, including hidden text in any of them.

Indirect injection matters most to builders because the person who types a message may be innocent: the harmful text came from a page, a file or an email. The cheat sheet adds a hiding place to check: ingestion pipelines usually hand the model a document's extracted text and discard how it looks, so text a reader cannot see still reaches the model. It reports a study of 196,682 resumes from one hiring platform in which more than 90 percent of the hidden prompts contained no explicit instruction, so a scanner that looks only for instruction-like phrases would miss most of them. Measure false positives on your own documents, because tiny fine print and character recognition errors cause them.

What input screening can and cannot do

The cheat sheet is direct: input screening can flag known attack phrasing for logging, review or rate limiting, but it is not a security boundary, because models do not enforce a boundary between instructions and data and an instruction can be rephrased in many ways a deny list will not match. Keyword filters also make two kinds of mistakes. They block harmless requests that contain words like delete, system or reveal, such as 'How do I delete my account?', so match phrases rather than single words and measure the false positive rate on benign requests. And they miss paraphrases, other languages and encoded text.

Normalization helps but has limits. The cheat sheet notes that one common Unicode normalization form folds look-alike forms such as fullwidth letters but does not remove zero-width characters or map look-alike letters from other scripts. Invisible Unicode tag characters can carry hidden instructions that models read, and if you screen a cleaned copy but forward the original, the hidden text still reaches the model. It advises sending unexpected zero-width or tag characters to review, decoding encoded text for inspection only, and limiting input length before normalization, decoding and matching.

Structure, monitoring and people for consequential actions

The cheat sheet recommends keeping trusted instructions apart from untrusted data in the prompt, with a warning: do not treat text labels or prompt wording as an enforcement boundary. Their examples illustrate formatting only; permissions must be enforced at the tool boundary. Monitor outputs for signs that an injection worked, and require human approval for consequential tool actions, based on the proposed operation, its target and arguments, and the caller's authority. Keyword counts in the user's prompt do not tell you the action's risk, and the execution component must verify approval for the exact action.

Model-based guardrails, meaning a separate model that screens inputs and outputs, sit alongside deterministic controls, not in place of them. The cheat sheet adds that you should check which languages a guardrail model supports and not assume it protects equally in others. Language-independent controls such as tool permissions and parameter validation hold whatever language the injected text uses.

Test with a budget and report the limits

Attackers can try many variations. The cheat sheet cites a 2024 study that reported 89 percent attack success on one model and 78 percent on another when up to 10,000 augmented prompts were allowed per request, and stresses these are results for tested models and configurations, not universal predictions. Its advice for builders is to test repeated attempts within a defined budget, to keep authorization and least privilege outside the model, and to evaluate filters against varied inputs rather than assuming a blocked example proves safety.

Its smoke-testing section sets out how to be honest about results. Call the examples a smoke test, not a security benchmark. Define each case's intended violation and observable outcome before running it. Use dummy data and sandboxed substitutes for tools, and never put a real secret in a prompt for testing. Grade by outcome: a dummy marker that appears in a response, instrumented tool calls and changes to dummy data, or dummy data reaching a test destination. A refusal in the final answer does not undo an action already taken, and a clean text answer does not show that no data left by another channel. Include benign requests in every language you support so the false positive rate is measured for each.

Key points

Sources

Practice in the labs

Build a labelled test set, measure attack success, gate it in a build and see what filters miss. All labs use dummy data and run on your own machine.

Knowledge check

  1. A model reads a web page for a user and obeys a hidden instruction in the page. What kind of injection is this? Answer: Indirect injection. The instruction came from external content the application fetched, not from the user's own message; the cheat sheet calls this remote or indirect prompt injection.
  2. Why is a keyword filter not a security boundary? Answer: An instruction can be rephrased, translated or encoded in ways the list does not match, and the model does not enforce an instruction/data boundary. OWASP says input screening is useful for logging, review and rate limiting but is not a boundary, because attackers can rephrase and the model does not separate instructions from data.
  3. Your test shows the model's final answer refused a harmful request. Did the defence work? Answer: Not necessarily: a refusal does not undo a tool action already taken or show that no data left another way. The cheat sheet's smoke-testing section says to grade by observable outcome: instrumented tool calls, dummy data changes and test destinations, because the text of a reply may not reflect what happened.
  4. What should you put in a prompt when testing whether a secret leaks? Answer: A dummy marker, never a real secret. The cheat sheet says to use dummy data and a dummy marker, and never put a real secret in the prompt for testing; marker absence only shows that exact marker was not seen.

Back to the syllabus or see all classes.

Related