rails-html-sanitizer-1.3.0/ 0000755 0000041 0000041 00000000000 13550471667 015643 5 ustar www-data www-data rails-html-sanitizer-1.3.0/test/ 0000755 0000041 0000041 00000000000 13550471667 016622 5 ustar www-data www-data rails-html-sanitizer-1.3.0/test/scrubbers_test.rb 0000644 0000041 0000041 00000010633 13550471667 022203 0 ustar www-data www-data require "minitest/autorun"
require "rails-html-sanitizer"
class ScrubberTest < Minitest::Test
protected
def assert_scrubbed(html, expected = html)
output = Loofah.scrub_fragment(html, @scrubber).to_s
assert_equal expected, output
end
def to_node(text)
Loofah.fragment(text).children.first
end
def assert_node_skipped(text)
assert_scrub_returns(Loofah::Scrubber::CONTINUE, text)
end
def assert_scrub_stopped(text)
assert_scrub_returns(Loofah::Scrubber::STOP, text)
end
def assert_scrub_returns(return_value, text)
node = to_node(text)
assert_equal return_value, @scrubber.scrub(node)
end
end
class PermitScrubberTest < ScrubberTest
def setup
@scrubber = Rails::Html::PermitScrubber.new
end
def test_responds_to_scrub
assert @scrubber.respond_to?(:scrub)
end
def test_default_scrub_behavior
assert_scrubbed 'hello', 'hello'
end
def test_default_attributes_removal_behavior
assert_scrubbed '
hello
', 'hello
'
end
def test_leaves_supplied_tags
@scrubber.tags = %w(a)
assert_scrubbed 'hello'
end
def test_leaves_only_supplied_tags
html = 'leave me now'
@scrubber.tags = %w(tag)
assert_scrubbed html, 'leave me now'
end
def test_leaves_only_supplied_tags_nested
html = 'leave me now'
@scrubber.tags = %w(tag)
assert_scrubbed html, 'leave me now'
end
def test_leaves_supplied_attributes
@scrubber.attributes = %w(cooler)
assert_scrubbed ''
end
def test_leaves_only_supplied_attributes
@scrubber.attributes = %w(cooler)
assert_scrubbed '', ''
end
def test_leaves_supplied_tags_and_attributes
@scrubber.tags = %w(tag)
@scrubber.attributes = %w(cooler)
assert_scrubbed ''
end
def test_leaves_only_supplied_tags_and_attributes
@scrubber.tags = %w(tag)
@scrubber.attributes = %w(cooler)
html = ''
assert_scrubbed html, ''
end
def test_leaves_text
assert_scrubbed('some text')
end
def test_skips_text_nodes
assert_node_skipped('some text')
end
def test_tags_accessor_validation
e = assert_raises(ArgumentError) do
@scrubber.tags = 'tag'
end
assert_equal "You should pass :tags as an Enumerable", e.message
assert_nil @scrubber.tags, "Tags should be nil when validation fails"
end
def test_attributes_accessor_validation
e = assert_raises(ArgumentError) do
@scrubber.attributes = 'cooler'
end
assert_equal "You should pass :attributes as an Enumerable", e.message
assert_nil @scrubber.attributes, "Attributes should be nil when validation fails"
end
end
class TargetScrubberTest < ScrubberTest
def setup
@scrubber = Rails::Html::TargetScrubber.new
end
def test_targeting_tags_removes_only_them
@scrubber.tags = %w(a h1)
html = ''
assert_scrubbed html, ''
end
def test_targeting_tags_removes_only_them_nested
@scrubber.tags = %w(a)
html = ''
assert_scrubbed html, ''
end
def test_targeting_attributes_removes_only_them
@scrubber.attributes = %w(class id)
html = ''
assert_scrubbed html, ''
end
def test_targeting_tags_and_attributes_removes_only_them
@scrubber.tags = %w(tag)
@scrubber.attributes = %w(remove)
html = ''
assert_scrubbed html, ''
end
end
class TextOnlyScrubberTest < ScrubberTest
def setup
@scrubber = Rails::Html::TextOnlyScrubber.new
end
def test_removes_all_tags_and_keep_the_content
assert_scrubbed 'hello', 'hello'
end
def test_skips_text_nodes
assert_node_skipped('some text')
end
end
class ReturningStopFromScrubNodeTest < ScrubberTest
class ScrubStopper < Rails::Html::PermitScrubber
def scrub_node(node)
Loofah::Scrubber::STOP
end
end
def setup
@scrubber = ScrubStopper.new
end
def test_returns_stop_from_scrub_if_scrub_node_does
assert_scrub_stopped ''
end
end
rails-html-sanitizer-1.3.0/test/sanitizer_test.rb 0000644 0000041 0000041 00000051171 13550471667 022223 0 ustar www-data www-data require "minitest/autorun"
require "rails-html-sanitizer"
require "rails/dom/testing/assertions/dom_assertions"
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
sanitizer = Rails::Html::SafeListSanitizer.new
assert_equal '<script>alert("XSS");</script>', sanitizer.sanitize('alert("XSS");/', tags: %w(em))
end
def test_sanitize_nested_script_in_style
sanitizer = Rails::Html::SafeListSanitizer.new
assert_equal '<script>alert("XSS");</script>', sanitizer.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'
assert_equal ' hi', 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
assert_equal "This is ", full_sanitize("This is <-- not\n a comment here.")
end
def test_strip_cdata
assert_equal "This has a ]]> here.", full_sanitize("This has a ]]> here.")
end
def test_strip_unclosed_cdata
assert_equal "This has an unclosed ]] here...", full_sanitize("This has an unclosed ]] here...")
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 "Dont touch me", full_sanitize("Dont 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_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 = ''
assert_equal '', safe_list_sanitize(input, attributes: %w(style))
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 %(