Skip to content

Commit 11ee440

Browse files
authored
Adjust data: URI mediatype tests for loofah 2.25.2 (#222)
Loofah v2.25.2 will parse `data:` URI mediatypes following the WHATWG `data:` URL spec, so an omitted or invalid mediatype will resolve to `text/plain` and thus will be allowed, matching how a browser reads it. This gem supports `loofah ~> 2.25`, so the tests branch on `Loofah::VERSION` and pass against both old and new loofah. ref: flavorjones/loofah#305 ref: https://fetch.spec.whatwg.org/#data-urls
1 parent bc9622c commit 11ee440

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

test/sanitizer_test.rb

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -846,16 +846,59 @@ def test_mediatype_image_svg_xml_disallowed
846846
assert_equal(expected, actual)
847847
end
848848

849-
def test_mediatype_other_disallowed
850-
input = '<a href="data:foo;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
851-
expected = "<a>foo</a>"
852-
actual = safe_list_sanitize(input)
853-
assert_equal(expected, actual)
849+
# Loofah 2.25.2 changed the behavior of `data:` URLs with invalid mediatypes to be treated as
850+
# text/plain, which is an allowed media type. So we need to adjust the test accordingly.
851+
if Gem::Version.new(Loofah::VERSION) <= Gem::Version.new("2.25.1")
852+
def test_mediatype_other_disallowed
853+
input = '<a href="data:foo;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
854+
expected = "<a>foo</a>"
855+
actual = safe_list_sanitize(input)
856+
assert_equal(expected, actual)
854857

855-
input = '<a href="DATA:foo;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
856-
expected = "<a>foo</a>"
857-
actual = safe_list_sanitize(input)
858-
assert_equal(expected, actual)
858+
input = '<a href="DATA:foo;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
859+
expected = "<a>foo</a>"
860+
actual = safe_list_sanitize(input)
861+
assert_equal(expected, actual)
862+
end
863+
else
864+
def test_mediatype_other_disallowed
865+
input = '<a href="data:foo/bar;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
866+
expected = "<a>foo</a>"
867+
actual = safe_list_sanitize(input)
868+
assert_equal(expected, actual)
869+
870+
input = '<a href="DATA:foo/bar;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
871+
expected = "<a>foo</a>"
872+
actual = safe_list_sanitize(input)
873+
assert_equal(expected, actual)
874+
end
875+
876+
def test_mediatype_missing_treated_as_text_plain
877+
# https://www.rfc-editor.org/rfc/rfc2397.html and https://fetch.spec.whatwg.org/#data-urls
878+
# state browsers should treat this as text/plain, so we can allow it.
879+
input = '<a href="data:,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
880+
expected = input
881+
actual = safe_list_sanitize(input)
882+
assert_equal(expected, actual)
883+
884+
input = '<a href="DATA:,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
885+
expected = input
886+
actual = safe_list_sanitize(input)
887+
assert_equal(expected, actual)
888+
end
889+
890+
def test_mediatype_invalid_treated_as_text_plain
891+
# https://fetch.spec.whatwg.org/#data-urls states browsers should treat this as text/plain, so we can allow it.
892+
input = '<a href="data:foo;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
893+
expected = input
894+
actual = safe_list_sanitize(input)
895+
assert_equal(expected, actual)
896+
897+
input = '<a href="DATA:foo;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4=">foo</a>'
898+
expected = input
899+
actual = safe_list_sanitize(input)
900+
assert_equal(expected, actual)
901+
end
859902
end
860903

861904
def test_scrubbing_svg_attr_values_that_allow_ref

0 commit comments

Comments
 (0)