SYNTROX.ai
Blog

When an AI Agent Writes SQL, Shell Commands or HTML: Improper Output Handling and the Injection It Creates

5 min read

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 goesWhat goes wrongControl
DatabaseSQL injection, destructive queriesFixed queries with bound parameters; read-only role on views
Shell or interpreterRemote code execution, argument injectionNo general shell; narrow tools; sandbox for code
BrowserXSSContext-aware encoding; sanitiser; CSP
File systemPath traversalResolve paths and confine them to an allowed directory
Email and templatesScript injection, phishingEscape, or send plain text
URLs a tool fetchesSSRFAn allowlist of destinations

An improper output handling checklist

  1. Do you know every place an agent's output is executed, queried, rendered or fetched?
  2. Could any of those tools be replaced by one with fixed queries or fixed commands?
  3. Does every database role an agent uses have the least privilege its task needs, and no admin rights?
  4. Do access-deciding values come from the session rather than from the model?
  5. Does model-written code run in a sandbox with no credentials and no default network access?
  6. 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.

5 min read

The OWASP Top 10 for LLM Applications, Read for AI Agents: Which Risks Grow When the Model Can Act

Once a model can call tools, four OWASP entries matter most: LLM01 Prompt Injection, LLM06 Excessive Agency, LLM05 Improper Output Handling and LLM02 Sensitive Information Disclosure, because text the agent reads can now become an action. The other six change too, from supply chain to unbounded consumption. Each has an agent-specific control, and almost all of them sit outside the model.

Read

5 min read

AI Coding Agents With Production Access: Why the Agent Should Never Hold the Keys to Prod

Keep production out of a coding agent's reach instead of asking it to stay away. Five guardrails: separate environments so production is unreachable from the agent's workspace; sandbox what it executes; give it no standing production credentials; block destructive commands at the target, not only in the prompt; and make recovery a tested path. A code freeze written in a prompt is not a control.

Read

5 min read

Data Exfiltration Through AI Agents: Five Ways Data Gets Out, and How to Close Them

An agent leaks data through the same channels it uses to work: tool-call arguments, URLs and rendered images, email and messages, files and pull requests it writes, and third-party MCP servers. Close those paths outside the model: allowlist where the agent can send data, inspect tool-call arguments before they leave, strip untrusted links and images from output, and scope every credential to the task.

Read