LLM output going into innerHTML or SQL? Treat it as user input

The OWASP list of LLM app risks says the model's output can be steered by the prompt, so treat it like any user's input: encoding by context, parameterized queries, and a CSP.

Published 3 min read#AI#App security

You built a chatbot that summarizes customer reviews, and the answer is shown on the page with innerHTML so the formatting looks nice. Another time you let the model write an SQL query from the user's question and ran it directly. In both cases you treated the model's answer as trusted text. In this article you will see why that answer has to be treated like a stranger's input, and what you change in the code.

What is improper output handling?

Improper output handling is sending the LLM's answer to other parts of your system without enough validation, sanitization and encoding. It is entry LLM05:2025 on the OWASP list of risks for LLM applications.

The OWASP LLM05:2025 entry explains why: the model's output can be steered by the prompt, so it is like giving the user indirect access to your system's functions.

Why is the model's answer not trusted text?

The model's answer is not trusted because anyone who writes in the prompt, or in a page or file the model reads, can affect what comes out.

So when you put the answer into HTML, it is as if you put the user's words into HTML. And when you run it as a query, it is as if you ran a query the user wrote. The same old mistakes, through a new door.

Where exactly does the hole appear?

The hole appears where the answer lands: the browser, the database, the shell, the file system, or email.

The entry lists five examples:

Where the answer went

What comes out

shell, exec or eval

remote code execution (RCE)

JavaScript or Markdown returned to the user

XSS in the browser

SQL query without parameters

SQL injection

file path without sanitizing

path traversal

email template without escaping

phishing attacks

How do you protect the code that receives the answer?

You protect the code by treating the model like any other user, with a zero-trust approach, and putting the protection where the answer is going.

From the entry's steps:

  1. Validate the model's answer before it reaches backend functions, like any input from a user.

  2. Encode the answer by context: HTML encoding for web content, and SQL escaping for queries.

  3. Use parameterized queries or prepared statements for every database operation that involves the model's answer.

  4. Use a strict Content Security Policy to reduce XSS from model content.

  5. Log and monitor the model's answers so you notice anything unusual.

This is the difference in code:

// wrong: the answer runs as HTML
summaryEl.innerHTML = llmAnswer;

// right: the answer is text
summaryEl.textContent = llmAnswer;

// wrong: a query the model wrote runs as is
await db.query(llmSql);

// right: the model picks a value, and the query is fixed with parameters
await db.query('SELECT * FROM reviews WHERE product_id = $1', [productIdFromLlm]);

Where do you start now?

The lesson for you as a developer: the LLM's answer is outside input, not a result of your code, so every place it lands needs the same protection you give to user text.

  1. Search your code for every place the model's answer lands: innerHTML, queries, commands, file paths.

  2. For each place, ask: if a stranger wrote this text, would you put it here as is?

  3. If the answer is no, add the encoding or the parameters before it gets there.

The same protections have their own articles: innerHTML and textContent, parameterized queries, and for the other side of the model, the system prompt leakage article. And if you want someone to test your AI-built app, there is Muhassan.

Where does the model's answer go in your app right now?

FAQ

The model is from a big company and trusted. Why treat it like a stranger?

Because the answer is not only from the model: part of it comes from the prompt, and from pages and files it read, and anyone who writes in them can steer the answer.

Is it enough to ask in the prompt for no HTML?

No. A request in the prompt can be broken. The protection lives in the code that receives the answer: encoding before display, and parameters in queries.

I show the chatbot's answer as Markdown. What do I do?

Convert the Markdown to HTML with a library and sanitize it after, and do not allow raw HTML inside it. Add a strict CSP so that even if something slips through, it does not run.

Share

More articles