File: test_sanitize_css.rb

package info (click to toggle)
ruby-sanitize 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: ruby: 3,153; makefile: 6
file content (468 lines) | stat: -rw-r--r-- 15,540 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# frozen_string_literal: true

require_relative "common"

describe "Sanitize::CSS" do
  make_my_diffs_pretty!
  parallelize_me!

  describe "instance methods" do
    before do
      @default = Sanitize::CSS.new
      @relaxed = Sanitize::CSS.new(Sanitize::Config::RELAXED[:css])
      @custom = Sanitize::CSS.new(properties: %w[background color width])
    end

    describe "#properties" do
      it "should sanitize CSS properties" do
        css = 'background: #fff; width: expression(alert("hi"));'

        _(@default.properties(css)).must_equal " "
        _(@relaxed.properties(css)).must_equal "background: #fff; "
        _(@custom.properties(css)).must_equal "background: #fff; "
      end

      it "should allow allowlisted URL protocols" do
        [
          "background: url(relative.jpg)",
          "background: url('relative.jpg')",
          "background: url(http://example.com/http.jpg)",
          "background: url('ht\\tp://example.com/http.jpg')",
          "background: url(https://example.com/https.jpg)",
          "background: url('https://example.com/https.jpg')",
          "background: image-set('relative.jpg' 1x, 'relative-2x.jpg' 2x)",
          "background: image-set('https://example.com/https.jpg' 1x, 'https://example.com/https-2x.jpg' 2x)",
          "background: image-set('https://example.com/https.jpg' type('image/jpeg'), 'https://example.com/https.avif' type('image/avif'))",
          "background: -webkit-image-set('relative.jpg' 1x, 'relative-2x.jpg' 2x)",
          "background: -webkit-image-set('https://example.com/https.jpg' 1x, 'https://example.com/https-2x.jpg' 2x)",
          "background: -webkit-image-set('https://example.com/https.jpg' type('image/jpeg'), 'https://example.com/https.avif' type('image/avif'))",
          "background: image('relative.jpg');",
          "background: image('https://example.com/https.jpg');",
          "background: image(rtl 'https://example.com/https.jpg');"
        ].each do |css|
          _(@default.properties(css)).must_equal ""
          _(@relaxed.properties(css)).must_equal css
          _(@custom.properties(css)).must_equal ""
        end
      end

      it "should not allow non-allowlisted URL protocols" do
        [
          "background: url(javascript:alert(0))",
          "background: url(ja\\56 ascript:alert(0))",
          "background: url('javascript:foo')",
          "background: url('ja\\56 ascript:alert(0)')",
          "background: url('ja\\va\\script\\:alert(0)')",
          "background: url('javas\\\ncript:alert(0)')",
          "background: url('java\\0script:foo')"
        ].each do |css|
          _(@default.properties(css)).must_equal ""
          _(@relaxed.properties(css)).must_equal ""
          _(@custom.properties(css)).must_equal ""
        end
      end

      it "should not allow -moz-binding" do
        css = "-moz-binding:url('http://ha.ckers.org/xssmoz.xml#xss')"

        _(@default.properties(css)).must_equal ""
        _(@relaxed.properties(css)).must_equal ""
        _(@custom.properties(css)).must_equal ""
      end

      it "should not allow expressions" do
        [
          "width:expression(alert(1))",
          "width:  /**/expression(alert(1)",
          "width:e\\78 pression(\n\nalert(\n1)",
          "width:\nexpression(alert(1));",
          "xss:expression(alert(1))",
          "height: foo(expression(alert(1)));"
        ].each do |css|
          _(@default.properties(css)).must_equal ""
          _(@relaxed.properties(css)).must_equal ""
          _(@custom.properties(css)).must_equal ""
        end
      end

      it "should not allow behaviors" do
        css = "behavior: url(xss.htc);"

        _(@default.properties(css)).must_equal ""
        _(@relaxed.properties(css)).must_equal ""
        _(@custom.properties(css)).must_equal ""
      end

      describe "when :allow_comments is true" do
        it "should preserve comments" do
          _(@relaxed.properties("color: #fff; /* comment */ width: 100px;"))
            .must_equal "color: #fff; /* comment */ width: 100px;"

          _(@relaxed.properties("color: #fff; /* \n\ncomment */ width: 100px;"))
            .must_equal "color: #fff; /* \n\ncomment */ width: 100px;"
        end
      end

      describe "when :allow_comments is false" do
        it "should strip comments" do
          _(@custom.properties("color: #fff; /* comment */ width: 100px;"))
            .must_equal "color: #fff;  width: 100px;"

          _(@custom.properties("color: #fff; /* \n\ncomment */ width: 100px;"))
            .must_equal "color: #fff;  width: 100px;"
        end
      end

      describe "when :allow_hacks is true" do
        it "should allow common CSS hacks" do
          _(@relaxed.properties("_border: 1px solid #fff; *width: 10px"))
            .must_equal "_border: 1px solid #fff; *width: 10px"
        end
      end

      describe "when :allow_hacks is false" do
        it "should not allow common CSS hacks" do
          _(@custom.properties("_border: 1px solid #fff; *width: 10px"))
            .must_equal " "
        end
      end
    end

    describe "#stylesheet" do
      it "should sanitize a CSS stylesheet" do
        css = %[
          /* Yay CSS! */
          .foo { color: #fff; }
          #bar { background: url(yay.jpg); }

          @media screen (max-width:480px) {
            .foo { width: 400px; }
            #bar:not(.baz) { height: 100px; }
          }
        ].strip

        _(@default.stylesheet(css).strip).must_equal %(
          .foo {  }
          #bar {  }
        ).strip

        _(@relaxed.stylesheet(css)).must_equal css

        _(@custom.stylesheet(css).strip).must_equal %(
          .foo { color: #fff; }
          #bar {  }
        ).strip
      end

      describe "when :allow_comments is true" do
        it "should preserve comments" do
          _(@relaxed.stylesheet(".foo { color: #fff; /* comment */ width: 100px; }"))
            .must_equal ".foo { color: #fff; /* comment */ width: 100px; }"

          _(@relaxed.stylesheet(".foo { color: #fff; /* \n\ncomment */ width: 100px; }"))
            .must_equal ".foo { color: #fff; /* \n\ncomment */ width: 100px; }"
        end
      end

      describe "when :allow_comments is false" do
        it "should strip comments" do
          _(@custom.stylesheet(".foo { color: #fff; /* comment */ width: 100px; }"))
            .must_equal ".foo { color: #fff;  width: 100px; }"

          _(@custom.stylesheet(".foo { color: #fff; /* \n\ncomment */ width: 100px; }"))
            .must_equal ".foo { color: #fff;  width: 100px; }"
        end
      end

      describe "when :allow_hacks is true" do
        it "should allow common CSS hacks" do
          _(@relaxed.stylesheet(".foo { _border: 1px solid #fff; *width: 10px }"))
            .must_equal ".foo { _border: 1px solid #fff; *width: 10px }"
        end
      end

      describe "when :allow_hacks is false" do
        it "should not allow common CSS hacks" do
          _(@custom.stylesheet(".foo { _border: 1px solid #fff; *width: 10px }"))
            .must_equal ".foo {  }"
        end
      end
    end

    describe "#tree!" do
      it "should sanitize a Crass CSS parse tree" do
        tree = Crass.parse("@import url(foo.css);\n" \
          ".foo { background: #fff; font: 16pt 'Comic Sans MS'; }\n" \
          "#bar { top: 125px; background: green; }")

        _(@custom.tree!(tree)).must_be_same_as tree

        _(Crass::Parser.stringify(tree)).must_equal "\n" \
          ".foo { background: #fff;  }\n" \
          "#bar {  background: green; }"
      end
    end
  end

  describe "class methods" do
    describe ".properties" do
      it "should sanitize CSS properties with the given config" do
        css = 'background: #fff; width: expression(alert("hi"));'

        _(Sanitize::CSS.properties(css)).must_equal " "
        _(Sanitize::CSS.properties(css, Sanitize::Config::RELAXED[:css])).must_equal "background: #fff; "
        _(Sanitize::CSS.properties(css, properties: %w[background color width])).must_equal "background: #fff; "
      end
    end

    describe ".stylesheet" do
      it "should sanitize a CSS stylesheet with the given config" do
        css = %[
          /* Yay CSS! */
          .foo { color: #fff; }
          #bar { background: url(yay.jpg); }

          @media screen (max-width:480px) {
            .foo { width: 400px; }
            #bar:not(.baz) { height: 100px; }
          }
        ].strip

        _(Sanitize::CSS.stylesheet(css).strip).must_equal %(
          .foo {  }
          #bar {  }
        ).strip

        _(Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED[:css])).must_equal css

        _(Sanitize::CSS.stylesheet(css, properties: %w[background color width]).strip).must_equal %(
          .foo { color: #fff; }
          #bar {  }
        ).strip
      end
    end

    describe ".tree!" do
      it "should sanitize a Crass CSS parse tree with the given config" do
        tree = Crass.parse("@import url(foo.css);\n" \
          ".foo { background: #fff; font: 16pt 'Comic Sans MS'; }\n" \
          "#bar { top: 125px; background: green; }")

        _(Sanitize::CSS.tree!(tree, properties: %w[background color width])).must_be_same_as tree

        _(Crass::Parser.stringify(tree)).must_equal "\n" \
          ".foo { background: #fff;  }\n" \
          "#bar {  background: green; }"
      end
    end
  end

  describe "functionality" do
    before do
      @default = Sanitize::CSS.new
      @relaxed = Sanitize::CSS.new(Sanitize::Config::RELAXED[:css])
    end

    # https://github.com/rgrove/sanitize/issues/121
    it "should parse the contents of @media rules properly" do
      css = '@media { p[class="center"] { text-align: center; }}'
      _(@relaxed.stylesheet(css)).must_equal css

      css = %[
        @media (max-width: 720px) {
          p.foo > .bar { float: right; width: expression(body.scrollLeft + 50 + 'px'); }
          #baz { color: green; }

          @media (orientation: portrait) {
            #baz { color: red; }
          }
        }
      ].strip

      _(@relaxed.stylesheet(css)).must_equal %[
        @media (max-width: 720px) {
          p.foo > .bar { float: right;  }
          #baz { color: green; }

          @media (orientation: portrait) {
            #baz { color: red; }
          }
        }
      ].strip
    end

    it "should parse @page rules properly" do
      css = %[
        @page { margin: 2cm } /* All margins set to 2cm */

        @page :right {
          @top-center { content: "Preliminary edition" }
          @bottom-center { content: counter(page) }
        }

        @page {
          size: 8.5in 11in;
          margin: 10%;

          @top-left {
            content: "Hamlet";
          }
          @top-right {
            content: "Page " counter(page);
          }
        }
      ].strip

      _(@relaxed.stylesheet(css)).must_equal css
    end

    describe ":at_rules" do
      it "should remove blockless at-rules that aren't allowlisted" do
        css = %[
          @charset 'utf-8';
          @import url('foo.css');
          .foo { color: green; }
        ].strip

        _(@relaxed.stylesheet(css).strip).must_equal %(
          .foo { color: green; }
        ).strip
      end

      it "preserves allowlisted @container at-rules" do
        # Sample code courtesy of MDN:
        # https://developer.mozilla.org/en-US/docs/Web/CSS/@container
        css = %(
          @container (width > 400px) {
            h2 {
              font-size: 1.5em;
            }
          }

          /* with an optional <container-name> */
          @container tall (height > 30rem) {
            h2 {
              line-height: 1.6;
            }
          }

          /* multiple queries in a single condition */
          @container (width > 400px) and style(--responsive: true) {
            h2 {
              font-size: 1.5em;
            }
          }

          /* condition list */
          @container card (width > 400px), style(--responsive: true) {
            h2 {
              font-size: 1.5em;
            }
          }
        ).strip

        _(@relaxed.stylesheet(css).strip).must_equal css
      end

      describe "when blockless at-rules are allowlisted" do
        before do
          @scss = Sanitize::CSS.new(Sanitize::Config.merge(Sanitize::Config::RELAXED[:css], {
            at_rules: ["charset", "import"]
          }))
        end

        it "should not remove them" do
          css = %[
            @charset 'utf-8';
            @import url('foo.css');
            .foo { color: green; }
          ].strip

          _(@scss.stylesheet(css)).must_equal %[
            @charset 'utf-8';
            @import url('foo.css');
            .foo { color: green; }
          ].strip
        end

        it "should remove them if they have invalid blocks" do
          css = %(
            @charset { color: green }
            @import { color: green }
            .foo { color: green; }
          ).strip

          _(@scss.stylesheet(css).strip).must_equal %(
            .foo { color: green; }
          ).strip
        end
      end

      describe "when validating @import rules" do
        describe "with no validation proc specified" do
          before do
            @scss = Sanitize::CSS.new(Sanitize::Config.merge(Sanitize::Config::RELAXED[:css], {
              at_rules: ["import"]
            }))
          end

          it "should allow any URL value" do
            css = %[
              @import url('https://somesite.com/something.css');
            ].strip

            _(@scss.stylesheet(css).strip).must_equal %[
              @import url('https://somesite.com/something.css');
            ].strip
          end
        end

        describe "with a validation proc specified" do
          before do
            google_font_validator = proc { |url| url.start_with?("https://fonts.googleapis.com") }

            @scss = Sanitize::CSS.new(Sanitize::Config.merge(Sanitize::Config::RELAXED[:css], {
              at_rules: ["import"], import_url_validator: google_font_validator
            }))
          end

          it "should allow a google fonts url" do
            css = %[
              @import 'https://fonts.googleapis.com/css?family=Indie+Flower';
              @import url('https://fonts.googleapis.com/css?family=Indie+Flower');
            ].strip

            _(@scss.stylesheet(css).strip).must_equal %[
              @import 'https://fonts.googleapis.com/css?family=Indie+Flower';
              @import url('https://fonts.googleapis.com/css?family=Indie+Flower');
            ].strip
          end

          it "should not allow a nasty url" do
            css = %[
              @import 'https://fonts.googleapis.com/css?family=Indie+Flower';
              @import 'https://nastysite.com/nasty_hax0r.css';
              @import url('https://nastysite.com/nasty_hax0r.css');
            ].strip

            _(@scss.stylesheet(css).strip).must_equal %(
              @import 'https://fonts.googleapis.com/css?family=Indie+Flower';
            ).strip
          end

          it "should not allow a blank url" do
            css = %[
              @import 'https://fonts.googleapis.com/css?family=Indie+Flower';
              @import '';
              @import url('');
            ].strip

            _(@scss.stylesheet(css).strip).must_equal %(
              @import 'https://fonts.googleapis.com/css?family=Indie+Flower';
            ).strip
          end
        end
      end
    end
  end
end