Skip to content

Commit ab16fa4

Browse files
committed
fix: pass comment nodes to the scrubber
Some scrubbers want to allow comments through, but in v1.4.0 didn't get the chance because only elements were passed through to `keep_node?`. This change allows comments and elements through, but still omits other non-elements like processing instructions (see #115).
1 parent 2e9ec19 commit ab16fa4

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## next / unreleased
2+
3+
* Fix regression in v1.4.0 that did not pass comment nodes to the scrubber.
4+
5+
Some scrubbers will want to override the default behavior and allow comments, but v1.4.0 only
6+
passed through elements to the scrubber's `keep_node?` method.
7+
8+
This change once again allows the scrubber to make the decision on comment nodes, but still skips
9+
other non-elements like processing instructions (see #115).
10+
11+
*Mike Dalessio*
12+
113
## 1.4.0 / 2021-08-18
214

315
* Processing Instructions are no longer allowed by Rails::Html::PermitScrubber

lib/rails/html/scrubbers.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def scrub(node)
6868
end
6969
return CONTINUE if skip_node?(node)
7070

71-
unless node.element? && keep_node?(node)
71+
unless (node.comment? || node.element?) && keep_node?(node)
7272
return STOP if scrub_node(node) == STOP
7373
end
7474

test/scrubbers_test.rb

+44
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,50 @@ def test_attributes_accessor_validation
112112
end
113113
end
114114

115+
class PermitScrubberSubclassTest < ScrubberTest
116+
def setup
117+
@scrubber = Class.new(::Rails::Html::PermitScrubber) do
118+
attr :nodes_seen
119+
120+
def initialize
121+
super()
122+
@nodes_seen = []
123+
end
124+
125+
def keep_node?(node)
126+
@nodes_seen << node.name
127+
super(node)
128+
end
129+
end.new
130+
end
131+
132+
def test_elements_are_checked
133+
html = %Q("<div></div><a></a><tr></tr>")
134+
Loofah.scrub_fragment(html, @scrubber)
135+
assert_includes(@scrubber.nodes_seen, "div")
136+
assert_includes(@scrubber.nodes_seen, "a")
137+
assert_includes(@scrubber.nodes_seen, "tr")
138+
end
139+
140+
def test_comments_are_checked
141+
# this passes in v1.3.0 but fails in v1.4.0
142+
html = %Q("<div></div><!-- ohai --><tr></tr>")
143+
Loofah.scrub_fragment(html, @scrubber)
144+
assert_includes(@scrubber.nodes_seen, "div")
145+
assert_includes(@scrubber.nodes_seen, "comment")
146+
assert_includes(@scrubber.nodes_seen, "tr")
147+
end
148+
149+
def test_craftily_named_processing_instructions_are_not_checked
150+
# this fails in v1.3.0 but passes in v1.4.0
151+
html = %Q("<div></div><?a content><tr></tr>")
152+
Loofah.scrub_fragment(html, @scrubber)
153+
assert_includes(@scrubber.nodes_seen, "div")
154+
refute_includes(@scrubber.nodes_seen, "a")
155+
assert_includes(@scrubber.nodes_seen, "tr")
156+
end
157+
end
158+
115159
class TargetScrubberTest < ScrubberTest
116160
def setup
117161
@scrubber = Rails::Html::TargetScrubber.new

0 commit comments

Comments
 (0)