Codexportfolio intelligence

@philiprehberger/dotnet-html-sanitizer

Whitelist-based HTML sanitizer for XSS prevention with configurable allowed tags, attributes, and URL schemes.

.NETNuGet

Capabilities

README

Philiprehberger.HtmlSanitizer

CI NuGet Last updated

Whitelist-based HTML sanitizer for XSS prevention with configurable allowed tags, attributes, and URL schemes.

Installation

dotnet add package Philiprehberger.HtmlSanitizer

Usage

using Philiprehberger.HtmlSanitizer;

var clean = Sanitizer.Sanitize("<b>Hello</b><script>alert('xss')</script>");
// "<b>Hello</b>"

var stripped = Sanitizer.StripAll("<p>Hello <b>world</b></p>");
// "Hello world"

Custom Options

using Philiprehberger.HtmlSanitizer;

var options = new SanitizerOptions
{
    AllowedTags = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "p", "b", "i" },
    AllowedAttributes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "class" },
    AllowedSchemes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "https" }
};

var clean = Sanitizer.Sanitize("<p class=\"info\"><a href=\"http://evil.com\">link</a></p>", options);
// "<p class=\"info\">link</p>"

CSS Class Whitelisting

using Philiprehberger.HtmlSanitizer;

var options = new SanitizerOptions
{
    AllowedAttributes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "class" },
    AllowedClasses = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "safe", "info" }
};

var clean = Sanitizer.Sanitize("<p class=\"safe danger info\">text</p>", options);
// "<p class=\"safe info\">text</p>"

Data Attribute Support

using Philiprehberger.HtmlSanitizer;

var options = new SanitizerOptions
{
    AllowedDataAttributes = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "data-id", "data-name" }
};

var clean = Sanitizer.Sanitize("<p data-id=\"42\" data-evil=\"bad\">text</p>", options);
// "<p data-id=\"42\">text</p>"

Sanitization Report

using Philiprehberger.HtmlSanitizer;

var result = Sanitizer.SanitizeWithReport("<b>ok</b><div onclick=\"evil()\">text</div>");
// result.SanitizedHtml == "<b>ok</b>text"
// result.Report.Removals contains entries for the removed <div> tag and onclick attribute
foreach (var removal in result.Report.Removals)
{
    Console.WriteLine($"{removal.Kind}: {removal.Name} - {removal.Reason}");
}

External Link Safety

using Philiprehberger.HtmlSanitizer;

var options = new SanitizerOptions
{
    ForceExternalLinkSafety = true
};

var clean = Sanitizer.Sanitize("<a href=\"https://example.com\">link</a>", options);
// "<a href=\"https://example.com\" target=\"_blank\" rel=\"noopener noreferrer\">link</a>"

API

Sanitizer

MethodDescription
Sanitize(string html)Sanitize HTML using default options
Sanitize(string html, SanitizerOptions options)Sanitize HTML using custom options
SanitizeWithReport(string html)Sanitize HTML and return a detailed removal report
SanitizeWithReport(string html, SanitizerOptions options)Sanitize HTML with custom options and return a removal report
StripAll(string html)Remove all HTML tags, returning plain text

SanitizerOptions

PropertyTypeDefaultDescription
AllowedTagsHashSet<string>Common formatting tagsTags permitted in output
AllowedAttributesHashSet<string>href, src, alt, titleAttributes permitted in output
AllowedSchemesHashSet<string>http, https, mailtoURL schemes permitted in href/src
AllowedClassesHashSet<string>EmptyCSS classes permitted in class attribute; empty allows all
AllowedDataAttributesHashSet<string>EmptyAllowed data-* attribute names; empty allows none
ForceExternalLinkSafetyboolfalseAdd target="_blank" rel="noopener noreferrer" to all anchor tags

SanitizationResult

PropertyTypeDescription
SanitizedHtmlstringThe cleaned HTML string
ReportSanitizationReportDetailed report of all removals

SanitizationReport

PropertyTypeDescription
RemovalsList<SanitizationRemoval>List of all elements removed during sanitization

SanitizationRemoval

PropertyTypeDescription
KindstringType of removal: "tag", "attribute", or "url"
NamestringName of the removed element
ReasonstringHuman-readable explanation for the removal

Development

dotnet build src/Philiprehberger.HtmlSanitizer.csproj --configuration Release

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT