# frozen_string_literal: true require "minitest/autorun" require "rails-html-sanitizer" puts "nokogiri version info: #{Nokogiri::VERSION_INFO}" puts "html5 support: #{Rails::HTML::Sanitizer.html5_support?}" # # NOTE that many of these tests contain multiple acceptable results. # # In some cases, this is because of how the HTML4 parser's recovery behavior changed in libxml2 # 2.9.14 and 2.10.0. For more details, see: # # - https://github.com/sparklemotion/nokogiri/releases/tag/v1.13.5 # - https://gitlab.gnome.org/GNOME/libxml2/-/issues/380 # # In other cases, multiple acceptable results are provided because Nokogiri's vendored libxml2 is # patched to entity-escape server-side includes (aks "SSI", aka ``). # # In many other cases, it's because the parser used by Nokogiri on JRuby (xerces+nekohtml) parses # slightly differently than libxml2 in edge cases. # module SanitizerTests def self.loofah_html5_support? Loofah.respond_to?(:html5_support?) && Loofah.html5_support? end class BaseSanitizerTest < Minitest::Test class XpathRemovalTestSanitizer < Rails::HTML::Sanitizer def sanitize(html, options = {}) fragment = Loofah.fragment(html) remove_xpaths(fragment, options[:xpaths]).to_s end end def test_sanitizer_sanitize_raises_not_implemented_error assert_raises NotImplementedError do Rails::HTML::Sanitizer.new.sanitize("asdf") end end def test_remove_xpaths_removes_an_xpath html = %(
hello
hello
It no longer contains any HTML.
This is a test.
") end def test_escape_tags_with_many_open_quotes assert_equal "<<", full_sanitize("<<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.replace("