sanitize 7.0.1 → 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +8 -1
- data/lib/sanitize/config.rb +0 -2
- data/lib/sanitize/css.rb +54 -18
- data/lib/sanitize/transformers/clean_comment.rb +38 -2
- data/lib/sanitize/transformers/clean_css.rb +5 -1
- data/lib/sanitize/transformers/clean_element.rb +85 -5
- data/lib/sanitize/version.rb +1 -1
- data/lib/sanitize.rb +1 -2
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0a93325511ec2929036ce3e0736fd3de9344f000e177e8699f8baf785f096f71
|
|
4
|
+
data.tar.gz: 70a4b6bec7a667347bdeff21aac84ea7ab4d48b654ef4dfe621b22a1e36553d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ff96afaa502b8275ccefac4171cdfb5da41d9190a206a0838f5e3979cdd63bd1635e3db7ee670c3eee4798e00493f17889e05a645dbc9022a9588a0384a7250b
|
|
7
|
+
data.tar.gz: fb3f80a7d2ff91605d008004edaecda1d30452226a9a4798f70b5b92d7d8f224127bb959dd6936091ae6d38ebdce12d790f250caeaad5b096245e73b3409b748
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to Sanitize are documented in this file. The format (since version 7.0.0) is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
4
4
|
|
|
5
|
+
## 7.0.3 (2026-12-01)
|
|
6
|
+
|
|
7
|
+
### Security
|
|
8
|
+
|
|
9
|
+
- 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)
|
|
10
|
+
|
|
11
|
+
- 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)
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- 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](https://github.com/rgrove/sanitize/issues/255)
|
|
16
|
+
|
|
17
|
+
## 7.0.2 (2026-11-02)
|
|
18
|
+
|
|
19
|
+
### Security
|
|
20
|
+
|
|
21
|
+
- 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)
|
|
22
|
+
|
|
23
|
+
- 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)
|
|
24
|
+
|
|
25
|
+
- 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)
|
|
26
|
+
|
|
27
|
+
- 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)
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
|
|
31
|
+
- 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.
|
|
32
|
+
|
|
5
33
|
## 7.0.1 (2026-10-21)
|
|
6
34
|
|
|
7
35
|
### Security
|
data/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all HTML and/o
|
|
|
4
4
|
|
|
5
5
|
Using a simple configuration syntax, you can tell Sanitize to allow certain HTML elements, certain attributes within those elements, and even certain URL protocols within attributes that contain URLs. You can also allow specific CSS properties, @ rules, and URL protocols in elements or attributes containing CSS. Any HTML or CSS that you don't explicitly allow will be removed.
|
|
6
6
|
|
|
7
|
-
Sanitize is based on the [Nokogiri HTML5 parser][nokogiri], which parses HTML the same way modern browsers do, and [Crass][crass], which parses CSS the same way modern browsers do.
|
|
7
|
+
Sanitize is based on the [Nokogiri HTML5 parser][nokogiri], which parses HTML the same way modern browsers do, and [Crass][crass], which parses CSS the same way modern browsers do.
|
|
8
8
|
|
|
9
9
|
[](http://badge.fury.io/rb/sanitize)
|
|
10
10
|
[](https://github.com/rgrove/sanitize/actions?query=workflow%3ATests)
|
|
@@ -51,6 +51,9 @@ Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED)
|
|
|
51
51
|
Sanitize::CSS.properties(css, Sanitize::Config::RELAXED)
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
> [!IMPORTANT]
|
|
55
|
+
> Input that can't be parsed as HTML or CSS may cause an exception to be raised. How you handle exceptions (or whether you allow them to crash your application) is up to you, but be sure to keep this in mind if you're passing untrusted content to Sanitize.
|
|
56
|
+
|
|
54
57
|
## Usage
|
|
55
58
|
|
|
56
59
|
Sanitize can sanitize the following types of input:
|
|
@@ -410,6 +413,8 @@ If this is `true`, Sanitize will remove the contents of any non-allowlisted elem
|
|
|
410
413
|
|
|
411
414
|
If this is an Array or Set of element names, then only the contents of the specified elements (when filtered) will be removed, and the contents of all other filtered elements will be left behind.
|
|
412
415
|
|
|
416
|
+
Removed contents are discarded before they're traversed, so transformers won't be called for them.
|
|
417
|
+
|
|
413
418
|
The default value can be seen in the [default config](lib/sanitize/config/default.rb).
|
|
414
419
|
|
|
415
420
|
#### :transformers (Array or callable)
|
|
@@ -495,6 +500,8 @@ Sanitize.fragment(html, transformers: transformer)
|
|
|
495
500
|
|
|
496
501
|
Transformers have a tremendous amount of power, including the power to completely bypass Sanitize's built-in filtering. Be careful! Your safety is in your own hands.
|
|
497
502
|
|
|
503
|
+
Be especially careful with transformers that add nodes. If a transformer adds something that Sanitize then removes, such as an element that isn't allowed by the config or isn't allowed where it was added (like an `<a>` element inside a `<style>`, `<textarea>`, `<title>`, or `<option>` element), Sanitize may keep whatever was inside it and call the transformer again for those nodes. If the transformer adds the same thing again, Sanitize will be stuck in an infinite loop. Since the input determines where content appears, an attacker may be able to trigger this deliberately. Sanitize can't protect against this, so make sure your transformers only add things that will be allowed where they're added. For example, a transformer that turns URLs into links should check that `env[:config][:elements]` includes `a`, and should skip text inside elements that can't contain links.
|
|
504
|
+
|
|
498
505
|
### Example: Transformer to allow image URLs by domain
|
|
499
506
|
|
|
500
507
|
The following example demonstrates how to remove image elements unless they use a relative URL or are hosted on a specific domain. It assumes that the `<img>` element and its `src` attribute are already allowlisted.
|
data/lib/sanitize/config.rb
CHANGED
data/lib/sanitize/css.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "crass"
|
|
4
|
-
require "set"
|
|
5
4
|
|
|
6
5
|
class Sanitize
|
|
7
6
|
class CSS
|
|
@@ -299,7 +298,7 @@ class Sanitize
|
|
|
299
298
|
if child.key?(:name)
|
|
300
299
|
name = child[:name].downcase
|
|
301
300
|
|
|
302
|
-
if name
|
|
301
|
+
if url_function?(name)
|
|
303
302
|
return nil unless valid_url?(child)
|
|
304
303
|
end
|
|
305
304
|
|
|
@@ -402,14 +401,38 @@ class Sanitize
|
|
|
402
401
|
tree
|
|
403
402
|
end
|
|
404
403
|
|
|
404
|
+
# Returns `true` if the given CSS function name is a substitution function
|
|
405
|
+
# whose value is determined by the browser at computed-value time. This
|
|
406
|
+
# includes calls to custom functions defined with `@function`, whose names
|
|
407
|
+
# begin with `--`.
|
|
408
|
+
def substitution_function?(name)
|
|
409
|
+
name.start_with?("--") || ["attr", "env", "if", "inherit", "var"].include?(name)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# Returns `true` if the given CSS function name is a URL function like
|
|
413
|
+
# `url()` or `src()`.
|
|
414
|
+
def url_function?(name)
|
|
415
|
+
["src", "url"].include?(name)
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# Returns `true` if the given URL string uses an allowlisted protocol, or is
|
|
419
|
+
# relative and relative URLs are allowlisted.
|
|
420
|
+
def url_protocol_allowed?(url)
|
|
421
|
+
if url =~ Sanitize::REGEX_PROTOCOL
|
|
422
|
+
@config[:protocols].include?($1.downcase)
|
|
423
|
+
else
|
|
424
|
+
@config[:protocols].include?(:relative)
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
|
|
405
428
|
# Returns `true` if the given node (which may be of type `:url` or
|
|
406
429
|
# `:function`, since the CSS syntax can produce both) uses an allowlisted
|
|
407
|
-
# protocol.
|
|
430
|
+
# protocol. A `:function` node may be either a `url()` or `src()` function.
|
|
408
431
|
def valid_url?(node)
|
|
409
432
|
type = node[:node]
|
|
410
433
|
|
|
411
434
|
if type == :function
|
|
412
|
-
return false unless node.key?(:name) && node[:name].downcase
|
|
435
|
+
return false unless node.key?(:name) && url_function?(node[:name].downcase)
|
|
413
436
|
return false unless Array === node[:value]
|
|
414
437
|
|
|
415
438
|
# A URL function's `:value` should be an array containing no more than
|
|
@@ -443,34 +466,47 @@ class Sanitize
|
|
|
443
466
|
return false
|
|
444
467
|
end
|
|
445
468
|
|
|
446
|
-
|
|
447
|
-
@config[:protocols].include?($1.downcase)
|
|
448
|
-
else
|
|
449
|
-
@config[:protocols].include?(:relative)
|
|
450
|
-
end
|
|
469
|
+
url_protocol_allowed?(url)
|
|
451
470
|
end
|
|
452
471
|
|
|
453
|
-
# Returns `true` if the given node is an image-related function
|
|
454
|
-
#
|
|
472
|
+
# Returns `true` if the given node is an image-related function in which
|
|
473
|
+
# every string at any depth (other than a MIME type in a `type()` function)
|
|
474
|
+
# uses an allowlisted protocol, and which contains no substitution functions
|
|
475
|
+
# (see {#substitution_function?}) at any depth.
|
|
476
|
+
#
|
|
477
|
+
# Substitution functions are rejected because their values aren't known
|
|
478
|
+
# until a browser computes them and could be URLs that use a disallowed
|
|
479
|
+
# protocol. For example, `image-set(var(--x) 1x)` would load an image from
|
|
480
|
+
# `http://example.com` if `--x` were set to `"http://example.com"`.
|
|
455
481
|
def valid_image?(node)
|
|
456
482
|
return false unless node[:node] == :function
|
|
457
483
|
return false unless node.key?(:name) && image_function?(node[:name].downcase)
|
|
458
484
|
return false unless Array === node[:value]
|
|
459
485
|
|
|
460
|
-
node[:value].
|
|
486
|
+
stack = node[:value].map { |token| [token, nil] }
|
|
487
|
+
|
|
488
|
+
until stack.empty?
|
|
489
|
+
token, parent_function_name = stack.pop
|
|
461
490
|
return false unless Hash === token
|
|
462
491
|
|
|
492
|
+
function_name = nil
|
|
493
|
+
|
|
463
494
|
case token[:node]
|
|
495
|
+
when :function
|
|
496
|
+
function_name = token[:name].to_s.downcase
|
|
497
|
+
return false if substitution_function?(function_name)
|
|
498
|
+
|
|
464
499
|
when :string
|
|
465
|
-
|
|
466
|
-
return false unless
|
|
467
|
-
else
|
|
468
|
-
return false unless @config[:protocols].include?(:relative)
|
|
500
|
+
unless parent_function_name == "type"
|
|
501
|
+
return false unless url_protocol_allowed?(token[:value])
|
|
469
502
|
end
|
|
470
|
-
else
|
|
471
|
-
next
|
|
472
503
|
end
|
|
504
|
+
|
|
505
|
+
value = token[:value]
|
|
506
|
+
stack.concat(value.map { |child| [child, function_name] }) if Array === value
|
|
473
507
|
end
|
|
508
|
+
|
|
509
|
+
true
|
|
474
510
|
end
|
|
475
511
|
end
|
|
476
512
|
end
|
|
@@ -2,12 +2,48 @@
|
|
|
2
2
|
|
|
3
3
|
class Sanitize
|
|
4
4
|
module Transformers
|
|
5
|
+
# Names of elements whose content is parsed as text rather than markup when
|
|
6
|
+
# they're in the HTML namespace.
|
|
7
|
+
#
|
|
8
|
+
# An element with one of these names can contain markup if it's in a foreign
|
|
9
|
+
# namespace like MathML or SVG. If Sanitize removes the foreign ancestor but
|
|
10
|
+
# keeps the element, its serialized contents will be reparsed as text inside
|
|
11
|
+
# an HTML element, so a closing tag inside a comment or an unescaped raw
|
|
12
|
+
# text element could break out of the element and inject arbitrary markup.
|
|
13
|
+
# To prevent this, Sanitize removes all elements and comments inside these
|
|
14
|
+
# elements, leaving only text.
|
|
15
|
+
TEXT_CONTENT_ELEMENTS = Set.new(%w[
|
|
16
|
+
iframe
|
|
17
|
+
noembed
|
|
18
|
+
noframes
|
|
19
|
+
noscript
|
|
20
|
+
plaintext
|
|
21
|
+
script
|
|
22
|
+
style
|
|
23
|
+
textarea
|
|
24
|
+
title
|
|
25
|
+
xmp
|
|
26
|
+
]).freeze
|
|
27
|
+
|
|
5
28
|
CleanComment = lambda do |env|
|
|
6
29
|
node = env[:node]
|
|
7
30
|
|
|
8
|
-
|
|
9
|
-
|
|
31
|
+
return unless node.type == Nokogiri::XML::Node::COMMENT_NODE
|
|
32
|
+
|
|
33
|
+
# Comments inside text content elements are always removed, even if
|
|
34
|
+
# they're allowlisted, since they can never be serialized safely.
|
|
35
|
+
parent = node.parent
|
|
36
|
+
|
|
37
|
+
while parent&.element?
|
|
38
|
+
if TEXT_CONTENT_ELEMENTS.include?(parent.name.downcase)
|
|
39
|
+
node.unlink
|
|
40
|
+
return
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
parent = parent.parent
|
|
10
44
|
end
|
|
45
|
+
|
|
46
|
+
node.unlink unless env[:config][:allow_comments] || env[:is_allowlisted]
|
|
11
47
|
end
|
|
12
48
|
end
|
|
13
49
|
end
|
|
@@ -48,11 +48,15 @@ class Sanitize
|
|
|
48
48
|
|
|
49
49
|
css = @scss.stylesheet(node.content)
|
|
50
50
|
|
|
51
|
+
# The original contents are removed even if the element is removed so
|
|
52
|
+
# that they won't be traversed, since they'll never be part of the
|
|
53
|
+
# output.
|
|
54
|
+
node.children.unlink
|
|
55
|
+
|
|
51
56
|
if css.strip.empty?
|
|
52
57
|
node.unlink
|
|
53
58
|
else
|
|
54
59
|
css.gsub!("</", '<\/')
|
|
55
|
-
node.children.unlink
|
|
56
60
|
node << Nokogiri::XML::Text.new(css, node.document)
|
|
57
61
|
end
|
|
58
62
|
end
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "set"
|
|
4
|
-
|
|
5
3
|
class Sanitize
|
|
6
4
|
module Transformers
|
|
7
5
|
class CleanElement
|
|
6
|
+
# Maps the namespace URI of each foreign element type supported by HTML to
|
|
7
|
+
# the name of its root element.
|
|
8
|
+
FOREIGN_NAMESPACE_ROOTS = {
|
|
9
|
+
"http://www.w3.org/1998/Math/MathML" => "math",
|
|
10
|
+
"http://www.w3.org/2000/svg" => "svg"
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
8
13
|
# Matches a valid HTML5 data attribute name. The unicode ranges included
|
|
9
14
|
# here are a conservative subset of the full range of characters that are
|
|
10
15
|
# technically allowed, with the intent of matching the most common
|
|
@@ -20,6 +25,14 @@ class Sanitize
|
|
|
20
25
|
# https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes
|
|
21
26
|
REGEX_DATA_ATTR = /\Adata-(?!xml)[a-z_][\w.\u00E0-\u00F6\u00F8-\u017F\u01DD-\u02AF-]*\z/u
|
|
22
27
|
|
|
28
|
+
# Maps `<select>` and related elements to the names of the elements that
|
|
29
|
+
# browsers using legacy `<select>` parsing rules allow as their children.
|
|
30
|
+
SELECT_CONTENT_MODEL = {
|
|
31
|
+
"optgroup" => Set["option"].freeze,
|
|
32
|
+
"option" => Set.new.freeze,
|
|
33
|
+
"select" => Set["hr", "optgroup", "option"].freeze
|
|
34
|
+
}.freeze
|
|
35
|
+
|
|
23
36
|
# Elements whose content is treated as unescaped text by HTML parsers.
|
|
24
37
|
UNESCAPED_TEXT_ELEMENTS = Set.new(%w[
|
|
25
38
|
iframe
|
|
@@ -90,9 +103,18 @@ class Sanitize
|
|
|
90
103
|
|
|
91
104
|
def call(env)
|
|
92
105
|
node = env[:node]
|
|
93
|
-
return if node.type != Nokogiri::XML::Node::ELEMENT_NODE
|
|
106
|
+
return if node.type != Nokogiri::XML::Node::ELEMENT_NODE
|
|
94
107
|
|
|
95
108
|
name = env[:node_name]
|
|
109
|
+
parent = node.parent
|
|
110
|
+
|
|
111
|
+
# Some elements are always removed, even if they're allowlisted, since
|
|
112
|
+
# browsers would parse them differently than Nokogiri did.
|
|
113
|
+
force_remove = orphaned_foreign_element?(node, name, parent) ||
|
|
114
|
+
invalid_select_descendant?(name, parent) ||
|
|
115
|
+
text_content_element?(parent)
|
|
116
|
+
|
|
117
|
+
return if env[:is_allowlisted] && !force_remove
|
|
96
118
|
|
|
97
119
|
# Delete any element that isn't in the config allowlist, unless the node
|
|
98
120
|
# has already been deleted from the document.
|
|
@@ -100,7 +122,7 @@ class Sanitize
|
|
|
100
122
|
# It's important that we not try to reparent the children of a node that
|
|
101
123
|
# has already been deleted, since that seems to trigger a memory leak in
|
|
102
124
|
# Nokogiri.
|
|
103
|
-
|
|
125
|
+
if (force_remove || !@elements.include?(name)) && !parent.nil?
|
|
104
126
|
# Elements like br, div, p, etc. need to be replaced with whitespace
|
|
105
127
|
# in order to preserve readability.
|
|
106
128
|
if @whitespace_elements.include?(name)
|
|
@@ -112,7 +134,12 @@ class Sanitize
|
|
|
112
134
|
end
|
|
113
135
|
|
|
114
136
|
unless node.children.empty?
|
|
115
|
-
|
|
137
|
+
if @remove_all_contents || @remove_element_contents.include?(name)
|
|
138
|
+
# Remove the contents now rather than leaving them in the removed
|
|
139
|
+
# element so that they won't be traversed, since they'll never be
|
|
140
|
+
# part of the output.
|
|
141
|
+
node.children.unlink
|
|
142
|
+
else
|
|
116
143
|
node.add_previous_sibling(node.children)
|
|
117
144
|
end
|
|
118
145
|
end
|
|
@@ -241,10 +268,63 @@ class Sanitize
|
|
|
241
268
|
# because Nokogiri can't fully replicate the parsing behavior of a
|
|
242
269
|
# scripting-enabled browser. The safest thing to do is to simply remove
|
|
243
270
|
# all `<noscript>` elements.
|
|
271
|
+
#
|
|
272
|
+
# The contents are removed first so that they won't be traversed, since
|
|
273
|
+
# they'll never be part of the output.
|
|
244
274
|
when "noscript"
|
|
275
|
+
node.children.unlink
|
|
245
276
|
node.unlink
|
|
246
277
|
end
|
|
247
278
|
end
|
|
279
|
+
|
|
280
|
+
private
|
|
281
|
+
|
|
282
|
+
# Returns `true` if _parent_ is an HTML `<select>`, `<option>`, or
|
|
283
|
+
# `<optgroup>` element and _name_ isn't an element that browsers allow
|
|
284
|
+
# inside it.
|
|
285
|
+
#
|
|
286
|
+
# Browsers that use legacy `<select>` parsing rules ignore the start tags
|
|
287
|
+
# of most elements inside a `<select>` element, including raw text
|
|
288
|
+
# elements like `<style>` and `<xmp>`, so these elements' contents would
|
|
289
|
+
# be parsed as markup. Nokogiri parses these elements the same way, but
|
|
290
|
+
# they can still end up inside a `<select>` element via a `<template>`
|
|
291
|
+
# element or foreign content that Sanitize removes. Since traversal is
|
|
292
|
+
# top-down, removing these elements ensures that only allowed elements
|
|
293
|
+
# and text remain inside `<select>` elements.
|
|
294
|
+
def invalid_select_descendant?(name, parent)
|
|
295
|
+
return false unless parent&.element? && SELECT_CONTENT_MODEL.key?(parent.name.downcase)
|
|
296
|
+
return false if FOREIGN_NAMESPACE_ROOTS.key?(parent.namespace&.href)
|
|
297
|
+
|
|
298
|
+
!SELECT_CONTENT_MODEL[parent.name.downcase].include?(name)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# Returns `true` if _node_ is a MathML or SVG element other than a
|
|
302
|
+
# `<math>` or `<svg>` element and its parent isn't a MathML or SVG element.
|
|
303
|
+
#
|
|
304
|
+
# This happens when Sanitize removes a `<math>` or `<svg>` element but
|
|
305
|
+
# keeps its contents. Nokogiri parsed these descendants as foreign
|
|
306
|
+
# elements, but browsers would reparse them as HTML elements, which may
|
|
307
|
+
# be parsed very differently. For example, the contents of a foreign
|
|
308
|
+
# `<textarea>` element are parsed as markup rather than as text. Since
|
|
309
|
+
# traversal is top-down, removing these elements ensures that no foreign
|
|
310
|
+
# element is kept without a foreign root.
|
|
311
|
+
def orphaned_foreign_element?(node, name, parent)
|
|
312
|
+
root_name = FOREIGN_NAMESPACE_ROOTS[node.namespace&.href]
|
|
313
|
+
return false if root_name.nil? || name == root_name
|
|
314
|
+
|
|
315
|
+
!(parent&.element? && FOREIGN_NAMESPACE_ROOTS.key?(parent.namespace&.href))
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# Returns `true` if _parent_ is a text content element, such as
|
|
319
|
+
# `<textarea>` or `<xmp>`.
|
|
320
|
+
#
|
|
321
|
+
# Browsers parse the contents of these elements as text when they're in
|
|
322
|
+
# the HTML namespace, so any element inside one must have been parsed in
|
|
323
|
+
# a foreign namespace. Since traversal is top-down, removing these
|
|
324
|
+
# elements ensures that only text remains inside text content elements.
|
|
325
|
+
def text_content_element?(parent)
|
|
326
|
+
!!parent&.element? && TEXT_CONTENT_ELEMENTS.include?(parent.name.downcase)
|
|
327
|
+
end
|
|
248
328
|
end
|
|
249
329
|
end
|
|
250
330
|
end
|
data/lib/sanitize/version.rb
CHANGED
data/lib/sanitize.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "nokogiri"
|
|
4
|
-
require "set"
|
|
5
4
|
|
|
6
5
|
require_relative "sanitize/version"
|
|
7
6
|
require_relative "sanitize/config"
|
|
@@ -97,7 +96,7 @@ class Sanitize
|
|
|
97
96
|
# Default transformers always run at the end of the chain, after any custom
|
|
98
97
|
# transformers.
|
|
99
98
|
@transformers << Transformers::CleanElement.new(@config)
|
|
100
|
-
@transformers << Transformers::CleanComment
|
|
99
|
+
@transformers << Transformers::CleanComment
|
|
101
100
|
|
|
102
101
|
if @config[:elements].include?("style")
|
|
103
102
|
scss = Sanitize::CSS.new(config)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sanitize
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 7.0.
|
|
4
|
+
version: 7.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryan Grove
|
|
@@ -15,28 +15,28 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 1.0.
|
|
18
|
+
version: 1.0.9
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 1.0.
|
|
25
|
+
version: 1.0.9
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: nokogiri
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 1.
|
|
32
|
+
version: 1.19.7
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 1.
|
|
39
|
+
version: 1.19.7
|
|
40
40
|
description: |
|
|
41
41
|
Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all HTML
|
|
42
42
|
and/or CSS from a string except the elements, attributes, and properties you
|
|
@@ -76,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
76
76
|
requirements:
|
|
77
77
|
- - ">="
|
|
78
78
|
- !ruby/object:Gem::Version
|
|
79
|
-
version: 3.
|
|
79
|
+
version: 3.2.0
|
|
80
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
requirements:
|
|
82
82
|
- - ">="
|