20
20
# In many other cases, it's because the parser used by Nokogiri on JRuby (xerces+nekohtml) parses
21
21
# slightly differently than libxml2 in edge cases.
22
22
#
23
- module TestRailsSanitizers
24
- class XpathRemovalTestSanitizer < Rails ::Html ::Sanitizer
25
- def sanitize ( html , options = { } )
26
- fragment = Loofah . fragment ( html )
27
- remove_xpaths ( fragment , options [ :xpaths ] ) . to_s
28
- end
23
+ module SanitizerTests
24
+ def self . loofah_html5_support?
25
+ Loofah . respond_to? ( :html5_support? ) && Loofah . html5_support?
29
26
end
30
27
31
28
class BaseSanitizerTest < Minitest ::Test
29
+ class XpathRemovalTestSanitizer < Rails ::HTML ::Sanitizer
30
+ def sanitize ( html , options = { } )
31
+ fragment = Loofah . fragment ( html )
32
+ remove_xpaths ( fragment , options [ :xpaths ] ) . to_s
33
+ end
34
+ end
35
+
32
36
def test_sanitizer_sanitize_raises_not_implemented_error
33
37
assert_raises NotImplementedError do
34
- Rails ::Html ::Sanitizer . new . sanitize ( "asdf" )
38
+ Rails ::HTML ::Sanitizer . new . sanitize ( "asdf" )
35
39
end
36
40
end
37
41
@@ -65,7 +69,15 @@ def xpath_sanitize(input, options = {})
65
69
end
66
70
end
67
71
68
- class FullSanitizerTest < Minitest ::Test
72
+ module ModuleUnderTest
73
+ def module_under_test
74
+ self . class . instance_variable_get ( :@module_under_test )
75
+ end
76
+ end
77
+
78
+ module FullSanitizerTest
79
+ include ModuleUnderTest
80
+
69
81
def test_strip_tags_with_quote
70
82
input = '<" <img src="trollface.gif" onload="alert(1)"> hi'
71
83
result = full_sanitize ( input )
@@ -164,11 +176,23 @@ def test_full_sanitize_respect_html_escaping_of_the_given_string
164
176
165
177
protected
166
178
def full_sanitize ( input , options = { } )
167
- Rails :: Html ::FullSanitizer . new . sanitize ( input , options )
179
+ module_under_test ::FullSanitizer . new . sanitize ( input , options )
168
180
end
169
181
end
170
182
171
- class LinkSanitizerTest < Minitest ::Test
183
+ class HTML4FullSanitizerTest < Minitest ::Test
184
+ @module_under_test = Rails ::HTML4
185
+ include FullSanitizerTest
186
+ end
187
+
188
+ class HTML5FullSanitizerTest < Minitest ::Test
189
+ @module_under_test = Rails ::HTML5
190
+ include FullSanitizerTest
191
+ end if loofah_html5_support?
192
+
193
+ module LinkSanitizerTest
194
+ include ModuleUnderTest
195
+
172
196
def test_strip_links_with_tags_in_tags
173
197
expected = "<a href='hello'>all <b>day</b> long</a>"
174
198
input = "<<a>a href='hello'>all <b>day</b> long<</A>/a>"
@@ -201,11 +225,23 @@ def test_strip_links_with_linkception
201
225
202
226
protected
203
227
def link_sanitize ( input , options = { } )
204
- Rails :: Html ::LinkSanitizer . new . sanitize ( input , options )
228
+ module_under_test ::LinkSanitizer . new . sanitize ( input , options )
205
229
end
206
230
end
207
231
208
- class SafeListSanitizerTest < Minitest ::Test
232
+ class HTML4LinkSanitizerTest < Minitest ::Test
233
+ @module_under_test = Rails ::HTML4
234
+ include LinkSanitizerTest
235
+ end
236
+
237
+ class HTML5LinkSanitizerTest < Minitest ::Test
238
+ @module_under_test = Rails ::HTML5
239
+ include LinkSanitizerTest
240
+ end if loofah_html5_support?
241
+
242
+ module SafeListSanitizerTest
243
+ include ModuleUnderTest
244
+
209
245
def test_sanitize_nested_script
210
246
assert_equal '<script>alert("XSS");</script>' , safe_list_sanitize ( '<script><script></script>alert("XSS");<script><</script>/</script><script>script></script>' , tags : %w( em ) )
211
247
end
@@ -369,7 +405,7 @@ def test_custom_attributes_overrides_allowed_attributes
369
405
end
370
406
371
407
def test_should_allow_prune
372
- sanitizer = Rails :: Html ::SafeListSanitizer . new ( prune : true )
408
+ sanitizer = module_under_test ::SafeListSanitizer . new ( prune : true )
373
409
text = "<u>leave me <b>now</b></u>"
374
410
assert_equal "<u>leave me </u>" , sanitizer . sanitize ( text , tags : %w( u ) )
375
411
end
@@ -919,31 +955,31 @@ def test_should_sanitize_across_newlines
919
955
920
956
protected
921
957
def safe_list_sanitize ( input , options = { } )
922
- Rails :: Html ::SafeListSanitizer . new . sanitize ( input , options )
958
+ module_under_test ::SafeListSanitizer . new . sanitize ( input , options )
923
959
end
924
960
925
961
def assert_sanitized ( input , expected = nil )
926
962
assert_equal ( ( expected || input ) , safe_list_sanitize ( input ) )
927
963
end
928
964
929
965
def scope_allowed_tags ( tags )
930
- old_tags = Rails :: Html ::SafeListSanitizer . allowed_tags
931
- Rails :: Html ::SafeListSanitizer . allowed_tags = tags
932
- yield Rails :: Html ::SafeListSanitizer . new
966
+ old_tags = module_under_test ::SafeListSanitizer . allowed_tags
967
+ module_under_test ::SafeListSanitizer . allowed_tags = tags
968
+ yield module_under_test ::SafeListSanitizer . new
933
969
ensure
934
- Rails :: Html ::SafeListSanitizer . allowed_tags = old_tags
970
+ module_under_test ::SafeListSanitizer . allowed_tags = old_tags
935
971
end
936
972
937
973
def scope_allowed_attributes ( attributes )
938
- old_attributes = Rails :: Html ::SafeListSanitizer . allowed_attributes
939
- Rails :: Html ::SafeListSanitizer . allowed_attributes = attributes
940
- yield Rails :: Html ::SafeListSanitizer . new
974
+ old_attributes = module_under_test ::SafeListSanitizer . allowed_attributes
975
+ module_under_test ::SafeListSanitizer . allowed_attributes = attributes
976
+ yield module_under_test ::SafeListSanitizer . new
941
977
ensure
942
- Rails :: Html ::SafeListSanitizer . allowed_attributes = old_attributes
978
+ module_under_test ::SafeListSanitizer . allowed_attributes = old_attributes
943
979
end
944
980
945
981
def sanitize_css ( input )
946
- Rails :: HTML4 ::SafeListSanitizer . new . sanitize_css ( input )
982
+ module_under_test ::SafeListSanitizer . new . sanitize_css ( input )
947
983
end
948
984
949
985
# note that this is used for testing CSS hex encoding: \\[0-9a-f]{1,6}
@@ -957,4 +993,14 @@ def convert_to_css_hex(string, escape_parens = false)
957
993
end . join
958
994
end
959
995
end
996
+
997
+ class HTML4SafeListSanitizerTest < Minitest ::Test
998
+ @module_under_test = Rails ::HTML4
999
+ include SafeListSanitizerTest
1000
+ end
1001
+
1002
+ class HTML5SafeListSanitizerTest < Minitest ::Test
1003
+ @module_under_test = Rails ::HTML5
1004
+ include SafeListSanitizerTest
1005
+ end if loofah_html5_support?
960
1006
end
0 commit comments