sanitize 6.1.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sanitize/css.rb CHANGED
@@ -1,321 +1,427 @@
1
- # encoding: utf-8
2
-
3
- require 'crass'
4
- require 'set'
5
-
6
- class Sanitize; class CSS
7
- attr_reader :config
8
-
9
- # -- Class Methods -----------------------------------------------------------
10
-
11
- # Sanitizes inline CSS style properties.
12
- #
13
- # This is most useful for sanitizing non-stylesheet fragments of CSS like you
14
- # would find in the `style` attribute of an HTML element. To sanitize a full
15
- # CSS stylesheet, use {.stylesheet}.
16
- #
17
- # @example
18
- # Sanitize::CSS.properties("background: url(foo.png); color: #fff;")
19
- #
20
- # @return [String] Sanitized CSS properties.
21
- def self.properties(css, config = {})
22
- self.new(config).properties(css)
23
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "crass"
4
+
5
+ class Sanitize
6
+ class CSS
7
+ attr_reader :config
8
+
9
+ # -- Class Methods ---------------------------------------------------------
10
+
11
+ # Sanitizes inline CSS style properties.
12
+ #
13
+ # This is most useful for sanitizing non-stylesheet fragments of CSS like
14
+ # you would find in the `style` attribute of an HTML element. To sanitize a
15
+ # full CSS stylesheet, use {.stylesheet}.
16
+ #
17
+ # @example
18
+ # Sanitize::CSS.properties("background: url(foo.png); color: #fff;")
19
+ #
20
+ # @return [String] Sanitized CSS properties.
21
+ def self.properties(css, config = {})
22
+ new(config).properties(css)
23
+ end
24
24
 
25
- # Sanitizes a full CSS stylesheet.
26
- #
27
- # A stylesheet may include selectors, at-rules, and comments. To sanitize only
28
- # inline style properties such as the contents of an HTML `style` attribute,
29
- # use {.properties}.
30
- #
31
- # @example
32
- # css = %[
33
- # .foo {
34
- # background: url(foo.png);
35
- # color: #fff;
36
- # }
37
- #
38
- # #bar {
39
- # font: 42pt 'Comic Sans MS';
40
- # }
41
- # ]
42
- #
43
- # Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED)
44
- #
45
- # @return [String] Sanitized CSS stylesheet.
46
- def self.stylesheet(css, config = {})
47
- self.new(config).stylesheet(css)
48
- end
25
+ # Sanitizes a full CSS stylesheet.
26
+ #
27
+ # A stylesheet may include selectors, at-rules, and comments. To sanitize
28
+ # only inline style properties such as the contents of an HTML `style`
29
+ # attribute, use {.properties}.
30
+ #
31
+ # @example
32
+ # css = %[
33
+ # .foo {
34
+ # background: url(foo.png);
35
+ # color: #fff;
36
+ # }
37
+ #
38
+ # #bar {
39
+ # font: 42pt 'Comic Sans MS';
40
+ # }
41
+ # ]
42
+ #
43
+ # Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED)
44
+ #
45
+ # @return [String] Sanitized CSS stylesheet.
46
+ def self.stylesheet(css, config = {})
47
+ new(config).stylesheet(css)
48
+ end
49
49
 
