Show / Hide Table of Contents

Class Whitelist

Whitelists define what HTML (elements and attributes) to allow through the cleaner.

Inheritance
System.Object
Whitelist
Namespace: Supremes.Safety
Assembly: Supremes.dll
Syntax
public sealed class Whitelist : object
Remarks

Everything else is removed.

Start with one of the defaults:

  • None
  • SimpleText
  • Basic
  • BasicWithImages
  • Relaxed

If you need to allow more through (please be careful!), tweak a base whitelist with:

  • AddTags(String[])
  • AddAttributes(String, String[])
  • AddEnforcedAttribute(String, String, String)
  • AddProtocols(String, String, String[])

The cleaner and these whitelists assume that you want to clean a body fragment of HTML (to add user supplied HTML into a templated page), and not to clean a full HTML document. If the latter is the case, either wrap the document HTML around the cleaned body HTML, or create a whitelist that allows html and head elements as appropriate.

If you are going to extend a whitelist, please be very careful. Make sure you understand what attributes may lead to XSS attack vectors. URL attributes are particularly vulnerable and require careful validation. See http://ha.ckers.org/xss.html for some XSS attack examples.

Constructors

Whitelist()

Create a new, empty whitelist.

Declaration
public Whitelist()
Remarks

Generally it will be better to start with a default prepared whitelist instead.

See Also
Basic
BasicWithImages
SimpleText
Relaxed

Properties

Basic

This whitelist allows a fuller range of text nodes: a, b, blockquote, br, cite, code, dd, dl, dt, em, i, li, ol, p, pre, q, small, span, strike, strong, sub, sup, u, ul, and appropriate attributes.

Declaration
public static Whitelist Basic { get; }
Property Value
Type Description
Whitelist

whitelist

Remarks

Links (a elements) can point to http, https, ftp, mailto, and have an enforced rel=nofollow attribute.

Does not allow images.

BasicWithImages

This whitelist allows the same text tags as Basic , and also allows img tags, with appropriate attributes, with src pointing to http or https.

Declaration
public static Whitelist BasicWithImages { get; }
Property Value
Type Description
Whitelist

whitelist

None

This whitelist allows only text nodes: all HTML will be stripped.

Declaration
public static Whitelist None { get; }
Property Value
Type Description
Whitelist

whitelist

Relaxed

This whitelist allows a full range of text and structural body HTML: a, b, blockquote, br, caption, cite, code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul

Links do not have an enforced rel=nofollow attribute, but you can add that if desired.

Declaration
public static Whitelist Relaxed { get; }
Property Value
Type Description
Whitelist

whitelist

Remarks

This whitelist allows a full range of text and structural body HTML: a, b, blockquote, br, caption, cite, code, col, colgroup, dd, div, dl, dt, em, h1, h2, h3, h4, h5, h6, i, img, li, ol, p, pre, q, small, span, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, u, ul

Links do not have an enforced rel=nofollow attribute, but you can add that if desired.

SimpleText

This whitelist allows only simple text formatting: b, em, i, strong, u.

Declaration
public static Whitelist SimpleText { get; }
Property Value
Type Description
Whitelist

whitelist

Remarks

All other HTML (tags and attributes) will be removed.

Methods

AddAttributes(String, String[])

Add a list of allowed attributes to a tag.

Declaration
public Whitelist AddAttributes(string tag, params string[] keys)
Parameters
Type Name Description
System.String tag

The tag the attributes are for. The tag will be added to the allowed tag list if necessary.

System.String[] keys

List of valid attributes for the tag

Returns
Type Description
Whitelist

this (for chaining)

Remarks

(If an attribute is not allowed on an element, it will be removed.)

E.g.: addAttributes("a", "href", "class") allows href and class attributes on a tags.

To make an attribute valid for all tags, use the pseudo tag :all, e.g. addAttributes(":all", "class").

AddEnforcedAttribute(String, String, String)

Add an enforced attribute to a tag.

Declaration
public Whitelist AddEnforcedAttribute(string tag, string key, string value)
Parameters
Type Name Description
System.String tag

The tag the enforced attribute is for. The tag will be added to the allowed tag list if necessary.

System.String key

The attribute key

System.String value

The enforced attribute value

Returns
Type Description
Whitelist

this (for chaining)

Remarks

An enforced attribute will always be added to the element. If the element already has the attribute set, it will be overridden.

E.g.: AddEnforcedAttribute("a", "rel", "nofollow") will make all a tags output as <a href="..." rel="nofollow">

AddProtocols(String, String, String[])

Add allowed URL protocols for an element's URL attribute.

Declaration
public Whitelist AddProtocols(string tag, string key, params string[] protocols)
Parameters
Type Name Description
System.String tag

Tag the URL protocol is for

System.String key

Attribute key

System.String[] protocols

List of valid protocols

Returns
Type Description
Whitelist

this, for chaining

Remarks

This restricts the possible values of the attribute to URLs with the defined protocol.

E.g.: AddProtocols("a", "href", "ftp", "http", "https")

AddTags(String[])

Add a list of allowed elements to a whitelist.

Declaration
public Whitelist AddTags(params string[] tags)
Parameters
Type Name Description
System.String[] tags

tag names to allow

Returns
Type Description
Whitelist

this (for chaining)

Remarks

(If a tag is not allowed, it will be removed from the HTML.)

PreserveRelativeLinks(Boolean)

Configure this Whitelist to preserve relative links in an element's URL attribute, or convert them to absolute links.

Declaration
public Whitelist PreserveRelativeLinks(bool preserve)
Parameters
Type Name Description
System.Boolean preserve

true to allow relative links, false (default) to deny

Returns
Type Description
Whitelist

this Whitelist, for chaining.

Remarks

Configure this Whitelist to preserve relative links in an element's URL attribute, or convert them to absolute links. By default, this is false: URLs will be made absolute (e.g. start with an allowed protocol, like e.g. http:// .

Note that when handling relative links, the input document must have an appropriate base URI set when parsing, so that the link's protocol can be confirmed. Regardless of the setting of the preserve relative links option, the link must be resolvable against the base URI to an allowed protocol; otherwise the attribute will be removed.

See Also
AddProtocols(String, String, String[])
Back to top Generated by DocFX