public class Whitelist extends Object
Start with one of the defaults:
If you need to allow more through (please be careful!), tweak a base whitelist with:
addTags(java.lang.String...)
addAttributes(java.lang.String, java.lang.String...)
addEnforcedAttribute(java.lang.String, java.lang.String, java.lang.String)
addProtocols(java.lang.String, java.lang.String, java.lang.String...)
You can remove any setting from an existing whitelist with:
removeTags(java.lang.String...)
removeAttributes(java.lang.String, java.lang.String...)
removeEnforcedAttribute(java.lang.String, java.lang.String)
removeProtocols(java.lang.String, java.lang.String, java.lang.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.
Constructor and Description |
---|
Whitelist()
Create a new, empty whitelist.
|
Modifier and Type | Method and Description |
---|---|
Whitelist |
addAttributes(String tag, String... keys)
Add a list of allowed attributes to a tag.
|
Whitelist |
addEnforcedAttribute(String tag, String key, String value)
Add an enforced attribute to a tag.
|
Whitelist |
addProtocols(String tag, String key, String... protocols)
Add allowed URL protocols for an element's URL attribute.
|
Whitelist |
addTags(String... tags)
Add a list of allowed elements to a whitelist.
|
static Whitelist |
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.
|
static Whitelist |
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 .
|
protected boolean |
isSafeAttribute(String tagName, Element el, Attribute attr)
Test if the supplied attribute is allowed by this whitelist for this tag
|
protected boolean |
isSafeTag(String tag)
Test if the supplied tag is allowed by this whitelist
|
static Whitelist |
none()
This whitelist allows only text nodes: all HTML will be stripped.
|
Whitelist |
preserveRelativeLinks(boolean preserve)
Configure this Whitelist to preserve relative links in an element's URL attribute, or convert them to absolute links.
|
static 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
|
Whitelist |
removeAttributes(String tag, String... keys)
Remove a list of allowed attributes from a tag.
|
Whitelist |
removeEnforcedAttribute(String tag, String key)
Remove a previously configured enforced attribute from a tag.
|
Whitelist |
removeProtocols(String tag, String key, String... protocols)
Remove allowed URL protocols for an element's URL attribute.
|
Whitelist |
removeTags(String... tags)
Remove a list of allowed elements from a whitelist.
|
static Whitelist |
simpleText()
This whitelist allows only simple text formatting: b, em, i, strong, u .
|
public Whitelist()
basic()
, basicWithImages()
, simpleText()
, relaxed()
public static Whitelist none()
public static Whitelist simpleText()
b, em, i, strong, u
. All other HTML (tags and attributes) will be removed.
public static Whitelist 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.
Links (a
elements) can point to http, https, ftp, mailto
, and have an enforced rel=nofollow
attribute.
Does not allow images.
public static Whitelist basicWithImages()
basic()
, and also allows img
tags, with appropriate attributes, with src
pointing to http
or https
.
public static Whitelist relaxed()
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.
public Whitelist addTags(String... tags)
tags
- tag names to allow
public Whitelist removeTags(String... tags)
tags
- tag names to disallow
public Whitelist addAttributes(String tag, String... keys)
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")
.
tag
- The tag the attributes are for. The tag will be added to the allowed tag list if necessary.
keys
- List of valid attributes for the tag
public Whitelist removeAttributes(String tag, String... keys)
E.g.: removeAttributes("a", "href", "class")
disallows href
and class
attributes on a
tags.
To make an attribute invalid for all tags, use the pseudo tag :all
, e.g. removeAttributes(":all", "class")
.
tag
- The tag the attributes are for.
keys
- List of invalid attributes for the tag
public Whitelist addEnforcedAttribute(String tag, String key, String value)
E.g.: addEnforcedAttribute("a", "rel", "nofollow")
will make all a
tags output as
tag
- The tag the enforced attribute is for. The tag will be added to the allowed tag list if necessary.
key
- The attribute key
value
- The enforced attribute value
public Whitelist removeEnforcedAttribute(String tag, String key)
tag
- The tag the enforced attribute is for.
key
- The attribute key
public Whitelist preserveRelativeLinks(boolean preserve)
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.
preserve
- true
to allow relative links, false
(default) to deny
addProtocols(java.lang.String, java.lang.String, java.lang.String...)
public Whitelist addProtocols(String tag, String key, String... protocols)
E.g.: addProtocols("a", "href", "ftp", "http", "https")
To allow a link to an in-page URL anchor (i.e. , add a
#
:
E.g.: addProtocols("a", "href", "#")
tag
- Tag the URL protocol is for
key
- Attribute key
protocols
- List of valid protocols
public Whitelist removeProtocols(String tag, String key, String... protocols)
E.g.: removeProtocols("a", "href", "ftp")
tag
- Tag the URL protocol is for
key
- Attribute key
protocols
- List of invalid protocols
protected boolean isSafeTag(String tag)
tag
- test tag
protected boolean isSafeAttribute(String tagName, Element el, Attribute attr)
tagName
- tag to consider allowing the attribute in
el
- element under test, to confirm protocol
attr
- attribute under test
Copyright © 1998–2019 iText Group NV. All rights reserved.