50
- # Sanitizes the given Crass CSS parse tree and all its children, modifying it
51
- # in place.
52
- #
53
- # @example
54
- # css = %[
55
- # .foo {
56
- # background: url(foo.png);
57
- # color: #fff;
58
- # }
59
- #
60
- # #bar {
61
- # font: 42pt 'Comic Sans MS';
62
- # }
63
- # ]
64
- #
65
- # tree = Crass.parse(css)
66
- # Sanitize::CSS.tree!(tree, Sanitize::Config::RELAXED)
67
- #
68
- # @return [Array] Sanitized Crass CSS parse tree.
69
- def self.tree!(tree, config = {})
70
- self.new(config).tree!(tree)
71
- end
50
+ # Sanitizes the given Crass CSS parse tree and all its children, modifying
51
+ # it in place.
52
+ #
53
+ # @example
54
+ # css = %[
55
+ # .foo {
56
+ # background: url(foo.png);
57
+ # color: #fff;
58
+ # }
59
+ #
60
+ # #bar {
61
+ # font: 42pt 'Comic Sans MS';
62
+ # }
63
+ # ]
64
+ #
65
+ # tree = Crass.parse(css)
66
+ # Sanitize::CSS.tree!(tree, Sanitize::Config::RELAXED)
67
+ #
68
+ # The tree must be a list of rules, such as the tree returned by
69
+ # `Crass.parse`. To sanitize a list of declarations, such as the contents
70
+ # of an HTML `style` attribute, use {.properties}.
71
+ #
72
+ # @return [Array] Sanitized Crass CSS parse tree.
73
+ def self.tree!(tree, config = {})
74
+ new(config).tree!(tree)
75
+ end
72
76
 
73
- # -- Instance Methods --------------------------------------------------------
77
+ # -- Instance Methods ------------------------------------------------------
74
78
 
75
- # Returns a new Sanitize::CSS object initialized with the settings in
76
- # _config_.
77
- def initialize(config = {})
78
- @config = Config.merge(Config::DEFAULT[:css], config[:css] || config)
79
+ # Returns a new Sanitize::CSS object initialized with the settings in
80
+ # _config_.
81
+ def initialize(config = {})
82
+ @config = Config.merge(Config::DEFAULT[:css], config[:css] || config)
79
83
 
80
- @at_rules = Set.new(@config[:at_rules])
81
- @at_rules_with_properties = Set.new(@config[:at_rules_with_properties])
82
- @at_rules_with_styles = Set.new(@config[:at_rules_with_styles])
83
- @import_url_validator = @config[:import_url_validator]
84
- end
84
+ @at_rules = Set.new(@config[:at_rules])
85
+ @at_rules_with_properties = Set.new(@config[:at_rules_with_properties])
86
+ @at_rules_with_styles = Set.new(@config[:at_rules_with_styles])
87
+ @import_url_validator = @config[:import_url_validator]
85
88
 
86
- # Sanitizes inline CSS style properties.
87
- #
88
- # This is most useful for sanitizing non-stylesheet fragments of CSS like you
89
- # would find in the `style` attribute of an HTML element. To sanitize a full
90
- # CSS stylesheet, use {#stylesheet}.
91
- #
92
- # @example
93
- # scss = Sanitize::CSS.new(Sanitize::Config::RELAXED)
94
- # scss.properties("background: url(foo.png); color: #fff;")
95
- #
96
- # @return [String] Sanitized CSS properties.
97
- def properties(css)
98
- tree = Crass.parse_properties(css,
99
- :preserve_comments => @config[:allow_comments],
100
- :preserve_hacks => @config[:allow_hacks])
101
-
102
- tree!(tree)
103
- Crass::Parser.stringify(tree)
104
- end
89
+ @parse_options = {
90
+ preserve_comments: @config[:allow_comments],
91
+ preserve_hacks: @config[:allow_hacks]
92
+ }.freeze
93
+ end
105
94
 
