When an AI Agent Writes SQL, Shell Commands or HTML: Improper Output Handling and the Injection It Creates
Treat every query, command and page an agent writes as untrusted input, because whoever shapes the agent's context can shape what it writes. OWASP LLM05:2025 names the results: SQL injection, remote code execution, XSS and path traversal. The fixes are classic ones applied to model output: fixed queries with bound parameters, least-privileged database roles, sandboxed execution and context-aware output encoding.
On this page
In June 2024, JFrog researcher Natan Nehorai published a remote code execution flaw in Vanna.AI, a Python library that turns questions into SQL. The flaw, CVE-2024-5565, was not a SQL injection. After running a query, the library asked a model to write Plotly code to chart the results, then passed that code to Python's exec.
The prompt for the chart included the user's question and the generated SQL. So Nehorai asked for a SQL query that simply selected a string, and the string contained instructions for the next model call: add a line of Python to the start of the chart code. The chart step obeyed, and the line ran.
Two model outputs, chained, and the second went straight into an interpreter. That is improper output handling.
What improper output handling means for an AI agent
OWASP defines improper output handling (LLM05:2025) as "insufficient validation, sanitization, and handling of the outputs generated by large language models before they are passed downstream". Because prompt input can steer that output, OWASP says the behaviour "is similar to providing users indirect access to additional functionality".
Its list of consequences is the classic web vulnerability list: XSS and CSRF in browsers, and SSRF, privilege escalation or remote code execution on backend systems. Its first mitigation is the principle everything else follows from: "Treat the model as any other user, adopting a zero-trust approach".
For an agent, the person steering the output may not be the user. Instructions hidden in a retrieved document or a tool result can shape the query the agent writes, which is why indirect prompt injection and improper output handling often arrive together. Our reading of the OWASP Top 10 for agents puts LLM05 among the four entries that matter most once a model can act. This post is about the controls.
Prompt injection to SQL injection: why text-to-SQL is the hard case
Pedro and colleagues' study of prompt-to-SQL injection, an ICSE 2025 paper, examined applications built on LangChain and tested seven language models. They found such applications "highly susceptible" to attacks in which a prompt produces malicious SQL.
The usual defence does not transfer cleanly. OWASP's SQL injection prevention cheat sheet says to use "parameterized queries for data values, fixed application-owned choices for dynamic query structure, and least privilege to limit damage." Parameters protect the data values in a query. When the model writes the whole query, it also chooses the structure, and binding cannot protect that.
So there are two designs, and the first is better wherever it fits.
Let the agent fill parameters, not write queries. Give it a tool such as get_orders(from_date, status). The application owns the SQL, binds the model's values as parameters, and validates them first. Values that decide access, such as the customer ID, come from the session rather than from the model. As the cheat sheet notes, parameter binding stops a value changing the syntax, but "does not decide whether the caller may access the matching account".
When free-form SQL is the product, contain it. Analytics agents genuinely need to write queries. Then:
- Connect with a role that can only read, and only the views it needs. The cheat sheet's advice is unambiguous: do not give application accounts DBA or admin access, and "rarely, if ever, grant create or delete access".
- Use a separate database user for each agent or application, so one agent's rights are not another's.
- Parse the generated SQL and reject anything that is not a single read statement.
- Set row limits and statement timeouts, and point the agent at a replica where you can.
The cheat sheet is careful about what least privilege buys: it limits the damage of an injection, it "does not prevent SQL injection". OWASP's own LLM05 scenario is a user asking a chat feature for a query that deletes every table. A read-only role turns that into an error.
How to make an AI agent's shell commands safe
OWASP's OS command injection defence cheat sheet has a primary defence that suits agents well: avoid calling OS commands directly, and use library functions that "cannot be manipulated to perform tasks other than those it is intended to do".
For an agent, that means replacing a general shell tool with narrow tools. The AI agent security cheat sheet uses exactly this as its bad example: a tool described as "Execute any shell command", with every command allowed. Where a command is unavoidable, the command cheat sheet asks for an allowlist of commands and validation of every argument. Escaping alone is not enough. Its example: an escaped value can still be --help, an extra argument the command will act on.
Some agents must run code: data analysis, coding, the chart generation in the Vanna case. The same agent security cheat sheet lists, among its don'ts, allowing agents "to execute arbitrary code without sandboxing". A sandbox for model-written code should have:
- no credentials and no access to the host's environment variables;
- no network access by default, with an allowlist where the task needs it;
- a throwaway filesystem and limits on CPU, memory and run time;
- nothing in it you would mind losing.
For coding agents, which execute far more than a chart script, the same logic extends to keeping production out of the agent's reach entirely.
Encoding agent output for HTML, Markdown and email
OWASP's XSS prevention cheat sheet makes the point that matters most for agent output: browsers parse HTML, JavaScript, URLs and CSS differently, and "using the wrong encoding method may introduce weaknesses". Encode for the context the text lands in.
Modern frameworks escape by default, and the gaps are the escape hatches: React's dangerouslySetInnerHTML without sanitising is one of the cheat sheet's examples. If an agent's output must render as rich HTML, sanitise it; OWASP recommends DOMPurify. LLM05 adds a strict Content Security Policy and escaping in email templates. Markdown images deserve their own mention, because a rendered image URL is a data exfiltration channel, not just a display problem.
| Where the agent's output goes | What goes wrong | Control |
|---|---|---|
| Database | SQL injection, destructive queries | Fixed queries with bound parameters; read-only role on views |
| Shell or interpreter | Remote code execution, argument injection | No general shell; narrow tools; sandbox for code |
| Browser | XSS | Context-aware encoding; sanitiser; CSP |
| File system | Path traversal | Resolve paths and confine them to an allowed directory |
| Email and templates | Script injection, phishing | Escape, or send plain text |
| URLs a tool fetches | SSRF | An allowlist of destinations |
An improper output handling checklist
- Do you know every place an agent's output is executed, queried, rendered or fetched?
- Could any of those tools be replaced by one with fixed queries or fixed commands?
- Does every database role an agent uses have the least privilege its task needs, and no admin rights?
- Do access-deciding values come from the session rather than from the model?
- Does model-written code run in a sandbox with no credentials and no default network access?
- Is output encoded for the context it lands in, every time?
Where Syntrox fits
Nucleus, our judge, evaluates what an agent produced and treats any text inside it as evidence, never as instructions; an instruction addressed to the evaluator counts against the output. It returns pass, fail or uncertain, with a short reason that quotes the fragment that drove the verdict, a confidence and a severity score. Syntrox records each verdict outside the agent.