File: scrubbers_test.rb

package info (click to toggle)
ruby-rails-html-sanitizer 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 192 kB
  • sloc: ruby: 1,658; makefile: 6
file content (295 lines) | stat: -rw-r--r-- 8,141 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# frozen_string_literal: true

require "minitest/autorun"
require "rails-html-sanitizer"

class ScrubberTest < Minitest::Test
  protected
    def scrub_fragment(html)
      Loofah.scrub_fragment(html, @scrubber).to_s
    end

    def assert_scrubbed(html, expected = html)
      output = scrub_fragment(html)
      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 "<tag>hello</tag>", "hello"
  end

  def test_default_scrub_removes_comments
    assert_scrubbed("<div>one</div><!-- two --><span>three</span>",
                    "<div>one</div><span>three</span>")
  end

  def test_default_scrub_removes_processing_instructions
    input = "<div>one</div><?div two><span>three</span>"
    result = scrub_fragment(input)

    acceptable_results = [
      # jruby cyberneko (nokogiri < 1.14.0)
      "<div>one</div>",
      # everything else
      "<div>one</div><span>three</span>",
    ]

    assert_includes(acceptable_results, result)
  end

  def test_default_attributes_removal_behavior
    assert_scrubbed '<p cooler="hello">hello</p>', "<p>hello</p>"
  end

  def test_leaves_supplied_tags
    @scrubber.tags = %w(a)
    assert_scrubbed "<a>hello</a>"
  end

  def test_leaves_only_supplied_tags
    html = "<tag>leave me <span>now</span></tag>"
    @scrubber.tags = %w(tag)
    assert_scrubbed html, "<tag>leave me now</tag>"
  end

  def test_prunes_tags
    @scrubber = Rails::HTML::PermitScrubber.new(prune: true)
    @scrubber.tags = %w(tag)
    html = "<tag>leave me <span>now</span></tag>"
    assert_scrubbed html, "<tag>leave me </tag>"
  end

  def test_leaves_comments_when_supplied_as_tag
    @scrubber.tags = %w(div comment)
    assert_scrubbed("<div>one</div><!-- two --><span>three</span>",
                    "<div>one</div><!-- two -->three")
  end

  def test_leaves_only_supplied_tags_nested
    html = "<tag>leave <em>me <span>now</span></em></tag>"
    @scrubber.tags = %w(tag)
    assert_scrubbed html, "<tag>leave me now</tag>"
  end

  def test_leaves_supplied_attributes
    @scrubber.attributes = %w(cooler)
    assert_scrubbed '<a cooler="hello"></a>'
  end

  def test_leaves_only_supplied_attributes
    @scrubber.attributes = %w(cooler)
    assert_scrubbed '<a cooler="hello" b="c" d="e"></a>', '<a cooler="hello"></a>'
  end

  def test_leaves_supplied_tags_and_attributes
    @scrubber.tags = %w(tag)
    @scrubber.attributes = %w(cooler)
    assert_scrubbed '<tag cooler="hello"></tag>'
  end

  def test_leaves_only_supplied_tags_and_attributes
    @scrubber.tags = %w(tag)
    @scrubber.attributes = %w(cooler)
    html = '<a></a><tag href=""></tag><tag cooler=""></tag>'
    assert_scrubbed html, '<tag></tag><tag cooler=""></tag>'
  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 = "<script></script><a></a><h1></h1>"
    assert_scrubbed html, "<script></script>"
  end

  def test_targeting_tags_removes_only_them_nested
    @scrubber.tags = %w(a)
    html = "<tag><a><tag><a></a></tag></a></tag>"
    assert_scrubbed html, "<tag><tag></tag></tag>"
  end

  def test_targeting_attributes_removes_only_them
    @scrubber.attributes = %w(class id)
    html = '<a class="a" id="b" onclick="c"></a>'
    assert_scrubbed html, '<a onclick="c"></a>'
  end

  def test_targeting_tags_and_attributes_removes_only_them
    @scrubber.tags = %w(tag)
    @scrubber.attributes = %w(remove)
    html = '<tag remove="" other=""></tag><a remove="" other=""></a>'
    assert_scrubbed html, '<a other=""></a>'
  end

  def test_prunes_tags
    @scrubber = Rails::HTML::TargetScrubber.new(prune: true)
    @scrubber.tags = %w(span)
    html = "<tag>leave me <span>now</span></tag>"
    assert_scrubbed html, "<tag>leave me </tag>"
  end
end

class TextOnlyScrubberTest < ScrubberTest
  def setup
    @scrubber = Rails::HTML::TextOnlyScrubber.new
  end

  def test_removes_all_tags_and_keep_the_content
    assert_scrubbed "<tag>hello</tag>", "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 "<script>remove me</script>"
  end

  def test_returns_continue_from_scrub_if_scrub_node_does
    @scrubber = ScrubContinuer.new
    assert_node_skipped "<script>keep me</script>"
  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 = "<div class='foo' href='bar'></div>"
    frag = scrub_fragment(input)
    assert_equal("<div class=\"foo\"></div>", 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 = "<div class='foo' href='bar'><svg xlink:href='asdf'><set></set></svg></div>"
    frag = scrub_fragment(input)
    assert_equal("<div class=\"foo\"></div>", frag)

    assert_equal(["div"], @scrubber.instance_variable_get(:@scrub_attributes_args))
  end
end