Machine Learning for CybersecuritySource on GitHub

Unit 10 · Lesson 10.2

Prompt injection and jailbreaks

Lesson 10.1 established a structural fact. A language model receives its developer's instructions and the data it processes as one stream of tokens. This lesson studies the attacks that follow from that fact. It separates prompt injection from jailbreaking, describes direct and indirect injection, gives a formal definition that makes attacks measurable, and shows how to estimate an attack success rate with an interval that reflects the sample size. It then explains why filters and blocklists do not solve the problem, what the main defenses achieve and where they stop, and how the OWASP Top 10 for LLM Applications organizes the risks.

Two attacks on one weakness

Prompt injection is an attack on an application. The attacker supplies text that makes an application built on a language model carry out a task the attacker chose instead of, or in addition to, the task its developer intended. Perez and Ribeiro (2022) gave two early examples against applications built on GPT-3. In goal hijacking, the injected text makes the model print a phrase chosen by the attacker; in prompt leaking, it makes the model reveal the application's own prompt. Both worked with short handwritten inputs such as an instruction to ignore the previous prompt.

A jailbreak is an attack on the model's safety training. The attacker's goal is content that the model's developers trained it to refuse, such as instructions for causing harm. Wei et al. (2023) trace jailbreaks to two failure modes of safety training. Competing objectives arise when the model's training to be helpful and follow instructions conflicts with its training to refuse, so that a prompt can set one against the other. Mismatched generalization arises when the model's capabilities extend to inputs (an encoding, a rare language) that its safety training did not cover. Zou et al. (2023) automated the search. Their greedy coordinate gradient method appends a suffix of tokens to a request and optimizes the suffix, using the gradients of an open model, to maximize the probability that the model begins its answer affirmatively instead of refusing. Suffixes optimized on two open models transferred to other open models and to commercial chat products.

The two attacks overlap in technique, and a jailbreak string can be delivered by injection. They differ in who is harmed and what fails. A jailbreak defeats a property of the model; an injection defeats the application's control over its own task, even when the model's output is harmless in isolation. Most of this lesson concerns injection, because it is the attack that turns a language model into a confused deputy, a program that holds the user's or the application's privileges and is induced by a less privileged party to use them on that party's behalf.

Direct and indirect injection

In direct injection the attacker is the user and types the injected text into the application. In indirect injection, introduced by Greshake et al. (2023), the attacker never interacts with the application. The attacker plants instructions in content that the application will later retrieve on behalf of an innocent user: a web page, an email, a shared document, a code comment, a product review. Figure 10.4 contrasts the two.

Figure 10.4. Two routes for an injected instruction. (a) In direct injection the attacker is the application's user. (b) In indirect injection a benign user asks the application to process content that the attacker wrote earlier, and the attacker's instruction reaches the model through the

Figure 10.4. Two routes for an injected instruction. (a) In direct injection the attacker is the application's user. (b) In indirect injection a benign user asks the application to process content that the attacker wrote earlier, and the attacker's instruction reaches the model through the retrieval path (red arrows). In (b) the attacker needs no account and no direct interface to the application.

Greshake et al. (2023) demonstrated indirect injection against real systems, including a search-engine chat product built on GPT-4 and code-completion engines, and organized the consequences in a taxonomy that includes data theft, worming (an injected instruction that tells the model to copy itself into outgoing messages), contamination of the information the user receives, and control over which external functions the application calls. The more an application can do, the more an injection can do through it. An application that only summarizes can be made to summarize falsely; an application that can send email can be made to send it.

Indirect injection does not require visible text. Liu et al. (2024) note that an applicant could add an instruction to a résumé in white letters on a white background, invisible to a person but present after the PDF is converted to text. The same applies to HTML comments, alternative text of images and metadata fields, all of which a retrieval pipeline may pass to the model.

A formal definition

Liu et al. (2024) made the attack precise. An application has a target task \(t\), given by an instruction \(s^t\) (written by the developer or the user) and data \(x^t\) (the document, email or web page). The attacker chooses an injected task \(e\), with instruction \(s^e\) and data \(x^e\), and replaces the target data by compromised data

\[ \tilde{x} = \mathcal{A}(x^t, s^e, x^e), \label{eq:compromised} \]

