Meziantou.Framework.HtmlSanitizer 3.1.21

Prefix Reserved
dotnet add package Meziantou.Framework.HtmlSanitizer --version 3.1.21
                    
NuGet\Install-Package Meziantou.Framework.HtmlSanitizer -Version 3.1.21
                    
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="Meziantou.Framework.HtmlSanitizer" Version="3.1.21" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.HtmlSanitizer" Version="3.1.21" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Framework.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 Meziantou.Framework.HtmlSanitizer --version 3.1.21
                    
#r "nuget: Meziantou.Framework.HtmlSanitizer, 3.1.21"
                    
#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 Meziantou.Framework.HtmlSanitizer@3.1.21
                    
#: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=Meziantou.Framework.HtmlSanitizer&version=3.1.21
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Framework.HtmlSanitizer&version=3.1.21
                    
Install as a Cake Tool

Meziantou.Framework.HtmlSanitizer

A .NET library for sanitizing HTML fragments to prevent XSS attacks and other security vulnerabilities.

Usage

This library provides functionality to sanitize HTML content by removing potentially dangerous elements, attributes, and URLs while preserving safe HTML structure.

The fragment is parsed exactly as a browser parses it, with the HTML parser of Meziantou.Framework.WebPlatform, and the result is written with the HTML serialization algorithm: attribute values are written between double quotes, and void elements have no end tag (<img src="x">).

Basic Sanitization

HtmlSanitizer and UrlSanitizer are in the Meziantou.Framework.Sanitizers namespace.

var sanitizer = new HtmlSanitizer();
var safeHtml = sanitizer.SanitizeHtmlFragment("<p>Hello <script>alert('xss')</script>World</p>");
Assert.Equal("<p>Hello World</p>", safeHtml);

Remove Dangerous Attributes

The sanitizer removes potentially dangerous attributes like style, id, and event handlers:

var sanitizer = new HtmlSanitizer();

// Removes style attributes
var result1 = sanitizer.SanitizeHtmlFragment("<p style='color:red'>test</p>");
Assert.Equal("<p>test</p>", result1);

// Removes id attributes (not in allowed list)
var result2 = sanitizer.SanitizeHtmlFragment("<p id='test'>test</p>");
Assert.Equal("<p>test</p>", result2);

Sanitize URLs

The sanitizer validates URLs in attributes like href and src to prevent javascript: and other dangerous protocols:

var sanitizer = new HtmlSanitizer();

// Blocks javascript: URLs
var result1 = sanitizer.SanitizeHtmlFragment("<a href='javascript:alert(\"xss\")'>click</a>");
Assert.Equal("<a href=\"\">click</a>", result1);

// Allows safe URLs
var result2 = sanitizer.SanitizeHtmlFragment("<a href='https://example.com'>click</a>");
Assert.Equal("<a href=\"https://example.com\">click</a>", result2);

Sanitize Srcset Attributes

The sanitizer also validates srcset attributes used for responsive images:

var sanitizer = new HtmlSanitizer();

// Blocks srcset with dangerous URLs
var result1 = sanitizer.SanitizeHtmlFragment("<img srcset='javascript:alert() 300w, https://example.com 600w'>");
Assert.Equal("<img srcset=\"\">", result1);

// Allows safe srcset
var result2 = sanitizer.SanitizeHtmlFragment("<img srcset='https://example.com/img1.jpg 300w, https://example.com/img2.jpg 600w'>");
Assert.Equal("<img srcset=\"https://example.com/img1.jpg 300w, https://example.com/img2.jpg 600w\">", result2);

Text

Text and attribute values are decoded by the parser and encoded again when the result is written, so already encoded markup stays encoded. The markup is read as a browser reads it: an unterminated `

var sanitizer = new HtmlSanitizer();

Assert.Equal("<p>&lt;script&gt;</p>", sanitizer.SanitizeHtmlFragment("<p>&lt;script&gt;</p>"));
Assert.Equal("<p>a &lt; b &amp; c</p>", sanitizer.SanitizeHtmlFragment("<p>a < b & c</p>"));
Assert.Equal("<b></b>", sanitizer.SanitizeHtmlFragment("<b>

### Keeping Comments

Comments are removed by default: downlevel-hidden conditional comments (``)
are executed by legacy browsers. Set `AllowComments` to keep them:


```csharp
var sanitizer = new HtmlSanitizer { AllowComments = true };
var result = sanitizer.SanitizeHtmlFragment("<p>ab</p>");
Assert.Equal("<p>ab</p>", result);

Customizing Allowed Elements

You can customize which elements are allowed:

var sanitizer = new HtmlSanitizer();

// Add custom elements
sanitizer.ValidElements.Add("custom-element");

