Why model output is untrusted
The model's answer is shaped by everything in its context: the user's message, retrieved documents, web pages, tool results. Any of those can carry an injection that makes the model emit exactly the payload an attacker wants. So treat model output like user input, at every place it goes: the page, the database, the shell.
Sink 1: HTML (cross-site scripting)
Converting markdown to HTML and assigning it with innerHTML runs whatever HTML the model produced. <img src=x onerror=…> executes script in your origin, with your user's session.
import { marked } from 'marked';
import DOMPurify from 'dompurify';
// unsafe
el.innerHTML = marked.parse(modelText);
// safe: sanitise the HTML after markdown conversion
el.innerHTML = DOMPurify.sanitize(marked.parse(modelText));
// safest when you don't need formatting
el.textContent = modelText;In React, react-markdown does not render raw HTML unless you add a plugin such as rehype-raw; leave it out. Don't pass model text to dangerouslySetInnerHTML without DOMPurify.
Also watch markdown images and links:  leaks data the moment it renders. A Content-Security-Policy with a strict img-src (and connect-src) stops the browser from loading attacker URLs.
Sink 2: SQL
Two different cases. When the model picks values (a search term, a date), pass them as parameters, never by concatenation:
// unsafe
await sql.unsafe(`select * from products where name ilike '%${modelTerm}%'`);
// safe (postgres.js tagged template = parameters)
await sql`select * from products where name ilike ${'%' + modelTerm + '%'} limit 20`;When the model writes whole queries (text-to-SQL), parameters can't help. Contain it instead: a read-only transaction, a role that can only select the tables you allow, RLS for per-customer data, a statement timeout and a row limit. Never run generated SQL as postgres or with a service key.
Sink 3: shell commands
A template string passed to a shell turns $(…), ; and && into commands. Use an argument array without a shell, an allowlist of commands, and reject arguments that look like options:
import { execFile } from 'node:child_process';
const ALLOWED = new Set(['status', 'log', 'diff']);
export function git(sub: string, args: string[] = []): Promise<string> {
if (!ALLOWED.has(sub)) throw new Error('command not allowed');
if (args.some((a) => a.startsWith('-'))) throw new Error('options not allowed');
return new Promise((resolve, reject) =>
execFile('git', ['--no-pager', sub, '--', ...args], { timeout: 5000 }, (err, out) =>
err ? reject(err) : resolve(out)),
);
}Rejecting leading dashes matters: many tools have options that write files or run programs (for example git diff --output=…). In Python the equivalent is subprocess.run([...], shell=False). Run agents that need a real shell inside a container or VM with no secrets mounted.
Other sinks
The same rule covers model output used as a URL to fetch (server-side request forgery), a file path (path traversal), an email header, a redirect target, or code passed to eval. Validate against an allowlist at the point where the output is used.
How to verify
Add tests that feed these strings in as the model's output and assert nothing happens: <img src=x onerror=alert(1)>, [x](javascript:alert(1)), '; drop table users; --, $(id) and --output=/tmp/x. In the browser, the image tag must render as harmless text or be stripped, and no alert may appear.
Related guides
- Prompt injection through retrieved documents (RAG) – what it is and how to contain it
- LLM tool calls with over-broad permissions – a least-privilege pattern that holds
- AI API keys and system prompts in frontend code – find them and move the call server-side
- MCP server security basics – untrusted tool descriptions, local code and token scope
More in AI agent and LLM app security · all guides