Output Safety, XSS, and Sensitive Data

Model output is untrusted text. Render Markdown and HTML safely, sanitize any permitted HTML, and avoid direct unsanitized insertion into the DOM. Treat code, links, citations, and tool arguments as data. Validate URLs before making them clickable, especially schemes and hosts. If the model generates SQL or code, do not execute it automatically in a privileged environment.

Sensitive data handling includes minimization, redaction, retention, access controls, encryption in transit and at rest, and provider-specific review. Logs should be useful for debugging but should not become a secondary database of secrets or private conversations.

Safe text rendering
// Prefer textContent for plain text:
messageElement.textContent = modelResponse;

// If rendering Markdown, use a maintained renderer plus sanitization
// and an explicit link/HTML policy rather than injecting raw HTML.
💡 Using textContent prevents HTML interpretation for plain text. Rich Markdown needs a reviewed rendering pipeline, safe URL handling, and sanitization of any allowed HTML.
Key Takeaways
  • Escape plain text and sanitize permitted rich content.
  • Validate generated links and tool arguments.
  • Redact sensitive values in logs and define retention.
Knowledge Check

Why should model output be treated as untrusted when rendered in a web page?