Skip to content

Commit 0d0bf32

Browse files
committed
Move SafeListSanitizer prune option from class to instance variable
1 parent 096fd00 commit 0d0bf32

File tree

4 files changed

+20
-42
lines changed

4 files changed

+20
-42
lines changed

lib/rails/html/sanitizer.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,14 @@ class SafeListSanitizer < Sanitizer
104104
class << self
105105
attr_accessor :allowed_tags
106106
attr_accessor :allowed_attributes
107-
attr_accessor :prune
108107
end
109108
self.allowed_tags = Set.new(%w(strong em b i p code pre tt samp kbd var sub
110109
sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dl dt dd abbr
111110
acronym a img blockquote del ins))
112111
self.allowed_attributes = Set.new(%w(href src width height alt cite datetime title class name xml:lang abbr))
113-
self.prune = false
114112

115-
def initialize
116-
@permit_scrubber = PermitScrubber.new
113+
def initialize(prune: false)
114+
@permit_scrubber = PermitScrubber.new(prune: prune)
117115
end
118116

119117
def sanitize(html, options = {})
@@ -125,10 +123,9 @@ def sanitize(html, options = {})
125123
if scrubber = options[:scrubber]
126124
# No duck typing, Loofah ensures subclass of Loofah::Scrubber
127125
loofah_fragment.scrub!(scrubber)
128-
elsif allowed_tags(options) || allowed_attributes(options) || prune(options)
126+
elsif allowed_tags(options) || allowed_attributes(options)
129127
@permit_scrubber.tags = allowed_tags(options)
130128
@permit_scrubber.attributes = allowed_attributes(options)
131-
@permit_scrubber.prune = prune(options)
132129
loofah_fragment.scrub!(@permit_scrubber)
133130
else
134131
remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
@@ -151,10 +148,6 @@ def allowed_tags(options)
151148
def allowed_attributes(options)
152149
options[:attributes] || self.class.allowed_attributes
153150
end
154-
155-
def prune(options)
156-
options.key?(:prune) ? options[:prune] : self.class.prune
157-
end
158151
end
159152

160153
WhiteListSanitizer = SafeListSanitizer

lib/rails/html/scrubbers.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ module Html
4545
# See the documentation for +Nokogiri::XML::Node+ to understand what's possible
4646
# with nodes: https://nokogiri.org/rdoc/Nokogiri/XML/Node.html
4747
class PermitScrubber < Loofah::Scrubber
48-
attr_accessor :prune
49-
attr_reader :tags, :attributes
48+
attr_reader :tags, :attributes, :prune
5049

51-
def initialize
52-
@direction = :bottom_up
53-
@prune = false
50+
def initialize(prune: false)
51+
@prune = prune
52+
@direction = @prune ? :top_down : :bottom_up
5453
@tags, @attributes = nil, nil
5554
end
5655

test/sanitizer_test.rb

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,10 @@ def test_custom_attributes_overrides_allowed_attributes
258258
end
259259
end
260260

261-
def test_setting_default_prune_affects_sanitization
262-
scope_prune true do |sanitizer|
263-
input = '<u>leave me <b>now</b></u>'
264-
assert_equal '<u>leave me </u>', sanitizer.sanitize(input, tags: %w(u))
265-
end
266-
end
267-
268-
def test_custom_prune_overrides_default_prune
269-
scope_prune true do |sanitizer|
270-
input = '<u>leave me <b>now</b></u>'
271-
assert_equal '<u>leave me now</u>', sanitizer.sanitize(input, tags: %w(u), prune: false)
272-
end
273-
end
274-
275-
def test_should_allow_custom_prune
276-
input = '<u>leave me <b>now</b></u>'
277-
assert_equal '<u>leave me </u>', safe_list_sanitize(input, tags: %w(u), prune: true)
261+
def test_should_allow_prune
262+
sanitizer = Rails::Html::SafeListSanitizer.new(prune: true)
263+
text = '<u>leave me <b>now</b></u>'
264+
assert_equal "<u>leave me </u>", sanitizer.sanitize(text, tags: %w(u))
278265
end
279266

280267
def test_should_allow_custom_tags
@@ -648,14 +635,6 @@ def scope_allowed_attributes(attributes)
648635
Rails::Html::SafeListSanitizer.allowed_attributes = old_attributes
649636
end
650637

651-
def scope_prune(prune)
652-
old_prune = Rails::Html::SafeListSanitizer.prune
653-
Rails::Html::SafeListSanitizer.prune = prune
654-
yield Rails::Html::SafeListSanitizer.new
655-
ensure
656-
Rails::Html::SafeListSanitizer.prune = old_prune
657-
end
658-
659638
# note that this is used for testing CSS hex encoding: \\[0-9a-f]{1,6}
660639
def convert_to_css_hex(string, escape_parens=false)
661640
string.chars.map do |c|

test/scrubbers_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def test_leaves_only_supplied_tags
6767
end
6868

6969
def test_prunes_tags
70-
html = '<tag>leave me <span>now</span></tag>'
70+
@scrubber = Rails::Html::PermitScrubber.new(prune: true)
7171
@scrubber.tags = %w(tag)
72-
@scrubber.prune = true
72+
html = '<tag>leave me <span>now</span></tag>'
7373
assert_scrubbed html, '<tag>leave me </tag>'
7474
end
7575

@@ -164,6 +164,13 @@ def test_targeting_tags_and_attributes_removes_only_them
164164
html = '<tag remove="" other=""></tag><a remove="" other=""></a>'
165165
assert_scrubbed html, '<a other=""></a>'
166166
end
167+
168+
def test_prunes_tags
169+
@scrubber = Rails::Html::TargetScrubber.new(prune: true)
170+
@scrubber.tags = %w(span)
171+
html = '<tag>leave me <span>now</span></tag>'
172+
assert_scrubbed html, '<tag>leave me </tag>'
173+
end
167174
end
168175

169176
class TextOnlyScrubberTest < ScrubberTest

0 commit comments

Comments
 (0)