HtmlSanitizer Class
Sanitizes HTML fragments to prevent XSS attacks by removing dangerous elements, attributes, and URLs while preserving safe HTML structure.
public sealed class HtmlSanitizerExample
Basic HTML sanitization:
var sanitizer = new HtmlSanitizer();
var safeHtml = sanitizer.SanitizeHtmlFragment("<p>Hello <script>alert('xss')</script>World</p>");
Assert.Equal("<p>Hello World</p>", safeHtml);Constructors
| Name | Description |
|---|---|
| HtmlSanitizer() |
HtmlSanitizer()
HtmlSanitizer.cspublic HtmlSanitizer()No documentation is available.
Properties
| Name | Description |
|---|---|
| AllowComments | Gets or sets a value indicating whether HTML comments are kept in the sanitized output. Comments are removed by default because downlevel-hidden conditional comments (<!--[if IE]><script>…</script><![endif]-->) are executed by legacy browsers. |
| BlockedElements | Gets the set of HTML elements that will be completely removed from the output, including their content. By default includes script and style elements. |
| MaxElementCount | Gets or sets the maximum number of elements the parser creates for the fragment. HtmlSanitizer.SanitizeHtmlFragment throws an ArgumentException for a fragment that creates more elements. The number of elements is not bounded by the length of the fragment: the parser recreates the open formatting elements (b, i, a, …) in each new paragraph, so a few kilobytes can create millions of elements. The default is 100,000. Use Int32.MaxValue to remove the limit. |
| MaxInputLength | Gets or sets the maximum length of the fragment, in UTF-16 code units. HtmlSanitizer.SanitizeHtmlFragment throws an ArgumentException for a longer fragment. The default is 1,000,000. Use Int32.MaxValue to remove the limit. |
| SrcsetAttributes | Gets the set of attribute names that contain srcset values (responsive image sources) and should be validated for safety. |
| UriAttributes | Gets the set of attribute names that contain URLs and should be validated for safety. Unsafe URLs will be replaced with empty strings. |
| ValidAttributePrefixes | Gets the set of prefixes of the HTML attributes that are allowed in sanitized output, in addition to the attributes of HtmlSanitizer.ValidAttributes. By default, it contains aria-. |
| ValidAttributes | Gets the set of HTML attributes that are allowed in sanitized output. Attributes not in this set will be removed from elements. |
| ValidElements | Gets the set of HTML elements that are allowed in sanitized output. Elements not in this set are unwrapped: the tag is dropped but its content is kept, unless the element is in the BlockedElements set. |
AllowComments
HtmlSanitizer.csGets or sets a value indicating whether HTML comments are kept in the sanitized output. Comments are removed by default because downlevel-hidden conditional comments (<!--[if IE]><script>…</script><![endif]-->) are executed by legacy browsers.
public bool AllowComments { get; set; }Property value
BlockedElements
HtmlSanitizer.csGets the set of HTML elements that will be completely removed from the output, including their content. By default includes script and style elements.
Property value
MaxElementCount
HtmlSanitizer.csGets or sets the maximum number of elements the parser creates for the fragment. HtmlSanitizer.SanitizeHtmlFragment throws an ArgumentException for a fragment that creates more elements. The number of elements is not bounded by the length of the fragment: the parser recreates the open formatting elements (b, i, a, …) in each new paragraph, so a few kilobytes can create millions of elements. The default is 100,000. Use Int32.MaxValue to remove the limit.
public int MaxElementCount { get; set; }Property value
MaxInputLength
HtmlSanitizer.csGets or sets the maximum length of the fragment, in UTF-16 code units. HtmlSanitizer.SanitizeHtmlFragment throws an ArgumentException for a longer fragment. The default is 1,000,000. Use Int32.MaxValue to remove the limit.
public int MaxInputLength { get; set; }Property value
SrcsetAttributes
HtmlSanitizer.csGets the set of attribute names that contain srcset values (responsive image sources) and should be validated for safety.
Property value
UriAttributes
HtmlSanitizer.csGets the set of attribute names that contain URLs and should be validated for safety. Unsafe URLs will be replaced with empty strings.
Property value
ValidAttributePrefixes
HtmlSanitizer.csGets the set of prefixes of the HTML attributes that are allowed in sanitized output, in addition to the attributes of HtmlSanitizer.ValidAttributes. By default, it contains aria-.
Property value
ValidAttributes
HtmlSanitizer.csGets the set of HTML attributes that are allowed in sanitized output. Attributes not in this set will be removed from elements.
Property value
ValidElements
HtmlSanitizer.csGets the set of HTML elements that are allowed in sanitized output. Elements not in this set are unwrapped: the tag is dropped but its content is kept, unless the element is in the BlockedElements set.
Property value
Methods
| Name | Description |
|---|---|
| SanitizeHtmlFragment(string?) | Sanitizes an HTML fragment by removing dangerous elements, attributes, and URLs while preserving safe HTML structure. |
SanitizeHtmlFragment(string?)
HtmlSanitizer.csSanitizes an HTML fragment by removing dangerous elements, attributes, and URLs while preserving safe HTML structure.
Parameters
htmlstring?- The HTML fragment to sanitize.
Returns
string? A sanitized HTML fragment safe for rendering.
Exceptions
ArgumentException- The fragment is longer than
HtmlSanitizer.MaxInputLength, or creates more elements thanHtmlSanitizer.MaxElementCount.