@@ -152,7 +152,7 @@ def test_sanitize_plaintext
152152 end
153153
154154 def test_sanitize_script
155- assert_sanitized "a b c<script language=\" Javascript\" >blah blah blah</script>d e f" , "a b cd e f"
155+ assert_sanitized "a b c<script language=\" Javascript\" >blah blah blah</script>d e f" , "a b cblah blah blahd e f"
156156 end
157157
158158 def test_sanitize_js_handlers
@@ -173,17 +173,23 @@ def test_sanitize_image_src
173173 tags = Loofah ::HTML5 ::WhiteList ::ALLOWED_ELEMENTS - %w( script form )
174174 tags . each do |tag_name |
175175 define_method "test_should_allow_#{ tag_name } _tag" do
176- assert_sanitized "start <#{ tag_name } title=\" 1\" onclick=\" foo\" >foo <bad>bar</bad> baz</#{ tag_name } > end" , %(start <#{ tag_name } title="1">foo bar baz</#{ tag_name } > end)
176+ scope_allowed_tags ( tags ) do
177+ assert_sanitized "start <#{ tag_name } title=\" 1\" onclick=\" foo\" >foo <bad>bar</bad> baz</#{ tag_name } > end" , %(start <#{ tag_name } title="1">foo bar baz</#{ tag_name } > end)
178+ end
177179 end
178180 end
179181
180182 def test_should_allow_anchors
181- assert_sanitized %(<a href="foo" onclick="bar"><script>baz</script></a>) , %(<a href=\" foo\" ></a>)
183+ assert_sanitized %(<a href="foo" onclick="bar"><script>baz</script></a>) , %(<a href=\" foo\" >baz </a>)
182184 end
183185
184186 def test_video_poster_sanitization
185- assert_sanitized %(<video src="videofile.ogg" autoplay poster="posterimage.jpg"></video>) , %(<video src="videofile.ogg" poster="posterimage.jpg"></video>)
186- assert_sanitized %(<video src="videofile.ogg" poster=javascript:alert(1)></video>) , %(<video src="videofile.ogg"></video>)
187+ scope_allowed_tags ( %w( video ) ) do
188+ scope_allowed_attributes %w( src poster ) do
189+ assert_sanitized %(<video src="videofile.ogg" autoplay poster="posterimage.jpg"></video>) , %(<video src="videofile.ogg" poster="posterimage.jpg"></video>)
190+ assert_sanitized %(<video src="videofile.ogg" poster=javascript:alert(1)></video>) , %(<video src="videofile.ogg"></video>)
191+ end
192+ end
187193 end
188194
189195 # RFC 3986, sec 4.2
@@ -309,7 +315,7 @@ def test_should_block_script_tag
309315 end
310316
311317 def test_should_not_fall_for_xss_image_hack_with_uppercase_tags
312- assert_sanitized %(<IMG """><SCRIPT>alert("XSS")</SCRIPT>">) , " <img>\" >"
318+ assert_sanitized %(<IMG """><SCRIPT>alert("XSS")</SCRIPT>">) , %( <img>alert("XSS")" >)
313319 end
314320
315321 [ %(<IMG SRC="javascript:alert('XSS');">) ,
@@ -453,6 +459,16 @@ def test_sanitize_ascii_8bit_string
453459 end
454460 end
455461
462+ def test_sanitize_data_attributes
463+ assert_sanitized %(<a href="/blah" data-method="post">foo</a>) , %(<a href="/blah">foo</a>)
464+ assert_sanitized %(<a data-remote="true" data-type="script" data-method="get" data-cross-domain="true" href="attack.js">Launch the missiles</a>) , %(<a href="attack.js">Launch the missiles</a>)
465+ end
466+
467+ def test_allow_data_attribute_if_requested
468+ text = %(<a data-foo="foo">foo</a>)
469+ assert_equal %(<a data-foo="foo">foo</a>) , white_list_sanitize ( text , attributes : [ 'data-foo' ] )
470+ end
471+
456472protected
457473
458474 def xpath_sanitize ( input , options = { } )
@@ -484,18 +500,18 @@ def sanitize_css(input)
484500 end
485501
486502 def scope_allowed_tags ( tags )
503+ old_tags = Rails ::Html ::WhiteListSanitizer . allowed_tags
487504 Rails ::Html ::WhiteListSanitizer . allowed_tags = tags
488505 yield Rails ::Html ::WhiteListSanitizer . new
489-
490506 ensure
491- Rails ::Html ::WhiteListSanitizer . allowed_tags = nil
507+ Rails ::Html ::WhiteListSanitizer . allowed_tags = old_tags
492508 end
493509
494510 def scope_allowed_attributes ( attributes )
511+ old_attributes = Rails ::Html ::WhiteListSanitizer . allowed_attributes
495512 Rails ::Html ::WhiteListSanitizer . allowed_attributes = attributes
496513 yield Rails ::Html ::WhiteListSanitizer . new
497-
498514 ensure
499- Rails ::Html ::WhiteListSanitizer . allowed_attributes = nil
515+ Rails ::Html ::WhiteListSanitizer . allowed_attributes = old_attributes
500516 end
501517end
0 commit comments