File: test_malicious_html.rb

package info (click to toggle)
ruby-sanitize 6.0.0-1.1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 668 kB
  • sloc: ruby: 2,995; makefile: 6
file content (339 lines) | stat: -rw-r--r-- 12,040 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# encoding: utf-8
require_relative 'common'

# Miscellaneous attempts to sneak maliciously crafted HTML past Sanitize. Many
# of these are courtesy of (or inspired by) the OWASP XSS Filter Evasion Cheat
# Sheet.
#
# https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet

describe 'Malicious HTML' do
  make_my_diffs_pretty!
  parallelize_me!

  before do
    @s = Sanitize.new(Sanitize::Config::RELAXED)
  end

  describe 'comments' do
    it 'should not allow script injection via conditional comments' do
      _(@s.fragment(%[<!--[if gte IE 4]>\n<script>alert('XSS');</script>\n<![endif]-->])).
        must_equal ''
    end
  end

  describe 'interpolation (ERB, PHP, etc.)' do
    it 'should escape ERB-style tags' do
      _(@s.fragment('<% naughty_ruby_code %>')).
        must_equal '&lt;% naughty_ruby_code %&gt;'

      _(@s.fragment('<%= naughty_ruby_code %>')).
        must_equal '&lt;%= naughty_ruby_code %&gt;'
    end

    it 'should remove PHP-style tags' do
      _(@s.fragment('<? naughtyPHPCode(); ?>')).
        must_equal ''

      _(@s.fragment('<?= naughtyPHPCode(); ?>')).
        must_equal ''
    end
  end

  describe '<body>' do
    it 'should not be possible to inject JS via a malformed event attribute' do
      _(@s.document('<html><head></head><body onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert("XSS")></body></html>')).
        must_equal "<html><head></head><body></body></html>"
    end
  end

  describe '<iframe>' do
    it 'should not be possible to inject an iframe using an improperly closed tag' do
      _(@s.fragment(%[<iframe src=http://ha.ckers.org/scriptlet.html <])).
        must_equal ''
    end
  end

  describe '<img>' do
    it 'should not be possible to inject JS via an unquoted <img> src attribute' do
      _(@s.fragment("<img src=javascript:alert('XSS')>")).must_equal '<img>'
    end

    it 'should not be possible to inject JS using grave accents as <img> src delimiters' do
      _(@s.fragment("<img src=`javascript:alert('XSS')`>")).must_equal '<img>'
    end

    it 'should not be possible to inject <script> via a malformed <img> tag' do
      _(@s.fragment('<img """><script>alert("XSS")</script>">')).
        must_equal '<img>"&gt;'
    end

    it 'should not be possible to inject protocol-based JS' do
      _(@s.fragment('<img src=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>')).
        must_equal '<img>'

      _(@s.fragment('<img src=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>')).
        must_equal '<img>'

      _(@s.fragment('<img src=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>')).
        must_equal '<img>'

      # Encoded tab character.
      _(@s.fragment(%[<img src="jav&#x09;ascript:alert('XSS');">])).
        must_equal '<img>'

      # Encoded newline.
      _(@s.fragment(%[<img src="jav&#x0A;ascript:alert('XSS');">])).
        must_equal '<img>'

      # Encoded carriage return.
      _(@s.fragment(%[<img src="jav&#x0D;ascript:alert('XSS');">])).
        must_equal '<img>'

      # Null byte.
      _(@s.fragment(%[<img src=java\0script:alert("XSS")>])).
        must_equal '<img>'

      # Spaces plus meta char.
      _(@s.fragment(%[<img src=" &#14;  javascript:alert('XSS');">])).
        must_equal '<img>'

      # Mixed spaces and tabs.
      _(@s.fragment(%[<img src="j\na v\tascript://alert('XSS');">])).
        must_equal '<img>'
    end

    it 'should not be possible to inject protocol-based JS via whitespace' do
      _(@s.fragment(%[<img src="jav\tascript:alert('XSS');">])).
        must_equal '<img>'
    end

    it 'should not be possible to inject JS using a half-open <img> tag' do
      _(@s.fragment(%[<img src="javascript:alert('XSS')"])).
        must_equal ''
    end
  end

  describe '<script>' do
    it 'should not be possible to inject <script> using a malformed non-alphanumeric tag name' do
      _(@s.fragment(%[<script/xss src="http://ha.ckers.org/xss.js">alert(1)</script>])).
        must_equal ''
    end

    it 'should not be possible to inject <script> via extraneous open brackets' do
      _(@s.fragment(%[<<script>alert("XSS");//<</script>])).
        must_equal '&lt;'
    end
  end

  # libxml2 >= 2.9.2 doesn't escape comments within some attributes, in an
  # attempt to preserve server-side includes. This can result in XSS since an
  # unescaped double quote can allow an attacker to inject a non-allowlisted
  # attribute. Sanitize works around this by implementing its own escaping for
  # affected attributes.
  #
  # The relevant libxml2 code is here:
  # <https://github.com/GNOME/libxml2/commit/960f0e275616cadc29671a218d7fb9b69eb35588>
  describe 'unsafe libxml2 server-side includes in attributes' do
    using_unpatched_libxml2 = Nokogiri::VersionInfo.instance.libxml2_using_system?

    tag_configs = [
      {
        tag_name: 'a',
        escaped_attrs: %w[ action href src name ],
        unescaped_attrs: []
      },

      {
        tag_name: 'div',
        escaped_attrs: %w[ action href src ],
        unescaped_attrs: %w[ name ]
      }
    ]

    before do
      @s = Sanitize.new({
        elements: %w[ a div ],

        attributes: {
          all: %w[ action href src name ]
        }
      })
    end

    tag_configs.each do |tag_config|
      tag_name = tag_config[:tag_name]

      tag_config[:escaped_attrs].each do |attr_name|
        input = %[<#{tag_name} #{attr_name}='examp<!--" onmouseover=alert(1)>-->le.com'>foo</#{tag_name}>]

        it 'should escape unsafe characters in attributes' do
          skip "behavior should only exist in nokogiri's patched libxml" if using_unpatched_libxml2

          # This uses Nokogumbo's HTML-compliant serializer rather than
          # libxml2's.
          _(@s.fragment(input)).
            must_equal(%[<#{tag_name} #{attr_name}="examp<!--%22%20onmouseover=alert(1)>-->le.com">foo</#{tag_name}>])

          # This uses the not-quite-standards-compliant libxml2 serializer via
          # Nokogiri, so the output may be a little different as of Nokogiri
          # 1.10.2 when using Nokogiri's vendored libxml2 due to this patch:
          # https://github.com/sparklemotion/nokogiri/commit/4852e43cb6039e26d8c51af78621e539cbf46c5d
          fragment = Nokogiri::HTML.fragment(input)
          @s.node!(fragment)
          _(fragment.to_html).
            must_equal(%[<#{tag_name} #{attr_name}="examp&lt;!--%22%20onmouseover=alert(1)&gt;--&gt;le.com">foo</#{tag_name}>])
        end

        it 'should round-trip to the same output' do
          output = @s.fragment(input)
          _(@s.fragment(output)).must_equal(output)
        end
      end

      tag_config[:unescaped_attrs].each do |attr_name|
        input = %[<#{tag_name} #{attr_name}='examp<!--" onmouseover=alert(1)>-->le.com'>foo</#{tag_name}>]

        it 'should not escape characters unnecessarily' do
          skip "behavior should only exist in nokogiri's patched libxml" if using_unpatched_libxml2

          # This uses Nokogumbo's HTML-compliant serializer rather than
          # libxml2's.
          _(@s.fragment(input)).
            must_equal(%[<#{tag_name} #{attr_name}="examp<!--&quot; onmouseover=alert(1)>-->le.com">foo</#{tag_name}>])

          # This uses the not-quite-standards-compliant libxml2 serializer via
          # Nokogiri, so the output may be a little different as of Nokogiri
          # 1.10.2 when using Nokogiri's vendored libxml2 due to this patch:
          # https://github.com/sparklemotion/nokogiri/commit/4852e43cb6039e26d8c51af78621e539cbf46c5d
          fragment = Nokogiri::HTML.fragment(input)
          @s.node!(fragment)
          _(fragment.to_html).
            must_equal(%[<#{tag_name} #{attr_name}='examp&lt;!--" onmouseover=alert(1)&gt;--&gt;le.com'>foo</#{tag_name}>])
        end

        it 'should round-trip to the same output' do
          output = @s.fragment(input)
          _(@s.fragment(output)).must_equal(output)
        end
      end
    end
  end

  # https://github.com/rgrove/sanitize/security/advisories/GHSA-p4x4-rw2p-8j8m
  describe 'foreign content bypass in relaxed config' do
    it 'prevents a sanitization bypass via carefully crafted foreign content' do
      %w[iframe noembed noframes noscript plaintext script style xmp].each do |tag_name|
        _(@s.fragment(%[<math><#{tag_name}>/*&lt;/#{tag_name}&gt;&lt;img src onerror=alert(1)>*/])).
          must_equal ''

        _(@s.fragment(%[<svg><#{tag_name}>/*&lt;/#{tag_name}&gt;&lt;img src onerror=alert(1)>*/])).
          must_equal ''
      end
    end
  end

  # These tests cover an unsupported and unsafe custom config that allows MathML
  # and SVG elements, which Sanitize's docs specifically say multiple times in
  # big prominent warnings that you SHOULD NOT DO because Sanitize doesn't
  # support MathML or SVG.
  #
  # Do not use the custom configs you see in these tests! If you do, you may be
  # creating XSS vulnerabilities in your application.
  describe 'foreign content bypass in unsafe custom config that allows MathML or SVG' do
    unescaped_content_elements = %w[
      noembed
      noframes
      plaintext
      script
      xmp
    ]

    removed_content_elements = %w[
      iframe
    ]

    removed_elements = %w[
      noscript
      style
    ]

    before do
      @s = Sanitize.new(
        Sanitize::Config.merge(
          Sanitize::Config::RELAXED,
          elements: Sanitize::Config::RELAXED[:elements] +
            unescaped_content_elements +
            removed_content_elements +
            %w[math svg]
        )
      )
    end

    unescaped_content_elements.each do |name|
      it "forcibly escapes text content inside `<#{name}>` in a MathML namespace" do
        assert_equal(
          "<math><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}></math>",
          @s.fragment("<math><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}>")
        )
      end

      it "forcibly escapes text content inside `<#{name}>` in an SVG namespace" do
        assert_equal(
          "<svg><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}></svg>",
          @s.fragment("<svg><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}>")
        )
      end
    end

    removed_content_elements.each do |name|
      it "removes text content inside `<#{name}>` in a MathML namespace" do
        assert_equal(
          "<math><#{name}></#{name}></math>",
          @s.fragment("<math><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}>")
        )
      end

      it "removes text content inside `<#{name}>` in an SVG namespace" do
        assert_equal(
          "<svg><#{name}></#{name}></svg>",
          @s.fragment("<svg><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}>")
        )
      end
    end

    removed_elements.each do |name|
      it "removes `<#{name}>` elements in a MathML namespace" do
        assert_equal(
          '<math></math>',
          @s.fragment("<math><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}>")
        )
      end

      it "removes `<#{name}>` elements in an SVG namespace" do
        assert_equal(
          '<svg></svg>',
          @s.fragment("<svg><#{name}>&lt;img src=x onerror=alert(1)&gt;</#{name}>")
        )
      end
    end
  end

  describe 'sanitization bypass by exploiting scripting-disabled <noscript> behavior' do
    before do
      @s = Sanitize.new(
        Sanitize::Config.merge(
          Sanitize::Config::RELAXED,
          elements: Sanitize::Config::RELAXED[:elements] + ['noscript']
        )
      )
    end

    it 'is prevented by removing `<noscript>` elements regardless of the allowlist' do
      assert_equal(
        '',
        @s.fragment(%[<noscript><div id='</noscript>&lt;img src=x onerror=alert(1)&gt; '>])
      )
    end
  end
end