where \(\mathcal{A}\) is the attack's construction. The attack succeeds if the application, given \(s^t\) and \(\tilde{x}\), accomplishes the injected task instead of the target task. Different attacks are different choices of \(\mathcal{A}\). The simplest appends the injected instruction to the data. Context ignoring adds a phrase such as "Ignore previous instructions." Fake completion adds a fake answer to the target task, such as "Answer: task complete", so that the model treats the target task as finished. Their combined attack joins these with a special character \(c\) such as a newline,

\[ \begin{aligned} \tilde{x} &= x^t \oplus c \oplus r \oplus c \\ &\quad \oplus i \oplus s^e \oplus x^e, \end{aligned} \label{eq:combined} \]

where \(\oplus\) denotes concatenation, \(r\) the fake response and \(i\) the context-ignoring text. In their evaluation of 5 attacks and 10 defenses across 10 models and 7 tasks, the combined attack was consistently effective and outperformed the other attacks.

The definition turns injection into an experiment. Fix a set of target tasks and an injected task whose success can be checked automatically, measure how often the injected task is accomplished, and measure separately how well the application performs its target task when no attack is present. A defense that lowers attack success but also lowers performance on clean data has a cost that must be reported. Lab 10a follows this design. Its injected task is to print a canary, the string CANARY-7f3a, which never appears in a benign answer, so success can be checked by a string search with no ambiguity.

Estimating an attack success rate

In this lesson the attack success rate (ASR) is the share of injected documents for which the application carries out the injected task; its denominator is the number of injected documents, not all inputs. Suppose \(k\) of \(n\) injected documents lead to a successful attack. The point estimate of the ASR is \(\hat{p} = k/n\). With the small \(n\) typical of such experiments, the familiar interval \(\hat{p} \pm z\sqrt{\hat{p}(1-\hat{p})/n}\), where \(z\) is a quantile of the standard normal distribution, behaves badly, especially when \(\hat{p}\) is near 0 or 1. Brown et al. (2001) analyze its coverage and recommend, for \(n\) of 40 or less, the Wilson interval or the Jeffreys interval. The Wilson interval is

\[ \begin{aligned} &\frac{\hat{p} + \dfrac{z^2}{2n}}{1 + \dfrac{z^2}{n}} \\ &\quad \pm\; \frac{z}{1 + \dfrac{z^2}{n}} \sqrt{\frac{\hat{p}(1-\hat{p})}{n} + \frac{z^2}{4n^2}}, \end{aligned} \label{eq:wilson} \]

with \(z = 1.96\) for 95% confidence. For \(k = 6\) successes in \(n = 30\) trials it gives \([0.095, 0.373]\). For \(k = 1\) it gives \([0.006, 0.167]\), while the simple interval gives \([-0.031, 0.098]\), whose lower end is impossible and whose upper end understates the uncertainty.

The width of these intervals has a practical consequence. To tell an ASR of 20% from one of 10%, with a two-sided test at the 5% level and 80% power, the usual approximation for comparing two proportions \(p_1\) and \(p_2\) asks for

\[ \begin{aligned} n &\approx \frac{(z_{0.975} + z_{0.8})^2}{(p_1 - p_2)^2} \\ &\quad \times [p_1(1-p_1) + p_2(1-p_2)] \end{aligned} \label{eq:samplesize} \]

documents per defense, where \(z_q\) is the \(q\) quantile of the standard normal distribution. With \(p_1 = 0.2\) and \(p_2 = 0.1\) this is \((1.960 + 0.842)^2\) times \(0.25/0.01\), about 196 injected documents per defense. Studies that compare defenses on a few dozen documents can rank them only when the differences are large.

Show the answer

With \(k = 0\) and \(n = 25\), \(\hat{p} = 0\) and \(z^2/n = 0.1537\). The center is \(0.0768/1.1537 = 0.0666\). The half-width is \(1.96/1.1537 = 1.6989\) times \(\sqrt{3.842/2500} = 0.0392\), which is 0.0666, so the interval is \([0, 0.133]\). The data are consistent with a true ASR of up to about 13% against the attacks tested, and say nothing about attacks designed with knowledge of the defense. A defensible phrasing is "no successful attack among 25 attempts with these injections (95% upper bound 13%)".

Why filtering and blocklists are insufficient

