@@ -90,7 +90,6 @@ def test_titleize
90
90
reverse : nil ,
91
91
rstrip : nil ,
92
92
scrub : nil ,
93
- slice : "foo" ,
94
93
squeeze : nil ,
95
94
strip : nil ,
96
95
sub : [ "foo" , "bar" ] ,
@@ -212,28 +211,59 @@ def test_titleize
212
211
end
213
212
214
213
test "Should continue unsafe on slice" do
215
- x = "foo" . html_safe . gsub! ( "f" , '<script>alert("lolpwnd");</script>' )
214
+ safe_string = "foo" . html_safe . gsub! ( "f" , '<script>alert("lolpwnd");</script>' )
216
215
217
216
# calling gsub! makes the dirty flag true
218
- assert_not x . html_safe? , "should not be safe"
219
-
220
- # getting a slice of it
221
- y = x [ 0 ..-1 ]
217
+ assert_not safe_string . html_safe? , "should not be safe"
222
218
223
219
# should still be unsafe
224
- assert_not y . html_safe? , "should not be safe"
220
+ assert_not safe_string [ 0 ..-1 ] . html_safe? , "should not be safe"
221
+ assert_not safe_string . slice ( 0 ..-1 ) . html_safe? , "should not be safe"
222
+ assert_not safe_string . slice! ( 0 ..-1 ) . html_safe? , "should not be safe"
223
+ # even after slice! safe_string is still unsafe
224
+ assert_not safe_string . html_safe? , "should not be safe"
225
225
end
226
226
227
227
test "Should continue safe on slice" do
228
- x = "<div>foo</div>" . html_safe
229
-
230
- assert_predicate x , :html_safe?
228
+ safe_string = "<div>foo</div>" . html_safe
231
229
232
- # getting a slice of it
233
- y = x [ 0 ..-1 ]
230
+ assert_predicate safe_string , :html_safe?
234
231
235
232
# should still be safe
236
- assert_predicate y , :html_safe?
233
+ assert_predicate safe_string [ 0 ..-1 ] , :html_safe?
234
+ assert_predicate safe_string . slice ( 0 ..-1 ) , :html_safe?
235
+ assert_predicate safe_string . slice! ( 0 ...1 ) , :html_safe?
236
+
237
+ # even after slice! safe_string is still safe
238
+ assert_predicate safe_string , :html_safe?
239
+ end
240
+
241
+ test "Should continue safe on chr" do
242
+ safe_string = "<div>foo</div>" . html_safe
243
+
244
+ assert_predicate safe_string , :html_safe?
245
+ assert_predicate safe_string . chr , :html_safe?
246
+ end
247
+
248
+ test "Should continue unsafe on chr" do
249
+ safe_string = "<div>foo</div>"
250
+
251
+ assert_not safe_string . html_safe? , "should not be safe"
252
+ assert_not safe_string . chr . html_safe? , "should not be safe"
253
+ end
254
+
255
+ test "Should return a SafeBuffer on slice! if original value was safe" do
256
+ safe_string = "<div>foo</div>" . html_safe
257
+
258
+ assert safe_string . slice! ( 0 ...1 ) . is_a? ( ActiveSupport ::SafeBuffer )
259
+ end
260
+
261
+ test "Should return a String on slice! if original value was not safe" do
262
+ unsafe_string = +'<script>alert("XSS");</script>'
263
+
264
+ sliced_string = unsafe_string . slice! ( 0 ...1 )
265
+ assert_not sliced_string . is_a? ( ActiveSupport ::SafeBuffer )
266
+ assert sliced_string . is_a? ( String )
237
267
end
238
268
239
269
test "Should work with interpolation (array argument)" do
0 commit comments