2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2007-10-10 13:35:10 -04:00
|
|
|
|
2017-03-29 21:28:07 -04:00
|
|
|
# The exhaustive tests are in the rails-html-sanitizer gem.
|
2013-08-16 10:31:24 -04:00
|
|
|
# This tests that the helpers hook up correctly to the sanitizer classes.
|
2008-04-19 14:06:57 -04:00
|
|
|
class SanitizeHelperTest < ActionView::TestCase
|
|
|
|
tests ActionView::Helpers::SanitizeHelper
|
2007-10-10 13:35:10 -04:00
|
|
|
|
|
|
|
def test_strip_links
|
2021-04-15 02:57:01 -04:00
|
|
|
assert_equal "Don't touch me", strip_links("Don't touch me")
|
2007-10-10 13:35:10 -04:00
|
|
|
assert_equal "on my mind\nall day long", strip_links("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
|
2008-08-25 22:24:48 -04:00
|
|
|
assert_equal "Magic", strip_links("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
|
2007-10-10 13:35:10 -04:00
|
|
|
assert_equal "My mind\nall <b>day</b> long", strip_links("<a href='almost'>My mind</a>\n<A href='almost'>all <b>day</b> long</A>")
|
2017-02-27 13:51:26 -05:00
|
|
|
assert_equal "<malformed & link", strip_links('<<a href="https://example.org">malformed & link</a>')
|
2007-10-10 13:35:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sanitize_form
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "", sanitize("<form action=\"/foo/bar\" method=\"post\"><input></form>")
|
2007-10-10 13:35:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_sanitize_illegal_style_properties
|
|
|
|
raw = %(display:block; position:absolute; left:0; top:0; width:100%; height:100%; z-index:1; background-color:black; background-image:url(http://www.ragingplatypus.com/i/cam-full.jpg); background-x:center; background-y:center; background-repeat:repeat;)
|
2017-09-24 18:15:52 -04:00
|
|
|
expected = %r(\Adisplay:\s?block;\s?width:\s?100%;\s?height:\s?100%;\s?background-color:\s?black;\s?background-x:\s?center;\s?background-y:\s?center;\z)
|
|
|
|
assert_match expected, sanitize_css(raw)
|
2007-10-10 13:35:10 -04:00
|
|
|
end
|
|
|
|
|
2013-07-10 11:54:26 -04:00
|
|
|
def test_strip_tags
|
2021-04-15 02:57:01 -04:00
|
|
|
assert_equal("Don't touch me", strip_tags("Don't touch me"))
|
2013-07-10 11:54:26 -04:00
|
|
|
assert_equal("This is a test.", strip_tags("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
|
|
|
|
assert_equal "This has a here.", strip_tags("This has a <!-- comment --> here.")
|
2017-02-27 13:51:26 -05:00
|
|
|
assert_equal("Jekyll & Hyde", strip_tags("Jekyll & Hyde"))
|
2013-07-10 11:54:26 -04:00
|
|
|
assert_equal "", strip_tags("<script>")
|
|
|
|
end
|
|
|
|
|
2015-03-07 12:48:06 -05:00
|
|
|
def test_strip_tags_will_not_encode_special_characters
|
|
|
|
assert_equal "test\r\n\r\ntest", strip_tags("test\r\n\r\ntest")
|
|
|
|
end
|
|
|
|
|
2009-10-07 16:31:20 -04:00
|
|
|
def test_sanitize_is_marked_safe
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate sanitize("<html><script></script></html>"), :html_safe?
|
2007-10-10 13:35:10 -04:00
|
|
|
end
|
2020-06-10 01:30:06 -04:00
|
|
|
|
|
|
|
def test_sanitized_allowed_tags_class_method
|
|
|
|
expected = Set.new(["strong", "em", "b", "i", "p", "code", "pre", "tt", "samp", "kbd", "var",
|
|
|
|
"sub", "sup", "dfn", "cite", "big", "small", "address", "hr", "br", "div", "span", "h1", "h2",
|
|
|
|
"h3", "h4", "h5", "h6", "ul", "ol", "li", "dl", "dt", "dd", "abbr", "acronym", "a", "img",
|
|
|
|
"blockquote", "del", "ins"])
|
|
|
|
assert_equal(expected, self.class.sanitized_allowed_tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sanitized_allowed_attributes_class_method
|
|
|
|
expected = Set.new(["href", "src", "width", "height", "alt", "cite", "datetime", "title", "class", "name", "xml:lang", "abbr"])
|
|
|
|
assert_equal(expected, self.class.sanitized_allowed_attributes)
|
|
|
|
end
|
2008-01-05 08:32:06 -05:00
|
|
|
end
|