Skip to content

Commit fce5038

Browse files
authored
Merge pull request #138 from rails/flavorjones-prevent-select-style-combination
prevent combination of `select` and `style` allowed tags
2 parents 8264b72 + b28cf58 commit fce5038

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
*seyerian*
99

10+
## 1.4.3 / 2022-06-09
11+
12+
* Address a possible XSS vulnerability with certain configurations of Rails::Html::Sanitizer.
13+
14+
Prevent the combination of `select` and `style` as allowed tags in SafeListSanitizer.
15+
16+
Fixes CVE-2022-32209
17+
18+
*Mike Dalessio*
19+
1020
## 1.4.2 / 2021-08-23
1121

1222
* Slightly improve performance.

lib/rails/html/sanitizer.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,25 @@ def sanitize_css(style_string)
141141

142142
private
143143

144+
def loofah_using_html5?
145+
# future-proofing, see https://github.com/flavorjones/loofah/pull/239
146+
Loofah.respond_to?(:html5_mode?) && Loofah.html5_mode?
147+
end
148+
149+
def remove_safelist_tag_combinations(tags)
150+
if !loofah_using_html5? && tags.include?("select") && tags.include?("style")
151+
warn("WARNING: #{self.class}: removing 'style' from safelist, should not be combined with 'select'")
152+
tags.delete("style")
153+
end
154+
tags
155+
end
156+
144157
def allowed_tags(options)
145-
options[:tags] || self.class.allowed_tags
158+
if options[:tags]
159+
remove_safelist_tag_combinations(options[:tags])
160+
else
161+
self.class.allowed_tags
162+
end
146163
end
147164

148165
def allowed_attributes(options)

test/sanitizer_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,25 @@ def test_exclude_node_type_comment
587587
assert_equal("<div>text</div><b>text</b>", safe_list_sanitize("<div>text</div><!-- comment --><b>text</b>"))
588588
end
589589

590+
def test_disallow_the_dangerous_safelist_combination_of_select_and_style
591+
input = "<select><style><script>alert(1)</script></style></select>"
592+
tags = ["select", "style"]
593+
warning = /WARNING: Rails::Html::SafeListSanitizer: removing 'style' from safelist/
594+
sanitized = nil
595+
invocation = Proc.new { sanitized = safe_list_sanitize(input, tags: tags) }
596+
597+
if html5_mode?
598+
# if Loofah is using an HTML5 parser,
599+
# then "style" should be removed by the parser as an invalid child of "select"
600+
assert_silent(&invocation)
601+
else
602+
# if Loofah is using an HTML4 parser,
603+
# then SafeListSanitizer should remove "style" from the safelist
604+
assert_output(nil, warning, &invocation)
605+
end
606+
refute_includes(sanitized, "style")
607+
end
608+
590609
protected
591610

592611
def xpath_sanitize(input, options = {})
@@ -647,4 +666,8 @@ def convert_to_css_hex(string, escape_parens=false)
647666
def libxml_2_9_14_recovery?
648667
Nokogiri.method(:uses_libxml?).arity == -1 && Nokogiri.uses_libxml?(">= 2.9.14")
649668
end
669+
670+
def html5_mode?
671+
::Loofah.respond_to?(:html5_mode?) && ::Loofah.html5_mode?
672+
end
650673
end

0 commit comments

Comments
 (0)