Skip to content

Releases

v7.0.3

Choose a tag to compare

@rgrove rgrove released this 01 Dec 19:12
v7.0.3
0845b2e

Security

  • CSS properties that contain an image function like image-set() or image() are now removed if the image function contains a disallowed URL or a substitution function like var() at any depth. Previously, a value like image-set(var(--x, "http://example.com/a.png") 1x) could be used to load an image from a URL that the :protocols allowlist didn't allow. (GHSA-c48c-gjq9-vcq5)

  • URLs in CSS src() functions are now validated against the :protocols allowlist just like URLs in url() functions, and CSS properties that contain a src() function with a disallowed URL or with anything other than a single string are removed. No browsers currently support src(), so this is a defense in depth measure. (GHSA-c48c-gjq9-vcq5)

Fixed

  • Fixed a regression in 7.0.2 that could cause an infinite loop when a transformer added elements inside a removed <style>, <script>, or similar element. When Sanitize removes an element along with its contents, it now discards the contents immediately, so transformers are no longer called for them. #255

Full Changelog: v7.0.2...v7.0.3

v7.0.2

Choose a tag to compare

@rgrove rgrove released this 02 Nov 18:51
v7.0.2
cb998df

Security

  • MathML and SVG elements whose <math> or <svg> ancestor has been removed are now also removed (keeping their contents, subject to :remove_contents), even if they're allowed by the config or have been allowlisted by a transformer. Previously, if Sanitize removed a <math> or <svg> element but kept its contents, its descendants were kept even though they'd been parsed as foreign elements, and browsers would reparse them as HTML elements. Since some elements are parsed very differently in foreign content than in HTML, this could allow arbitrary HTML to be injected. For example, the contents of a <textarea>, <title>, <xmp>, <noembed>, or <noframes> element inside a MathML or SVG element are parsed as markup rather than text, so an attribute value, comment, or raw text element inside it like <svg><textarea><!--</textarea><img src=x onerror=alert(1)>--> could close the element early once it was reparsed as HTML. Similarly, browsers that use legacy <select> parsing rules would parse the contents of a raw text element inside a <select> element like <math><select><mi><xmp><input><img src=x onerror=alert(1)></xmp> as markup. This only affected custom configs that don't remove the contents of <math> or <svg> elements and allow one of the affected elements. Sanitize's default and built-in configs aren't affected. Thanks to Mike Dalessio (@flavorjones) for reporting this issue. (GHSA-599p-54q3-ff89)

  • Elements and comments inside text content elements such as <textarea>, <title>, <xmp>, <noembed>, and <noframes> are now always removed, even if they're allowed by the config or have been allowlisted by a transformer. Only their text is kept, subject to :remove_contents. This provides defense in depth against the issue above. (GHSA-599p-54q3-ff89)

  • Sanitize now requires Nokogiri 1.19.7 or higher. Nokogiri versions since 1.19.5 escape < and > in serialized attribute values to match browser behavior, which provides defense in depth against the issue above. (GHSA-599p-54q3-ff89)

  • Elements inside <select>, <option>, and <optgroup> elements that browsers using legacy <select> parsing rules wouldn't allow there are now always removed (keeping their contents, subject to :remove_contents), even if they're allowed by the config or have been allowlisted by a transformer. Previously, if Sanitize removed a <template> element inside a <select> element but kept its contents, any raw text element inside the <template> element would end up inside the <select> element. Browsers that use legacy <select> parsing rules ignore the start tags of raw text elements inside a <select> element and parse their contents as markup, so input like <select><template><xmp><input><img src=x onerror=alert(1)></xmp> could be used to inject arbitrary HTML. This only affected custom configs that allow <select> and at least one raw text element such as <style>, <xmp>, <noembed>, <noframes>, or <plaintext>. Sanitize's default and built-in configs aren't affected. (GHSA-mg79-gw37-jxx7)

Changed

  • Ruby 3.2.0 is now the oldest supported Ruby version, since Nokogiri 1.19.x requires Ruby 3.2.0 or higher. Ruby 3.1 reached end of life in March 2025.

Full Changelog: v7.0.1...v7.0.2

v7.0.1

Choose a tag to compare

@rgrove rgrove released this 21 Oct 23:38
v7.0.1
ac34f95