// Has no effect: the content of a raw text element is not markup, so the element is
// removed with its content whether or not it is in ValidElements
sanitizer.ValidElements.Add("iframe");

// Remove elements from allowed list
sanitizer.ValidElements.Remove("img");

// Add elements to blocked list (will be removed entirely)
sanitizer.BlockedElements.Add("iframe");

var result = sanitizer.SanitizeHtmlFragment("<custom-element>a</custom-element><iframe>b</iframe><img src='c.png'>");
Assert.Equal("<custom-element>a</custom-element>", result);

Customizing Allowed Attributes

You can customize which attributes are allowed:

var sanitizer = new HtmlSanitizer();

// Add custom attributes
sanitizer.ValidAttributes.Add("data-custom");

// Remove attributes from allowed list
sanitizer.ValidAttributes.Remove("target");

// Add attributes that should be URL-validated
sanitizer.UriAttributes.Add("data-url");

var result = sanitizer.SanitizeHtmlFragment("<a data-custom='value' target='_blank' href='https://example.com'>link</a>");
Assert.Equal("<a data-custom=\"value\" href=\"https://example.com\">link</a>", result);

Attributes can also be allowed by prefix with ValidAttributePrefixes. It contains aria- by default:

var sanitizer = new HtmlSanitizer();

// Allow every data-* attribute
sanitizer.ValidAttributePrefixes.Add("data-");

// Remove the ARIA attributes
sanitizer.ValidAttributePrefixes.Remove("aria-");

var result = sanitizer.SanitizeHtmlFragment("<p data-id='1' aria-label='text'>test</p>");
Assert.Equal("<p data-id=\"1\">test</p>", result);

An attribute allowed by a prefix is URL-validated only when it is in UriAttributes. Frameworks such as htmx or Knockout run the value of some data-* attributes (data-hx-on:click, data-bind), so only allow data- when the page does not use them.

The is attribute is removed like any other attribute that is not allowed, and the element is recreated without it: otherwise it would still be a customized built-in element, which a browser upgrades with the custom element of the page that has this name.

ValidAttributes and UriAttributes are separate sets, and only UriAttributes triggers URL validation. Adding an attribute to ValidAttributes alone lets any value through unchecked:

var sanitizer = new HtmlSanitizer();
sanitizer.ValidAttributes.Add("data-url");

// The value is not validated
Assert.Equal("<p data-url=\"javascript:alert(1)\">test</p>", sanitizer.SanitizeHtmlFragment("<p data-url='javascript:alert(1)'>test</p>"));

sanitizer.UriAttributes.Add("data-url");
Assert.Equal("<p data-url=\"\">test</p>", sanitizer.SanitizeHtmlFragment("<p data-url='javascript:alert(1)'>test</p>"));

Add any attribute that a browser resolves as a URL to both sets — action, formaction, poster, data, srcdoc, dynsrc and lowsrc among them. The same applies in reverse: removing an attribute from UriAttributes while leaving it in ValidAttributes stops its value from being checked. Removing it from ValidAttributes alone is safe, since the attribute is then dropped.

Limiting the Size of the Input

SanitizeHtmlFragment throws an ArgumentException when the fragment is longer than MaxInputLength (1,000,000 characters by default), or when the parser creates more elements than MaxElementCount (100,000 by default). A short fragment can create many elements: the parser recreates the open formatting elements in each new paragraph.

var sanitizer = new HtmlSanitizer
{
    MaxInputLength = 10_000,
    MaxElementCount = 1_000,
};

// A fragment longer than MaxInputLength is rejected before it is parsed
Assert.Throws<ArgumentException>(() => sanitizer.SanitizeHtmlFragment(new string('a', 10_001)));

// A short fragment can still create many elements: each paragraph recreates the open formatting elements
var amplified = "<p>" + string.Concat(Enumerable.Range(0, 100).Select(i => $"<b class={i}>")) + string.Concat(Enumerable.Repeat("<p>x", 100));
Assert.HasCountLessThan(sanitizer.MaxInputLength, amplified);
Assert.Throws<ArgumentException>(() => sanitizer.SanitizeHtmlFragment(amplified));

Default Configuration

Allowed Elements

By default, the sanitizer allows:

  • Block elements: address, article, aside, blockquote, caption, center, col, colgroup, dd, div, dl, dt, figure, figcaption, footer, h1-h6, header, hgroup, hr, li, nav, ol, p, pre, section, table, tbody, td, tfoot, th, thead, tr, ul, and more
  • Inline elements: a, abbr, acronym, b, bdi, bdo, big, br, cite, code, del, dfn, em, font, i, img, ins, kbd, label, map, mark, q, ruby, rp, rt, s, samp, small, span, strike, strong, sub, sup, time, tt, u, var
  • Void elements: area, br, col, hr, img, wbr