106
- # Sanitizes a full CSS stylesheet.
107
- #
108
- # A stylesheet may include selectors, at-rules, and comments. To sanitize only
109
- # inline style properties such as the contents of an HTML `style` attribute,
110
- # use {#properties}.
111
- #
112
- # @example
113
- # css = %[
114
- # .foo {
115
- # background: url(foo.png);
116
- # color: #fff;
117
- # }
118
- #
119
- # #bar {
120
- # font: 42pt 'Comic Sans MS';
121
- # }
122
- # ]
123
- #
124
- # scss = Sanitize::CSS.new(Sanitize::Config::RELAXED)
125
- # scss.stylesheet(css)
126
- #
127
- # @return [String] Sanitized CSS stylesheet.
128
- def stylesheet(css)
129
- tree = Crass.parse(css,
130
- :preserve_comments => @config[:allow_comments],
131
- :preserve_hacks => @config[:allow_hacks])
132
-
133
- tree!(tree)
134
- Crass::Parser.stringify(tree)
135
- end
95
+ # Sanitizes inline CSS style properties.
96
+ #
97
+ # This is most useful for sanitizing non-stylesheet fragments of CSS like
98
+ # you would find in the `style` attribute of an HTML element. To sanitize a
99
+ # full CSS stylesheet, use {#stylesheet}.
100
+ #
101
+ # @example
102
+ # scss = Sanitize::CSS.new(Sanitize::Config::RELAXED)
103
+ # scss.properties("background: url(foo.png); color: #fff;")
104
+ #
105
+ # @return [String] Sanitized CSS properties.
106
+ def properties(css)
107
+ tree = Crass.parse_properties(css, @parse_options)
108
+ sanitize_tree!(tree, declarations: true)
109
+ Crass::Parser.stringify(tree)
110
+ end
136
111
 
137
- # Sanitizes the given Crass CSS parse tree and all its children, modifying it
138
- # in place.
139
- #
140
- # @example
141
- # css = %[
142
- # .foo {
143
- # background: url(foo.png);
144
- # color: #fff;
145
- # }
146
- #
147
- # #bar {
148
- # font: 42pt 'Comic Sans MS';
149
- # }
150
- # ]
151
- #
152
- # scss = Sanitize::CSS.new(Sanitize::Config::RELAXED)
153
- # tree = Crass.parse(css)
154
- #
155
- # scss.tree!(tree)
156
- #
157
- # @return [Array] Sanitized Crass CSS parse tree.
158
- def tree!(tree)
159
- preceded_by_property = false
160
-
161
- tree.map! do |node|
162
- next nil if node.nil?
163
-
164
- case node[:node]
165
- when :at_rule
166
- preceded_by_property = false
167
- next at_rule!(node)
168
-
169
- when :comment
170
- next node if @config[:allow_comments]
171
-
172
- when :property
173
- prop = property!(node)
174
- preceded_by_property = !prop.nil?
175
- next prop
176
-
177
- when :semicolon
178
- # Only preserve the semicolon if it was preceded by an allowlisted
179
- # property. Otherwise, omit it in order to prevent redundant semicolons.
180
- if preceded_by_property
181
- preceded_by_property = false
182
- next node
112
+ # Sanitizes a full CSS stylesheet.
113
+ #
114
+ # A stylesheet may include selectors, at-rules, and comments. To sanitize
115
+ # only inline style properties such as the contents of an HTML `style`
116
+ # attribute, use {#properties}.
117
+ #
118
+ # @example
119
+ # css = %[
120
+ # .foo {
121
+ # background: url(foo.png);
122
+ # color: #fff;
123
+ # }
124
+ #
125
+ # #bar {
126
+ # font: 42pt 'Comic Sans MS';
127
+ # }
128
+ # ]
129
+ #
130
+ # scss = Sanitize::CSS.new(Sanitize::Config::RELAXED)
131
+ # scss.stylesheet(css)
132
+ #
133
+ # @return [String] Sanitized CSS stylesheet.
134
+ def stylesheet(css)
135
+ tree = Crass.parse(css, @parse_options)
136
+ tree!(tree)
137
+ Crass::Parser.stringify(tree)
138
+ end
139
+
140
+ # Sanitizes the given Crass CSS parse tree and all its children, modifying
141
+ # it in place.
142
+ #
143
+ # @example
144
+ # css = %[
145
+ # .foo {
146
+ # background: url(foo.png);
147
+ # color: #fff;
148
+ # }
149
+ #
150
+ # #bar {
151
+ # font: 42pt 'Comic Sans MS';
152
+ # }
153
+ # ]
154
+ #
155
+ # scss = Sanitize::CSS.new(Sanitize::Config::RELAXED)
156
+ # tree = Crass.parse(css)
157
+ #
158
+ # scss.tree!(tree)
159
+ #
160
+ # The tree must be a list of rules, such as the tree returned by
161
+ # `Crass.parse`. To sanitize a list of declarations, such as the contents
162
+ # of an HTML `style` attribute, use {#properties}.
163
+ #
164
+ # @return [Array] Sanitized Crass CSS parse tree.
165
+ def tree!(tree)
166
+ sanitize_tree!(tree, declarations: false)
167
+ end
168
+
169
+ # -- Protected Instance Methods --------------------------------------------
170
+ protected
171
+
172
+ # Sanitizes a CSS at-rule node. Returns the sanitized node, or `nil` if the
173
+ # current config doesn't allow this at-rule.
174
+ #
175
+ # If _declarations_ is `true`, the at-rule appears in a list of
176
+ # declarations (such as the contents of a style rule) rather than in a list
177
+ # of rules.
178
+ def at_rule!(rule, declarations:)
179
+ name = rule[:name].downcase
180
+
181
+ return nil if contains_invalid_node?(rule[:prelude])
182
+
183
+ if @at_rules_with_styles.include?(name)
184
+ # Browsers that support CSS nesting parse the block of an at-rule like
185
+ # `@media` that appears in a list of declarations as more declarations
186
+ # rather than as rules, so it must be sanitized as declarations.
187
+ # Otherwise a declaration like `position:fixed;a{}` would be parsed as
188
+ # a style rule with the selector `position:fixed;a` and would bypass
189
+ # the property allowlist.
190
+ styles = if declarations
191
+ Crass::Parser.parse_properties(rule[:block], @parse_options)
192
+ else
193
+ Crass::Parser.parse_rules(rule[:block], @parse_options)
183
194
  end
