@@ -414,8 +414,25 @@ def test_should_sanitize_img_dynsrc_lowsrc
414
414
end
415
415
416
416
def test_should_sanitize_div_background_image_unicode_encoded
417
- raw = %(background-image:\u0075 \u0072 \u006C \u0028 \u0027 \u006a \u0061 \u0076 \u0061 \u0073 \u0063 \u0072 \u0069 \u0070 \u0074 \u003a \u0061 \u006c \u0065 \u0072 \u0074 \u0028 \u0031 \u0032 \u0033 \u0034 \u0029 \u0027 \u0029 )
418
- assert_equal '' , sanitize_css ( raw )
417
+ [
418
+ convert_to_css_hex ( "url(javascript:alert(1))" , false ) ,
419
+ convert_to_css_hex ( "url(javascript:alert(1))" , true ) ,
420
+ convert_to_css_hex ( "url(https://example.com)" , false ) ,
421
+ convert_to_css_hex ( "url(https://example.com)" , true ) ,
422
+ ] . each do |propval |
423
+ raw = "background-image:" + propval
424
+ assert_empty ( sanitize_css ( raw ) )
425
+ end
426
+ end
427
+
428
+ def test_should_allow_div_background_image_unicode_encoded_safe_functions
429
+ [
430
+ convert_to_css_hex ( "rgb(255,0,0)" , false ) ,
431
+ convert_to_css_hex ( "rgb(255,0,0)" , true ) ,
432
+ ] . each do |propval |
433
+ raw = "background-image:" + propval
434
+ assert_includes ( sanitize_css ( raw ) , "background-image" )
435
+ end
419
436
end
420
437
421
438
def test_should_sanitize_div_style_expression
@@ -574,4 +591,15 @@ def scope_allowed_attributes(attributes)
574
591
ensure
575
592
Rails ::Html ::SafeListSanitizer . allowed_attributes = old_attributes
576
593
end
594
+
595
+ # note that this is used for testing CSS hex encoding: \\[0-9a-f]{1,6}
596
+ def convert_to_css_hex ( string , escape_parens = false )
597
+ string . chars . map do |c |
598
+ if !escape_parens && ( c == "(" || c == ")" )
599
+ c
600
+ else
601
+ format ( '\00%02X' , c . ord )
602
+ end
603
+ end . join
604
+ end
577
605
end
0 commit comments