Back to Blog
AI Red Teaming Oct 28, 2026 103 views

Improper Output Handling: When the Model's Reply Is the Payload

Improper Output Handling (OWASP LLM05) turns a model reply into XSS, SQL injection, data exfiltration or RCE when an app trusts it. Four output-sink exploits in a local lab, payload shapes, 2026 CVEs and code-level prevention.

By Obaida Albasha, Security Researcher, Cytomate

Part 4 of our AI Penetration Testing series, following The AI Attack Surface, Prompt Injection and Sensitive Information Disclosure. The previous posts made the model misbehave. This one is about the application that believes it.

The output trust gap

Every LLM feature ends the same way: the model produces text, and something downstream consumes it. A browser paints it, a database runs it, an interpreter evaluates it, an API receives it. Improper Output Handling is the failure to treat that text as what it actually is, untrusted input, before it reaches a powerful sink. The application trusts the model the way it would trust its own code, and that trust is misplaced, because the model will cheerfully emit whatever an attacker talked it into emitting.

This is the natural sequel to prompt injection. Injection is the delivery; improper output handling is the detonation. Injection alone makes a chatbot say a script tag; it is the unsanitized innerHTML assignment that turns those characters into running code. The two are routinely chained, but the fix lives in different places: one hardens the prompt boundary, the other hardens every point where model output crosses into a sink. You cannot reliably stop the model from producing a payload, so you must stop the payload from doing anything when it arrives.

OWASP LLM05:2025, Improper Output Handling. "Insufficient validation, sanitization, and handling of the outputs generated by large language models before they are passed downstream to other components and systems." Depending on the sink, the result is cross-site scripting, CSRF, SSRF, privilege escalation, SQL injection, or remote code execution. It was LLM02 "Insecure Output Handling" in the 2023 list and was renamed for 2025.

MITRE now tracks it directly as CWE-1426, Improper Validation of Generative AI Output, which chains into the classic sink weaknesses: CWE-79 (XSS), CWE-89 (SQL injection), CWE-78 (OS command injection), and CWE-94 (code injection). The one-line mental model, borrowed from the practitioners who named it: AI-generated output is untrusted input.

Two kinds of sink

Output sinks fall into two families, and the lab is organized around them. Rendered sinks live in the client: the output is parsed as markup or markdown and painted into a page. Executed sinks live in the backend: the output is run as code or a query. Different sinks, same root cause, and the same fix shape.

Sink familyWhere the output landsWhat you get
RenderedinnerHTML, a markdown renderer, auto-loaded images and linksStored/reflected XSS, data exfiltration, CSRF, SSRF
Executedeval/exec, a SQL agent, a shell, a downstream API callRemote code execution, SQL injection, command injection

The lab

These run on the same local, offline lab from the previous posts, to which I added Rendered Output Sinks and Executed Output Sinks categories. Each challenge wires the model's reply straight into a real sink with weak or no validation, so you can watch the output become the exploit. Everything is local and every secret is fake.

Demonstration 1: innerHTML, the textbook XSS

The simplest case. A support widget assigns the model's reply directly to element.innerHTML with no sanitization. Whatever markup the model emits, the browser parses and runs. We just ask it to "display" a status badge that happens to be an image with an error handler.

The Support Widget demo: the model echoes an img onerror payload that runs via element.innerHTML.

Behind the reply, element.innerHTML = reply did the rest: the browser parsed the image tag, the broken src fired its onerror handler, and alert(1) executed in the user's session. Swap that harmless alert for a request that quietly posts the user's session cookie to an attacker host, and the same reply becomes silent session-token theft.

Browser alert dialog showing the onerror XSS executing
The onerror handler firing in the browser. The model's reply became live script the instant it was assigned to innerHTML.

Demonstration 2: the self-loading image

