@@ -553,7 +553,16 @@ def test_should_sanitize_unclosed_script
553
553
end
554
554
555
555
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 )
557
566
end
558
567
559
568
def test_should_not_fall_for_ridiculous_hack
@@ -562,10 +571,18 @@ def test_should_not_fall_for_ridiculous_hack
562
571
end
563
572
564
573
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
567
578
%(<span title="'><script>alert()</script>">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 )
569
586
end
570
587
571
588
def test_should_sanitize_invalid_tag_names
@@ -577,7 +594,16 @@ def test_should_sanitize_non_alpha_and_non_digit_characters_in_tags
577
594
end
578
595
579
596
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 )
581
607
end
582
608
583
609
def test_should_sanitize_img_dynsrc_lowsrc
@@ -841,66 +867,106 @@ def test_scrubbing_svg_attr_values_that_allow_ref
841
867
842
868
def test_style_with_css_payload
843
869
input , tags = "<style>div > span { background: \" red\" ; }</style>" , [ "style" ]
844
- expected = "<style>div > span { background: \" red\" ; }</style>"
845
870
actual = safe_list_sanitize ( input , tags : tags )
871
+ acceptable_results = [
872
+ # libxml2
873
+ "<style>div > span { background: \" red\" ; }</style>" ,
874
+ # libgumbo
875
+ "<style>div > span { background: \" red\" ; }</style>" ,
876
+ ]
846
877
847
- assert_equal ( expected , actual )
878
+ assert_includes ( acceptable_results , actual )
848
879
end
849
880
850
881
def test_combination_of_select_and_style_with_css_payload
851
882
input , tags = "<select><style>div > span { background: \" red\" ; }</style></select>" , [ "select" , "style" ]
852
- expected = "<select><style>div > span { background: \" red\" ; }</style></select>"
853
883
actual = safe_list_sanitize ( input , tags : tags )
884
+ acceptable_results = [
885
+ # libxml2
886
+ "<select><style>div > span { background: \" red\" ; }</style></select>" ,
887
+ # libgumbo
888
+ "<select>div > span { background: \" red\" ; }</select>" ,
889
+ ]
854
890
855
- assert_equal ( expected , actual )
891
+ assert_includes ( acceptable_results , actual )
856
892
end
857
893
858
894
def test_combination_of_select_and_style_with_script_payload
859
895
input , tags = "<select><style><script>alert(1)</script></style></select>" , [ "select" , "style" ]
860
- expected = "<select><style><script>alert(1)</script></style></select>"
861
896
actual = safe_list_sanitize ( input , tags : tags )
897
+ acceptable_results = [
898
+ # libxml2
899
+ "<select><style><script>alert(1)</script></style></select>" ,
900
+ # libgumbo
901
+ "<select>alert(1)</select>" ,
902
+ ]
862
903
863
- assert_equal ( expected , actual )
904
+ assert_includes ( acceptable_results , actual )
864
905
end
865
906
866
907
def test_combination_of_svg_and_style_with_script_payload
867
908
input , tags = "<svg><style><script>alert(1)</script></style></svg>" , [ "svg" , "style" ]
868
- expected = "<svg><style><script>alert(1)</script></style></svg>"
869
909
actual = safe_list_sanitize ( input , tags : tags )
910
+ acceptable_results = [
911
+ # libxml2
912
+ "<svg><style><script>alert(1)</script></style></svg>" ,
913
+ # libgumbo
914
+ "<svg><style>alert(1)</style></svg>"
915
+ ]
870
916
871
- assert_equal ( expected , actual )
917
+ assert_includes ( acceptable_results , actual )
872
918
end
873
919
874
920
def test_combination_of_math_and_style_with_img_payload
875
921
input , tags = "<math><style><img src=x onerror=alert(1)></style></math>" , [ "math" , "style" ]
876
- expected = "<math><style><img src=x onerror=alert(1)></style></math>"
877
922
actual = safe_list_sanitize ( input , tags : tags )
923
+ acceptable_results = [
924
+ # libxml2
925
+ "<math><style><img src=x onerror=alert(1)></style></math>" ,
926
+ # libgumbo
927
+ "<math><style></style></math>" ,
928
+ ]
878
929
879
- assert_equal ( expected , actual )
930
+ assert_includes ( acceptable_results , actual )
880
931
end
881
932
882
933
def test_combination_of_math_and_style_with_img_payload_2
883
934
input , tags = "<math><style><img src=x onerror=alert(1)></style></math>" , [ "math" , "style" , "img" ]
884
- expected = "<math><style><img src=x onerror=alert(1)></style></math>"
885
935
actual = safe_list_sanitize ( input , tags : tags )
936
+ acceptable_results = [
937
+ # libxml2
938
+ "<math><style><img src=x onerror=alert(1)></style></math>" ,
939
+ # libgumbo
940
+ "<math><style></style></math><img src=\" x\" >" ,
941
+ ]
886
942
887
- assert_equal ( expected , actual )
943
+ assert_includes ( acceptable_results , actual )
888
944
end
889
945
890
946
def test_combination_of_svg_and_style_with_img_payload
891
947
input , tags = "<svg><style><img src=x onerror=alert(1)></style></svg>" , [ "svg" , "style" ]
892
- expected = "<svg><style><img src=x onerror=alert(1)></style></svg>"
893
948
actual = safe_list_sanitize ( input , tags : tags )
949
+ acceptable_results = [
950
+ # libxml2
951
+ "<svg><style><img src=x onerror=alert(1)></style></svg>" ,
952
+ # libgumbo
953
+ "<svg><style></style></svg>" ,
954
+ ]
894
955
895
- assert_equal ( expected , actual )
956
+ assert_includes ( acceptable_results , actual )
896
957
end
897
958
898
959
def test_combination_of_svg_and_style_with_img_payload_2
899
960
input , tags = "<svg><style><img src=x onerror=alert(1)></style></svg>" , [ "svg" , "style" , "img" ]
900
- expected = "<svg><style><img src=x onerror=alert(1)></style></svg>"
901
961
actual = safe_list_sanitize ( input , tags : tags )
962
+ acceptable_results = [
963
+ # libxml2
964
+ "<svg><style><img src=x onerror=alert(1)></style></svg>" ,
965
+ # libgumbo
966
+ "<svg><style></style></svg><img src=\" x\" >" ,
967
+ ]
902
968
903
- assert_equal ( expected , actual )
969
+ assert_includes ( acceptable_results , actual )
904
970
end
905
971
906
972
def test_should_sanitize_illegal_style_properties
0 commit comments