@@ -581,6 +581,25 @@ def test_exclude_node_type_comment
581
581
assert_equal ( "<div>text</div><b>text</b>" , safe_list_sanitize ( "<div>text</div><!-- comment --><b>text</b>" ) )
582
582
end
583
583
584
+ def test_disallow_the_dangerous_safelist_combination_of_select_and_style
585
+ input = "<select><style><script>alert(1)</script></style></select>"
586
+ tags = [ "select" , "style" ]
587
+ warning = /WARNING: Rails::Html::SafeListSanitizer: removing 'style' from safelist/
588
+ sanitized = nil
589
+ invocation = Proc . new { sanitized = safe_list_sanitize ( input , tags : tags ) }
590
+
591
+ if html5_mode?
592
+ # if Loofah is using an HTML5 parser,
593
+ # then "style" should be removed by the parser as an invalid child of "select"
594
+ assert_silent ( &invocation )
595
+ else
596
+ # if Loofah is using an HTML4 parser,
597
+ # then SafeListSanitizer should remove "style" from the safelist
598
+ assert_output ( nil , warning , &invocation )
599
+ end
600
+ refute_includes ( sanitized , "style" )
601
+ end
602
+
584
603
protected
585
604
586
605
def xpath_sanitize ( input , options = { } )
@@ -641,4 +660,8 @@ def convert_to_css_hex(string, escape_parens=false)
641
660
def libxml_2_9_14_recovery?
642
661
Nokogiri . method ( :uses_libxml? ) . arity == -1 && Nokogiri . uses_libxml? ( ">= 2.9.14" )
643
662
end
663
+
664
+ def html5_mode?
665
+ ::Loofah . respond_to? ( :html5_mode? ) && ::Loofah . html5_mode?
666
+ end
644
667
end
0 commit comments