F6 · Floor 6 · Customer Care
Improper Output Handling: The Helpful Renderer
A customer opened a ticket. The support agent who read it lost their account. In this lesson you investigate the incident, reproduce the attack yourself, then fix it.
Improper Output Handling in brief
Improper output handling is passing an LLM's output into another system (a browser, a database, a shell, an API) without validating or encoding it. Anyone who can influence the model's output, directly or by getting it to quote them, can then attack that system.
What you'll learn
- Model output is untrusted input. Treat it exactly like text a stranger typed.
- Encode for the destination: HTML-escape for pages, parameterised queries for SQL, argument lists (never string concatenation) for shells.
- Add a Content Security Policy so injected markup can't run scripts or send data out.
- Never eval, exec or run model-written code outside a locked-down sandbox.
- Prompts like "never output HTML" are wishes, not controls.
Why it matters
Security researchers have repeatedly shown chatbots and AI coding tools rendering attacker-supplied markdown and HTML, leaking chat history through image URLs and running script in support dashboards. The fix was always the same: encode or sanitise model output before it reaches the sink.
Every sink needs its own encoding
Output handling bugs are classic injection bugs (XSS, SQL injection, command injection, SSRF) with an LLM in the middle. The model is a new way to get attacker text into a trusted place, but the defences are the old ones.
Map every place model output goes. For HTML, render as text or sanitise with an allowlist library (and add a strict Content Security Policy). For markdown, disable raw HTML and auto-loading remote images. For SQL, use parameters. For shells, call fixed functions with argument arrays. For URLs the model generates, allowlist domains before fetching.
When an agent writes code, run it in a sandbox with no network, no secrets and a time limit, and never with the user's credentials.