184
195
 
185
- when :style_rule
186
- preceded_by_property = false
187
- tree!(node[:children])
188
- next node
196
+ rule[:block] = sanitize_tree!(styles, declarations: declarations)
197
+
198
+ elsif @at_rules_with_properties.include?(name)
199
+ props = Crass::Parser.parse_properties(rule[:block], @parse_options)
200
+ rule[:block] = sanitize_tree!(props, declarations: true)
189
201
 
190
- when :whitespace
191
- next node
202
+ elsif @at_rules.include?(name)
203
+ return nil if name == "import" && !import_url_allowed?(rule)
204
+ return nil if rule.has_key?(:block)
205
+ else
206
+ return nil
192
207
  end
193
208
 
194
- nil
209
+ rule
195
210
  end
196
211
 
197
- tree
198
- end
212
+ # Returns `true` if the given array of Crass nodes contains an invalid node
213
+ # (see {#invalid_node?}) at any depth.
214
+ def contains_invalid_node?(nodes)
215
+ contains_node?(nodes) { |node| invalid_node?(node) }
216
+ end
199
217
 
200
- # -- Protected Instance Methods ----------------------------------------------
201
- protected
218
+ # Returns `true` if the given array of Crass nodes contains a node at any
219
+ # depth for which the given block returns a truthy value.
220
+ def contains_node?(nodes)
221
+ return false unless Array === nodes
202
222
 
203
- # Sanitizes a CSS at-rule node. Returns the sanitized node, or `nil` if the
204
- # current config doesn't allow this at-rule.
205
- def at_rule!(rule)
206
- name = rule[:name].downcase
223
+ stack = nodes.dup
207
224
 
208
- if @at_rules_with_styles.include?(name)
209
- styles = Crass::Parser.parse_rules(rule[:block],
210
- :preserve_comments => @config[:allow_comments],
211
- :preserve_hacks => @config[:allow_hacks])
225
+ until stack.empty?
226
+ node = stack.pop
227
+ next unless Hash === node
228
+ return true if yield(node)
229
+
230
+ value = node[:value]
231
+ stack.concat(value) if Array === value
232
+ end
233
+
234
+ false
235
+ end
236
+
237
+ # Returns `true` if the given CSS function name is an image-related function
238
+ # that may contain image URLs that need to be validated.
239
+ def image_function?(name)
240
+ ["image", "image-set", "-webkit-image-set"].include?(name)
241
+ end
212
242
 
