Philiprehberger.HtmlSanitizer 0.2.1

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

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}");
}
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

Method Description
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

Property Type Default Description
AllowedTags HashSet<string> Common formatting tags Tags permitted in output
AllowedAttributes HashSet<string> href, src, alt, title Attributes permitted in output
AllowedSchemes HashSet<string> http, https, mailto URL schemes permitted in href/src
AllowedClasses HashSet<string> Empty CSS classes permitted in class attribute; empty allows all
AllowedDataAttributes HashSet<string> Empty Allowed data-* attribute names; empty allows none
ForceExternalLinkSafety bool false Add target="_blank" rel="noopener noreferrer" to all anchor tags

SanitizationResult

Property Type Description
SanitizedHtml string The cleaned HTML string
Report SanitizationReport Detailed report of all removals

SanitizationReport

Property Type Description
Removals List<SanitizationRemoval> List of all elements removed during sanitization

SanitizationRemoval

Property Type Description
Kind string Type of removal: "tag", "attribute", or "url"
Name string Name of the removed element
Reason string Human-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

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
0.2.1 120 4/1/2026
0.2.0 169 3/28/2026
0.1.0 111 3/23/2026