@@ -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+
590609protected
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
650673end
0 commit comments