LLM02: Insecure Output Handling (XSS, Command Execution, SQL)
Description
Insecure output handling arises when model outputs—HTML, Markdown, commands, SQL, code—are executed or rendered without validation. This enables cross-site scripting (XSS), command execution, data corruption, or privilege escalation in downstream systems. LLMs can produce plausible but unsafe content; treat outputs as untrusted input.
SEO keywords: LLM insecure output handling, LLM XSS, auto-execute commands, unsafe SQL generation, output validation.
Examples/Proof
-
HTML/Markdown rendering (Stored/Reflected XSS)
- Output contains
<img src=x onerror=alert(1)>or<svg onload=alert(1)>. If the chat UI renders unsanitized HTML, an attacker can execute scripts. See Web XSS:src/web-owasp-top-10/injection/stored-xss.md.
- Output contains
-
Auto-run code/commands (CI/agents)
- The agent suggests
rm -rf /tmp/*and the pipeline runs it automatically. Replace withecho POC > /tmp/poc.txtto safely test; if it executes, auto-run is enabled.
- The agent suggests
-
Dangerous SQL generation
- Model proposes
DROP TABLEor wideUPDATEwithout WHERE clause. If an executor tool runs it, confirm with a read-only environment; the behavior indicates missing guards.
- Model proposes
Detection and Monitoring
- HTML sanitization logs and CSP violations
- Enable CSP (no inline scripts); monitor violations; block dangerous tags/attributes at the renderer.
- Command/SQL allow-list mismatches
- Log rejected commands/queries; alert on attempts outside policy; require explicit approvals.
- Shadow execution
- Dry-run code and SQL in a sandbox; compare planned vs executed actions and flag destructive operations.
Remediation (Layered)
- Never auto-execute
- Insert human approval or policy gates; require diff/preview for any file/DB changes.
- Sanitize and sandbox aggressively
- HTML-escape user-visible content; render Markdown in a safe mode; execute code/SQL in isolated, least-privileged sandboxes.
- Validate with schemas and policies
- Enforce JSON Schema/Pydantic for structured outputs; parse commands/SQL and check against allow-lists and resource constraints.
- Use read-only by default
- Default tools to read-only; require opt-in elevation with justification and auditing.
Prevention Checklist
- HTML/Markdown sanitized; CSP enabled; iframes/safe renderers used
- Commands and SQL parsed and validated against allow-lists
- Structured outputs validated with JSON Schema/Pydantic
- Shadow/dry-run execution path for code and DB changes