Improper output handling is a vulnerability in which an application passes output from a large language model to downstream components without adequate validation or encoding, allowing that output to trigger cross-site scripting, injection, code execution or data exfiltration. In AI security, it is listed as LLM05 in the OWASP Top 10 for LLM Applications 2025; the 2023 edition called it insecure output handling.
The core problem is misplaced trust. Developers often treat model output as if it came from their own code, but an attacker can steer that output through prompt injection, poisoned documents or manipulated tool results. Output handling therefore decides whether a successful injection stays a harmless text anomaly or becomes a full compromise of the surrounding system.
How improper output handling creates an exploit chain
The flaw sits at the boundary between the model and the next interpreter. A language model produces a string; a browser, SQL engine, shell, template engine or API client then interprets that string. If the application does not constrain or encode it, the model effectively becomes a proxy that lets attackers reach those interpreters indirectly.
A typical chain has three steps. First, attacker-controlled text reaches the context window, directly or through indirect prompt injection in a web page, email or retrieved file. Second, the model emits a payload such as a script tag, a SQL fragment or a shell command. Third, the application executes or renders that payload with its own privileges.
The distinction from prompt injection matters. Prompt injection is the cause that manipulates the model; improper output handling is the downstream weakness that turns manipulated text into impact. Fixing either one breaks the chain, but only output handling can be enforced deterministically.
Typical sinks and resulting vulnerabilities
The impact depends entirely on where the output goes. The same model response can be harmless in a plain text log and critical inside an HTML page or a shell call.
The table maps common downstream sinks to the classic vulnerability each one produces.
| Sink | Example of unsafe use | Resulting vulnerability |
|---|---|---|
| Web page or chat UI | Rendering model HTML or Markdown unescaped | Cross-site scripting |
| Markdown image rendering | Loading image URLs the model composes | Silent data exfiltration through query strings |
| SQL database | Concatenating generated text into queries | SQL injection |
| Shell or code interpreter | Passing generated commands to exec or eval | Remote code execution |
| File system | Using generated paths for reads or writes | Path traversal |
| HTTP client or tool call | Fetching URLs chosen by the model | Server-side request forgery |
| Email or message templates | Inserting generated links into outbound mail | Phishing and header injection |
Markdown image exfiltration deserves special attention because it needs no user click. If a chat interface renders an image whose URL contains conversation data, the browser sends that data to the attacker’s server automatically when it loads the image.
Conditions that increase the risk
Some design choices make improper output handling far more likely or far more severe. The risk grows with the privileges of the consuming component and with the amount of untrusted content the model reads.
The following table lists the main risk factors and why each one matters.
| Risk factor | Why it increases risk | Typical setting |
|---|---|---|
| Agent with tool access | Output directly triggers actions | Coding agents and workflow automation |
| Retrieval of external content | Attackers can plant instructions in sources | RAG assistants and browsing agents |
| Rich rendering | HTML, Markdown and images become active content | Chat interfaces and email clients |
| Excessive privileges | A compromised call reaches sensitive systems | Service accounts with broad rights |
| No human review | Payloads execute before anyone sees them | Batch pipelines and autonomous agents |
These factors combine. A system that reads untrusted content, holds private data and can communicate externally matches the pattern often called the lethal trifecta, where careless output handling makes exfiltration close to inevitable.
Defenses against improper output handling
The governing principle is zero trust toward model output: treat every response as if a hostile user typed it. Established web security controls then apply directly, which is why the OWASP guidance points to context-aware output encoding and the Application Security Verification Standard rather than to AI-specific tricks.
The table matches each sink to the control that addresses it.
| Sink | Primary control | Supporting control |
|---|---|---|
| HTML and Markdown | Context-aware encoding and sanitization | Strict content security policy |
| Images and links | Allowlist of permitted domains | Proxy or strip external URLs |
| Databases | Parameterized queries only | Least-privilege database roles |
| Code and commands | Sandboxed execution without network access | Human approval for destructive actions |
| Tool and API calls | Strict schema validation of arguments | Allowlists of operations and hosts |
Structured output helps but does not replace validation. A JSON schema guarantees shape, not safety: a perfectly valid field can still contain a malicious URL or SQL fragment. Each field must be validated against its intended meaning, such as an enumerated value, a numeric range or a known identifier.
Common mistakes in practice
Teams frequently rely on filters inside the prompt instead of controls in code. Instructions such as “never output HTML” reduce accidental problems but cannot stop a determined attacker, because the same model that follows the instruction can be talked out of it by injected text.
- Sanitizing input only: cleaning the user prompt does nothing against injected content from documents or tools.
- Using an LLM as the only filter: a second model can reduce risk, but it is probabilistic and can be fooled.
- Trusting internal tools: output from one agent often flows into another without any checks.
- Logging raw output into dashboards: administrative interfaces can execute stored script payloads later.
Understanding it simply
An AI model writes text. Other programs then read that text and act on it. A web page may display it. A database may run it as a command. Improper output handling means those programs trust the text blindly.
That is dangerous because attackers can trick the model into writing harmful text. It is like a clerk who copies every note onto official forms without reading it. The safe approach is simple. Check and clean everything the model writes before another system uses it.
Applied in practice
A software company adds an AI assistant to its customer support portal. The assistant summarizes incoming tickets, and agents view the summaries in an internal dashboard that renders Markdown. During a security review, a tester submits a ticket containing hidden instructions asking the model to include an image whose URL carries the previous ticket’s customer email address.
The model complies, and the dashboard renders the image, so the agent’s browser sends the email address to an external server. No one clicks anything. The review also finds that the summary field is stored without encoding, which allows a stored cross-site scripting payload in the same dashboard.
The team fixes the issue in code rather than in the prompt. They encode all model output as plain text, permit images only from the company’s own domain, add a strict content security policy and validate every structured field. A repeat test with 50 injection payloads produces no exfiltration and no script execution.