z3l.FastHtmlParser.HtmlSanitizer 1.8.0

dotnet add package z3l.FastHtmlParser.HtmlSanitizer --version 1.8.0
                    
NuGet\Install-Package z3l.FastHtmlParser.HtmlSanitizer -Version 1.8.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="z3l.FastHtmlParser.HtmlSanitizer" Version="1.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="z3l.FastHtmlParser.HtmlSanitizer" Version="1.8.0" />
                    
Directory.Packages.props
<PackageReference Include="z3l.FastHtmlParser.HtmlSanitizer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add z3l.FastHtmlParser.HtmlSanitizer --version 1.8.0
                    
#r "nuget: z3l.FastHtmlParser.HtmlSanitizer, 1.8.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package z3l.FastHtmlParser.HtmlSanitizer@1.8.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=z3l.FastHtmlParser.HtmlSanitizer&version=1.8.0
                    
Install as a Cake Addin
#tool nuget:?package=z3l.FastHtmlParser.HtmlSanitizer&version=1.8.0
                    
Install as a Cake Tool

z3l.FastHtmlParser.HtmlSanitizer

Fast HTML sanitizer for .NET 9, .NET 10 and .NET 11 against XSS: keeps only the allowed tags, attributes, URL schemes and CSS properties of untrusted HTML (comments, posts, e-mails, scraped pages). No AngleSharp dependency.

Website: https://fasthtmlparser.com · Source: https://github.com/romanti3172/z3l.FastHtmlParser

8–18x faster than HtmlSanitizer (Ganss.Xss), with 11–37x less memory.

Sanitize with the default options z3l.FastHtmlParser HtmlSanitizer (Ganss.Xss, on AngleSharp)
A comment, 1 KB 7.6 μs, 3 KB 135 μs (18x), 115 KB (37x)
quotes.toscrape.com, 11 KB 67 μs, 32 KB 793 μs (12x), 698 KB (22x)
webscraper.io laptops, 38 KB 160 μs, 76 KB 1,623 μs (10x), 1,055 KB (14x)
books.toscrape.com, 51 KB 276 μs, 142 KB 2,263 μs (8x), 1,573 KB (11x)

.NET 10, Ryzen 7 3800X; SanitizeBenchmarks in benchmarks.

Install

dotnet add package z3l.FastHtmlParser.HtmlSanitizer

Use

using z3l.FastHtmlParser;

string safe = HtmlSanitizer.Sanitize("<p onclick=\"steal()\">Hi <script>alert(1)</script><a href=\"javascript:x()\">me</a></p>");
// "<p>Hi <a>me</a></p>"

// relative links made absolute, every link with rel="nofollow noopener", 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);

The result is never null: null or empty HTML gives an empty string.

What is kept

By default (every set of SanitizeOptions can be changed in place):

  • Tags: text, headings, lists, tables, links, images, quotes, code and the page sections (p, b, i, a, img, ul, table, blockquote, pre, article, section, ...). Not forms, frames, media or scripts.
  • Attributes: href, src, srcset, alt, title, class, id, style, the table and list attributes, and the old presentation ones (align, width, color, ...). data-* with AllowDataAttributes.
  • URL schemes: http, https, mailto, tel and relative URLs; data:image/... in img src with AllowDataImages. Made absolute when the page URL is given.
  • CSS: colors, fonts, text alignment, margins, padding, borders and sizes, with plain values only.

A tag that is not allowed is left out and its text stays; script, style, form controls, frames, media players and the like go with their content (RemovedWithContent).

What is never kept

Whatever the options say: event handlers (on*), srcdoc, and the tags script, style, template, noscript, noembed, noframes, xmp, plaintext, base, meta, link, frame, frameset, svg and math, whose content a browser does not read as HTML. A URL is dropped when its scheme is not allowed, however it is hidden (JaVaScRiPt:, jav&#x09;ascript:, leading control chars, entities); a CSS value with url(, expression, image(, a backslash or a comment is dropped with its property.

How it is safe

The result is written anew, never copied from the input: each kept tag by its lower-case name with its kept attributes in double quotes, every text and attribute value decoded and escaped again, comments and broken markup dropped, every opened element closed, and the end tags a browser would imply (a <div> ends an open <p>) written where it would imply them. So a browser reads the result exactly as it was written, whatever the input was.

The tests parse the result of random broken markup with AngleSharp, which follows the HTML standard as browsers do: a browser sees only allowed elements and attributes in it, no script URL, and sanitizing the result again changes nothing.

With the parser

The package is z3l.FastHtmlParser with one more class: the parser comes with it, with its CSS selectors, XPath, Markdown and article extraction.

var doc = new HtmlParser(html, url);

string safe = doc.Sanitize(options);                      // the same as HtmlSanitizer.Sanitize(html, url, options)
string? post = doc.CssFirst("div.post-body").Sanitize();  // one part of the page

Already using z3l.FastHtmlParser? You do not need this package: Sanitize() is a part of the parser.

License

See LICENSE.md.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.8.0 0 11/22/2026