SgmlSanitizer 1.0.1

.NET 5.0
dotnet add package SgmlSanitizer --version 1.0.1
NuGet\Install-Package SgmlSanitizer -Version 1.0.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="SgmlSanitizer" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SgmlSanitizer --version 1.0.1
#r "nuget: SgmlSanitizer, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install SgmlSanitizer as a Cake Addin
#addin nuget:?package=SgmlSanitizer&version=1.0.1

// Install SgmlSanitizer as a Cake Tool
#tool nuget:?package=SgmlSanitizer&version=1.0.1

SgmlSanitizer

A sanitizer to remove elements and attributes from SGML based text files, such as HTML and XML. Note that this is a very simple implementation, but by using whitelists and a naïve URL detection implementation XSS may be prevented. Use at your own risk though.

Usage

To sanitize some simple HTML to remove all attributes and only keep the original HTML tag (this is inserted if not provided) and A tags:

string html = "<html><a href=\"https://url\">Keep me</a><p><a>Don't keep me</a></p></html>";

ListSanitiationHandler list = new ListSanitationHandler();
list.Elements.Add("html");
list.Elements.Add("a");

Sanitizer sanitizer = new Sanitizer();
sanitizer.Handlers.Add(list);

string cleanHtml = sanitizer.Sanitize(html);
Console.WriteLine(cleanHtml);

This outputs:

<html><a href="https://url">Keep me</a></html>

The list sanitation handler can be used to whitelist or blacklist elements, attributes and element specific attributes. The UrlAttributeHandler will remove attributes which contain '.' or '/', as the definition of an url is very broad.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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
1.0.1 246 6/30/2021
1.0.0 270 6/27/2021