How do we…
How do we defend an LLM application against prompt injection?
Add to board memoOpen the memo
The short answer
- Recommendation
- Defend in layers and design on the assumption that the model will be fooled. Lower the chance of an injection working, then limit what a success can do with least tools and access, hard caps and a person approving the actions that matter.
- Reasoning
- An LLM application reads instructions and data as the same kind of thing, so prompt injection cannot be reliably filtered out. Keeping untrusted content apart, stripping hidden characters and limiting inputs lower the chance of success; they are not a barrier (GS-04). The security rests on what the system around the model allows, never on instructions in the prompt (GS-02). Most serious cases arrive in content the model reads, not in what users type. If the owner cannot accept the worst a success could do, the design changes.
- Considerations
- With all three of untrusted content, sensitive data and the power to act or send, a person approves every action, seeing the exact action (GS-24, GS-25). With two, the review records the residual risk for the approvers.
- Rewriting the prompt does not close a finding. The fix is a limit in code, permissions or approvals, then a retest of the same attack class.
- A clean test shows what the testers tried, not what is possible. Read it with the list of what was not tried.
- The depths, days and limits are the pack's defaults. The community sources it cites are not law.
The decision, step by step
Select any step for the reasoning behind it and where the pack covers it.
- Start
- Step
- Decision
- Outcome
- Loop back
Keyboard: Tab to a step, arrow keys move along the flow, Enter pins its explanation, + and − zoom, 0 fits the diagram, Esc closes. The text version below has the same content.
Where a step quotes a number, it is the default in the CISO Times pack. Your own standard may set a different one.
Read as text
The same flow written out: each step, its reasoning and where the pack covers it. 18 steps.
Start
An application sends text we do not control to a language model
Ordinary software keeps instructions and data apart. An LLM application cannot, because both are tokens on the same stream, as OWASP puts it. The UK NCSC warns that prompt injection may never be totally mitigated in the way SQL injection can be. OWASP says the defence is architectural rather than interceptive. So lower the chance of success, then limit what a success can do.
In the pack
- P25-A01 P25-A01
- P25-A02 Generative AI Security StandardHow this standard works (rule GS-02)
Step
Draw what the model reads and where its output goes
Prompt injection is direct when a user types it and indirect when the model reads it in a document, web page, email, tool result or retrieved passage. List every source the model reads and every place its output goes, and mark each source trusted or untrusted. Content from outside the organisation, and from anyone who can submit text, is untrusted whichever system holds it.
In the pack
- P25-A02 Generative AI Security StandardDesign (rule GS-03)
- P25-A04 LLM Application Threat CatalogueThreat Catalogue (rule LT-01)
Decision
Does it read content from anyone who can submit text?
Customers' messages, supplier documents, inbound email, web pages, tickets, public files and the output of a tool that fetches them all count. So does a wiki that anyone in the company can edit. Most serious cases arrive this way, in content that nobody typed into the feature. If you are not sure, answer yes.
- Yes, or not known: Keep what it reads apart from instructions and strip hidden text
- No, only our own staff type into it: Limit what users can send and expect override attempts
In the pack
- P25-A02 Generative AI Security StandardDefinitions
- P25-A04 LLM Application Threat CatalogueThreat Catalogue (rule LT-02)
Step
Keep what it reads apart from instructions and strip hidden text
Send untrusted content in a separate, labelled part of the request and never build instructions by joining text from untrusted sources. Strip hidden characters (tag characters, zero-width characters, variation selectors) on the way in and when output is shown. Filter every kind of input, not only text, and limit the size, number and type of inputs. These steps lower the chance of success. They are not a barrier.
In the pack
- P25-A02 Generative AI Security StandardInput handling (rule GS-04)
- P25-A02 Generative AI Security StandardInput handling (rule GS-05)
Step
Limit what users can send and expect override attempts
A feature that only staff use still faces direct injection: an instruction to ignore the rules, a role-play, or text in another language or an encoding. Limit the size, number and type of inputs and reject the rest. Do not rely on the system prompt to stop it. Treat it as readable by users, so it is not a security boundary.
In the pack
- P25-A02 Generative AI Security StandardInput handling (rule GS-05)
- P25-A02 Generative AI Security StandardSecrets (rule GS-11)
- P25-A04 LLM Application Threat CatalogueThreat Catalogue (rule LT-01)
Step
Treat the model's output as untrusted input to what receives it
Check output against a schema in application code, and encode it for where it goes: page, query, shell, link or terminal. Never pass it to an interpreter, query or template without that. A screen that shows output must not fetch addresses the output names (images, link previews, frames) unless they are on an allow list, because that is a way for data to leave. Show the full address of any link.
In the pack
- P25-A02 Generative AI Security StandardOutput handling (rule GS-06)
- P25-A02 Generative AI Security StandardOutput handling (rule GS-07)
- P25-A05 Generative AI Deployment Configuration ChecklistChecklist (rule DC-24)
Step
Keep secrets out of prompts and assume the prompt can be read
No key, password or token goes into a prompt, a system prompt, a tool description or a knowledge store. Treat the system prompt and tool descriptions as readable by users, so that revealing all of it would give an attacker no access they lack. A control that is only a sentence in the prompt is not a control.
In the pack
- P25-A02 Generative AI Security StandardSecrets (rule GS-10)
- P25-A02 Generative AI Security StandardDesign (rule GS-02)
Decision
How many of the three risky properties does it have?
The properties are: it reads untrusted content, it can reach data above Public or personal data, and it can change state or communicate outside the organisation. A successful injection needs something to act on, and the count shows how much there is. It is a count, never a score. If the owner cannot say whether a property holds, treat it as true.
- All three, or not known: A person approves every action, seeing the exact action
- Two of three: Write down the residual risk for the approvers to read
- One or none: Cut what a success can do: least tools, least access, hard caps
In the pack
- P25-A03 LLM Application Security Review ProcedureThe rule of two (rule GS-24)
Step
A person approves every action, seeing the exact action
With all three properties, a person approves every action the feature takes, seeing the exact action and not a summary of it. The review is at Depth 3 with an independent test. Some actions are never the agent's: a payment, a change of supplier bank details and a decision about a person.
In the pack
- P25-A02 Generative AI Security StandardHuman approval (rule GS-25)
- P25-A09 AI Agent Permission & Tool-Access Review ChecklistApproval Rules
Step
Write down the residual risk for the approvers to read
With two properties the review is at least Depth 2 and records in plain words what risk remains if an injection succeeds and who accepts it. The approvers read it. Judgement: the cheapest property to remove is often the third, such as a read-only tool in place of a write tool, or a person reading the output before it is used.
In the pack
- P25-A03 LLM Application Security Review ProcedureThe rule of two (rule GS-24)
- P25-A03 LLM Application Security Review ProcedureWho signs
Step
Cut what a success can do: least tools, least access, hard caps
Give the model only the tools the task needs, each doing one job with strict input. Each tool acts as the signed-in person or a named service identity with the least access its job needs, and code checks the person's permission on every call. Set limits on requests, tokens, steps and tool calls, with a hard spending cap, and a switch-off that one named person can use in one step.
In the pack
- P25-A02 Generative AI Security StandardAgents and tools (rule GS-22)
- P25-A02 Generative AI Security StandardAgents and tools (rule GS-23)
- P25-A02 Generative AI Security StandardResource limits (rule GS-21)
- P25-A02 Generative AI Security StandardHuman approval (rule GS-26)
Step
Log everything and watch for the injection signals
Log the request, the sources retrieved, every tool call with arguments and result, the output and the verdict of each filter, in a store the feature's administrators cannot change. Watch for output that follows content the feature has just read, addresses or encoded text in output that are not in the source, and many variations of one request. An event goes to the incident route, which decides whether it is an incident.
In the pack
- P25-A02 Generative AI Security StandardLogging (rule GS-19)
- P25-A02 Generative AI Security StandardMonitoring (rule GS-20)
- P25-A02 Generative AI Security StandardSignals to watch and evidence to keep
Step
Test with planted instructions in content and in user input
Send a maintained set of override and role-play prompts, including encoded text and other languages. In a test copy, plant instructions in the documents, web pages, emails, tickets, images and tool results the feature reads, in plain, hidden and encoded forms, then check what it says and does. The attack has worked when the feature leaves its task, ignores a rule, returns another person's data, or acts on instructions that came only from content it read.
In the pack
- P25-A04 LLM Application Threat CatalogueThreat Catalogue (rule LT-02)
- P25-A07 AI Red Team Test Plan TemplateAttack classes (rule AC-02)
Decision
Did the feature follow an instruction it should not have?
Any success, including a small one, is a finding. A test that finds nothing shows what the testers tried, not what is possible, so read a clean result together with the list of what was not tried. At Depth 3 the tester who did not build the feature runs these tests.
- Yes: Add a limit outside the model, rate the finding, then retest
- No, in the tests run: Could the owner and approvers accept the worst a success can do?
In the pack
- P25-A07 AI Red Team Test Plan TemplateSuccess criteria and results
- P25-A03 LLM Application Security Review ProcedureStage 5 (RP-5): Test
Step
Add a limit outside the model, rate the finding, then retest
A control that is only a sentence in a prompt is not a control, so rewriting the prompt does not close the gap. Add a limit in code, permissions or approvals that stops a success doing harm. Rate the finding on impact and likelihood, give it an owner and a due date, and put it in your findings register. Then retest the same attack class.
- Then: Test with planted instructions in content and in user input (back to an earlier step)
In the pack
- P25-A02 Generative AI Security StandardDesign (rule GS-02)
- P25-A03 LLM Application Security Review ProcedureRating a finding
- P25-A08 AI Security Findings & Exceptions RegisterFindings
Decision
Could the owner and approvers accept the worst a success can do?
No defence stops every injection, so ask what the worst successful one could do with the tools, data and audiences the feature has. The NCSC says that if the system's security cannot tolerate the remaining risk, it may not be a good use case for an LLM. Judgement: the owner states the worst case in plain words, and the approvers of the review depth accept it or do not.
- Yes: Take it to the release decision, then retest on every change
- No, or not known: Do not release as designed: remove a property or an action
In the pack
- P25-A01 P25-A01
- P25-A03 LLM Application Security Review ProcedureWho signs
Outcome
Do not release as designed: remove a property or an action
Take the worst case back to the design. Remove one of the three properties, such as the ability to send outside, the sensitive data or the untrusted source, or put a person in front of the action, then count again. If none can go, the use may not suit an LLM, and the approvers decide whether it proceeds.
In the pack
- P25-A01 P25-A01
- P25-A03 LLM Application Security Review ProcedureThe three outcomes
Outcome
Take it to the release decision, then retest on every change
The release decision is run under the review procedure, which names the approvers and the gates. Keep the test results and the settings copy with the review file. Re-run the injection tests after any change of model, model version, system prompt, tool, retrieval source or filter setting. The pack's default is a new review within 10 working days of the change or the provider's notice. Prompt injection stays on the monitoring list for as long as the feature runs.
In the pack
- P25-A03 LLM Application Security Review ProcedureWhat forces a new review
- P25-A03 LLM Application Security Review ProcedureStage 7 (RP-7): Release and watch
Practical checklist
- List every source the model reads and every place its output goes, and mark each source trusted or untrusted.
- Send untrusted content in a separate, labelled part of the request and strip hidden characters on the way in and out.
- Limit the size, number and type of inputs, and expect override attempts from users.
- Check output against a schema in code, encode it for where it goes, and fetch no addresses it names unless allow-listed.
- Keep secrets out of prompts, and treat the system prompt as readable by users.
- Give the model the least tools and access, set hard caps and a one-step switch-off, and add the approvals the property count requires.
- Log requests, sources, tool calls and outputs in a store administrators cannot change, and watch for the injection signals.
- Test with planted instructions in content and in user input, and retest after every change.
Evidence to have ready
- The data flow with each source marked trusted or untrusted.
- The three-property count, and the residual risk statement or the per-action approval it requires.
- The test set used, what was planted and where, the results and what was not tried.
- Each finding with its rating, owner, due date and the retest after the fix.
- The logs of tool calls and outputs, and the settings copy kept with the review file.
Assumptions
- You build or configure the LLM application, so output checks, tool permissions and caps are yours to set. Where a supplier controls them, the vendor questionnaire and the configuration checklist are the route.
- The feature is on your AI inventory with a named owner and has passed the P24 intake.
- The depths, days and limits shown are the pack's defaults, and your own standard may set others.
- OWASP and MITRE ATLAS are community sources, cited as they state them. They are not law.
Not covered here
- The full security assessment of a feature you build. That is “How do we assess the security of a GenAI feature we are building ourselves?”.
- How the release decision is run and signed. That is “How do we review an LLM feature or an AI agent for security before it goes live?”.
- How to run an attacker-style test in detail. That is “How do we test an LLM application the way an attacker would?”.
- What to do when an injection has already worked. That is “If an LLM application leaks data or is manipulated into a harmful action, then what do we do?”.
Related questions
- How do we assess the security of a GenAI feature we are building ourselves?
- How do we review an LLM feature or an AI agent for security before it goes live?
- What should an AI agent be allowed to do on its own, and what needs a person's approval?
- How do we test an LLM application the way an attacker would?
- If an LLM application leaks data or is manipulated into a harmful action, then what do we do?