Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass comment nodes to the scrubber #117

Merged
merged 1 commit into from Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,15 @@
## next / unreleased

* Fix regression in v1.4.0 that did not pass comment nodes to the scrubber.

Some scrubbers will want to override the default behavior and allow comments, but v1.4.0 only
passed through elements to the scrubber's `keep_node?` method.

This change once again allows the scrubber to make the decision on comment nodes, but still skips
other non-elements like processing instructions (see #115).

*Mike Dalessio*

## 1.4.0 / 2021-08-18

* Processing Instructions are no longer allowed by Rails::Html::PermitScrubber
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/html/scrubbers.rb
Expand Up @@ -68,7 +68,7 @@ def scrub(node)
end
return CONTINUE if skip_node?(node)

unless node.element? && keep_node?(node)
unless (node.comment? || node.element?) && keep_node?(node)
return STOP if scrub_node(node) == STOP
end

Expand Down
44 changes: 44 additions & 0 deletions test/scrubbers_test.rb
Expand Up @@ -112,6 +112,50 @@ def test_attributes_accessor_validation
end
end

class PermitScrubberSubclassTest < ScrubberTest
def setup
@scrubber = Class.new(::Rails::Html::PermitScrubber) do
attr :nodes_seen

def initialize
super()
@nodes_seen = []
end

def keep_node?(node)
@nodes_seen << node.name
super(node)
end
end.new
end

def test_elements_are_checked
html = %Q("<div></div><a></a><tr></tr>")
Loofah.scrub_fragment(html, @scrubber)
assert_includes(@scrubber.nodes_seen, "div")
assert_includes(@scrubber.nodes_seen, "a")
assert_includes(@scrubber.nodes_seen, "tr")
end

def test_comments_are_checked
# this passes in v1.3.0 but fails in v1.4.0
html = %Q("<div></div><!-- ohai --><tr></tr>")
Loofah.scrub_fragment(html, @scrubber)
assert_includes(@scrubber.nodes_seen, "div")
assert_includes(@scrubber.nodes_seen, "comment")
assert_includes(@scrubber.nodes_seen, "tr")
end

def test_craftily_named_processing_instructions_are_not_checked
# this fails in v1.3.0 but passes in v1.4.0
html = %Q("<div></div><?a content><tr></tr>")
Loofah.scrub_fragment(html, @scrubber)
assert_includes(@scrubber.nodes_seen, "div")
refute_includes(@scrubber.nodes_seen, "a")
assert_includes(@scrubber.nodes_seen, "tr")
end
end

class TargetScrubberTest < ScrubberTest
def setup
@scrubber = Rails::Html::TargetScrubber.new
Expand Down