Element Handling

An element is handled in one of three ways:

  • It is in BlockedElements, or its content is text rather than markup (script, style, title, textarea, iframe, noscript, noembed, noframes, plaintext, xmp): the element is removed with its content. This takes precedence over ValidElements — the content of most of these elements is written back exactly as it was read, so it can never be sanitized and adding such an element to ValidElements has no effect.
  • Otherwise, it is an HTML element in ValidElements: the element and its content are kept, and its attributes are sanitized. The content of a template element is sanitized too.
  • Otherwise the element is unwrapped: the tag is dropped but its content is kept and sanitized. SVG and MathML elements (svg, math and their descendants) are always unwrapped, even when their name is in ValidElements, as a browser parses them with different rules.
  • An element is unwrapped as well when it is where the parser of a browser never puts it, as a browser would not read the markup back as the same elements: a link (a) inside another link with no table cell between them, a heading (h1-h6) whose parent is a heading, a form in a form, a button in a button, a div (or another element whose start tag closes a p) in a p, an li in an li, an option in an option, a tr outside of a table section, a table that would contain text, ... The parser only builds them when an element separates them, such as object or template, and unwrapping that element (<a><object><a>) puts them together. The fragment is parsed as in a page that is not in quirks mode, so a table closes a p.
var sanitizer = new HtmlSanitizer();

// script is blocked, so its content is removed too
Assert.Equal("<p>Hello World</p>", sanitizer.SanitizeHtmlFragment("<p>Hello <script>alert('xss')</script>World</p>"));

// form is not allowed, but its content is kept
Assert.Equal("<p>Hello</p>", sanitizer.SanitizeHtmlFragment("<form><p>Hello</p></form>"));

Blocked Elements

By default, the sanitizer blocks (removes entirely):

  • script
  • style

Allowed Attributes

By default, the sanitizer allows common HTML attributes like:

  • URI attributes (with URL validation): background, cite, href, longdesc, src, xlink:href
  • Srcset attributes (with URL validation): srcset
  • General attributes: abbr, align, alt, axis, bgcolor, border, cellpadding, cellspacing, class, clear, color, cols, colspan, compact, coords, datetime, decoding, dir, face, headers, height, hidden, hreflang, hspace, ismap, lang, language, loading, nohref, nowrap, open, rel, rev, reversed, role, rows, rowspan, rules, scope, scrolling, shape, size, span, start, summary, tabindex, target, title, translate, type, valign, value, vspace, width
  • Attribute prefixes: aria-*

The general attributes other than class are shared with the generic attributes of Meziantou.Framework.Markdown, which allows them by default.

URL Sanitizer

The library includes a UrlSanitizer class that validates URLs:

// Check if a URL is safe
Assert.True(UrlSanitizer.IsSafeUrl("https://example.com"));
Assert.False(UrlSanitizer.IsSafeUrl("javascript:alert('xss')"));

// Check if a srcset value is safe
Assert.True(UrlSanitizer.IsSafeSrcset("https://example.com/img1.jpg 300w, https://example.com/img2.jpg 600w"));

Allowed URL Protocols

The URL sanitizer allows:

  • http: and https:
  • mailto:
  • ftp:
  • tel:
  • file:
  • Relative URLs (no protocol)
  • Safe data URLs (base64-encoded images, videos, and audio)

A URL is rejected when it contains a & before the first /, ? or #. This is what stops an entity from masking the scheme, as in java&#115;cript:alert(1) or javascript&#58;alert(1), when the URL is read from markup without decoding it. It also rejects the rare relative URL whose first segment contains a &, such as q&a.html — encode it as q%26a.html. HtmlSanitizer validates the attribute values once decoded, as the browser reads them.

Implementation Details

This library is inspired by:

The library uses Meziantou.Framework.WebPlatform for HTML parsing and serialization.

Additional Resources

Product Compatible and additional computed target framework versions.
.NET 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
3.1.21 0 11/29/2026
3.1.20 165 11/22/2026
3.1.19 95 11/20/2026
3.1.18 101 11/18/2026
3.1.17 918 11/15/2026
3.1.16 104 11/14/2026
3.1.15 105 11/13/2026
3.1.14 100 11/13/2026
3.1.13 448 11/8/2026
3.1.12 99 11/5/2026
3.1.11 123 11/1/2026
3.1.10 114 10/31/2026
3.1.9 116 10/31/2026
3.1.8 174 10/28/2026
3.1.7 147 10/25/2026
3.1.6 212 10/25/2026
3.1.5 111 10/24/2026
3.1.4 115 10/21/2026
3.1.3 275 9/30/2026
3.1.2 159 9/6/2026
Loading failed