Security

  • CSS properties that contain a {} block anywhere in their value are now removed. Browsers that support CSS nesting reparse an invalid declaration containing a {} block as a nested style rule, so a value like color:hover{position:fixed} could previously be used to smuggle disallowed properties and URLs past Sanitize in a stylesheet or <style> element. (GHSA-wgw9-qhqg-8hv5)

  • The contents of at-rules such as @media and @supports that appear in a list of declarations (inside a style rule, inside an at-rule like @font-face, or in CSS sanitized with Sanitize::CSS.properties) are now sanitized as declarations rather than as rules, matching how browsers that support CSS nesting parse them. Previously, a stylesheet like p{@media all{position:fixed;a{}}} could be used to smuggle disallowed properties and URLs past Sanitize, since Sanitize treated position:fixed;a as a style rule's selector. The same was true of CSS sanitized with Sanitize::CSS.properties if the result was later embedded in a style rule. Style rules nested inside these at-rules are now removed, just as style rules nested directly inside other style rules already were. (GHSA-wgw9-qhqg-8hv5)

  • CSS properties, style rules, and at-rules that contain blocks or functions nested more deeply than Crass's maximum nesting depth (25 levels by default) are now removed. (GHSA-wgw9-qhqg-8hv5)

  • Hardened CSS sanitization against tokenizer differentials involving bad URLs. CSS properties that contain a bad URL anywhere inside a simple block (such as [...] or (...)) are now removed, as are style rules whose selectors contain a bad URL and at-rules whose preludes contain a bad URL. URLs inside simple blocks are now also validated against the configured protocol allowlist. Previously, a bug in Crass (fixed in Crass 1.0.8) could be exploited to smuggle CSS declarations, rules, or at-rules past Sanitize by hiding them in a bad URL in one of these locations.

  • CSS style rules whose selectors contain a semicolon are now removed, as a defense in depth measure. A semicolon is never valid in a selector, but when a style rule appears where a browser that supports CSS nesting expects declarations, the browser treats everything before the semicolon as a declaration. This could be used to smuggle disallowed properties past Sanitize::CSS.tree! when it was given a list of declarations rather than a list of rules (though this isn't something Sanitize itself does).

Fixed

  • Removed an unused require "cgi" that caused a warning on Ruby 4.0, since the CGI library is no longer part of Ruby's standard library.

New Contributors

Full Changelog: v7.0.0...v7.0.1

v7.0.0

Choose a tag to compare

@rgrove rgrove released this 30 Dec 05:02
19ee751

Sanitize has no breaking API changes in this release, but the major version number has been incremented because we've dropped support for end-of-life versions of Ruby. As long as you're using Ruby 3.1.0 or later, this should be a painless upgrade!

Added

  • Added over 100 new CSS properties to the relaxed config, representing all properties that are listed with a status of "Working Draft" or better in the latest W3C "All Properties" list.

  • Added the @container CSS at-rule to the relaxed config.

  • Added the -webkit-text-fill-color CSS property to the relaxed config. @radar - #244

Changed

  • Ruby 3.1.0 is now the oldest supported Ruby version.

  • Sanitize now requires Nokogiri 1.16.8 or higher.

v6.1.3

Choose a tag to compare

@rgrove rgrove released this 14 Aug 17:17
v6.1.3
b0ec1d6

Bug Fixes

  • The CSS URL protocol allowlist is now enforced on the nonstandard -webkit-image-set CSS function. @ltk - #242

v6.1.2

Choose a tag to compare

@rgrove rgrove released this 27 Jul 18:58
v6.1.2
a98ac98

Bug Fixes

v6.1.1

Choose a tag to compare

@rgrove rgrove released this 13 Jun 00:12
v6.1.1
2bc3d4a

Bug Fixes

  • Proactively fixed a compatibility issue with libxml >= 2.13.0 (which will be used in an upcoming version of Nokogiri) that caused HTML doctype sanitization to fail. @flavorjones - #238

v6.1.0

Choose a tag to compare

@rgrove rgrove released this 14 Sep 21:54
7194dca

Features

  • Added the text-decoration-skip-ink and text-decoration-thickness CSS properties to the relaxed config. @martineriksson - #228

v6.0.2

Choose a tag to compare

@rgrove rgrove released this 06 Jul 14:57
76ed46e

Bug Fixes

  • CVE-2023-36823: Fixed an HTML+CSS sanitization bypass that could allow XSS (cross-site scripting). This issue affects Sanitize versions 3.0.0 through 6.0.1.

    When using Sanitize's relaxed config or a custom config that allows <style> elements and one or more CSS at-rules, carefully crafted input could be used to sneak arbitrary HTML through Sanitize.

    See the following security advisory for additional details: GHSA-f5ww-cq3m-q3g7

    Thanks to @cure53 for finding this issue.

v6.0.1

Choose a tag to compare

@rgrove rgrove released this 27 Jan 18:22
v6.0.1
a92f21c

Bug Fixes

  • Sanitize now always removes <noscript> elements and their contents, even when noscript is in the allowlist.

    This fixes a sanitization bypass that could occur when noscript was allowed by a custom allowlist. In this scenario, carefully crafted input could sneak arbitrary HTML through Sanitize, potentially enabling an XSS (cross-site scripting) attack.

    Sanitize's default configs don't allow <noscript> elements and are not vulnerable. This issue only affects users who are using a custom config that adds noscript to the element allowlist.

    The root cause of this issue is that HTML parsing rules treat the contents of a <noscript> element differently depending on whether scripting is enabled in the user agent. Nokogiri doesn't support scripting so it follows the "scripting disabled" rules, but a web browser with scripting enabled will follow the "scripting enabled" rules. This means that Sanitize can't reliably make the contents of a <noscript> element safe for scripting enabled browsers, so the safest thing to do is to remove the element and its contents entirely.

    See the following security advisory for additional details: GHSA-fw3g-2h3j-qmm7

    Thanks to David Klein from TU Braunschweig (@leeN) for reporting this issue.

  • Fixed an edge case in which the contents of an "unescaped text" element (such as <noembed> or <xmp>) were not properly escaped if that element was allowlisted and was also inside an allowlisted <math> or <svg> element.

    The only way to encounter this situation was to ignore multiple warnings in the readme and create a custom config that allowlisted all the elements involved, including <math> or <svg>. If you're using a default config or if you heeded the warnings about MathML and SVG not being supported, you're not affected by this issue.

    Please let this be a reminder that Sanitize cannot safely sanitize MathML or SVG content and does not support this use case. The default configs don't allow MathML or SVG elements, and allowlisting MathML or SVG elements in a custom config may create a security vulnerability in your application.

    Documentation has been updated to add more warnings and to make the existing warnings about this more prominent.

    Thanks to David Klein from TU Braunschweig (@leeN) for reporting this issue.