Skip to content

Commit 53f7409

Browse files
committed
test: fix tests to accommodate HTML5 parser behavior
This feels pretty good, not gonna lie. The majority of tests that needed to change were the ones related to the CDATA node issues: https://github.com/flavorjones/loofah/blob/main/docs/2022-10-decision-on-cdata-nodes.md and I'm happy to see everything working as expected.
1 parent ffb32dc commit 53f7409

File tree

1 file changed

+87
-21
lines changed

1 file changed

+87
-21
lines changed

test/sanitizer_test.rb

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,16 @@ def test_should_sanitize_unclosed_script
553553
end
554554

555555
def test_should_sanitize_half_open_scripts
556-
assert_sanitized %(<IMG SRC="javascript:alert('XSS')"), "<img>"
556+
input = %(<IMG SRC="javascript:alert('XSS')")
557+
result = safe_list_sanitize(input)
558+
acceptable_results = [
559+
# libxml2
560+
"<img>",
561+
# libgumbo
562+
"",
563+
]
564+
565+
assert_includes(acceptable_results, result)
557566
end
558567

559568
def test_should_not_fall_for_ridiculous_hack
@@ -562,10 +571,18 @@ def test_should_not_fall_for_ridiculous_hack
562571
end
563572

564573
def test_should_sanitize_attributes
565-
assert_sanitized(
566-
%(<SPAN title="'><script>alert()</script>">blah</SPAN>),
574+
input = %(<SPAN title="'><script>alert()</script>">blah</SPAN>)
575+
result = safe_list_sanitize(input)
576+
acceptable_results = [
577+
# libxml2
567578
%(<span title="'&gt;&lt;script&gt;alert()&lt;/script&gt;">blah</span>),
568-
)
579+
# libgumbo
580+
# this looks scary, but it's fine. for a more detailed analysis check out:
581+
# https://github.com/discourse/discourse/pull/21522#issuecomment-1545697968
582+
%(<span title="'><script>alert()</script>">blah</span>)
583+
]
584+
585+
assert_includes(acceptable_results, result)
569586
end
570587

571588
def test_should_sanitize_invalid_tag_names
@@ -577,7 +594,16 @@ def test_should_sanitize_non_alpha_and_non_digit_characters_in_tags
577594
end
578595

579596
def test_should_sanitize_invalid_tag_names_in_single_tags
580-
assert_sanitized('<img/src="http://ha.ckers.org/xss.js"/>', "<img>")
597+
input = %(<img/src="http://ha.ckers.org/xss.js"/>)
598+
result = safe_list_sanitize(input)
599+
acceptable_results = [
600+
# libxml2
601+
"<img>",
602+
# libgumbo
603+
%(<img src="http://ha.ckers.org/xss.js">),
604+
]
605+
606+
assert_includes(acceptable_results, result)
581607
end
582608

583609
def test_should_sanitize_img_dynsrc_lowsrc
@@ -841,66 +867,106 @@ def test_scrubbing_svg_attr_values_that_allow_ref
841867

842868
def test_style_with_css_payload
843869
input, tags = "<style>div > span { background: \"red\"; }</style>", ["style"]
844-
expected = "<style>div &gt; span { background: \"red\"; }</style>"
845870
actual = safe_list_sanitize(input, tags: tags)
871+
acceptable_results = [
872+
# libxml2
873+
"<style>div &gt; span { background: \"red\"; }</style>",
874+
# libgumbo
875+
"<style>div > span { background: \"red\"; }</style>",
876+
]
846877

847-
assert_equal(expected, actual)
878+
assert_includes(acceptable_results, actual)
848879
end
849880

850881
def test_combination_of_select_and_style_with_css_payload
851882
input, tags = "<select><style>div > span { background: \"red\"; }</style></select>", ["select", "style"]
852-
expected = "<select><style>div &gt; span { background: \"red\"; }</style></select>"
853883
actual = safe_list_sanitize(input, tags: tags)
884+
acceptable_results = [
885+
# libxml2
886+
"<select><style>div &gt; span { background: \"red\"; }</style></select>",
887+
# libgumbo
888+
"<select>div &gt; span { background: \"red\"; }</select>",
889+
]
854890

855-
assert_equal(expected, actual)
891+
assert_includes(acceptable_results, actual)
856892
end
857893