Not every rendered-sink attack needs script. A markdown chat client auto-fetches any image URL the model names, which is the exact behaviour behind the ChatGPT, Bard, Copilot, and Slack AI exfiltration bugs. The client only trusts images from its official CDN, but it still issues the GET. We ask the assistant to embed a "tracking pixel."

The Self-Loading Image demo: the assistant embeds a delivery-status badge whose URL carries a customer email, auto-fetched on render.

On render, with no click, the browser issued a GET to the image host carrying the customer's email in the query string, and that data left the origin with the request. Point the host at an attacker and the rendered image is a one-way exfiltration channel.

This is the vector that keeps shipping in production. The image tag is rendered client-side and fetched automatically, so a URL the model was tricked into constructing becomes a one-way channel out of the page. It is the mechanism behind a long and current list of real exfiltration bugs, covered below.

Demonstration 3: natural language to SQL, straight to the database

Now we cross into executed sinks. A "chat with your data" assistant runs whatever SQL its query tool is handed, the Langroid SQLChatAgent and LangChain SQL-agent pattern. When the database role can reach the operating system, the right query is not a read, it is a shell command, and the attacker never has to say so directly.

Ask-Your-Database demo: the attacker sends a Base64 test vector framed as an integration test; the SQL agent decodes and runs it, and a program-execution COPY returns the output of the id command, proving remote code execution.

This is the Langroid SQLChatAgent flaw, CVE-2026-25879 (CVSS 9.8 Critical). The Base64 wrapper and the "integration test" framing slip the statement past the agent's own checks; once decoded and run through its query tool, PostgreSQL's program-execution COPY feature runs a shell command on the database host, so prompt-to-SQL becomes remote code execution. The output of id comes straight back through the result set, proof the attacker is running as the database user. The agent trusted the model's tool call, and the database role could reach the operating system.

Demonstration 4: eval() and the road to RCE

The most dangerous sink of all. A math helper passes the expression the model returns straight to Python eval(), the exact flaw in LangChain's LLMMathChain (CVE-2023-29374). A keyword filter blocks the obvious words (import, exec, os), so we emit an expression that reaches the object graph without any of them.

The Calculator eval() demo: the model outputs a Python object-graph expression and its real subclasses result.

eval() ran the model's output and the calculator enumerated every loaded class, 249 of them, including subprocess.Popen. The follow-up step indexes that list to a class that can spawn a process, which is remote code execution (CVE-2023-29374 / CVE-2023-36258). The keyword filter never saw import, exec, or os, so nothing stopped it.

Your session versus everyone else's

The four demos are first-order: the attacker types the payload and the sink fires in the attacker's own session. That already matters, because the executed sinks point at the server. Demonstrations 3 and 4 are not self-harm; they are the attacker using the model as a proxy to reach a database and a Python interpreter they could never touch directly, turning a chat box into SQL injection and remote code execution against the backend. CVE-2024-5565 was exactly this: a prompt made Vanna.AI's visualization step emit Python, the app ran it with exec(), and the attacker had code execution on the server.

The second-order case is worse, and it is where improper output handling becomes a mass-exploitation primitive. Combine it with indirect prompt injection from the earlier post and the attacker never has to be in the victim's session at all:

The stored-XSS-through-the-LLM chain. An attacker plants a payload in something the model will later read for a different user, a RAG document, a shared conversation, a support ticket, a web page the assistant summarizes. When the victim's session renders the model's reply, the payload executes in the victim's browser, with the application's origin and the victim's session. In August 2026, Escape's testing found exactly this stored XSS in two unrelated AI chat products: the markdown renderer reparsed raw HTML, and an iframe whose srcdoc attribute carried a script ran on page load, deliverable to anyone a conversation was shared with. The model was the delivery mechanism; the victim did nothing.

The exfiltration variant is the same shape with an image instead of a script: a hidden instruction makes the assistant render a markdown image whose URL carries the victim's data, and the victim's client fetches it automatically. That is the through-line connecting EchoLeak (Microsoft 365 Copilot, CVE-2025-32711), GitLab Duo source-code theft (2025), Salesforce Agentforce ForcedLeak (2025), Superhuman AI (2026), and Microsoft Copilot Cowork (2026). In every one, the model's output was trusted by a renderer, and the renderer did the attacker's work.