A tempting response is to search incoming text for phrases such as "ignore previous instructions" and reject documents that contain them. A blocklist of this kind cannot serve as a defense on its own, for reasons that apply to filters in general. The space of instructions is the space of natural language. The same request can be paraphrased, translated, split across sentences, encoded or hidden in markup, and Zou et al. (2023) showed that effective adversarial text can be strings of tokens that look like noise and appear on no list. The filter and the model also read the text differently, because the filter matches characters while the model reads tokens and meaning.

Detectors that are more sophisticated than string matching fare better but not well enough. Liu et al. (2024) benchmarked detection-based defenses, including perplexity filters, which flag data whose perplexity (the exponential of the average negative log-likelihood of Lesson 10.1) exceeds a threshold, and using the language model itself to judge whether data is compromised. All but one either missed a large fraction of compromised data or flagged a large fraction of clean data as compromised, and the best one detected compromised data effectively in some cases but missed a large fraction in many others. A detector of injected documents is also subject to the base-rate effect of Lesson 1.2. If one retrieved document in ten thousand is malicious, a detector with a false positive rate of 1% raises about a hundred false alarms for every real one. And any filter can be studied by an adaptive attacker, who rewrites the injection until it passes.

Defenses and what they achieve

Defenses fall into two groups. The first changes how the model sees the data or how it was trained, which lowers the probability that it obeys an injection. The second changes the system around the model, which bounds what an obeyed injection can cause.

Spotlighting. Hines et al. (2024) studied spotlighting, a family of transformations that give the model a continuous signal of which text is data, in three forms. Delimiting wraps the data in special markers and tells the model not to follow instructions between them. Datamarking replaces every whitespace in the data with a marker character such as ^, so that every word of the data carries the signal. Encoding transforms the data, for example into base64, which strong models can still read. With GPT-3.5-Turbo on a document summarization task, special delimiters roughly halved the ASR and datamarking reduced it from about 50% to about 3%, with no detrimental effect on task performance in their benchmarks. Encoding brought the ASR to zero or close to it, and it can serve inputs such as code for which datamarking may be ineffective, but the authors recommend it only for the highest-capacity models, because GPT-3.5-Turbo lost task performance on encoded text. They also assume that an attacker may learn the system prompt and imitate the marking, and suggest changing the marking tokens dynamically or choosing them at random.

Reminders after the data. Repeating the instruction after the data (sometimes called sandwich prevention) makes the developer's instruction the last text the model reads before it answers. Liu et al. (2024) found that the prevention-based defenses they tested, including this one and delimiters, had limited effectiveness and some caused large utility losses on clean data.

Instruction hierarchy. Wallace et al. (2024) argue that a primary cause of injection is that models give system prompts, user messages and tool outputs the same priority. They define a hierarchy (system above user above tool outputs and other third-party text) and generate training data that teaches a model to ignore lower-privileged instructions that conflict with higher ones. Training in this way drastically increased robustness in their evaluation, including against attack types not seen in training, with minimal degradation of standard capabilities. This is a property learned by the weights, and the model's adherence to it is a probability, not a guarantee.

Lab 10a measures four prompt-level defenses, together with a design that has no defense, on a 360-million-parameter model. Figure 10.5 shows the result.

Figure 10.5. Results of Lab 10a for SmolLM2-360M-Instruct with 32-bit floating-point weights and greedy decoding, on 30 synthetic security documents. (a) Share of injected documents for which the output contained the canary, with Wilson 95% intervals from equation \eqref{eq:wilson}. (b) Share of ben

Figure 10.5. Results of Lab 10a for SmolLM2-360M-Instruct with 32-bit floating-point weights and greedy decoding, on 30 synthetic security documents. (a) Share of injected documents for which the output contained the canary, with Wilson 95% intervals from equation \eqref{eq:wilson}. (b) Share of benign documents answered correctly by the automatic check, with Wilson 95% intervals. The five designs use the same task instruction and differ only in where the instruction and a warning about the document are placed and in how the document is marked; the design labeled none has no warning.

Without a defense, 7 of 30 injected documents made the model print the canary (95% interval 0.12 to 0.41). With datamarking the count was 2, with delimiters 4, with the reminder 8, and with instructions in the system role 7. Task accuracy on benign documents stayed between 87% and 93% for all designs. The intervals of every pair of designs overlap, and by equation \eqref{eq:samplesize} thirty documents per design cannot reliably separate designs whose true rates differ by ten percentage points. The model is also far smaller and less capable than deployed models. The value of the lab is its method, which scales to larger models and larger document sets without change.

