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.
- checksums.yaml +4 -4
- data/{HISTORY.md → CHANGELOG.md} +90 -14
- data/LICENSE +3 -1
- data/README.md +130 -240
- data/lib/sanitize/config/basic.rb +15 -15
- data/lib/sanitize/config/default.rb +45 -45
- data/lib/sanitize/config/relaxed.rb +136 -32
- data/lib/sanitize/config/restricted.rb +2 -2
- data/lib/sanitize/config.rb +11 -15
- data/lib/sanitize/css.rb +410 -283
- data/lib/sanitize/transformers/clean_cdata.rb +9 -9
- data/lib/sanitize/transformers/clean_comment.rb +44 -8
- data/lib/sanitize/transformers/clean_css.rb +59 -55
- data/lib/sanitize/transformers/clean_doctype.rb +17 -13
- data/lib/sanitize/transformers/clean_element.rb +284 -231
- data/lib/sanitize/version.rb +3 -1
- data/lib/sanitize.rb +38 -39
- metadata +17 -56
- data/test/common.rb +0 -3
- data/test/test_clean_comment.rb +0 -47
- data/test/test_clean_css.rb +0 -67
- data/test/test_clean_doctype.rb +0 -71
- data/test/test_clean_element.rb +0 -552
- data/test/test_config.rb +0 -65
- data/test/test_malicious_css.rb +0 -55
- data/test/test_malicious_html.rb +0 -339
- data/test/test_parser.rb +0 -75
- data/test/test_sanitize.rb +0 -197
- data/test/test_sanitize_css.rb +0 -424
- data/test/test_transformers.rb +0 -230
data/lib/sanitize/css.rb
CHANGED
|
@@ -1,321 +1,427 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
require
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
77
|
+
# -- Instance Methods ------------------------------------------------------
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
191
|
-
|
|
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
|
-
|
|
209
|
+
rule
|
|
195
210
|
end
|
|
196
211
|
|
|
197
|
-
|
|
198
|
-
|
|
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
|
-
|
|
201
|
-
|
|
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
|
-
|
|
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
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
250
|
+
# don't allow @imports with no URL value
|
|
251
|
+
return false unless url_token && (import_url = url_token[:value])
|
|
221
252
|
|
|
222
|
-
|
|
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
|
-
|
|
230
|
-
|
|
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
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
|
|
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
|
-
|
|
240
|
-
return false unless url_token && (import_url = url_token[:value])
|
|
282
|
+
return nil unless @config[:properties].include?(name)
|
|
241
283
|
|
|
242
|
-
|
|
243
|
-
|
|
284
|
+
nodes = prop[:children].dup
|
|
285
|
+
combined_value = +""
|
|
244
286
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
-
|
|
251
|
-
if @config[:allow_hacks]
|
|
252
|
-
name.slice!(0) if name =~ /\A[*_]/
|
|
253
|
-
end
|
|
291
|
+
value = child[:value]
|
|
254
292
|
|
|
255
|
-
|
|
293
|
+
case child[:node]
|
|
294
|
+
when :ident
|
|
295
|
+
combined_value << value.downcase if String === value
|
|
256
296
|
|
|
257
|
-
|
|
258
|
-
|
|
297
|
+
when :function
|
|
298
|
+
if child.key?(:name)
|
|
299
|
+
name = child[:name].downcase
|
|
259
300
|
|
|
260
|
-
|
|
261
|
-
|
|
301
|
+
if name == "url"
|
|
302
|
+
return nil unless valid_url?(child)
|
|
303
|
+
end
|
|
262
304
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
305
|
+
if image_function?(name)
|
|
306
|
+
return nil unless valid_image?(child)
|
|
307
|
+
end
|
|
266
308
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
309
|
+
combined_value << name
|
|
310
|
+
return nil if name == "expression" || combined_value == "expression"
|
|
311
|
+
end
|
|
270
312
|
|
|
271
|
-
if
|
|
272
|
-
|
|
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
|
-
|
|
276
|
-
return nil
|
|
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
|
-
|
|
280
|
-
|
|
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
|
-
|
|
288
|
-
|
|
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
|
-
|
|
291
|
-
|
|
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
|
-
|
|
296
|
-
|
|
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
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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
|
-
|
|
309
|
-
|
|
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
|
-
|
|
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
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
return
|
|
343
|
-
|
|
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
|
-
|
|
346
|
-
|
|
459
|
+
node[:value].each do |token|
|
|
460
|
+
return false unless Hash === token
|
|
347
461
|
|
|
348
|
-
|
|
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
|