sanitize 6.1.0 → 7.0.2
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/{HISTORY.md → CHANGELOG.md} +90 -14
- data/LICENSE +3 -1
- data/README.md +130 -240
- data/lib/sanitize/config/basic.rb +15 -15
- data/lib/sanitize/config/default.rb +45 -45
- data/lib/sanitize/config/relaxed.rb +136 -32
- data/lib/sanitize/config/restricted.rb +2 -2
- data/lib/sanitize/config.rb +11 -15
- data/lib/sanitize/css.rb +410 -283
- data/lib/sanitize/transformers/clean_cdata.rb +9 -9
- data/lib/sanitize/transformers/clean_comment.rb +44 -8
- data/lib/sanitize/transformers/clean_css.rb +59 -55
- data/lib/sanitize/transformers/clean_doctype.rb +17 -13
- data/lib/sanitize/transformers/clean_element.rb +284 -231
- data/lib/sanitize/version.rb +3 -1
- data/lib/sanitize.rb +38 -39
- metadata +17 -56
- data/test/common.rb +0 -3
- data/test/test_clean_comment.rb +0 -47
- data/test/test_clean_css.rb +0 -67
- data/test/test_clean_doctype.rb +0 -71
- data/test/test_clean_element.rb +0 -552
- data/test/test_config.rb +0 -65
- data/test/test_malicious_css.rb +0 -55
- data/test/test_malicious_html.rb +0 -339
- data/test/test_parser.rb +0 -75
- data/test/test_sanitize.rb +0 -197
- data/test/test_sanitize_css.rb +0 -424
- data/test/test_transformers.rb +0 -230
|
@@ -1,268 +1,321 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
@
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
end
|
|
88
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Sanitize
|
|
4
|
+
module Transformers
|
|
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
|
+
|
|
13
|
+
# Matches a valid HTML5 data attribute name. The unicode ranges included
|
|
14
|
+
# here are a conservative subset of the full range of characters that are
|
|
15
|
+
# technically allowed, with the intent of matching the most common
|
|
16
|
+
# characters used in data attribute names while excluding uncommon or
|
|
17
|
+
# potentially misleading characters, or characters with the potential to
|
|
18
|
+
# be normalized into unsafe or confusing forms.
|
|
19
|
+
#
|
|
20
|
+
# If you need data attr names with characters that aren't included here
|
|
21
|
+
# (such as combining marks, full-width characters, or CJK), please
|
|
22
|
+
# consider creating a custom transformer to validate attributes according
|
|
23
|
+
# to your needs.
|
|
24
|
+
#
|
|
25
|
+
# https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes
|
|
26
|
+
REGEX_DATA_ATTR = /\Adata-(?!xml)[a-z_][\w.\u00E0-\u00F6\u00F8-\u017F\u01DD-\u02AF-]*\z/u
|
|
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
|
+
|
|
36
|
+
# Elements whose content is treated as unescaped text by HTML parsers.
|
|
37
|
+
UNESCAPED_TEXT_ELEMENTS = Set.new(%w[
|
|
38
|
+
iframe
|
|
39
|
+
noembed
|
|
40
|
+
noframes
|
|
41
|
+
noscript
|
|
42
|
+
plaintext
|
|
43
|
+
script
|
|
44
|
+
style
|
|
45
|
+
xmp
|
|
46
|
+
])
|
|
47
|
+
|
|
48
|
+
# Attributes that need additional escaping on `<a>` elements due to unsafe
|
|
49
|
+
# libxml2 behavior.
|
|
50
|
+
UNSAFE_LIBXML_ATTRS_A = Set.new(%w[
|
|
51
|
+
name
|
|
52
|
+
])
|
|
53
|
+
|
|
54
|
+
# Attributes that need additional escaping on all elements due to unsafe
|
|
55
|
+
# libxml2 behavior.
|
|
56
|
+
UNSAFE_LIBXML_ATTRS_GLOBAL = Set.new(%w[
|
|
57
|
+
action
|
|
58
|
+
href
|
|
59
|
+
src
|
|
60
|
+
])
|
|
61
|
+
|
|
62
|
+
# Mapping of original characters to escape sequences for characters that
|
|
63
|
+
# should be escaped in attributes affected by unsafe libxml2 behavior.
|
|
64
|
+
UNSAFE_LIBXML_ESCAPE_CHARS = {
|
|
65
|
+
" " => "%20",
|
|
66
|
+
'"' => "%22"
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# Regex that matches any single character that needs to be escaped in
|
|
70
|
+
# attributes affected by unsafe libxml2 behavior.
|
|
71
|
+
UNSAFE_LIBXML_ESCAPE_REGEX = /[ "]/
|
|
72
|
+
|
|
73
|
+
def initialize(config)
|
|
74
|
+
@add_attributes = config[:add_attributes]
|
|
75
|
+
@attributes = config[:attributes].dup
|
|
76
|
+
@elements = config[:elements]
|
|
77
|
+
@protocols = config[:protocols]
|
|
78
|
+
@remove_all_contents = false
|
|
79
|
+
@remove_element_contents = Set.new
|
|
80
|
+
@whitespace_elements = {}
|
|
81
|
+
|
|
82
|
+
@attributes.each do |element_name, attrs|
|
|
83
|
+
unless element_name == :all
|
|
84
|
+
@attributes[element_name] = Set.new(attrs).merge(@attributes[:all] || [])
|
|
85
|
+
end
|
|
86
|
+
end
|
|
89
87
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
# already been deleted from the document.
|
|
98
|
-
#
|
|
99
|
-
# It's important that we not try to reparent the children of a node that has
|
|
100
|
-
# already been deleted, since that seems to trigger a memory leak in
|
|
101
|
-
# Nokogiri.
|
|
102
|
-
unless @elements.include?(name) || node.parent.nil?
|
|
103
|
-
# Elements like br, div, p, etc. need to be replaced with whitespace in
|
|
104
|
-
# order to preserve readability.
|
|
105
|
-
if @whitespace_elements.include?(name)
|
|
106
|
-
node.add_previous_sibling(Nokogiri::XML::Text.new(@whitespace_elements[name][:before].to_s, node.document))
|
|
107
|
-
|
|
108
|
-
unless node.children.empty?
|
|
109
|
-
node.add_next_sibling(Nokogiri::XML::Text.new(@whitespace_elements[name][:after].to_s, node.document))
|
|
88
|
+
# Backcompat: if :whitespace_elements is a Set, convert it to a hash.
|
|
89
|
+
if config[:whitespace_elements].is_a?(Set)
|
|
90
|
+
config[:whitespace_elements].each do |element|
|
|
91
|
+
@whitespace_elements[element] = {before: " ", after: " "}
|
|
92
|
+
end
|
|
93
|
+
else
|
|
94
|
+
@whitespace_elements = config[:whitespace_elements]
|
|
110
95
|
end
|
|
111
|
-
end
|
|
112
96
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
97
|
+
if config[:remove_contents].is_a?(Enumerable)
|
|
98
|
+
@remove_element_contents.merge(config[:remove_contents].map(&:to_s))
|
|
99
|
+
else
|
|
100
|
+
@remove_all_contents = !!config[:remove_contents]
|
|
116
101
|
end
|
|
117
102
|
end
|
|
118
103
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
104
|
+
def call(env)
|
|
105
|
+
node = env[:node]
|
|
106
|
+
return if node.type != Nokogiri::XML::Node::ELEMENT_NODE
|
|
122
107
|
|
|
123
|
-
|
|
108
|
+
name = env[:node_name]
|
|
109
|
+
parent = node.parent
|
|
124
110
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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)
|
|
130
116
|
|
|
131
|
-
|
|
132
|
-
node.attribute_nodes.each do |attr|
|
|
133
|
-
attr_name = attr.name.downcase
|
|
117
|
+
return if env[:is_allowlisted] && !force_remove
|
|
134
118
|
|
|
135
|
-
unless
|
|
136
|
-
|
|
137
|
-
|
|
119
|
+
# Delete any element that isn't in the config allowlist, unless the node
|
|
120
|
+
# has already been deleted from the document.
|
|
121
|
+
#
|
|
122
|
+
# It's important that we not try to reparent the children of a node that
|
|
123
|
+
# has already been deleted, since that seems to trigger a memory leak in
|
|
124
|
+
# Nokogiri.
|
|
125
|
+
if (force_remove || !@elements.include?(name)) && !parent.nil?
|
|
126
|
+
# Elements like br, div, p, etc. need to be replaced with whitespace
|
|
127
|
+
# in order to preserve readability.
|
|
128
|
+
if @whitespace_elements.include?(name)
|
|
129
|
+
node.add_previous_sibling(Nokogiri::XML::Text.new(@whitespace_elements[name][:before].to_s, node.document))
|
|
130
|
+
|
|
131
|
+
unless node.children.empty?
|
|
132
|
+
node.add_next_sibling(Nokogiri::XML::Text.new(@whitespace_elements[name][:after].to_s, node.document))
|
|
133
|
+
end
|
|
134
|
+
end
|
|
138
135
|
|
|
139
|
-
unless
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
next
|
|
136
|
+
unless node.children.empty?
|
|
137
|
+
unless @remove_all_contents || @remove_element_contents.include?(name)
|
|
138
|
+
node.add_previous_sibling(node.children)
|
|
139
|
+
end
|
|
144
140
|
end
|
|
141
|
+
|
|
142
|
+
node.unlink
|
|
143
|
+
return
|
|
145
144
|
end
|
|
146
145
|
|
|
147
|
-
|
|
146
|
+
attr_allowlist = @attributes[name] || @attributes[:all]
|
|
147
|
+
|
|
148
|
+
if attr_allowlist.nil?
|
|
149
|
+
# Delete all attributes from elements with no allowlisted attributes.
|
|
150
|
+
node.attribute_nodes.each { |attr| attr.unlink }
|
|
151
|
+
else
|
|
152
|
+
allow_data_attributes = attr_allowlist.include?(:data)
|
|
148
153
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
# Delete any attribute that isn't allowed on this element.
|
|
155
|
+
node.attribute_nodes.each do |attr|
|
|
156
|
+
attr_name = attr.name.downcase
|
|
152
157
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
158
|
+
unless attr_allowlist.include?(attr_name)
|
|
159
|
+
# The attribute isn't in the allowlist, but may still be allowed
|
|
160
|
+
# if it's a data attribute.
|
|
161
|
+
|
|
162
|
+
unless allow_data_attributes && attr_name.start_with?("data-") && attr_name =~ REGEX_DATA_ATTR
|
|
163
|
+
# Either the attribute isn't a data attribute or arbitrary data
|
|
164
|
+
# attributes aren't allowed. Remove the attribute.
|
|
165
|
+
attr.unlink
|
|
166
|
+
next
|
|
167
|
+
end
|
|
157
168
|
end
|
|
158
169
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
# The attribute is allowed.
|
|
171
|
+
|
|
172
|
+
# Remove any attributes that use unacceptable protocols.
|
|
173
|
+
if @protocols.include?(name) && @protocols[name].include?(attr_name)
|
|
174
|
+
attr_protocols = @protocols[name][attr_name]
|
|
175
|
+
|
|
176
|
+
if attr.value =~ REGEX_PROTOCOL
|
|
177
|
+
unless attr_protocols.include?($1.downcase)
|
|
178
|
+
attr.unlink
|
|
179
|
+
next
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
else
|
|
183
|
+
unless attr_protocols.include?(:relative)
|
|
184
|
+
attr.unlink
|
|
185
|
+
next
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Leading and trailing whitespace around URLs is ignored at parse
|
|
190
|
+
# time. Stripping it here prevents it from being escaped by the
|
|
191
|
+
# libxml2 workaround below.
|
|
192
|
+
attr.value = attr.value.strip
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# libxml2 >= 2.9.2 doesn't escape comments within some attributes,
|
|
196
|
+
# in an attempt to preserve server-side includes. This can result in
|
|
197
|
+
# XSS since an unescaped double quote can allow an attacker to
|
|
198
|
+
# inject a non-allowlisted attribute.
|
|
199
|
+
#
|
|
200
|
+
# Sanitize works around this by implementing its own escaping for
|
|
201
|
+
# affected attributes, some of which can exist on any element and
|
|
202
|
+
# some of which can only exist on `<a>` elements.
|
|
203
|
+
#
|
|
204
|
+
# This fix is technically no longer necessary with Nokogumbo >= 2.0
|
|
205
|
+
# since it no longer uses libxml2's serializer, but it's retained to
|
|
206
|
+
# avoid breaking use cases where people might be sanitizing
|
|
207
|
+
# individual Nokogiri nodes and then serializing them manually
|
|
208
|
+
# without Nokogumbo.
|
|
209
|
+
#
|
|
210
|
+
# The relevant libxml2 code is here:
|
|
211
|
+
# <https://github.com/GNOME/libxml2/commit/960f0e275616cadc29671a218d7fb9b69eb35588>
|
|
212
|
+
if UNSAFE_LIBXML_ATTRS_GLOBAL.include?(attr_name) ||
|
|
213
|
+
(name == "a" && UNSAFE_LIBXML_ATTRS_A.include?(attr_name))
|
|
214
|
+
|
|
215
|
+
attr.value = attr.value.gsub(UNSAFE_LIBXML_ESCAPE_REGEX, UNSAFE_LIBXML_ESCAPE_CHARS)
|
|
163
216
|
end
|
|
164
217
|
end
|
|
218
|
+
end
|
|
165
219
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
attr.value = attr.value.strip
|
|
220
|
+
# Add required attributes.
|
|
221
|
+
if @add_attributes.include?(name)
|
|
222
|
+
@add_attributes[name].each { |key, val| node[key] = val }
|
|
170
223
|
end
|
|
171
224
|
|
|
172
|
-
#
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
#
|
|
176
|
-
#
|
|
177
|
-
#
|
|
178
|
-
#
|
|
179
|
-
#
|
|
180
|
-
#
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
# <https://github.com/GNOME/libxml2/commit/960f0e275616cadc29671a218d7fb9b69eb35588>
|
|
188
|
-
if UNSAFE_LIBXML_ATTRS_GLOBAL.include?(attr_name) ||
|
|
189
|
-
(name == 'a' && UNSAFE_LIBXML_ATTRS_A.include?(attr_name))
|
|
225
|
+
# Element-specific special cases.
|
|
226
|
+
case name
|
|
227
|
+
|
|
228
|
+
# If this is an allowlisted iframe that has children, remove all its
|
|
229
|
+
# children. The HTML standard says iframes shouldn't have content, but
|
|
230
|
+
# when they do, this content is parsed as text and is serialized
|
|
231
|
+
# verbatim without being escaped, which is unsafe because legacy
|
|
232
|
+
# browsers may still render it and execute `<script>` content. So the
|
|
233
|
+
# safe and correct thing to do is to always remove iframe content.
|
|
234
|
+
when "iframe"
|
|
235
|
+
if !node.children.empty?
|
|
236
|
+
node.children.each do |child|
|
|
237
|
+
child.unlink
|
|
238
|
+
end
|
|
239
|
+
end
|
|
190
240
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
241
|
+
# Prevent the use of `<meta>` elements that set a charset other than
|
|
242
|
+
# UTF-8, since Sanitize's output is always UTF-8.
|
|
243
|
+
when "meta"
|
|
244
|
+
if node.has_attribute?("charset") &&
|
|
245
|
+
node["charset"].downcase != "utf-8"
|
|
195
246
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
247
|
+
node["charset"] = "utf-8"
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
if node.has_attribute?("http-equiv") &&
|
|
251
|
+
node.has_attribute?("content") &&
|
|
252
|
+
node["http-equiv"].downcase == "content-type" &&
|
|
253
|
+
node["content"].downcase =~ /;\s*charset\s*=\s*(?!utf-8)/
|
|
200
254
|
|
|
201
|
-
|
|
202
|
-
# elements that are inside a math or svg namespace are properly escaped so
|
|
203
|
-
# that they don't get parsed as HTML.
|
|
204
|
-
#
|
|
205
|
-
# Sanitize is explicitly documented as not supporting MathML or SVG, but
|
|
206
|
-
# people sometimes allow `<math>` and `<svg>` elements in their custom
|
|
207
|
-
# configs without realizing that it's not safe. This workaround makes it
|
|
208
|
-
# slightly less unsafe, but you still shouldn't allow `<math>` or `<svg>`
|
|
209
|
-
# because Nokogiri doesn't parse them the same way browsers do and Sanitize
|
|
210
|
-
# can't guarantee that their contents are safe.
|
|
211
|
-
unless node.namespace.nil?
|
|
212
|
-
prefix = node.namespace.prefix
|
|
213
|
-
|
|
214
|
-
if (prefix == 'math' || prefix == 'svg') && UNESCAPED_TEXT_ELEMENTS.include?(name)
|
|
215
|
-
node.children.each do |child|
|
|
216
|
-
if child.type == Nokogiri::XML::Node::TEXT_NODE
|
|
217
|
-
child.content = CGI.escapeHTML(child.content)
|
|
255
|
+
node["content"] = node["content"].gsub(/;\s*charset\s*=.+\z/, ";charset=utf-8")
|
|
218
256
|
end
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
|
-
end
|
|
222
257
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if !node.children.empty?
|
|
234
|
-
node.children.each do |child|
|
|
235
|
-
child.unlink
|
|
258
|
+
# A `<noscript>` element's content is parsed differently in browsers
|
|
259
|
+
# depending on whether or not scripting is enabled. Since Nokogiri
|
|
260
|
+
# doesn't support scripting, it always parses `<noscript>` elements as
|
|
261
|
+
# if scripting is disabled. This results in edge cases where it's not
|
|
262
|
+
# possible to reliably sanitize the contents of a `<noscript>` element
|
|
263
|
+
# because Nokogiri can't fully replicate the parsing behavior of a
|
|
264
|
+
# scripting-enabled browser. The safest thing to do is to simply remove
|
|
265
|
+
# all `<noscript>` elements.
|
|
266
|
+
when "noscript"
|
|
267
|
+
node.unlink
|
|
236
268
|
end
|
|
237
269
|
end
|
|
238
270
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
271
|
+
private
|
|
272
|
+
|
|
273
|
+
# Returns `true` if _parent_ is an HTML `<select>`, `<option>`, or
|
|
274
|
+
# `<optgroup>` element and _name_ isn't an element that browsers allow
|
|
275
|
+
# inside it.
|
|
276
|
+
#
|
|
277
|
+
# Browsers that use legacy `<select>` parsing rules ignore the start tags
|
|
278
|
+
# of most elements inside a `<select>` element, including raw text
|
|
279
|
+
# elements like `<style>` and `<xmp>`, so these elements' contents would
|
|
280
|
+
# be parsed as markup. Nokogiri parses these elements the same way, but
|
|
281
|
+
# they can still end up inside a `<select>` element via a `<template>`
|
|
282
|
+
# element or foreign content that Sanitize removes. Since traversal is
|
|
283
|
+
# top-down, removing these elements ensures that only allowed elements
|
|
284
|
+
# and text remain inside `<select>` elements.
|
|
285
|
+
def invalid_select_descendant?(name, parent)
|
|
286
|
+
return false unless parent&.element? && SELECT_CONTENT_MODEL.key?(parent.name.downcase)
|
|
287
|
+
return false if FOREIGN_NAMESPACE_ROOTS.key?(parent.namespace&.href)
|
|
288
|
+
|
|
289
|
+
!SELECT_CONTENT_MODEL[parent.name.downcase].include?(name)
|
|
246
290
|
end
|
|
247
291
|
|
|
248
|
-
if
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
292
|
+
# Returns `true` if _node_ is a MathML or SVG element other than a
|
|
293
|
+
# `<math>` or `<svg>` element and its parent isn't a MathML or SVG element.
|
|
294
|
+
#
|
|
295
|
+
# This happens when Sanitize removes a `<math>` or `<svg>` element but
|
|
296
|
+
# keeps its contents. Nokogiri parsed these descendants as foreign
|
|
297
|
+
# elements, but browsers would reparse them as HTML elements, which may
|
|
298
|
+
# be parsed very differently. For example, the contents of a foreign
|
|
299
|
+
# `<textarea>` element are parsed as markup rather than as text. Since
|
|
300
|
+
# traversal is top-down, removing these elements ensures that no foreign
|
|
301
|
+
# element is kept without a foreign root.
|
|
302
|
+
def orphaned_foreign_element?(node, name, parent)
|
|
303
|
+
root_name = FOREIGN_NAMESPACE_ROOTS[node.namespace&.href]
|
|
304
|
+
return false if root_name.nil? || name == root_name
|
|
305
|
+
|
|
306
|
+
!(parent&.element? && FOREIGN_NAMESPACE_ROOTS.key?(parent.namespace&.href))
|
|
254
307
|
end
|
|
255
308
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
309
|
+
# Returns `true` if _parent_ is a text content element, such as
|
|
310
|
+
# `<textarea>` or `<xmp>`.
|
|
311
|
+
#
|
|
312
|
+
# Browsers parse the contents of these elements as text when they're in
|
|
313
|
+
# the HTML namespace, so any element inside one must have been parsed in
|
|
314
|
+
# a foreign namespace. Since traversal is top-down, removing these
|
|
315
|
+
# elements ensures that only text remains inside text content elements.
|
|
316
|
+
def text_content_element?(parent)
|
|
317
|
+
!!parent&.element? && TEXT_CONTENT_ELEMENTS.include?(parent.name.downcase)
|
|
318
|
+
end
|
|
265
319
|
end
|
|
266
320
|
end
|
|
267
|
-
|
|
268
|
-
end; end; end
|
|
321
|
+
end
|
data/lib/sanitize/version.rb
CHANGED