213
- rule[:block] = tree!(styles)
243
+ # Passes the URL value of an @import rule to a block to ensure
244
+ # it's an allowed URL
245
+ def import_url_allowed?(rule)
246
+ return true unless @import_url_validator
214
247
 
215
- elsif @at_rules_with_properties.include?(name)
216
- props = Crass::Parser.parse_properties(rule[:block],
217
- :preserve_comments => @config[:allow_comments],
218
- :preserve_hacks => @config[:allow_hacks])
248
+ url_token = rule[:tokens].detect { |t| t[:node] == :url || t[:node] == :string }
219
249
 
220
- rule[:block] = tree!(props)
250
+ # don't allow @imports with no URL value
251
+ return false unless url_token && (import_url = url_token[:value])
221
252
 
222
- elsif @at_rules.include?(name)
223
- return nil if name == "import" && !import_url_allowed?(rule)
224
- return nil if rule.has_key?(:block)
225
- else
226
- return nil
253
+ @import_url_validator.call(import_url)
227
254
  end
228
255
 
229
- rule
230
- end
256
+ # Returns `true` if the given Crass node is a `:bad_url` or `:error` node.
257
+ #
258
+ # A bad URL is invalid CSS that a browser will discard, but a parser that
259
+ # disagrees with the browser about where a bad URL ends could be tricked
260
+ # into hiding CSS inside it.
261
+ #
262
+ # An `:error` node is CSS that Crass couldn't or wouldn't parse, such as a
263
+ # block nested more deeply than Crass's maximum nesting depth. Its contents
264
+ # are never inspected, but they're preserved verbatim when the tree is
265
+ # stringified, so a browser would still see them.
266
+ #
267
+ # It's safest to reject any construct that contains either one.
268
+ def invalid_node?(node)
269
+ node[:node] == :bad_url || node[:node] == :error
270
+ end
231
271
 
232
- # Passes the URL value of an @import rule to a block to ensure
233
- # it's an allowed URL
234
- def import_url_allowed?(rule)
235
- return true unless @import_url_validator
272
+ # Sanitizes a CSS property node. Returns the sanitized node, or `nil` if the
273
+ # current config doesn't allow this property.
274
+ def property!(prop)
275
+ name = prop[:name].downcase
236
276
 
237
- url_token = rule[:tokens].detect { |t| t[:node] == :url || t[:node] == :string }
277
+ # Preserve IE * and _ hacks if desired.
278
+ if @config[:allow_hacks]
279
+ name.slice!(0) if /\A[*_]/.match?(name)
280
+ end
238
281
 
239
- # don't allow @imports with no URL value
240
- return false unless url_token && (import_url = url_token[:value])
282
+ return nil unless @config[:properties].include?(name)
241
283
 
242
- @import_url_validator.call(import_url)
243
- end
284
+ nodes = prop[:children].dup
285
+ combined_value = +""
244
286
 
245
- # Sanitizes a CSS property node. Returns the sanitized node, or `nil` if the
246
- # current config doesn't allow this property.
247
- def property!(prop)
248
- name = prop[:name].downcase
287
+ nodes.each do |child|
288
+ # Reject properties that contain an invalid node at any depth.
289
+ return nil if invalid_node?(child)
249
290
 
250
- # Preserve IE * and _ hacks if desired.
251
- if @config[:allow_hacks]
252
- name.slice!(0) if name =~ /\A[*_]/
253
- end
291
+ value = child[:value]
254
292
 
255
- return nil unless @config[:properties].include?(name)
293
+ case child[:node]
294
+ when :ident
295
+ combined_value << value.downcase if String === value
256
296
 
257
- nodes = prop[:children].dup
258
- combined_value = String.new
297
+ when :function
298
+ if child.key?(:name)
299
+ name = child[:name].downcase
259
300
 
