three",
]
assert_includes(acceptable_results, result)
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_prunes_tags
@scrubber = Rails::HTML::PermitScrubber.new(prune: true)
@scrubber.tags = %w(tag)
html = "leave me now"
assert_scrubbed html, "leave me "
end
def test_leaves_comments_when_supplied_as_tag
@scrubber.tags = %w(div comment)
assert_scrubbed("
one
three",
"
one
three")
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_does_not_allow_safelisted_mglyph
# https://hackerone.com/reports/2519936
assert_output(nil, /WARNING: 'mglyph' tags cannot be allowed by the PermitScrubber/) do
@scrubber.tags = ["div", "mglyph", "span"]
end
assert_equal(["div", "span"], @scrubber.tags)
end
def test_does_not_allow_safelisted_malignmark
# https://hackerone.com/reports/2519936
assert_output(nil, /WARNING: 'malignmark' tags cannot be allowed by the PermitScrubber/) do
@scrubber.tags = ["div", "malignmark", "span"]
end
assert_equal(["div", "span"], @scrubber.tags)
end
def test_does_not_allow_safelisted_noscript
# https://hackerone.com/reports/2509647
assert_output(nil, /WARNING: 'noscript' tags cannot be allowed by the PermitScrubber/) do
@scrubber.tags = ["div", "noscript", "span"]
end
assert_equal(["div", "span"], @scrubber.tags)
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
def test_prunes_tags
@scrubber = Rails::HTML::TargetScrubber.new(prune: true)
@scrubber.tags = %w(span)
html = "leave me now"
assert_scrubbed html, "leave me "
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
class ScrubContinuer < Rails::HTML::PermitScrubber
def scrub_node(node)
Loofah::Scrubber::CONTINUE
end
end
def test_returns_stop_from_scrub_if_scrub_node_does
@scrubber = ScrubStopper.new
assert_scrub_stopped ""
end
def test_returns_continue_from_scrub_if_scrub_node_does
@scrubber = ScrubContinuer.new
assert_node_skipped ""
end
end
class PermitScrubberMinimalOperationsTest < ScrubberTest
class TestPermitScrubber < Rails::HTML::PermitScrubber
def initialize
@scrub_attribute_args = []
@scrub_attributes_args = []
super
self.tags = ["div"]
self.attributes = ["class"]
end
def scrub_attributes(node)
@scrub_attributes_args << node.name
super
end
def scrub_attribute(node, attr)
@scrub_attribute_args << [node.name, attr.name]
super
end
end
def test_does_not_scrub_removed_attributes
@scrubber = TestPermitScrubber.new
input = ""
frag = scrub_fragment(input)
assert_equal("", frag)
assert_equal([["div", "class"]], @scrubber.instance_variable_get(:@scrub_attribute_args))
end
def test_does_not_scrub_attributes_of_a_removed_node
@scrubber = TestPermitScrubber.new
input = ""
frag = scrub_fragment(input)
assert_equal("", frag)
assert_equal(["div"], @scrubber.instance_variable_get(:@scrub_attributes_args))
end
end