$html-docs
InstallDevelopersPricingSign inOpen drive
All HTML guides
Publishing9 min read·July 29, 2026

How to Handle Untrusted HTML Safely

Treat imported markup as code, define an allowlist, isolate rendering, and avoid trusting a sanitized string forever.

The short version

HTML from a user, model, clipboard, or URL is untrusted until your application enforces a policy.

01

Start with a threat model

Untrusted HTML can contain scripts, event handlers, dangerous URLs, form submissions, embedded documents, and CSS that obscures the surrounding interface. The risk depends on where and how the markup is rendered.

Escaping is correct when the goal is to display source code. Sanitization is required when the goal is to render a permitted subset of HTML.

02

Use an allowlist and defense in depth

A sanitizer is security infrastructure. Use a maintained library, keep it updated, and test known payload classes. Regular expressions are not an HTML parser.

  • Allow only the elements and attributes the product needs.
  • Remove inline event handlers and unsafe URL schemes.
  • Sanitize on the server at the trust boundary.
  • Apply a restrictive Content Security Policy.
  • Use an iframe or Shadow DOM when isolation matches the use case.
03

Revisit the policy when features change

A page sanitized under an older policy may become unsafe if the renderer later enables new attributes, custom elements, or URL handling. Store provenance when possible and re-sanitize when the trust boundary changes.

HTML Docs separates imported shell structure from editable regions and renders in an isolated surface, but product-specific policies still need to decide which behaviors are allowed.

Official references

  • OWASP: Cross Site Scripting Prevention
  • W3C: Content Security Policy

In this guide

  1. 01Start with a threat model
  2. 02Use an allowlist and defense in depth
  3. 03Revisit the policy when features change
Publish a page

Keep going

Related HTML guides

Browse all 24 guides
Publishing7 min read

How to Write HTML Metadata for Search and Sharing

Create useful titles, descriptions, canonical URLs, and social cards without treating metadata as a bag of keywords.

Read guide
Publishing7 min read

How to Add Structured Data to an HTML Page

Use JSON-LD to describe articles, organizations, products, and breadcrumbs without making claims the visible page cannot support.

Read guide
Publishing7 min read

How to Make an HTML Page Print and Export Cleanly

Add print styles that preserve the document hierarchy, avoid awkward page breaks, and remove interface-only controls.

Read guide
© 2026 HTML DocsInstallSupportPrivacyTermsAPI