260
- nodes.each do |child|
261
- value = child[:value]
301
+ if name == "url"
302
+ return nil unless valid_url?(child)
303
+ end
262
304
 
263
- case child[:node]
264
- when :ident
265
- combined_value << value.downcase if String === value
305
+ if image_function?(name)
306
+ return nil unless valid_image?(child)
307
+ end
266
308
 
267
- when :function
268
- if child.key?(:name)
269
- name = child[:name].downcase
309
+ combined_value << name
310
+ return nil if name == "expression" || combined_value == "expression"
311
+ end
270
312
 
271
- if name == 'url'
272
- return nil unless valid_url?(child)
313
+ if Array === value
314
+ nodes.concat(value)
315
+ elsif String === value
316
+ lowercase_value = value.downcase
317
+ combined_value << lowercase_value
318
+ return nil if lowercase_value == "expression" || combined_value == "expression"
273
319
  end
274
320
 
275
- combined_value << name
276
- return nil if name == 'expression' || combined_value == 'expression'
321
+ when :url
322
+ return nil unless valid_url?(child)
323
+
324
+ when :simple_block
325
+ # Reject properties that contain a `{}` block at any depth.
326
+ #
327
+ # A `{}` block isn't valid in a standard property value, and browsers
328
+ # that support CSS nesting will reparse an invalid declaration that
329
+ # contains one as a nested style rule (e.g.
330
+ # `color:hover{position:fixed}` becomes the rule
331
+ # `color:hover { position: fixed }`), which would allow disallowed
332
+ # properties to be smuggled past the sanitizer.
333
+ return nil if child[:start] == "{"
334
+
335
+ nodes.concat(value) if Array === value
277
336
  end
337
+ end
278
338
 
279
- if Array === value
280
- nodes.concat(value)
281
- elsif String === value
282
- lowercase_value = value.downcase
283
- combined_value << lowercase_value
284
- return nil if lowercase_value == 'expression' || combined_value == 'expression'
285
- end
339
+ prop
340
+ end
286
341
 
287
- when :url
288
- return nil unless valid_url?(child)
342
+ # Sanitizes the given Crass CSS parse tree and all its children, modifying
343
+ # it in place. See {#tree!}.
344
+ #
345
+ # If _declarations_ is `true`, the tree is a list of declarations (such as
346
+ # the contents of a style rule) rather than a list of rules.
347
+ def sanitize_tree!(tree, declarations:)
348
+ preceded_by_property = false
289
349
 
290
- when :bad_url
291
- return nil
350
+ tree.map! do |node|
351
+ next nil if node.nil?
352
+
353
+ case node[:node]
354
+ when :at_rule
355
+ preceded_by_property = false
356
+ next at_rule!(node, declarations: declarations)
357
+
358
+ when :comment
359
+ next node if @config[:allow_comments]
360
+
361
+ when :property
362
+ prop = property!(node)
363
+ preceded_by_property = !prop.nil?
364
+ next prop
365
+
366
+ when :semicolon
367
+ # Only preserve the semicolon if it was preceded by an allowlisted
368
+ # property. Otherwise, omit it in order to prevent redundant
369
+ # semicolons.
370
+ if preceded_by_property
371
+ preceded_by_property = false
372
+ next node
373
+ end
374
+
375
+ when :style_rule
376
+ preceded_by_property = false
377
+
378
+ # Reject style rules whose selectors contain an invalid node (see
379
+ # {#invalid_node?}) or a semicolon at any depth.
380
+ #
381
+ # A semicolon is never valid in a selector, but if a style rule
382
+ # appears where a browser expects declarations (such as the contents
383
+ # of an `@media` rule in a list of declarations), the browser will
384
+ # treat everything before the semicolon as a declaration. For
385
+ # example, `position:fixed;a{}` would be treated as the declaration
386
+ # `position:fixed` followed by the style rule `a{}`.
387
+ next nil if contains_node?(node.dig(:selector, :tokens)) { |token|
388
+ invalid_node?(token) || token[:node] == :semicolon
389
+ }
390
+
391
+ sanitize_tree!(node[:children], declarations: true)
392
+ next node
393
+
394
+ when :whitespace
395
+ next node
396
+ end
397
+
398
+ nil
292
399
  end