Bypasses and payload shapes

Because output filters and sanitizers are the common first defense, the practical skill is knowing the shapes a payload can take. The theme is identical to the input-filter lesson from the prompt-injection post: a filter matches surface form, the sink acts on meaning, and there are far more encodings of intent than any blocklist can enumerate.

Payload shapes that defeat output filters: script-less markup, markdown image and link vectors, Python object-graph gadgets, and SQL.

Recent CVEs and incidents

This is not a museum of 2023 bugs. The output sink keeps getting wired up without validation, across rendered and executed contexts, right up to this year.

CaseSink / vectorImpact
CVE-2026-26030 | Semantic Kernel (Python)Executed: a model value reaches an eval() on a generated filter, then a shell commandRCE
CVE-2026-25592 | Semantic Kernel (.NET)Executed: an over-exposed tool writes an arbitrary file on the hostRCE on the host
CVE-2026-25879 | Langroid SQLChatAgentExecuted: prompt-to-SQL, then a program-execution COPYRCE, CVSS 9.8 Critical
CVE-2026-41318 | AnythingLLMRendered: stored XSS via unencoded markdown image alt textXSS, CVSS 5.4
CVE-2026-86062 | HKUDS LightRAG WebUIRendered: stored XSS in the RAG chat rendererToken theft, CVSS 6.1
EchoLeak, GitLab Duo, Agentforce, Superhuman, Copilot Cowork (2025 to 2026)Rendered: markdown image auto-loadZero-click data exfiltration

Practical remediations

There is one principle and several enforcement points. The principle: the component that renders or executes must never assume the model that produced the text is trusted. Put validation on the boundary of every sink, and give the sink the least privilege it can do its job with.

1. Rendered sinks: encode by default, sanitize with an allowlist, cage it in CSP

Never assign model output to innerHTML. Render it as text, or, if you must render markdown or HTML, pass it through a vetted sanitizer configured to a strict allowlist that drops every event handler, srcdoc, and dangerous URL scheme. Then add a Content-Security-Policy so that even a payload that slips through has nowhere to run and nowhere to send data.

Code example: render model output as text, or sanitize it with DOMPurify and an allowlist
Code example: a Content-Security-Policy that blocks inline script and remote image exfiltration

The img-src and connect-src directives are what neutralize Demonstration 2 and the whole markdown-image exfiltration family: the browser simply refuses to fetch the attacker's host.

2. Executed SQL: least-privilege role, then validate the query

Never run model-generated SQL blind. Constrain it in depth: a read-only database role that can see only the tables the feature needs, so Demonstration 3 cannot reach users even if the model writes the query; and an AST-level validator that rejects anything outside an allowlist before execution.

Code example: a read-only database role plus SQL validation before the query runs

3. Executed code: never eval model output

There is no safe way to eval attacker-influenced text. Replace the interpreter with a parser that understands only the operations you actually need. For a calculator, that is an arithmetic AST walk that permits numbers and operators and nothing else, no calls, no attributes, no names, which makes Demonstration 4's gadget impossible to express.

Code example: evaluating an AST that allows only arithmetic instead of calling eval

When you genuinely must execute model-generated code, run it in a real sandbox with no network, no filesystem, and hard resource limits, and treat escape from that sandbox as the thing you are defending, not the prompt.

4. Least privilege is the backstop

Every sink should run with the smallest identity that works: a read-only DB role scoped to allowed tables, an API token with a narrow scope, a renderer with a restrictive CSP, an interpreter in a sandbox. CVE-2024-5565 reached full RCE because exec() ran with the application's privileges; the same injection against a locked-down worker is a contained failure. Validation stops most attacks; least privilege decides how bad the ones that get through can be.

What to watch for