Privilege separation. System-level defenses accept that the model may obey an injection and arrange that obeying it cannot matter. Beurer-Kellner et al. (2025) state the guiding principle as follows. Once an agent has ingested untrusted input, it must be constrained so that this input cannot trigger consequential actions. Their design patterns include the dual model pattern, in which a privileged model plans and calls tools but never reads untrusted data, while a quarantined model reads untrusted data but has no tools. In the more involved version of the pattern, the quarantined model's outputs are stored in variables that the privileged model can pass around by name without reading, and ordinary code substitutes the values when tools are called. Debenedetti et al. (2026) built this idea into CaMeL, which extracts the control flow and data flow from the trusted user query, so that retrieved data can never change which tools are called, and attaches capabilities to values to enforce policies on where data may flow. On the AgentDojo benchmark, CaMeL solved 77% of tasks with provable security, against 84% for the same system without the defense. The cost is utility, since the system can perform only tasks whose plan can be fixed from the user's request.

Output handling. Whatever the model produces is text from an untrusted source, because an injection may have shaped it. Output that is rendered as HTML must be escaped, output that enters a database query must be passed as a parameter, output must never be executed as code or shell commands without isolation, and links and images in the output must not be fetched automatically, since an injected instruction can ask the model to put stolen data into a URL that the client then requests. Lesson 10.3 applies these principles to agents.

Show the answer

Output handling stops it most directly. If the dashboard does not render images or fetch URLs from model output (or only from an allowlist of domains), the URL is never requested and the data stays put. Privilege separation also helps. If the summarizer that reads untrusted emails never receives other customers' emails in its context, there is nothing for the injected instruction to exfiltrate. Spotlighting the email (delimiting or datamarking it) would lower the chance that the model obeys, but would not guarantee it.

The OWASP Top 10 for LLM Applications

The OWASP Gen AI Security Project publishes a ranked list of risks for applications built on language models. Version 2025 of the OWASP Top 10 for LLM Applications, released on 18 November 2024 (OWASP Foundation, 2024), names the ten items listed in the table below, together with the part of this course that treats each one. Each code in the table carries the suffix :2025 in the official list (LLM01:2025, for example).

Code Risk Treated in
LLM01 Prompt Injection This lesson; Lab 10a
LLM02 Sensitive Information Disclosure Lesson 8.2 (membership inference); Lesson 10.3
LLM03 Supply Chain Lesson 10.3; Lab 10b
LLM04 Data and Model Poisoning Lesson 8.1; Lesson 10.3
LLM05 Improper Output Handling This lesson; Lesson 10.3
LLM06 Excessive Agency Lesson 10.3; Lab 10b
LLM07 System Prompt Leakage This lesson (prompt leaking)
LLM08 Vector and Embedding Weaknesses Lesson 10.3
LLM09 Misinformation Lesson 10.1 (hallucination)
LLM10 Unbounded Consumption Lesson 10.1 (tokens and cost)

The table maps where things go wrong; working through it does not by itself make an application safe. Several items describe the same weakness seen from different sides. Prompt injection (LLM01) becomes dangerous through excessive agency (LLM06) or improper output handling (LLM05); system prompt leakage (LLM07) matters mainly when a system prompt contains secrets or security logic that should have been enforced elsewhere. In August 2026 the project published a 2026 edition, the OWASP Top 10 for LLM Applications 2026 (OWASP Foundation, 2026). It keeps prompt injection first and sensitive information disclosure second, moves excessive agency from sixth to third, moves improper output handling from fifth to tenth, and renames system prompt leakage as hidden context exposure (LLM08:2026), which it describes as a broader framing of the same failure. Several entries were also broadened; prompt injection, for example, now covers instructions hidden in images or audio. This course refers to the 2025 item codes, and every risk in the table above remains in the 2026 list under the same or a broader name.

What the lab shows

Lab 10a

Measuring indirect prompt injection on a small model

Lab 10a builds 30 short security documents, an injected copy of each with one of five injection styles (including the combined attack of equation \eqref{eq:combined}), and five prompt designs. It reports the attack success rate and the task accuracy of every design with Wilson intervals, breaks the undefended attack success down by injection style and position, and prints outputs so that you can check the automatic scoring yourself.

Summary

