All Projects → Vereyon → HtmlRuleSanitizer

Vereyon / HtmlRuleSanitizer

Licence: MIT license
A rule based HTML sanitizer built on top of the HTML Agility pack

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to HtmlRuleSanitizer

PersianDataAnnotations
PersianDataAnnotations is ASP.NET Core MVC & ASP.NET MVC Custom Localization DataAnnotations (Localized MVC Errors) for Persian(Farsi) language - فارسی سازی خطاهای اعتبارسنجی توکار ام.وی.سی. و کور.ام.وی.سی. برای نمایش اعتبار سنجی سمت کلاینت
Stars: ✭ 38 (-32.14%)
Mutual labels:  nuget
Pluralize.NET
📘 Pluralize or singularize any English word.
Stars: ✭ 50 (-10.71%)
Mutual labels:  nuget
SpotifyWebApi
A .net core wrapper for the Spotify Web API
Stars: ✭ 19 (-66.07%)
Mutual labels:  nuget
Blazor.PersianDatePicker
A free JavaScript Jalali (Persian) and Gregorian (Miladi) dual datepicker library for Blazor applications
Stars: ✭ 40 (-28.57%)
Mutual labels:  nuget
Apache-IoTDB-Client-CSharp
C# client for Apache IoTDB
Stars: ✭ 36 (-35.71%)
Mutual labels:  nuget
taxjar.net
Sales Tax API Client for .NET / C#
Stars: ✭ 21 (-62.5%)
Mutual labels:  nuget
EntryCustomReturnPlugin
Xamarin.Forms Plugin to customize the Xamarin.Forms.Entry Keyboard Return Button
Stars: ✭ 81 (+44.64%)
Mutual labels:  nuget
aarbac
An Automated Role Based Access Control .NET framework with T-SQL Query Parser which automatically parse select, insert, update, delete queries based on the logged in user role
Stars: ✭ 18 (-67.86%)
Mutual labels:  nuget
IGeekFan.AspNetCore.Knife4jUI
support .NET Core3.0+,.NET Standard2.0 Swagger UI knife4j ui,you can use NSwagger or Swashbuckle.AspNetCore in packages
Stars: ✭ 178 (+217.86%)
Mutual labels:  nuget
nuget-tree
📦 [ALPHA] Shows nuget package dependencies in a hierarchy
Stars: ✭ 21 (-62.5%)
Mutual labels:  nuget
ViewFaceCore
C# 超简单的离线人脸识别库。( 基于 SeetaFace6 )
Stars: ✭ 345 (+516.07%)
Mutual labels:  nuget
BadMedicine
Library and CLI for randomly generating medical data like you might get out of an Electronic Health Records (EHR) system
Stars: ✭ 18 (-67.86%)
Mutual labels:  nuget
open-pdd-net-sdk
拼多多开放平台DotNet SDK
Stars: ✭ 133 (+137.5%)
Mutual labels:  nuget
ZetaProducerHtmlCompressor
A .NET port of Google’s HtmlCompressor library to minify HTML source code.
Stars: ✭ 31 (-44.64%)
Mutual labels:  nuget
REstate
Portable state-flows (state-machine based workflows)
Stars: ✭ 35 (-37.5%)
Mutual labels:  nuget
i-am-root-nuget-package
📦🏴‍☠️ NuGet package that shows we can run arbitrary code from any NuGet package
Stars: ✭ 22 (-60.71%)
Mutual labels:  nuget
KuttSharp
🔪 .NET Package for kutt.it url shortener
Stars: ✭ 29 (-48.21%)
Mutual labels:  nuget
covidtrackerapiwrapper
CovidSharp is a crossplatform C# API wrapper for the Coronavirus tracking API (https://github.com/ExpDev07/coronavirus-tracker-api)
Stars: ✭ 11 (-80.36%)
Mutual labels:  nuget
beacon
A code of conduct reporting and management system created by Coraline Ada Ehmke, the author of the Contributor Covenant.
Stars: ✭ 30 (-46.43%)
Mutual labels:  enforcement
Centrifuge
Cross-platform runtime mod loader and API for any Unity-based game. Supports Unity 5 and up!
Stars: ✭ 27 (-51.79%)
Mutual labels:  nuget

