Join our Newsletter — 33% off our NHI Course
Home FAQ Cyber Security What breaks when LLM outputs are used directly…
Cyber Security

What breaks when LLM outputs are used directly in SQL queries or shell commands?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated September 7, 2026 Domain: Cyber Security

Directly reusing model output in executable contexts can convert a generation error into a security incident. A malicious or malformed response may trigger injection, destructive commands, or unintended data access. Safe handling requires validation, escaping, allowlisting, and separate execution controls before any output reaches a database, terminal, or automation step.

Why direct LLM output becomes dangerous in executable contexts

When an LLM’s output is treated as if it were trusted input to SQL or a shell, the model stops being just a language generator and starts influencing execution. That is where prompt mistakes, malicious user input, and parsing ambiguity can become injection, data exposure, or destructive command execution. The core issue is not the model alone, but the decision to cross the boundary from text generation into execution without a control layer. OWASP’s OWASP Agentic AI Top 10 is a useful reference because it treats tool use and action execution as security-relevant, not just model quality.

SQL and shell are high-impact sinks because both are expressive enough to turn untrusted text into instructions. In SQL, that can mean query alteration, unauthorized reads, writes, or schema changes. In shell, it can mean command chaining, argument injection, file tampering, or process control. The practical mistake is assuming the model will only produce “reasonable” text, when the real control question is whether the surrounding system will execute whatever text appears. In practice, many security teams encounter the weakness only after a harmless-looking output has already been routed into a live database or terminal.

How unsafe execution happens in practice

The failure usually begins when developers concatenate model output into a command string or query string, then pass it to an interpreter that treats the whole string as executable. If the output contains quotes, operators, separators, metacharacters, or SQL syntax, the receiving system may interpret those characters as instructions rather than data. This is why validation and escaping help only when they are applied in the right place and matched to the execution context; generic “cleaning” is not enough.

For SQL, the safer pattern is parameterization, because it separates code from data. For shell commands, the safer pattern is to avoid shell interpretation altogether and invoke commands with structured arguments, allowlisted verbs, and explicit execution boundaries. Where an LLM selects from a set of actions, the model should return a constrained identifier or structured object, not free-form executable text. NIST’s NIST AI 600-1 Generative AI Profile and the broader NIST AI Risk Management Framework both reinforce the need to govern how generative outputs are used, not only how they are produced.

  • Use the model for classification, drafting, or selection, then map its result to preapproved actions.
  • Treat every output destined for SQL or shell as untrusted until it passes context-specific validation.
  • Keep execution privileges separate from generation privileges so a bad output cannot escalate by default.
  • Log the raw output, the normalized payload, and the final executed action for review and rollback.

This guidance breaks down when teams rely on post hoc sanitisation after a free-form string has already been assembled for execution.

Where the boundary gets blurry

Tighter execution control often increases integration overhead, requiring teams to balance automation speed against the need to keep the model away from direct command construction. That tradeoff becomes most visible in assistants that both reason and act, because the same output may be used for display, routing, and execution.

The standard answer is clear for obvious SQL injection or shell injection, but edge cases are more nuanced. A model output may be safe as a literal string in one context and unsafe in another, especially if it is later re-parsed by a script, ORM, template engine, or orchestration layer. Likewise, allowlisting works well only when the action space is small and stable; it becomes brittle when teams try to let the model improvise complex commands. Guidance versus consensus: there is broad agreement that free-form execution is unsafe, but teams still debate how much structured autonomy is acceptable in agentic workflows.

Another common edge case is indirect execution. A model output may not be sent to SQL or a shell immediately, but it may be stored, transformed, and eventually consumed by something executable. That delayed path often defeats simple front-end checks. The most reliable control point is the last trusted boundary before execution, not the first point where the text was generated. If the architecture cannot identify that boundary clearly, the control model is already too loose.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Agentic AI Top 10 and MITRE ATT&CK address the attack and risk surface, while CIS Controls v8 and NIST AI RMF set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
OWASP Agentic AI Top 10A2 — Action and Tool ExecutionDirectly addresses unsafe model-to-tool execution boundaries.
Recommendation — Constrain model outputs to approved actions before any tool or command execution.
CIS Controls v86 — Access Control ManagementLimits who and what can execute privileged database or shell actions.
16 — Application Software SecurityCovers safe handling of untrusted input before it reaches interpreters.
Recommendation — Restrict execution rights so untrusted outputs cannot trigger privileged operations. Implement parameterization and validation before input reaches SQL or shell parsing.
MITRE ATT&CKT1059 — Command and Scripting InterpreterShell command abuse is the primary exploit class in this question.
T1190 — Exploit Public-Facing ApplicationDirect execution of injected output can expose application-facing entry points.
Recommendation — Monitor for command construction paths that expose scripting interpreters to untrusted text. Hunt for injection paths where external input alters application behavior or data access.
NIST AI RMFMAP — Measure, Analyze, and ManageApplies to governing generative outputs before they are operationalised.
Recommendation — Assess and govern model outputs before they are allowed to influence execution.

Practitioner Guidance

What to prioritise: Separate generation from execution first. If the system cannot prove that model output is never interpreted as code, treat that as a design flaw rather than a tuning problem.

What to verify: Verify that SQL uses parameterised queries and that shell actions are invoked with structured arguments or fixed allowlisted commands. Also verify that no downstream helper reintroduces string concatenation after the first safeguard.

Common mistake: Teams often validate the natural-language response and then assume the same validation protects the executable sink. It does not, because the risk sits in the interpreter boundary, not in the model output alone.

What good looks like: The model returns a constrained decision, the application maps that decision to a known action, and the execution layer rejects anything outside the approved shape. That is the point where output becomes controllable rather than merely plausible.

Practitioner takeaway: The safest design is to make the LLM choose among preapproved actions, not to let it author executable text that the system must then try to trust.

Deepen Your Knowledge

Sign up to our weekly newsletter — get 33% off our NHI Foundation Level Course

    NHIMG Editorial Note
    Reviewed and updated by the NHIMG editorial team on September 7, 2026.
    NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org