
.NET library that sanitizes HTML fragments and documents using AngleSharp, stripping XSS vectors via configurable allowlists for tags, attributes, CSS properties, and URI schemes.
HtmlSanitizer is a .NET library for cleaning HTML fragments and documents from constructs that can lead to XSS attacks. It uses AngleSharp to parse, manipulate, and render HTML and CSS.
Because HtmlSanitizer is based on a robust HTML parser it can also shield you from deliberate or accidental "tag poisoning" where invalid HTML in one fragment can corrupt the whole document leading to broken layout or style.
In order to facilitate different use cases, HtmlSanitizer can be customized at several levels:
AllowedTags. All other tags will be stripped.AllowedAttributes. All other attributes will be stripped.AllowedCssProperties. All other styles will be stripped.AllowedAtRules. All other at-rules will be stripped.AllowedSchemes. All other URIs will be stripped.UriAttributes.UriListAttributes. Every entry is checked separately.<body>, through SanitizeFragment(). This keeps markup such as a lone <th> that is only valid deeper in the tree.HtmlSanitizerOptions object to the constructor. new HtmlSanitizerOptions() starts pre-populated with the same defaults as new HtmlSanitizer(), so you only need to set what you want to change; use HtmlSanitizerOptions.Empty() instead if you want a blank slate, see Configuring with HtmlSanitizerOptions.Install the HtmlSanitizer NuGet package. Then:
using Ganss.Xss;
var sanitizer = new HtmlSanitizer();
var html = @"<script>alert('xss')</script><div onload=""alert('xss')"""
+ @"style=""background-color: rgba(0, 0, 0, 1)"">Test<img src=""test.png"""
+ @"style=""background-image: url(javascript:alert('xss')); margin: 10px""></div>";
var sanitized = sanitizer.Sanitize(html, "https://www.example.com");
var expected = @"<div style=""background-color: rgba(0, 0, 0, 1)"">"
+ @"Test<img src=""https://www.example.com/test.png"" style=""margin: 10px""></div>";
Assert.Equal(expected, sanitized);
There's an online demo, plus there's also a .NET Fiddle you can play with.
More example code and a description of possible options can be found in the Wiki.
HtmlSanitizerOptionsA sanitizer can also be configured in one go by passing an HtmlSanitizerOptions object to the constructor. Every collection on HtmlSanitizerOptions starts out pre-populated from HtmlSanitizerDefaults, the same defaults new HtmlSanitizer() uses, so you only need to set what you want to change:
var options = new HtmlSanitizerOptions();
options.AllowedTags.Clear();
options.AllowedTags.Add("a");
options.AllowedTags.Add("img");
// AllowedAttributes, UriAttributes, AllowedSchemes, etc. remain at their defaults
var sanitizer = new HtmlSanitizer(options);
Reassigning a collection outright, rather than mutating the pre-populated one, still replaces the default for that property alone - the other properties are unaffected:
var sanitizer = new HtmlSanitizer(new HtmlSanitizerOptions
{
AllowedTags = new HashSet<string> { "a", "img" },
AllowedAttributes = new HashSet<string> { "href", "src" },
// UriAttributes and AllowedSchemes are not reassigned here, so they keep their defaults
});
If you want a blank slate instead - nothing allowed until you add it - start from HtmlSanitizerOptions.Empty():
var options = HtmlSanitizerOptions.Empty();
options.AllowedTags.Add("a");
options.AllowedAttributes.Add("href");
var sanitizer = new HtmlSanitizer(options);
// UriAttributes is empty, so href is *not* screened against AllowedSchemes
// <a href="javascript:alert(1)">click</a> is preserved as-is
UriAttributes is a screening list rather than an allow list: an attribute not in it is not treated as carrying a URI at all, so its value is never checked against AllowedSchemes. This matters most when building an allowlist from Empty(), where it's easy to allow href without also adding it to UriAttributes. And UriAttributes and AllowedSchemes need to be set together - screening an attribute against an empty scheme set drops every absolute URI, valid https ones included:
var options = HtmlSanitizerOptions.Empty();
options.AllowedTags.Add("a");
options.AllowedAttributes.Add("href");
options.UriAttributes.Add("href");
// AllowedSchemes left empty - every absolute URI is dropped
var sanitizer = new HtmlSanitizer(options);
// <a href="https://example.com">ok</a> becomes <a>ok</a>
a,
abbr,
acronym,
address,
area,
article,
aside,
b,
bdi,
big,
blockquote,
body,
br,
button,
caption,
center,
cite,
code,
col,
colgroup,
data,
datalist,
dd,
del,
details,
dfn,
dir,
div,
dl,
dt,
em,
fieldset,
figcaption,
figure,
font,
footer,
form,
h1,
h2,
h3,
h4,
h5,
h6,
head,
header,
hr,
html,
i,
img,
input,
ins,
kbd,
keygen,
label,
legend,
li,
main,
map,
mark,
menu,
menuitem,
meter,
nav,
ol,
optgroup,
option,
output,
p,
pre,
progress,
q,
rp,
rt,
ruby,
s,
samp,
section,
select,
small,
span,
strike,
strong,
sub,
summary,
sup,
table,
tbody,
td,
textarea,
tfoot,
th,
thead,
time,
tr,
tt,
u,
ul,
var,
wbr

Open-Source-XDR- und SIEM-Plattform für Bedrohungserkennung, Protokollanalyse, Dateiintegritätsüberwachung, Schwachstellenbewertung und Compliance-Management für Endpunkte und Cloud-Workloads.

Open-Source-Netzwerk-IDS/IPS/NSM-Engine für Echtzeit-Datenverkehrsinspektion, Eindringungserkennung und -verhinderung, Protokollanalyse und regelbasiertes Threat Hunting.

Zeek ist ein leistungsstarkes Netzwerkanalyse-Framework, das sich stark von den typischen IDS unterscheidet, die Sie vielleicht kennen.