Vereyon.Web.HtmlSanitizer 1.8.0
HtmlRuleSanitizer
HtmlRuleSanitizer is a white list rule based HTML sanitizer built on top of the HTML Agility Pack. Use it to cleanup HTML and removing malicious content.
var sanitizer = HtmlSanitizer.SimpleHtml5Sanitizer();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);
Without configuration HtmlRuleSanitizer will strip absolutely everything. This ensures that you are in control of what HTML is getting through. It was inspired by the client side parser of the wysihtml5 editor.
Use cases
HtmlRuleSanitizer was designed with the following use cases in mind:
- Prevent cross-site scripting (XSS) attacks by removing javascript and other malicious HTML fragments.
- Restrict HTML to simple markup in order to allow for easy transformation to other document types without having to deal with all possible HTML tags.
- Enforce nofollow on links to discourage link spam.
- Cleanup submitted HTML by removing empty tags for example.
- Restrict HTML to a limited set of tags, for example in a comment system.
Features
- CSS class white listing
- Empty tag removal
- Tag white listing
- Tag attribute and CSS class enforcement
- Tag flattening to simplify document structure while maintaining content
- Tag renaming
- Attribute checks (e.g. URL validity) and white listing
- Attribute quote normalization
- A fluent style configuration interface
- HTML entity encoding
- Comment removal
Usage
Install the HtmlRuleSanitizer NuGet package.
Optionally add the following using
statement in the file where you intend to use HtmlRuleSanitizer:
using Vereyon.Web;
Basic usage
var sanitizer = HtmlSanitizer.SimpleHtml5Sanitizer();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);
Note: the SimpleHtml5Sanitizer returns a rule set which does not allow for a full document definition. Use SimpleHtml5DocumentSanitizer
Sanitize a document
When dealing with full HTML documents including the html
and body
tags, use SimpleHtml5DocumentSanitizer
:
var sanitizer = HtmlSanitizer.SimpleHtml5DocumentSanitizer();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);
Configuration
The code below demonstrates how to configure a rule set which only allows strong, i and a tags and which enforces the link tags to have a valid url, be no-follow and open in a new window. In addition, any b tag is renamed to strong because they more or less do the same anyway and b is deprecated. Any empty tags are removed to get rid of them. This would be a nice example for comment processing.
var sanitizer = new HtmlSanitizer();
sanitizer.Tag("strong").RemoveEmpty();
sanitizer.Tag("b").Rename("strong").RemoveEmpty();
sanitizer.Tag("i").RemoveEmpty();
sanitizer.Tag("a").SetAttribute("target", "_blank")
.SetAttribute("rel", "nofollow")
.CheckAttributeUrl("href")
.RemoveEmpty();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);
CSS class shitelisting
Global CSS class whitelisting is achieved as follows where CSS classes are space separated:
sanitizer.AllowCss("legal also-legal");
Custom attribute sanitization
Attribute sanitization can be peformed by implementing a custom IHtmlAttributeSanitizer
. The code below illustrates a simple custom sanitizer which overrides the attribute value:
class CustomSanitizer : IHtmlAttributeSanitizer
{
public SanitizerOperation SanitizeAttribute(HtmlAttribute attribute, HtmlSanitizerTagRule tagRule)
{
// Override the attribute value and leave the attribute as be.
attribute.Value = "123";
return SanitizerOperation.DoNothing;
}
}
The custom sanitizer can then be assigned to the desired attributes as follows:
var sanitizer = new HtmlSanitizer();
var attributeSanitizer = new CustomSanitizer();
sanitizer.Tag("span").SanitizeAttributes("style", attributeSanitizer);
Custom element sanitization
Element sanitization can be performed by implement a customer IHtmlElementSanitizer
, much like custom attribute sanitization.
The code below illustrates a custom sanitizer which will remove span
elements which contain the text "remove me":
var sanitizer = new HtmlSanitizer();
sanitizer.Tag("span").Sanitize(new CustomSanitizer(element =>
{
return element.InnerText == "remove me"
? SanitizerOperation.RemoveTag
: SanitizerOperation.DoNothing;
}));
Contributing
Contributions are welcome through a GitHub pull request.
Setup
dotnet restore
Tests
Got tests? Yes, see the tests project. It uses xUnit.
cd Web.HtmlSanitizer.Tests/
dotnet test
More information
License
No packages depend on Vereyon.Web.HtmlSanitizer.
.NET Framework 4.6.2
- HtmlAgilityPack (>= 1.7.1)
.NET Framework 4.8
- HtmlAgilityPack (>= 1.7.1)
.NET 6.0
- HtmlAgilityPack (>= 1.7.1)
.NET 7.0
- HtmlAgilityPack (>= 1.7.1)
.NET Standard 2.0
- HtmlAgilityPack (>= 1.7.1)
.NET Standard 2.1
- HtmlAgilityPack (>= 1.7.1)
Version | Downloads | Last updated |
---|---|---|
1.8.0 | 6 | 22.09.2024 |
1.7.1 | 5 | 22.09.2024 |
1.7.0 | 3 | 22.09.2024 |
1.6.0.1 | 3 | 22.09.2024 |
1.6.0 | 8 | 27.02.2024 |
1.6.0-beta1 | 3 | 22.09.2024 |
1.5.1 | 6 | 22.09.2024 |
1.5.0 | 3 | 22.09.2024 |
1.4.0 | 3 | 22.09.2024 |
1.3.1.1 | 3 | 22.09.2024 |
1.3.1 | 3 | 22.09.2024 |
1.3.0 | 3 | 22.09.2024 |
1.2.1 | 4 | 22.09.2024 |
1.2.0 | 3 | 22.09.2024 |
1.1.4 | 2 | 25.09.2024 |
1.1.3 | 4 | 22.09.2024 |
1.1.2 | 3 | 22.09.2024 |
1.1.1 | 4 | 22.09.2024 |
1.1.0 | 4 | 22.09.2024 |
1.0.0 | 3 | 22.09.2024 |