HtmlRuleSanitizer

Nuget version

HtmlRuleSanitizer is a white list rule based HTML sanitizer built on top of the HTML Agility Pack.

var sanitizer = HtmlSanitizer.SimpleHtml5Sanitizer();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);

Without configuration HtmlRuleSanitizer will strip absolutely everything. This ensures that you are in control of what HTML is getting through. It was inspired by the client side parser of the wysihtml5 editor.

Use cases

HtmlRuleSanitizer was designed with the following use cases in mind:

  • Prevent cross-site scripting (XSS) attacks by removing javascript and other malicious HTML fragments.
  • Restrict HTML to simple markup in order to allow for easy transformation to other document types without having to deal with all possible HTML tags.
  • Enforce nofollow on links to discourage link spam.
  • Cleanup submitted HTML by removing empty tags for example.
  • Restrict HTML to a limited set of tags, for example in a comment system.

Features

  • CSS class white listing
  • Empty tag removal
  • Tag white listing
  • Tag attribute and CSS class enforcement
  • Tag flattening to simplify document structure while maintaining content
  • Tag renaming
  • Attribute checks (e.g. URL validity) and white listing
  • A fluent style configuration interface
  • HTML entity encoding

Usage

Install the HtmlRuleSanitizer NuGet package. Optionally add the following using statement in the file where you intend to use HtmlRuleSanitizer:

using Vereyon.Web;

Basic usage

var sanitizer = HtmlSanitizer.SimpleHtml5Sanitizer();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);

Note: the SimpleHtml5Sanitizer returns a rule set which does not allow for a full document definition. Use SimpleHtml5DocumentSanitizer

Sanitize a document

var sanitizer = HtmlSanitizer.SimpleHtml5DocumentSanitizer();
string cleanHtml = sanitizer.Sanitize(dirtyHtml);

Configuration

The code below demonstrates how to configure a rule set which only allows strong, i and a tags and which enforces the link tags to have a valid url, be no-follow and open in a new window. In addition, any b tag is renamed to strong because they more or less do the same anyway and b is deprecated. Any empty tags are removed to get rid of them. This would be a nice example for comment processing.

var sanitizer = new HtmlSanitizer();
sanitizer.Tag("strong").RemoveEmpty();
sanitizer.Tag("b").Rename("strong").RemoveEmpty();
sanitizer.Tag("i").RemoveEmpty();
sanitizer.Tag("a").SetAttribute("target", "_blank")
	.SetAttribute("rel", "nofollow")
	.CheckAttributeUrl("href")
	.RemoveEmpty();

string cleanHtml = sanitizer.Sanitize(dirtyHtml);

Custom attribute sanitization

Attribute sanitization can be peformed by implementing a custom IHtmlAttributeSanitizer. The code below illustrates a simple custom sanitizer which overrides the attribute value:

class CustomSanitizer : IHtmlAttributeSanitizer
{
    public SanitizerOperation SanitizeAttribute(HtmlAttribute attribute, HtmlSanitizerTagRule tagRule)
    {
		// Override the attribute value and leave the attribute as be.
        attribute.Value = "123";
        return SanitizerOperation.DoNothing;
    }
}

The custom sanitizer can then be assigned to the desired attributes as follows:

var sanitizer = new HtmlSanitizer();
var attributeSanitizer = new CustomSanitizer();
sanitizer.Tag("span").SanitizeAttributes("style", attributeSanitizer);

Contributing

Setup

Using .NET Core

dotnet restore

Tests

Got tests? Yes, see the tests project. It uses xUnit.

Using .NET Core

cd Web.HtmlSanitizer.Tests/
dotnet test

More information

License

MIT X11

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].