SgmlSanitizer 1.0.1

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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
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 382 6/30/2021
1.0.0 371 6/27/2021