require "minitest/autorun"
require "rails-html-sanitizer"
require "rails/dom/testing/assertions/dom_assertions"
puts Nokogiri::VERSION_INFO
class SanitizersTest < Minitest::Test
include Rails::Dom::Testing::Assertions::DomAssertions
def test_sanitizer_sanitize_raises_not_implemented_error
assert_raises NotImplementedError do
Rails::Html::Sanitizer.new.sanitize('')
end
end
def test_sanitize_nested_script
assert_equal '<script>alert("XSS");</script>', safe_list_sanitize('alert("XSS");/', tags: %w(em))
end
def test_sanitize_nested_script_in_style
assert_equal '<script>alert("XSS");</script>', safe_list_sanitize('alert("XSS");/', tags: %w(em))
end
class XpathRemovalTestSanitizer < Rails::Html::Sanitizer
def sanitize(html, options = {})
fragment = Loofah.fragment(html)
remove_xpaths(fragment, options[:xpaths]).to_s
end
end
def test_remove_xpaths_removes_an_xpath
html = %(
hello
)
assert_equal %(hello
), xpath_sanitize(html, xpaths: %w(.//script))
end
def test_remove_xpaths_removes_all_occurrences_of_xpath
html = %()
assert_equal %(), xpath_sanitize(html, xpaths: %w(.//script))
end
def test_remove_xpaths_called_with_faulty_xpath
assert_raises Nokogiri::XML::XPath::SyntaxError do
xpath_sanitize('hello', xpaths: %w(..faulty_xpath))
end
end
def test_remove_xpaths_called_with_xpath_string
assert_equal '', xpath_sanitize('', xpaths: './/a')
end
def test_remove_xpaths_called_with_enumerable_xpaths
assert_equal '', xpath_sanitize('', xpaths: %w(.//a .//span))
end
def test_strip_tags_with_quote
input = '<" hi'
expected = libxml_2_9_14_recovery_lt? ? %{<" hi} : %{ hi}
assert_equal(expected, full_sanitize(input))
end
def test_strip_invalid_html
assert_equal "<<", full_sanitize("<<This is a test.\n\n\n\nIt no longer contains any HTML.
\n}
assert_equal expected, full_sanitize(input)
end
def test_remove_unclosed_tags
input = "This is <-- not\n a comment here."
expected = libxml_2_9_14_recovery_lt? ? %{This is <-- not\n a comment here.} : %{This is }
assert_equal(expected, full_sanitize(input))
end
def test_strip_cdata
input = "This has a ]]> here."
expected = libxml_2_9_14_recovery_lt_bang? ? %{This has a <![CDATA[]]> here.} : %{This has a ]]> here.}
assert_equal(expected, full_sanitize(input))
end
def test_strip_unclosed_cdata
input = "This has an unclosed ]] here..."
expected = libxml_2_9_14_recovery_lt_bang? ? %{This has an unclosed <![CDATA[]] here...} : %{This has an unclosed ]] here...}
assert_equal(expected, full_sanitize(input))
end
def test_strip_blank_string
assert_nil full_sanitize(nil)
assert_equal "", full_sanitize("")
assert_equal " ", full_sanitize(" ")
end
def test_strip_tags_with_plaintext
assert_equal "Don't touch me", full_sanitize("Don't touch me")
end
def test_strip_tags_with_tags
assert_equal "This is a test.", full_sanitize("This is a test.
")
end
def test_escape_tags_with_many_open_quotes
assert_equal "<<", full_sanitize("<<")
end
def test_strip_tags_with_sentence
assert_equal "This is a test.", full_sanitize("This is a test.")
end
def test_strip_tags_with_comment
assert_equal "This has a here.", full_sanitize("This has a here.")
end
def test_strip_tags_with_frozen_string
assert_equal "Frozen string with no tags", full_sanitize("Frozen string with no tags".freeze)
end
def test_full_sanitize_respect_html_escaping_of_the_given_string
assert_equal 'test\r\nstring', full_sanitize('test\r\nstring')
assert_equal '&', full_sanitize('&')
assert_equal '&', full_sanitize('&')
assert_equal '&', full_sanitize('&')
assert_equal 'omg <script>BOM</script>', full_sanitize('omg <script>BOM</script>')
end
def test_strip_links_with_tags_in_tags
expected = "<a href='hello'>all day long</a>"
input = "<a href='hello'>all day long</a>"
assert_equal expected, link_sanitize(input)
end
def test_strip_links_with_unclosed_tags
assert_equal "", link_sanitize("on my mind\nall day long")
end
def test_strip_links_leaves_nonlink_tags
assert_equal "My mind\nall day long", link_sanitize("My mind\nall day long")
end
def test_strip_links_with_links
assert_equal "0wn3d", link_sanitize("0wn3d")
end
def test_strip_links_with_linkception
assert_equal "Magic", link_sanitize("Magic")
end
def test_sanitize_form
assert_sanitized "", ''
end
def test_sanitize_plaintext
assert_sanitized "foo", "foo"
end
def test_sanitize_script
assert_sanitized "a b cd e f", "a b cblah blah blahd e f"
end
def test_sanitize_js_handlers
raw = %{onthis="do that" hello}
assert_sanitized raw, %{onthis="do that" hello}
end
def test_sanitize_javascript_href
raw = %{href="javascript:bang" foo, bar}
assert_sanitized raw, %{href="javascript:bang" foo, bar}
end
def test_sanitize_image_src
raw = %{src="javascript:bang" foo, bar}
assert_sanitized raw, %{src="javascript:bang" foo, bar}
end
tags = Loofah::HTML5::SafeList::ALLOWED_ELEMENTS - %w(script form)
tags.each do |tag_name|
define_method "test_should_allow_#{tag_name}_tag" do
scope_allowed_tags(tags) do
assert_sanitized "start <#{tag_name} title=\"1\" onclick=\"foo\">foo bar baz#{tag_name}> end", %(start <#{tag_name} title="1">foo bar baz#{tag_name}> end)
end
end
end
def test_should_allow_anchors
assert_sanitized %(), %(baz)
end
def test_video_poster_sanitization
scope_allowed_tags(%w(video)) do
scope_allowed_attributes %w(src poster) do
assert_sanitized %(), %()
assert_sanitized %(), %()
end
end
end
# RFC 3986, sec 4.2
def test_allow_colons_in_path_component
assert_sanitized "foo"
end
%w(src width height alt).each do |img_attr|
define_method "test_should_allow_image_#{img_attr}_attribute" do
assert_sanitized %(), %()
end
end
def test_should_handle_non_html
assert_sanitized 'abc'
end
def test_should_handle_blank_text
[nil, '', ' '].each { |blank| assert_sanitized blank }
end
def test_setting_allowed_tags_affects_sanitization
scope_allowed_tags %w(u) do |sanitizer|
assert_equal '', sanitizer.sanitize('')
end
end
def test_setting_allowed_attributes_affects_sanitization
scope_allowed_attributes %w(foo) do |sanitizer|
input = ''
assert_equal '', sanitizer.sanitize(input)
end
end
def test_custom_tags_overrides_allowed_tags
scope_allowed_tags %(u) do |sanitizer|
input = ''
assert_equal '', sanitizer.sanitize(input, tags: %w(a))
end
end
def test_custom_attributes_overrides_allowed_attributes
scope_allowed_attributes %(foo) do |sanitizer|
input = ''
assert_equal '', sanitizer.sanitize(input, attributes: %w(bar))
end
end
def test_should_allow_prune
sanitizer = Rails::Html::SafeListSanitizer.new(prune: true)
text = 'leave me now'
assert_equal "leave me ", sanitizer.sanitize(text, tags: %w(u))
end
def test_should_allow_custom_tags
text = "foo"
assert_equal text, safe_list_sanitize(text, tags: %w(u))
end
def test_should_allow_only_custom_tags
text = "foo with bar"
assert_equal "foo with bar", safe_list_sanitize(text, tags: %w(u))
end
def test_should_allow_custom_tags_with_attributes
text = %(foo
)
assert_equal text, safe_list_sanitize(text)
end
def test_should_allow_custom_tags_with_custom_attributes
text = %(Lorem ipsum
)
assert_equal text, safe_list_sanitize(text, attributes: ['foo'])
end
def test_scrub_style_if_style_attribute_option_is_passed
input = ''
actual = safe_list_sanitize(input, attributes: %w(style))
assert_includes(['', ''], actual)
end
def test_should_raise_argument_error_if_tags_is_not_enumerable
assert_raises ArgumentError do
safe_list_sanitize('some html', tags: 'foo')
end
end
def test_should_raise_argument_error_if_attributes_is_not_enumerable
assert_raises ArgumentError do
safe_list_sanitize('some html', attributes: 'foo')
end
end
def test_should_not_accept_non_loofah_inheriting_scrubber
scrubber = Object.new
def scrubber.scrub(node); node.name = 'h1'; end
assert_raises Loofah::ScrubberNotFound do
safe_list_sanitize('some html', scrubber: scrubber)
end
end
def test_should_accept_loofah_inheriting_scrubber
scrubber = Loofah::Scrubber.new
def scrubber.scrub(node); node.name = 'h1'; end
html = ""
assert_equal "hello!
", safe_list_sanitize(html, scrubber: scrubber)
end
def test_should_accept_loofah_scrubber_that_wraps_a_block
scrubber = Loofah::Scrubber.new { |node| node.name = 'h1' }
html = ""
assert_equal "hello!
", safe_list_sanitize(html, scrubber: scrubber)
end
def test_custom_scrubber_takes_precedence_over_other_options
scrubber = Loofah::Scrubber.new { |node| node.name = 'h1' }
html = ""
assert_equal "hello!
", safe_list_sanitize(html, scrubber: scrubber, tags: ['foo'])
end
[%w(img src), %w(a href)].each do |(tag, attr)|
define_method "test_should_strip_#{attr}_attribute_in_#{tag}_with_bad_protocols" do
assert_sanitized %(<#{tag} #{attr}="javascript:bang" title="1">boo#{tag}>), %(<#{tag} title="1">boo#{tag}>)
end
end
def test_should_block_script_tag
assert_sanitized %(), ""
end
def test_should_not_fall_for_xss_image_hack_with_uppercase_tags
assert_sanitized %(">), %(alert("XSS")">)
end
[%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%(),
%()].each do |img_hack|
define_method "test_should_not_fall_for_xss_image_hack_#{img_hack}" do
assert_sanitized img_hack, ""
end
end
def test_should_sanitize_tag_broken_up_by_null
assert_sanitized %(alert(\"XSS\")), ""
end
def test_should_sanitize_invalid_script_tag
assert_sanitized %(), ""
end
def test_should_sanitize_script_tag_with_multiple_open_brackets
assert_sanitized %(<), "<alert(\"XSS\");//<"
assert_sanitized %(