Skip to content

Commit e46ad49

Browse files
committed
add tests for surfacing issue, check if passed text is a range and if so change to string
1 parent b1b3398 commit e46ad49

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/rails/html/sanitizer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Concern
3434
module ComposedSanitize
3535
def sanitize(html, options = {})
3636
return unless html
37+
html = html.instance_of?(Range) ? html.to_s : html
3738
return html if html.empty?
3839

3940
serialize(scrub(parse_fragment(html), options))

test/sanitizer_test.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def test_strip_cdata
137137
assert_includes(acceptable_results, result)
138138
end
139139

140+
def test_strip_passed_passed_duck_typed_range
141+
input = 2000..2005
142+
result = full_sanitize(input)
143+
acceptable_results = [
144+
"2000..2005",
145+
]
146+
147+
assert_includes(acceptable_results, result)
148+
end
149+
140150
def test_strip_blank_string
141151
assert_nil full_sanitize(nil)
142152
assert_equal "", full_sanitize("")
@@ -211,6 +221,10 @@ def test_strip_links_with_unclosed_tags
211221
assert_equal "", link_sanitize("<a<a")
212222
end
213223

224+
def test_strip_links_with_passed_duck_typed_range
225+
assert_equal "2001..2005", link_sanitize(2001..2005)
226+
end
227+
214228
def test_strip_links_with_plaintext
215229
assert_equal "Don't touch me", link_sanitize("Don't touch me")
216230
end
@@ -295,6 +309,10 @@ def test_sanitize_form
295309
assert_sanitized "<form action=\"/foo/bar\" method=\"post\"><input></form>", ""
296310
end
297311

312+
def test_sanitize_passed_duck_typed_range
313+
assert_sanitized 2001..2005, "2001..2005"
314+
end
315+
298316
def test_sanitize_plaintext
299317
# note that the `plaintext` tag has been deprecated since HTML 2
300318
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext
@@ -306,7 +324,19 @@ def test_sanitize_plaintext
306324
# xerces+nekohtml-unit
307325
"&lt;span&gt;foo&lt;/span&gt;&lt;/plaintext&gt;",
308326
# xerces+cyberneko
309-
"&lt;span&gt;foo&lt;/span&gt;"
327+
"&lt;span&gt;foo&lt;/span&gt;",
328+
]
329+
330+
assert_includes(acceptable_results, result)
331+
end
332+
333+
def test_safe_sanitize_passed_duck_typed_range
334+
# note that the `plaintext` tag has been deprecated since HTML 2
335+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext
336+
input = 2001..2005
337+
result = safe_list_sanitize(input)
338+
acceptable_results = [
339+
"2001..2005",
310340
]
311341

312342
assert_includes(acceptable_results, result)

0 commit comments

Comments
 (0)