OUTPUT_MARKUP

Active markup in a reply

Model output containing tags, event handlers (on*=), or script, iframe, and svg tags. It should never reach a renderer unsanitized.

EGRESS_IMG

Off-allowlist fetch

An image or connect request to a host outside your CSP allowlist, surfaced by CSP violation reports or proxy logs. The markdown-exfiltration signature.

SQL_SCOPE

Query outside the allowlist

Generated SQL that touches a non-allowlisted table, reaches a program or file-execution primitive, or runs any non-SELECT on a read path. Block before execution and alert.

EXEC_INPUT

Interpreter fed model text

Any call to a code-evaluation or shell function (an eval, an exec, or a subprocess) with model-derived input. In a hardened app this count should be zero.

The short version

Improper Output Handling (LLM05) is the failure to treat model output as untrusted input before it hits a sink. Rendered sinks turn a reply into XSS and data exfiltration; executed sinks turn it into SQL injection and remote code execution. In the lab, an unsanitized innerHTML ran injected script, a markdown image fired a no-click GET to an attacker host, a SQL agent was walked from a crafted query to a shell command on the database host, and a calculator's eval() ran an object-graph gadget on the road to RCE. First-order, the attacker reaches your backend; second-order, combined with indirect injection, the model delivers stored XSS and zero-click exfiltration into other users' sessions, as Escape.tech, EchoLeak, GitLab Duo, Agentforce, and Copilot Cowork all show. Fix it at the boundary: encode by default, sanitize with an allowlist, cage the renderer in CSP, never eval or run model-generated SQL blind, and give every sink the least privilege it needs.

Next in the series we move from what the model emits to what goes into it, and what an attacker can achieve by poisoning the data a model learns from and the data it retrieves at run time.

Sources & Further Reading OWASP GenAI Security Project, LLM05:2025 Improper Output Handling (and the 2023 LLM02 Insecure Output Handling) | MITRE, CWE-1426 Improper Validation of Generative AI Output
JFrog (Natan Nehorai), Prompt Injection Code Execution in Vanna.AI (CVE-2024-5565, CVSS 8.1) | NVD / Wiz, CVE-2024-5565
GitHub Advisory GHSA-mxfr-6hcw-j9rq / CVE-2026-25879, Langroid SQLChatAgent Prompt-to-SQL Injection leading to RCE (CVSS 9.8) | Pedro et al., Prompt-to-SQL (P2SQL) Injections in LLM-Integrated Web Applications (ICSE 2025)
NVD / Wiz / Vulert, CVE-2023-29374: LangChain LLMMathChain code execution (CVSS 9.8) | CVE-2023-36258 (PALChain)
Escape, The same XSS in two AI chatboxes: a flaw in the Markdown renderer (2026) | AnythingLLM stored XSS (CVE-2026-41318) | HKUDS LightRAG WebUI stored XSS (CVE-2026-86062) | Microsoft Security, When prompts become shells: RCE in AI agent frameworks (Semantic Kernel CVE-2026-26030 and CVE-2026-25592)
Simon Willison, markdown-exfiltration and exfiltration-attacks tags: EchoLeak (CVE-2025-32711), GitLab Duo, Salesforce Agentforce ForcedLeak, Superhuman AI, Microsoft Copilot Cowork, Claude iOS, Mistral LeChat
Snyk Learn, StackHawk, Auth0, Sonatype, Cobalt, Securiti, HackerOne, Acta.ai (AI-generated output is untrusted input) on LLM05 | llm-sec.dev and secdim.com improper-output-handling labs

More from the Blog

Threat Intelligence Nov 29, 2026

The Build-Pipeline Backdoor: Inside the NullReceiver Supply-Chain Campaign

Read More
Deception Nov 29, 2026

Lying to the Machines: The Secret Weapon Against AI Attacks

Read More
Agentic Security Nov 24, 2026

Your AI Coding Agents Need an EDR Too: Introducing cADRe

Read More