Lorixen.
EN

Rich text editors and XSS: sanitising user-written HTML

Short answer: If your website or app lets people format text, through a blog editor, product descriptions written by sellers, a support form or comments, the HTML that editor produces must be cleaned before it is shown to anyone else. Escaping everything would break the formatting, so use a maintained HTML sanitiser such as DOMPurify with a short allowlist of tags and attributes, sanitise on the server as well as in the browser, never modify the HTML after sanitising, keep the library updated and add a Content Security Policy as a second layer.

Why rich text is a special case

The standard defence against cross-site scripting (XSS) is output encoding: before you put user input into a page, convert characters like < and > into harmless entities so the browser shows them as text. That works perfectly for names, addresses and plain comments.

Rich text editors are different. A staff member writing a blog post, a seller adding a product description to a marketplace, or a customer formatting a support ticket expects bold text, lists, links and perhaps images to appear as formatting. OWASP's Cross Site Scripting Prevention Cheat Sheet addresses this directly: when users author HTML in a WYSIWYG editor, output encoding would prevent XSS but break the intended functionality, because the styling would not be rendered. In those cases, it says, HTML sanitisation should be used.

What can go wrong

An attacker, or simply a compromised account, does not need your editor's toolbar. They can send raw HTML straight to the endpoint that saves the content. If that HTML is stored and displayed unchanged, it can include:

  • A script tag or an event handler such as onerror on an image, which runs JavaScript in other visitors' browsers.
  • Links using the javascript: scheme that run code when clicked.
  • Forms or iframes styled to look like your login page, collecting passwords.
  • Styles that overlay fake content on top of your real page.

Because the content sits on your own domain, any script runs with the trust of your site: it can read what the page shows, act as the logged-in user and change what customers see.

Sanitisation in plain terms

A sanitiser parses the HTML, keeps only the elements and attributes you permit and removes everything else, returning a safe string. OWASP's cheat sheet recommends DOMPurify for HTML sanitisation and shows the basic call: pass the untrusted HTML to DOMPurify.sanitize() and use the clean result.

DOMPurify describes itself as a DOM-only, fast XSS sanitiser for HTML, MathML and SVG, which works with a secure default but offers configuration and hooks. Its documentation shows how to narrow the defaults with ALLOWED_TAGS and ALLOWED_ATTR, for example allowing only a few elements.

Choose a tight allowlist

The fewer tags you allow, the less can go wrong. For most business content a sensible list is:

  • Paragraphs, line breaks, headings at two or three levels.
  • Bold, italic, lists and blockquotes.
  • Links, with the href restricted to http, https and mailto.
  • Images only if needed, from your own storage domain.

Leave out scripts, iframes, forms, inputs, style tags and inline style attributes unless you have a specific reason. If sellers need to embed videos, offer a dedicated "add video" field that takes a URL and builds the embed yourself, rather than accepting raw iframe code.

Sanitise where it matters

Browser-side sanitising is useful for previews, but it can be bypassed by anyone sending requests directly. Sanitise on the server before content is stored or before it is rendered, ideally both. DOMPurify's documentation notes that it can run on the server with Node.js, but requires a DOM to be present; it is tested with jsdom and recommends keeping jsdom up to date, while warning that some alternative DOM libraries are not considered safe with it. In other languages, use a maintained sanitiser library for that platform rather than writing your own filters.

Four rules from OWASP

The cheat sheet adds points that are easy to miss:

  1. Do not modify content after sanitising. If you sanitise and then change the HTML, for example to add classes or rewrite links, you can void your security efforts. Do any changes first, then sanitise last.
  2. Watch what libraries do with it. If you pass sanitised HTML to another library, check it does not mutate the string.
  3. Patch the sanitiser regularly. Browsers change, and bypasses are discovered, so keep the library updated.
  4. Use CSP as an extra layer, not the main defence. OWASP describes Content Security Policy as an allowlist that prevents content being loaded, notes it is easy to get wrong, and advises using it as an additional layer of defence.

Existing content

If your site has been storing unsanitised HTML, do not assume past content is clean. Run existing records through the sanitiser in a one-off migration, review what it removed, and from then on sanitise every save. Log or flag content where the sanitiser removed something significant; repeated attempts are worth investigating.

A worked example

Picture a hypothetical Perth marketplace for local makers, where sellers write product descriptions in a rich text editor. A security researcher reported that a description containing an image with an onerror attribute ran script on the product page, visible to every shopper.

The developers added DOMPurify on the server with an allowlist of paragraphs, headings, bold, italic, lists and links, restricted link schemes, and removed inline styles. All existing descriptions were re-sanitised, and two suspicious listings were removed. A Content Security Policy now blocks inline scripts as a backup. Sellers kept their formatting; the researcher confirmed the issue was fixed.

FAQ

Is Markdown safer than HTML?

It can be, if you render Markdown to HTML and then sanitise the result. Many Markdown renderers allow raw HTML by default.

Can I write my own filter with regular expressions?

That is a common source of bypasses. Use a maintained, tested sanitiser.

Does my CMS do this for me?

Many do for some roles. Check what each user role is allowed to post and test it.

If your site displays formatted content written by staff, sellers or customers, tell us where that content comes from.

Sources

WhatsAppBook a call