858894
def test_combination_of_select_and_style_with_script_payload
859895
input, tags = "<select><style><script>alert(1)</script></style></select>", ["select", "style"]
860-
expected = "<select><style>&lt;script&gt;alert(1)&lt;/script&gt;</style></select>"
861896
actual = safe_list_sanitize(input, tags: tags)
897+
acceptable_results = [
898+
# libxml2
899+
"<select><style>&lt;script&gt;alert(1)&lt;/script&gt;</style></select>",
900+
# libgumbo
901+
"<select>alert(1)</select>",
902+
]
862903

863-
assert_equal(expected, actual)
904+
assert_includes(acceptable_results, actual)
864905
end
865906

866907
def test_combination_of_svg_and_style_with_script_payload
867908
input, tags = "<svg><style><script>alert(1)</script></style></svg>", ["svg", "style"]
868-
expected = "<svg><style>&lt;script&gt;alert(1)&lt;/script&gt;</style></svg>"
869909
actual = safe_list_sanitize(input, tags: tags)
910+
acceptable_results = [
911+
# libxml2
912+
"<svg><style>&lt;script&gt;alert(1)&lt;/script&gt;</style></svg>",
913+
# libgumbo
914+
"<svg><style>alert(1)</style></svg>"
915+
]
870916

871-
assert_equal(expected, actual)
917+
assert_includes(acceptable_results, actual)
872918
end
873919

874920
def test_combination_of_math_and_style_with_img_payload
875921
input, tags = "<math><style><img src=x onerror=alert(1)></style></math>", ["math", "style"]
876-
expected = "<math><style>&lt;img src=x onerror=alert(1)&gt;</style></math>"
877922
actual = safe_list_sanitize(input, tags: tags)
923+
acceptable_results = [
924+
# libxml2
925+
"<math><style>&lt;img src=x onerror=alert(1)&gt;</style></math>",
926+
# libgumbo
927+
"<math><style></style></math>",
928+
]
878929

879-
assert_equal(expected, actual)
930+
assert_includes(acceptable_results, actual)
880931
end
881932

882933
def test_combination_of_math_and_style_with_img_payload_2
883934
input, tags = "<math><style><img src=x onerror=alert(1)></style></math>", ["math", "style", "img"]
884-
expected = "<math><style>&lt;img src=x onerror=alert(1)&gt;</style></math>"
885935
actual = safe_list_sanitize(input, tags: tags)
936+
acceptable_results = [
937+
# libxml2
938+
"<math><style>&lt;img src=x onerror=alert(1)&gt;</style></math>",
939+
# libgumbo
940+
"<math><style></style></math><img src=\"x\">",
941+
]
886942

887-
assert_equal(expected, actual)
943+
assert_includes(acceptable_results, actual)
888944
end
889945

890946
def test_combination_of_svg_and_style_with_img_payload
891947
input, tags = "<svg><style><img src=x onerror=alert(1)></style></svg>", ["svg", "style"]
892-
expected = "<svg><style>&lt;img src=x onerror=alert(1)&gt;</style></svg>"
893948
actual = safe_list_sanitize(input, tags: tags)
949+
acceptable_results = [
950+
# libxml2
951+
"<svg><style>&lt;img src=x onerror=alert(1)&gt;</style></svg>",
952+
# libgumbo
953+
"<svg><style></style></svg>",
954+
]
894955

895-
assert_equal(expected, actual)
956+
assert_includes(acceptable_results, actual)
896957
end
897958

898959
def test_combination_of_svg_and_style_with_img_payload_2
899960
input, tags = "<svg><style><img src=x onerror=alert(1)></style></svg>", ["svg", "style", "img"]
900-
expected = "<svg><style>&lt;img src=x onerror=alert(1)&gt;</style></svg>"
901961
actual = safe_list_sanitize(input, tags: tags)
962+
acceptable_results = [
963+
# libxml2
964+
"<svg><style>&lt;img src=x onerror=alert(1)&gt;</style></svg>",
965+
# libgumbo
966+
"<svg><style></style></svg><img src=\"x\">",
967+
]
902968

903-
assert_equal(expected, actual)
969+
assert_includes(acceptable_results, actual)
904970
end
905971

906972
def test_should_sanitize_illegal_style_properties

0 commit comments

Comments
 (0)