Skip to content

Commit 18f2f2c

Browse files
committedJun 9, 2022
test: finally use the CSS hex encoding originally intended
This was mis-fixed in c190b32 which encoded the Ruby strings as unicode to fix the previous bad encoding which dated back to the original Instiki that should have single-quoted the CSS unicode strings.
1 parent c86fed1 commit 18f2f2c

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed
 

‎test/sanitizer_test.rb

+30-2
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,25 @@ def test_should_sanitize_img_dynsrc_lowsrc
414414
end
415415

416416
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
419436
end
420437

421438
def test_should_sanitize_div_style_expression
@@ -574,4 +591,15 @@ def scope_allowed_attributes(attributes)
574591
ensure
575592
Rails::Html::SafeListSanitizer.allowed_attributes = old_attributes
576593
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
577605
end

0 commit comments

Comments
 (0)
Please sign in to comment.