@@ -152,7 +152,7 @@ def test_sanitize_plaintext
152
152
end
153
153
154
154
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"
156
156
end
157
157
158
158
def test_sanitize_js_handlers
@@ -173,17 +173,23 @@ def test_sanitize_image_src
173
173
tags = Loofah ::HTML5 ::WhiteList ::ALLOWED_ELEMENTS - %w( script form )
174
174
tags . each do |tag_name |
175
175
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
177
179
end
178
180
end
179
181
180
182
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>)
182
184
end
183
185
184
186
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
187
193
end
188
194
189
195
# RFC 3986, sec 4.2
@@ -309,7 +315,7 @@ def test_should_block_script_tag
309
315
end
310
316
311
317
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")" >)
313
319
end
314
320
315
321
[ %(<IMG SRC="javascript:alert('XSS');">) ,
@@ -453,6 +459,16 @@ def test_sanitize_ascii_8bit_string
453
459
end
454
460
end
455
461
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
+
456
472
protected
457
473
458
474
def xpath_sanitize ( input , options = { } )
@@ -484,18 +500,18 @@ def sanitize_css(input)
484
500
end
485
501
486
502
def scope_allowed_tags ( tags )
503
+ old_tags = Rails ::Html ::WhiteListSanitizer . allowed_tags
487
504
Rails ::Html ::WhiteListSanitizer . allowed_tags = tags
488
505
yield Rails ::Html ::WhiteListSanitizer . new
489
-
490
506
ensure
491
- Rails ::Html ::WhiteListSanitizer . allowed_tags = nil
507
+ Rails ::Html ::WhiteListSanitizer . allowed_tags = old_tags
492
508
end
493
509
494
510
def scope_allowed_attributes ( attributes )
511
+ old_attributes = Rails ::Html ::WhiteListSanitizer . allowed_attributes
495
512
Rails ::Html ::WhiteListSanitizer . allowed_attributes = attributes
496
513
yield Rails ::Html ::WhiteListSanitizer . new
497
-
498
514
ensure
499
- Rails ::Html ::WhiteListSanitizer . allowed_attributes = nil
515
+ Rails ::Html ::WhiteListSanitizer . allowed_attributes = old_attributes
500
516
end
501
517
end
0 commit comments