.NET 9 · .NET 10 · .NET 11 · built on z3l.FastHtmlParser

Untrusted HTML made safe

A fast HTML sanitizer for .NET: comments, posts, e-mails and scraped pages keep their text, links and formatting, and lose every script.

Or:


      
      

One call, safe HTML

A static method, a string in, a string out. The result is never null.

  • 1
    Allowlists you can changeTags, attributes, URL schemes and CSS properties are sets with the safe defaults in them: add what you need, remove what you do not.
  • 2
    Links for commentsEvery link gets rel="nofollow noopener" with one option; relative links are made absolute with the page URL.
  • 3
    A part of the pageThe parser comes with the package: sanitize only the post body you picked with a CSS selector.
  • 4
    No AngleSharpNo dependencies but the parser, no reflection, trimming and Native AOT compatible, thread-safe.
$dotnet add package z3l.FastHtmlParser.HtmlSanitizer
using z3l.FastHtmlParser;

string safe = HtmlSanitizer.Sanitize(html);

// comments: nofollow links, no inline styles, YouTube embeds allowed
var options = new SanitizeOptions { LinkRel = "nofollow noopener" };
options.AllowedAttributes.Remove("style");
options.AllowedTags.Add("iframe");
options.AllowedAttributes.Add("allowfullscreen");
string comment = HtmlSanitizer.Sanitize(html, "https://example.com/post/1", options);

// a part of a page: the parser comes with the package
var doc = new HtmlParser(html, url);
string? body = doc.CssFirst("div.post-body").Sanitize();

Written anew, so a browser reads it as written

Nothing of the input is copied as it is. Each kept tag is written by its lower-case name with its kept attributes in double quotes, every text and value is escaped again, every element is closed, and the end tags a browser would imply are written where it would imply them.

Never kept

Event handlers (on*), srcdoc, and script, style, template, noscript, svg, math and the other tags whose content a browser does not read as HTML, whatever the options say.

URLs checked as a browser reads them

Only http, https, mailto, tel and relative URLs, however a scheme is hidden: JaVaScRiPt:, jav	ascript:, entities, control chars. srcset is checked URL by URL.

CSS with plain values

Colors, fonts, alignment, margins, borders and sizes. A value with url(, expression, image(, a backslash or a comment goes with its property.

The text stays

A tag that is not allowed is left out and its text stays. Scripts, styles, form controls, frames and media players go with their content.

Checked against a browser's parser

The tests sanitize random broken markup and read the result with AngleSharp, which parses HTML as browsers do: only allowed elements and attributes, no script URL, and a second pass changes nothing.

Linear time on any markup

One pass over the parsed tags. A 1 KB comment takes microseconds, a whole page a fraction of a millisecond.

Benchmarks

From the HTML string to the safe HTML string, with the default options of both. BenchmarkDotNet, .NET 10, AMD Ryzen 7 3800X. Shorter is better.

Time

Memory allocated

HtmlSanitizer 11.0.26 (Ganss.Xss) parses with AngleSharp and writes the DOM back. The code and all the numbers: benchmarks/README.md.

Free for most, one price for the rest

The license of FastHtmlParser: free for individuals and for organizations under USD 1M of annual revenue and under 100 people, commercial use included. Larger organizations pay USD 1,000 once, for every z3l library.

See the pricing Read the license

Questions

I already use z3l.FastHtmlParser. Do I need this package?

No. The sanitizer is a part of the parser: doc.Sanitize(), tag.Sanitize() and HtmlText.Sanitize(html). This package is the parser with one more class, HtmlSanitizer, for those who look for a sanitizer. They can be installed together.

How is it different from HtmlSanitizer (Ganss.Xss)?

Both keep what an allowlist allows. That one parses the page into an AngleSharp DOM, removes what is not allowed and writes the DOM back; this one reads the tags as the parser recorded them and writes the allowed part anew, in one pass: 8–18x faster with 11–37x less memory. The class has the same name; the namespaces differ.

Can I allow YouTube embeds?

Add iframe to AllowedTags (and allowfullscreen to AllowedAttributes). Its src is checked as every URL; srcdoc is never kept. To allow only some hosts, check the result's iframes, or sanitize with a pattern of your own before.

Is the result valid HTML?

It is well-formed: every element opened is closed, attributes are quoted, text is escaped. Sanitizing it again gives the same string.

What does this demo do with the HTML I send?

The server parses and sanitizes it and keeps the parsed page in memory for 20 minutes, so that you can change the options without sending it again; nothing is written to disk or logged. Only public http / https addresses are loaded, up to 5 MB.

Do I need a commercial license?

Only if your organization, together with its affiliates, has USD 1,000,000 or more in annual revenue, or 100 or more employees and contractors. Everyone else uses it free, including in commercial products.