Output, data and cost: what comes out and what it costs

Unit 3 of 5 in Securing LLM applications.

You will be able to handle model output safely, keep sensitive data out of prompts and logs, and limit the cost of abuse.

Lesson

Model output is input to your next system

OWASP defines improper output handling as insufficient validation, sanitization and handling of model outputs before they are passed downstream. Because model output can be influenced by what goes into the prompt, passing it on unchecked is like giving the user indirect access to extra functionality. The page lists possible results: cross-site scripting and cross-site request forgery in browsers, and server-side request forgery, privilege escalation or remote code execution on backend systems.

It also lists what raises the impact: granting the model more privilege than the user should have, being open to indirect prompt injection, third-party extensions that do not validate input, missing output encoding for the context, too little monitoring and logging of outputs, and no rate limiting or anomaly detection.

The sinks, and the fixes you already know

OWASP's examples are the old injection bugs with a new source. Model output put straight into a shell or an eval-like function can give remote code execution. JavaScript or Markdown from the model, returned to a browser, can run as cross-site scripting. Generated SQL run without parameters can be SQL injection. Output used to build file paths can allow path traversal, and output used in email templates without escaping can help phishing.

Its mitigations are the ones from the web class: treat the model like any other user and validate its output; encode output for where it will be used (HTML for pages, escaping for databases); use parameterized queries for any database operation that involves model output; use a strict Content Security Policy; and log and monitor outputs for unusual patterns. There is nothing special about the model here. The skill is remembering to apply ordinary controls to a new kind of input.

Rendered output can be a quiet way out

The prompt injection cheat sheet names HTML and Markdown injection: markup the model produces that is rendered in the response, such as links disguised as helpful content or hidden image references that send data to another server, plus risks in rendering Markdown while a reply is still streaming. The reader may see nothing unusual, because the data leaves when the page loads the image.

This is our reasoning, built on the two OWASP pages: the fixes are the web ones. Render model output as inert text unless you need rich formatting, encode it for its context, and use a Content Security Policy that restricts where a page may load images and other resources from, so a stray reference cannot reach an outside server. The lab for this unit lets you measure the channel and close it.

Sensitive data: keep it out, then limit who and what can reach it

OWASP's list of mitigations for sensitive information disclosure starts with sanitization: scrub or mask sensitive content before it is used in training, and validate inputs. It continues with strict access controls on the principle of least privilege, restricting the data sources a model can reach, privacy techniques such as federated learning and differential privacy, user education and transparency about data retention and opt-out, concealing the system's initial instructions, and tokenization and redaction that detect and replace confidential content before processing.

Prompts and logs need the same care. The agent cheat sheet lists sensitive data exposure, meaning personal data, credentials or confidential data included in agent context or logs, as a key risk. The prompt injection cheat sheet advises logging security-relevant metadata and decisions while excluding credentials, secrets and unnecessary sensitive prompt or response content. Its test advice is blunt: never put a real secret in a prompt. A secret in a prompt can be repeated in an answer or stored in a log.

Cost is a security property too

OWASP's page on unbounded consumption lists controls that will look familiar: strict input size limits, rate limits and user quotas, dynamic management of resources so no single user or request takes too much, timeouts and throttling, sandboxing the model's access to networks and internal services, logging and anomaly detection on resource use, graceful degradation under load, limits on queued actions, and strong access control on model repositories. It also advises restricting how much detail about the model's token probabilities your interface exposes.

The agent cheat sheet names the money version, denial of wallet: attacks that cause excessive API or compute cost through unbounded agent loops. Put a budget and a rate limit on every key, and alert on a sudden burst or a very large prompt from one key, which is exactly what the usage-log lab teaches.

Key points

Sources

Practice in the labs

Close an output hole, build a data-loss guard, scrub a log, then limit and watch usage. All labs run on your machine with dummy data.

Knowledge check

  1. A model returns Markdown that your page renders as HTML. Which fix applies? Answer: Encode or sanitize the output for the page and set a strict Content Security Policy. OWASP's output handling guidance lists context-aware encoding and a strict Content Security Policy as mitigations for code reaching a browser from model output.
  2. Generated SQL is run against your database. What is the safest design? Answer: Use parameterized queries and validate the model's output as you would any user input. OWASP recommends parameterized queries or prepared statements for all database operations that involve model output, and zero-trust validation of what the model returns.
  3. Why exclude secrets from the text you log about LLM requests? Answer: A log with secrets becomes a second place for them to leak. The cheat sheet advises logging security-relevant metadata while excluding credentials, secrets and unnecessary sensitive content, and names data exposure in context and logs as an agent risk.
  4. Which control limits the money lost to a leaked key or a runaway loop? Answer: Per-key budgets, rate limits and usage alerts. OWASP's unbounded consumption controls include rate limits, quotas, timeouts, input size limits and monitoring, and the agent cheat sheet describes denial of wallet through unbounded loops.

Back to the syllabus or see all classes.

Related