Skip to content

Commit 3a5174b

Browse files
committed
Adjust data: URI mediatype tests for loofah 2.25.2
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 3a5174b

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