Prompt injection makes an application perform an attacker's task; a jailbreak makes a model produce what its safety training was meant to prevent. Indirect injection needs no access to the application, only the ability to place text where it will be retrieved. The formal view of Liu and colleagues turns injection into an experiment with two measurements, attack success on compromised data and task performance on clean data, each of which should be reported with an interval that reflects the small samples such experiments use. Blocklists and filters cannot cover the space of natural-language instructions. Delimiting, datamarking, reminders and training for an instruction hierarchy lower the probability of obedience without bounding it, and the bound has to come from the design of the system, through privilege separation so that untrusted text cannot choose actions and through careful handling of everything the model outputs.

References

  1. Beurer-Kellner, L., Buesser, B., Creţu, A.-M., Debenedetti, E., Dobos, D., Fabian, D., Fischer, M., Froelicher, D., Grosse, K., Naeff, D., Ozoani, E., Paverd, A., Tramèr, F., and Volhejn, V. (2025). Design patterns for securing LLM agents against prompt injections. arXiv preprint arXiv:2506.08837. https://arxiv.org/abs/2506.08837
  2. Brown, L. D., Cai, T. T., and DasGupta, A. (2001). Interval estimation for a binomial proportion. Statistical Science, 16(2), 101-133. https://doi.org/10.1214/ss/1009213286
  3. Debenedetti, E., Shumailov, I., Fan, T., Hayes, J., Carlini, N., Fabian, D., Kern, C., Shi, C., Terzis, A., and Tramèr, F. (2026). Defeating prompt injections by design. 2026 IEEE Conference on Secure and Trustworthy Machine Learning (SaTML), 587-618. https://doi.org/10.1109/SaTML68715.2026.00040
  4. Greshake, K., Abdelnabi, S., Mishra, S., Endres, C., Holz, T., and Fritz, M. (2023). Not what you've signed up for: Compromising real-world LLM-integrated applications with indirect prompt injection. Proceedings of the 16th ACM Workshop on Artificial Intelligence and Security (AISec), 79-90. https://doi.org/10.1145/3605764.3623985
  5. Hines, K., Lopez, G., Hall, M., Zarfati, F., Zunger, Y., and Kiciman, E. (2024). Defending against indirect prompt injection attacks with spotlighting. Proceedings of the Conference on Applied Machine Learning in Information Security (CAMLIS 2024), CEUR Workshop Proceedings 3920, 48-62. https://ceur-ws.org/Vol-3920/paper03.pdf
  6. Liu, Y., Jia, Y., Geng, R., Jia, J., and Gong, N. Z. (2024). Formalizing and benchmarking prompt injection attacks and defenses. Proceedings of the 33rd USENIX Security Symposium, 1831-1847. https://www.usenix.org/conference/usenixsecurity24/presentation/liu-yupei
  7. OWASP Foundation (2024). OWASP Top 10 for LLM Applications 2025. OWASP Gen AI Security Project. Version 2025, released 18 November 2024. https://genai.owasp.org/resource/owasp-top-10-for-llm-applications-2025/
  8. OWASP Foundation (2026). OWASP Top 10 for LLM Applications 2026. OWASP Gen AI Security Project. Version 2026, listed on the project site on 3 August 2026. https://genai.owasp.org/resource/owasp-genai-llm-top-10-2026/
  9. Perez, F., and Ribeiro, I. (2022). Ignore previous prompt: Attack techniques for language models. ML Safety Workshop, 36th Conference on Neural Information Processing Systems (NeurIPS 2022). arXiv preprint arXiv:2211.09527. https://arxiv.org/abs/2211.09527
  10. Wallace, E., Xiao, K., Leike, R., Weng, L., Heidecke, J., and Beutel, A. (2024). The instruction hierarchy: Training LLMs to prioritize privileged instructions. arXiv preprint arXiv:2404.13208. https://arxiv.org/abs/2404.13208
  11. Wei, A., Haghtalab, N., and Steinhardt, J. (2023). Jailbroken: How does LLM safety training fail?. Advances in Neural Information Processing Systems, 36, 80079-80110. https://proceedings.neurips.cc/paper_files/paper/2023/hash/fd6613131889a4b656206c50a8bd7790-Abstract-Conference.html
  12. Zou, A., Wang, Z., Carlini, N., Nasr, M., Kolter, J. Z., and Fredrikson, M. (2023). Universal and transferable adversarial attacks on aligned language models. arXiv preprint arXiv:2307.15043. https://arxiv.org/abs/2307.15043