Improper output handling

Also known as: insecure output handling

On this page 7

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.

SinkExample of unsafe useResulting vulnerability
Web page or chat UIRendering model HTML or Markdown unescapedCross-site scripting
Markdown image renderingLoading image URLs the model composesSilent data exfiltration through query strings
SQL databaseConcatenating generated text into queriesSQL injection
Shell or code interpreterPassing generated commands to exec or evalRemote code execution
File systemUsing generated paths for reads or writesPath traversal
HTTP client or tool callFetching URLs chosen by the modelServer-side request forgery
Email or message templatesInserting generated links into outbound mailPhishing and header injection
Downstream sinks and resulting vulnerabilities

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 factorWhy it increases riskTypical setting
Agent with tool accessOutput directly triggers actionsCoding agents and workflow automation
Retrieval of external contentAttackers can plant instructions in sourcesRAG assistants and browsing agents
Rich renderingHTML, Markdown and images become active contentChat interfaces and email clients
Excessive privilegesA compromised call reaches sensitive systemsService accounts with broad rights
No human reviewPayloads execute before anyone sees themBatch pipelines and autonomous agents
Risk factors and their effect

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.

SinkPrimary controlSupporting control
HTML and MarkdownContext-aware encoding and sanitizationStrict content security policy
Images and linksAllowlist of permitted domainsProxy or strip external URLs
DatabasesParameterized queries onlyLeast-privilege database roles
Code and commandsSandboxed execution without network accessHuman approval for destructive actions
Tool and API callsStrict schema validation of argumentsAllowlists of operations and hosts
Controls by sink

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.