Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Core
  3. HtmlSanitizer

HtmlSanitizer

MITC#v9.2.1039

A .NET library for cleaning HTML fragments and documents to prevent XSS attacks using a robust HTML parser.

GitHubGitHub
1.7k stars229 forks0 contributors

What is HtmlSanitizer?

HtmlSanitizer is a .NET library that cleans HTML input to prevent cross-site scripting (XSS) attacks by removing dangerous tags, attributes, and CSS. It parses HTML using AngleSharp, ensuring that only safe, whitelisted content remains while protecting against malicious code injection and tag poisoning.

Target Audience

Developers building .NET web applications who need to securely process user-generated HTML content, such as in forums, comment systems, or rich-text editors.

Value Proposition

It offers a highly configurable and robust solution for HTML sanitization, with thread-safe operations and fine-grained control over allowed elements, making it a reliable choice for preventing XSS vulnerabilities in .NET environments.

Overview

Cleans HTML to avoid XSS attacks

Use Cases

Best For

  • Securing user-generated HTML in .NET web applications
  • Preventing XSS attacks in comment sections or forums
  • Sanitizing rich-text editor output before storage or display
  • Protecting against tag poisoning in HTML fragments
  • Configuring custom HTML/CSS allowlists for specific use cases
  • Ensuring thread-safe HTML processing in multi-threaded environments

Not Ideal For

  • Applications requiring exact HTML input preservation, such as archival systems or precise content editing tools
  • High-throughput web services where the performance cost of full HTML parsing is prohibitive
  • Non-.NET projects, as the library is specifically designed for .NET ecosystems
  • Scenarios where a simpler, regex-based sanitizer suffices for basic filtering without full HTML structure validation

Pros & Cons

Pros

Robust HTML Parsing

Uses AngleSharp to parse and manipulate HTML, protecting against tag poisoning and invalid HTML that could break layouts, as highlighted in the README for shielding from deliberate or accidental corruption.

Highly Configurable Rules

Allows fine-grained control over allowed tags, attributes, CSS properties, and URI schemes, with extensive default lists provided, enabling customization for specific security needs.

Thread-Safe Operations

The Sanitize() and SanitizeDocument() methods are safe for multi-threaded use on a pre-configured instance, facilitating performance in concurrent environments, as stated in the Thread Safety section.

Comprehensive URI Handling

Supports base URI configuration to resolve relative links and control allowed schemes, ensuring safe link management in sanitized content, with examples for adding schemes like 'mailto'.

Cons

Alters HTML Output

As noted in the README, text content is not preserved exactly—special characters are encoded (e.g., '4 < 5' becomes '4 &lt; 5') and HTML structure may change, which can be problematic for applications needing input fidelity.

Can Output Invalid HTML

Despite parsing, the sanitizer might still output invalid HTML fragments, such as incorrect nesting like '<div><li>test</li></div>', as admitted in the README, potentially affecting layout integrity.

Configuration Complexity

The extensive customization options, while powerful, require careful setup and understanding of HTML/CSS standards, which can be daunting for developers unfamiliar with security nuances or those needing quick deployment.

Frequently Asked Questions

Quick Stats

Stars1,710
Forks229
Contributors0
Open Issues15
Last commit1 day ago
CreatedSince 2013

Tags

#web-security#xss#sanitizer#html-sanitization#nuget-package#input-validation#security#dotnet#html#xss-prevention

Built With

A
AngleSharp
.
.NET

Included in

Core21.2k
Auto-fetched 1 day ago

Related Projects

Jwt.NetJwt.Net

Jwt.Net, a JWT (JSON Web Token) implementation for .NET

Stars2,186
Forks468
Last commit8 days ago
SecuritySecurity

[Archived] Middleware for security and authorization of web apps. Project moved to https://github.com/aspnet/AspNetCore

Stars1,296
Forks576
Last commit8 years ago
jose-jwtjose-jwt

Ultimate Javascript Object Signing and Encryption (JOSE), JSON Web Token (JWT) and Json Web Keys (JWK) Implementation for .NET and .NET Core

Stars1,023
Forks194
Last commit6 months ago
SecurityHeadersSecurityHeaders

Small package to allow adding security headers to ASP.NET Core websites

Stars854
Forks90
Last commit3 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub