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. |
| 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
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. |