Sanitize::Policy::HTMLSanitizer
Inherits Sanitize::Policy::Whitelist / Sanitize::Policy / Reference / Object
This policy serves as a good default configuration that should fit most typical use cases for HTML sanitization.
Configurations
It comes in three different configurations with different sets of supported HTML tags.
They only differ in the default configuration of allowed tags and attributes. The transformation behaviour is otherwise the same.
Common Configuration
.common: Accepts most standard tags and thus allows using a good
amount of HTML features (see COMMON_SAFELIST).
This is the recommended default configuration and should work for typical use cases unless strong restrictions on allowed content is required.
sanitizer = Sanitize::Policy::HTMLSanitizer.common
sanitizer.process(%(<a href="javascript:alert('foo')">foo</a>)) # => %(foo)
sanitizer.process(%(<p><a href="foo">foo</a></p>)) # => %(<p><a href="foo" rel="nofollow">foo</a></p>)
sanitizer.process(%(<img src="foo.jpg">)) # => %(<img src="foo.jpg">)
sanitizer.process(%(<table><tr><td>foo</td><td>bar</td></tr></table>)) # => %(<table><tr><td>foo</td><td>bar</td></tr></table>)
NOTE: This configuration (nor any other) does not accept &lt;html&gt;,
&lt;head&gt;, or # &lt;body&gt; tags by default. In order to use
#sanitized_document they need to be added explicitly to accepted_arguments.
Basic Configuration
.basic: This set accepts some basic tags including paragraphs, headlines,
lists, and images (see BASIC_SAFELIST).
sanitizer = Sanitize::Policy::HTMLSanitizer.basic
sanitizer.process(%(<a href="javascript:alert('foo')">foo</a>)) # => %(foo)
sanitizer.process(%(<p><a href="foo">foo</a></p>)) # => %(<p><a href="foo" rel="nofollow">foo</a></p>)
sanitizer.process(%(<img src="foo.jpg">)) # => %(<img src="foo.jpg">)
sanitizer.process(%(<table><tr><td>foo</td><td>bar</td></tr></table>)) # => %(foo bar)
Inline Configuration
.inline: Accepts only a limited set of inline tags (see INLINE_SAFELIST).
sanitizer = Sanitize::Policy::HTMLSanitizer.inline
sanitizer.process(%(<a href="javascript:alert('foo')">foo</a>)) # => %(foo)
sanitizer.process(%(<p><a href="foo">foo</a></p>)) # => %(<a href="foo" rel="nofollow">foo</a>)
sanitizer.process(%(<img src="foo.jpg">)) # => %()
sanitizer.process(%(<table><tr><td>foo</td><td>bar</td></tr></table>)) # => %(foo bar)
Attribute Transformations
Attribute transformations are identical in all three configurations. But more
advanced transforms won't apply if the respective attribute is not allowed in
accepted_tags.
So you can easily add additional elements and attributes to lower-tier sets
and get the same attribute validation. For example: .inline doesn't include
&lt;img&gt; tags, but when img is added to accepted_attributes,
the policy validates img tags the same way as in .common.
URL Sanitization
This transformation applies to attributes that contain a URL (configurable
through (url_attributes).
- Makes sure the value is a valid URI (via
URI.parse). If it does not parse, the attribute value is set to empty string. - Sanitizes the URI via
URISanitizer (configurable troughuri_sanitizer). If the sanitizer returnsnil`, the attribute value is set to empty string.
The same URISanitizer is used for any URL attributes.
Anchor Tags
For &lt;a&gt; tags with a href attribute, there are two transforms:
rel="nofollow"is added (can be disabled withadd_rel_nofollow).rel="noopener"is added to links withtargetattribute (can be disabled withadd_rel_noopener).
Anchor tags the have neither a href, name or id attribute are stripped.
NOTE: name and id attributes are not in any of the default sets of
accepted attributes, so they can only be used when explicitly enabled.
Image Tags
&lt;img&gt; tags are stripped if they don't have a src attribute.
Size Attributes
If a tag has width or height attributes, the values are validated to be
numerical or percent values.
By default, these attributes are only accepted for <img> tags.
Alignment Attribute
The align attribute is validated against allowed values for this attribute:
center, left, right, justify, char.
If the value is invalid, the attribute is stripped.
Classes
class attributes are filtered to accept only classes described by
valid_classes. String values need to match the class name exactly, regex
values need to match the entire class name.
class is accepted as a global attribute in the default configuration, but no
values are allowed in valid_classes.
All classes can be accepted by adding the match-all regular expression /.*/
to valid_classes.
Constants
Compatible with basic Markdown features.
Accepts most standard tags and thus allows using a good amount of HTML features.
Only limited elements for inline text markup.
Constructors
Creates an instance which accepts more basic tags including paragraphs,
headlines, lists, and images (see BASIC_SAFELIST).
Creates an instance which accepts even more standard tags and thus allows
using a good amount of HTML features (see COMMON_SAFELIST).
Unless you need tight restrictions on allowed content, this is the recommended default.
Instance methods
Add rel="nofollow" to every &lt;a&gt; tag with href attribute.
Add rel="nofollow" to every &lt;a&gt; tag with href attribute.
Add rel="noopener" to every &lt;a&gt; tag with href and target attribute.
Add rel="noopener" to every &lt;a&gt; tag with href and target attribute.
Removes anchor tag (&lt;a&gt; from the list of accepted tags).
NOTE: This doesn't reject attributes with URL values for other tags.
Configures the URISanitizer to use for sanitizing URL attributes.
Configures which attributes are considered to contain URLs. If empty, URL sanitization is disabled.
Default value: Set{"src", "href", "action", "cite", "longdesc"}.
Configures which attributes are considered to contain URLs. If empty, URL sanitization is disabled.
Default value: Set{"src", "href", "action", "cite", "longdesc"}.
Configures which classes are valid for class attributes.
String values need to match the class name exactly, regex values need to match the entire class name.
Default value: empty
Configures which classes are valid for class attributes.
String values need to match the class name exactly, regex values need to match the entire class name.
Default value: empty