400
+
401
+ tree
293
402
  end
294
403
 
295
- prop
296
- end
404
+ # Returns `true` if the given node (which may be of type `:url` or
405
+ # `:function`, since the CSS syntax can produce both) uses an allowlisted
406
+ # protocol.
407
+ def valid_url?(node)
408
+ type = node[:node]
297
409
 
298
- # Returns `true` if the given node (which may be of type `:url` or
299
- # `:function`, since the CSS syntax can produce both) uses an allowlisted
300
- # protocol.
301
- def valid_url?(node)
302
- type = node[:node]
410
+ if type == :function
411
+ return false unless node.key?(:name) && node[:name].downcase == "url"
412
+ return false unless Array === node[:value]
303
413
 
304
- if type == :function
305
- return false unless node.key?(:name) && node[:name].downcase == 'url'
306
- return false unless Array === node[:value]
414
+ # A URL function's `:value` should be an array containing no more than
415
+ # one `:string` node and any number of `:whitespace` nodes.
416
+ #
417
+ # If it contains more than one `:string` node, or if it contains any
418
+ # other nodes except `:whitespace` nodes, it's not valid.
419
+ url_string_node = nil
307
420
 
308
- # A URL function's `:value` should be an array containing no more than one
309
- # `:string` node and any number of `:whitespace` nodes.
310
- #
311
- # If it contains more than one `:string` node, or if it contains any other
312
- # nodes except `:whitespace` nodes, it's not valid.
313
- url_string_node = nil
421
+ node[:value].each do |token|
422
+ return false unless Hash === token
314
423
 
315
- node[:value].each do |token|
316
- return false unless Hash === token
317
-
318
- case token[:node]
424
+ case token[:node]
319
425
  when :string
320
426
  return false unless url_string_node.nil?
321
427
  url_string_node = token
@@ -325,24 +431,45 @@ class Sanitize; class CSS
325
431
 
326
432
  else
327
433
  return false
434
+ end
328
435
  end
436
+
437
+ return false if url_string_node.nil?
438
+ url = url_string_node[:value]
439
+ elsif type == :url
440
+ url = node[:value]
441
+ else
442
+ return false
329
443
  end
330
444
 
331
- return false if url_string_node.nil?
332
- url = url_string_node[:value]
333
- elsif type == :url
334
- url = node[:value]
335
- else
336
- return false
445
+ if url =~ Sanitize::REGEX_PROTOCOL
446
+ @config[:protocols].include?($1.downcase)
447
+ else
448
+ @config[:protocols].include?(:relative)
449
+ end
337
450
  end
338
451
 
339
- if url =~ Sanitize::REGEX_PROTOCOL
340
- return @config[:protocols].include?($1.downcase)
341
- else
342
- return @config[:protocols].include?(:relative)
343
- end
452
+ # Returns `true` if the given node is an image-related function and contains
453
+ # only strings that use an allowlisted protocol.
454
+ def valid_image?(node)
455
+ return false unless node[:node] == :function
456
+ return false unless node.key?(:name) && image_function?(node[:name].downcase)
457
+ return false unless Array === node[:value]
344
458
 
345
- false
346
- end
459
+ node[:value].each do |token|
460
+ return false unless Hash === token
347
461
 
348
- end; end
462
+ case token[:node]
463
+ when :string
464
+ if token[:value] =~ Sanitize::REGEX_PROTOCOL
465
+ return false unless @config[:protocols].include?($1.downcase)
466
+ else
467
+ return false unless @config[:protocols].include?(:relative)
468
+ end
469
+ else
470
+ next
471
+ end
472
+ end
473
+ end
474
+ end
475
+ end