sanitize 6.0.2 → 7.0.0
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} +64 -14
- data/LICENSE +3 -1
- data/README.md +120 -239
- data/lib/sanitize/config/basic.rb +15 -15
- data/lib/sanitize/config/default.rb +45 -45
- data/lib/sanitize/config/relaxed.rb +138 -32
- data/lib/sanitize/config/restricted.rb +2 -2
- data/lib/sanitize/config.rb +12 -14
- data/lib/sanitize/css.rb +317 -284
- data/lib/sanitize/transformers/clean_cdata.rb +9 -9
- data/lib/sanitize/transformers/clean_comment.rb +9 -9
- 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 +220 -237
- data/lib/sanitize/version.rb +3 -1
- data/lib/sanitize.rb +38 -38
- data/test/common.rb +4 -3
- data/test/test_clean_comment.rb +26 -25
- data/test/test_clean_css.rb +14 -13
- data/test/test_clean_doctype.rb +21 -20
- data/test/test_clean_element.rb +258 -273
- data/test/test_config.rb +22 -21
- data/test/test_malicious_css.rb +20 -19
- data/test/test_malicious_html.rb +100 -99
- data/test/test_parser.rb +26 -25
- data/test/test_sanitize.rb +70 -69
- data/test/test_sanitize_css.rb +158 -114
- data/test/test_transformers.rb +81 -83
- metadata +16 -43
data/README.md
CHANGED
|
@@ -1,21 +1,10 @@
|
|
|
1
|
-
Sanitize
|
|
2
|
-
========
|
|
1
|
+
# Sanitize
|
|
3
2
|
|
|
4
|
-
Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all HTML
|
|
5
|
-
and/or CSS from a string except the elements, attributes, and properties you
|
|
6
|
-
choose to allow.
|
|
3
|
+
Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all HTML and/or CSS from a string except the elements, attributes, and properties you choose to allow.
|
|
7
4
|
|
|
8
|
-
Using a simple configuration syntax, you can tell Sanitize to allow certain HTML
|
|
9
|
-
elements, certain attributes within those elements, and even certain URL
|
|
10
|
-
protocols within attributes that contain URLs. You can also allow specific CSS
|
|
11
|
-
properties, @ rules, and URL protocols in elements or attributes containing CSS.
|
|
12
|
-
Any HTML or CSS that you don't explicitly allow will be removed.
|
|
5
|
+
Using a simple configuration syntax, you can tell Sanitize to allow certain HTML elements, certain attributes within those elements, and even certain URL protocols within attributes that contain URLs. You can also allow specific CSS properties, @ rules, and URL protocols in elements or attributes containing CSS. Any HTML or CSS that you don't explicitly allow will be removed.
|
|
13
6
|
|
|
14
|
-
Sanitize is based on the [Nokogiri HTML5 parser][nokogiri], which parses HTML
|
|
15
|
-
the same way modern browsers do, and [Crass][crass], which parses CSS the same
|
|
16
|
-
way modern browsers do. As long as your allowlist config only allows safe markup
|
|
17
|
-
and CSS, even the most malformed or malicious input will be transformed into
|
|
18
|
-
safe output.
|
|
7
|
+
Sanitize is based on the [Nokogiri HTML5 parser][nokogiri], which parses HTML the same way modern browsers do, and [Crass][crass], which parses CSS the same way modern browsers do. As long as your allowlist config only allows safe markup and CSS, even the most malformed or malicious input will be transformed into safe output.
|
|
19
8
|
|
|
20
9
|
[](http://badge.fury.io/rb/sanitize)
|
|
21
10
|
[](https://github.com/rgrove/sanitize/actions?query=workflow%3ATests)
|
|
@@ -23,8 +12,7 @@ safe output.
|
|
|
23
12
|
[crass]:https://github.com/rgrove/crass
|
|
24
13
|
[nokogiri]:https://github.com/sparklemotion/nokogiri
|
|
25
14
|
|
|
26
|
-
Links
|
|
27
|
-
-----
|
|
15
|
+
## Links
|
|
28
16
|
|
|
29
17
|
* [Home](https://github.com/rgrove/sanitize/)
|
|
30
18
|
* [API Docs](https://rubydoc.info/github/rgrove/sanitize/Sanitize)
|
|
@@ -32,15 +20,13 @@ Links
|
|
|
32
20
|
* [Release History](https://github.com/rgrove/sanitize/releases)
|
|
33
21
|
* [Online Demo](https://sanitize-web.fly.dev/)
|
|
34
22
|
|
|
35
|
-
Installation
|
|
36
|
-
-------------
|
|
23
|
+
## Installation
|
|
37
24
|
|
|
38
25
|
```
|
|
39
26
|
gem install sanitize
|
|
40
27
|
```
|
|
41
28
|
|
|
42
|
-
Quick Start
|
|
43
|
-
-----------
|
|
29
|
+
## Quick Start
|
|
44
30
|
|
|
45
31
|
```ruby
|
|
46
32
|
require 'sanitize'
|
|
@@ -59,8 +45,7 @@ Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED)
|
|
|
59
45
|
Sanitize::CSS.properties(css, Sanitize::Config::RELAXED)
|
|
60
46
|
```
|
|
61
47
|
|
|
62
|
-
Usage
|
|
63
|
-
-----
|
|
48
|
+
## Usage
|
|
64
49
|
|
|
65
50
|
Sanitize can sanitize the following types of input:
|
|
66
51
|
|
|
@@ -71,7 +56,7 @@ Sanitize can sanitize the following types of input:
|
|
|
71
56
|
* Standalone CSS stylesheets
|
|
72
57
|
* Standalone CSS properties
|
|
73
58
|
|
|
74
|
-
>
|
|
59
|
+
> [!WARNING]
|
|
75
60
|
>
|
|
76
61
|
> Sanitize cannot fully sanitize the contents of `<math>` or `<svg>` elements. MathML and SVG elements are [foreign elements](https://html.spec.whatwg.org/multipage/syntax.html#foreign-elements) that don't follow normal HTML parsing rules.
|
|
77
62
|
>
|
|
@@ -79,31 +64,26 @@ Sanitize can sanitize the following types of input:
|
|
|
79
64
|
|
|
80
65
|
### HTML Fragments
|
|
81
66
|
|
|
82
|
-
A fragment is a snippet of HTML that doesn't contain a root-level `<html>`
|
|
83
|
-
element.
|
|
67
|
+
A fragment is a snippet of HTML that doesn't contain a root-level `<html>` element.
|
|
84
68
|
|
|
85
|
-
If you don't specify any configuration options, Sanitize will use its strictest
|
|
86
|
-
settings by default, which means it will strip all HTML and leave only safe text
|
|
87
|
-
behind.
|
|
69
|
+
If you don't specify any configuration options, Sanitize will use its strictest settings by default, which means it will strip all HTML and leave only safe text behind.
|
|
88
70
|
|
|
89
71
|
```ruby
|
|
90
72
|
html = '<b><a href="http://foo.com/">foo</a></b><img src="bar.jpg">'
|
|
91
73
|
Sanitize.fragment(html)
|
|
92
|
-
# =>
|
|
74
|
+
# => "foo"
|
|
93
75
|
```
|
|
94
76
|
|
|
95
77
|
To keep certain elements, add them to the element allowlist.
|
|
96
78
|
|
|
97
79
|
```ruby
|
|
98
|
-
Sanitize.fragment(html, :
|
|
99
|
-
# =>
|
|
80
|
+
Sanitize.fragment(html, elements: ['b'])
|
|
81
|
+
# => "<b>foo</b>"
|
|
100
82
|
```
|
|
101
83
|
|
|
102
84
|
### HTML Documents
|
|
103
85
|
|
|
104
|
-
When sanitizing a document, the `<html>` element must be allowlisted. You can
|
|
105
|
-
also set `:allow_doctype` to `true` to allow well-formed document type
|
|
106
|
-
definitions.
|
|
86
|
+
When sanitizing a document, the `<html>` element must be allowlisted. You can also set `:allow_doctype` to `true` to allow well-formed document type definitions.
|
|
107
87
|
|
|
108
88
|
```ruby
|
|
109
89
|
html = %[
|
|
@@ -114,23 +94,15 @@ html = %[
|
|
|
114
94
|
]
|
|
115
95
|
|
|
116
96
|
Sanitize.document(html,
|
|
117
|
-
:
|
|
118
|
-
:
|
|
97
|
+
allow_doctype: true,
|
|
98
|
+
elements: ['html']
|
|
119
99
|
)
|
|
120
|
-
# =>
|
|
121
|
-
# <!DOCTYPE html>
|
|
122
|
-
# <html>foo
|
|
123
|
-
#
|
|
124
|
-
# </html>
|
|
125
|
-
# ]
|
|
100
|
+
# => "<!DOCTYPE html><html>foo\n \n</html>"
|
|
126
101
|
```
|
|
127
102
|
|
|
128
103
|
### CSS in HTML
|
|
129
104
|
|
|
130
|
-
To sanitize CSS in an HTML fragment or document, first allowlist the `<style>`
|
|
131
|
-
element and/or the `style` attribute. Then allowlist the CSS properties,
|
|
132
|
-
@ rules, and URL protocols you wish to allow. You can also choose whether to
|
|
133
|
-
allow CSS comments or browser compatibility hacks.
|
|
105
|
+
To sanitize CSS in an HTML fragment or document, first allowlist the `<style>` element and/or the `style` attribute. Then allowlist the CSS properties, @ rules, and URL protocols you wish to allow. You can also choose whether to allow CSS comments or browser compatibility hacks.
|
|
134
106
|
|
|
135
107
|
```ruby
|
|
136
108
|
html = %[
|
|
@@ -143,11 +115,11 @@ html = %[
|
|
|
143
115
|
]
|
|
144
116
|
|
|
145
117
|
Sanitize.fragment(html,
|
|
146
|
-
:
|
|
147
|
-
:
|
|
118
|
+
elements: ['div', 'style'],
|
|
119
|
+
attributes: {'div' => ['style']},
|
|
148
120
|
|
|
149
|
-
:
|
|
150
|
-
:
|
|
121
|
+
css: {
|
|
122
|
+
properties: ['width']
|
|
151
123
|
}
|
|
152
124
|
)
|
|
153
125
|
#=> %[
|
|
@@ -162,8 +134,7 @@ Sanitize.fragment(html,
|
|
|
162
134
|
|
|
163
135
|
### Standalone CSS
|
|
164
136
|
|
|
165
|
-
Sanitize will happily clean up a standalone CSS stylesheet or property string
|
|
166
|
-
without needing to invoke the HTML parser.
|
|
137
|
+
Sanitize will happily clean up a standalone CSS stylesheet or property string without needing to invoke the HTML parser.
|
|
167
138
|
|
|
168
139
|
```ruby
|
|
169
140
|
css = %[
|
|
@@ -181,7 +152,6 @@ Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED)
|
|
|
181
152
|
# => %[
|
|
182
153
|
#
|
|
183
154
|
#
|
|
184
|
-
#
|
|
185
155
|
# a { text-decoration: none; }
|
|
186
156
|
#
|
|
187
157
|
# a:hover {
|
|
@@ -198,15 +168,11 @@ Sanitize::CSS.properties(%[
|
|
|
198
168
|
#
|
|
199
169
|
# text-decoration: underline;
|
|
200
170
|
# ]
|
|
201
|
-
|
|
202
171
|
```
|
|
203
172
|
|
|
204
|
-
Configuration
|
|
205
|
-
-------------
|
|
173
|
+
## Configuration
|
|
206
174
|
|
|
207
|
-
In addition to the ultra-safe default settings, Sanitize comes with three other
|
|
208
|
-
built-in configurations that you can use out of the box or adapt to meet your
|
|
209
|
-
needs.
|
|
175
|
+
In addition to the ultra-safe default settings, Sanitize comes with three other built-in configurations that you can use out of the box or adapt to meet your needs.
|
|
210
176
|
|
|
211
177
|
### Sanitize::Config::RESTRICTED
|
|
212
178
|
|
|
@@ -214,16 +180,14 @@ Allows only very simple inline markup. No links, images, or block elements.
|
|
|
214
180
|
|
|
215
181
|
```ruby
|
|
216
182
|
Sanitize.fragment(html, Sanitize::Config::RESTRICTED)
|
|
217
|
-
# =>
|
|
183
|
+
# => "<b>foo</b>"
|
|
218
184
|
```
|
|
219
185
|
|
|
220
186
|
### Sanitize::Config::BASIC
|
|
221
187
|
|
|
222
188
|
Allows a variety of markup including formatting elements, links, and lists.
|
|
223
189
|
|
|
224
|
-
Images and tables are not allowed, links are limited to FTP, HTTP, HTTPS, and
|
|
225
|
-
mailto protocols, and a `rel="nofollow"` attribute is added to all links to
|
|
226
|
-
mitigate SEO spam.
|
|
190
|
+
Images and tables are not allowed, links are limited to FTP, HTTP, HTTPS, and mailto protocols, and a `rel="nofollow"` attribute is added to all links to mitigate SEO spam.
|
|
227
191
|
|
|
228
192
|
```ruby
|
|
229
193
|
Sanitize.fragment(html, Sanitize::Config::BASIC)
|
|
@@ -232,10 +196,7 @@ Sanitize.fragment(html, Sanitize::Config::BASIC)
|
|
|
232
196
|
|
|
233
197
|
### Sanitize::Config::RELAXED
|
|
234
198
|
|
|
235
|
-
Allows an even wider variety of markup, including images and tables, as well as
|
|
236
|
-
safe CSS. Links are still limited to FTP, HTTP, HTTPS, and mailto protocols,
|
|
237
|
-
while images are limited to HTTP and HTTPS. In this mode, `rel="nofollow"` is
|
|
238
|
-
not added to links.
|
|
199
|
+
Allows an even wider variety of markup, including images and tables, as well as safe CSS. Links are still limited to FTP, HTTP, HTTPS, and mailto protocols, while images are limited to HTTP and HTTPS. In this mode, `rel="nofollow"` is not added to links.
|
|
239
200
|
|
|
240
201
|
```ruby
|
|
241
202
|
Sanitize.fragment(html, Sanitize::Config::RELAXED)
|
|
@@ -244,50 +205,43 @@ Sanitize.fragment(html, Sanitize::Config::RELAXED)
|
|
|
244
205
|
|
|
245
206
|
### Custom Configuration
|
|
246
207
|
|
|
247
|
-
If the built-in modes don't meet your needs, you can easily specify a custom
|
|
248
|
-
configuration:
|
|
208
|
+
If the built-in modes don't meet your needs, you can easily specify a custom configuration:
|
|
249
209
|
|
|
250
210
|
```ruby
|
|
251
211
|
Sanitize.fragment(html,
|
|
252
|
-
:
|
|
212
|
+
elements: ['a', 'span'],
|
|
253
213
|
|
|
254
|
-
:
|
|
255
|
-
'a'
|
|
214
|
+
attributes: {
|
|
215
|
+
'a' => ['href', 'title'],
|
|
256
216
|
'span' => ['class']
|
|
257
217
|
},
|
|
258
218
|
|
|
259
|
-
:
|
|
219
|
+
protocols: {
|
|
260
220
|
'a' => {'href' => ['http', 'https', 'mailto']}
|
|
261
221
|
}
|
|
262
222
|
)
|
|
263
223
|
```
|
|
264
224
|
|
|
265
|
-
You can also start with one of Sanitize's built-in configurations and then
|
|
266
|
-
customize it to meet your needs.
|
|
225
|
+
You can also start with one of Sanitize's built-in configurations and then customize it to meet your needs.
|
|
267
226
|
|
|
268
|
-
The built-in configs are deeply frozen to prevent people from modifying them
|
|
269
|
-
(either accidentally or maliciously). To customize a built-in config, create a
|
|
270
|
-
new copy using `Sanitize::Config.merge()`, like so:
|
|
227
|
+
The built-in configs are deeply frozen to prevent people from modifying them (either accidentally or maliciously). To customize a built-in config, create a new copy using `Sanitize::Config.merge()`, like so:
|
|
271
228
|
|
|
272
229
|
```ruby
|
|
273
230
|
# Create a customized copy of the Basic config, adding <div> and <table> to the
|
|
274
231
|
# existing allowlisted elements.
|
|
275
232
|
Sanitize.fragment(html, Sanitize::Config.merge(Sanitize::Config::BASIC,
|
|
276
|
-
:
|
|
277
|
-
:
|
|
233
|
+
elements: Sanitize::Config::BASIC[:elements] + ['div', 'table'],
|
|
234
|
+
remove_contents: true
|
|
278
235
|
))
|
|
279
236
|
```
|
|
280
237
|
|
|
281
|
-
The example above adds the `<div>` and `<table>` elements to a copy of the
|
|
282
|
-
existing list of elements in `Sanitize::Config::BASIC`. If you instead want to
|
|
283
|
-
completely overwrite the elements array with your own, you can omit the `+`
|
|
284
|
-
operation:
|
|
238
|
+
The example above adds the `<div>` and `<table>` elements to a copy of the existing list of elements in `Sanitize::Config::BASIC`. If you instead want to completely overwrite the elements array with your own, you can omit the `+` operation:
|
|
285
239
|
|
|
286
240
|
```ruby
|
|
287
241
|
# Overwrite :elements instead of creating a copy with new entries.
|
|
288
242
|
Sanitize.fragment(html, Sanitize::Config.merge(Sanitize::Config::BASIC,
|
|
289
|
-
:
|
|
290
|
-
:
|
|
243
|
+
elements: ['div', 'table'],
|
|
244
|
+
remove_contents: true
|
|
291
245
|
))
|
|
292
246
|
```
|
|
293
247
|
|
|
@@ -295,66 +249,56 @@ Sanitize.fragment(html, Sanitize::Config.merge(Sanitize::Config::BASIC,
|
|
|
295
249
|
|
|
296
250
|
#### :add_attributes (Hash)
|
|
297
251
|
|
|
298
|
-
Attributes to add to specific elements. If the attribute already exists, it will
|
|
299
|
-
be replaced with the value specified here. Specify all element names and
|
|
300
|
-
attributes in lowercase.
|
|
252
|
+
Attributes to add to specific elements. If the attribute already exists, it will be replaced with the value specified here. Specify all element names and attributes in lowercase.
|
|
301
253
|
|
|
302
254
|
```ruby
|
|
303
|
-
:
|
|
255
|
+
add_attributes: {
|
|
304
256
|
'a' => {'rel' => 'nofollow'}
|
|
305
257
|
}
|
|
306
258
|
```
|
|
307
259
|
|
|
308
260
|
#### :allow_comments (boolean)
|
|
309
261
|
|
|
310
|
-
Whether or not to allow HTML comments. Allowing comments is strongly
|
|
311
|
-
discouraged, since IE allows script execution within conditional comments. The
|
|
312
|
-
default value is `false`.
|
|
262
|
+
Whether or not to allow HTML comments. Allowing comments is strongly discouraged, since IE allows script execution within conditional comments. The default value is `false`.
|
|
313
263
|
|
|
314
264
|
#### :allow_doctype (boolean)
|
|
315
265
|
|
|
316
|
-
Whether or not to allow well-formed HTML doctype declarations such as "<!DOCTYPE
|
|
317
|
-
html>" when sanitizing a document. This setting is ignored when sanitizing
|
|
318
|
-
fragments. The default value is `false`.
|
|
266
|
+
Whether or not to allow well-formed HTML doctype declarations such as "<!DOCTYPE html>" when sanitizing a document. This setting is ignored when sanitizing fragments. The default value is `false`.
|
|
319
267
|
|
|
320
268
|
#### :attributes (Hash)
|
|
321
269
|
|
|
322
|
-
Attributes to allow on specific elements. Specify all element names and
|
|
323
|
-
attributes in lowercase.
|
|
270
|
+
Attributes to allow on specific elements. Specify all element names and attributes in lowercase.
|
|
324
271
|
|
|
325
272
|
```ruby
|
|
326
|
-
:
|
|
327
|
-
'a'
|
|
273
|
+
attributes: {
|
|
274
|
+
'a' => ['href', 'title'],
|
|
328
275
|
'blockquote' => ['cite'],
|
|
329
|
-
'img'
|
|
276
|
+
'img' => ['alt', 'src', 'title']
|
|
330
277
|
}
|
|
331
278
|
```
|
|
332
279
|
|
|
333
|
-
If you'd like to allow certain attributes on all elements, use the symbol `:all`
|
|
334
|
-
instead of an element name.
|
|
280
|
+
If you'd like to allow certain attributes on all elements, use the symbol `:all` instead of an element name.
|
|
335
281
|
|
|
336
282
|
```ruby
|
|
337
283
|
# Allow the class attribute on all elements.
|
|
338
|
-
:
|
|
284
|
+
attributes: {
|
|
339
285
|
:all => ['class'],
|
|
340
|
-
'a'
|
|
286
|
+
'a' => ['href', 'title']
|
|
341
287
|
}
|
|
342
288
|
```
|
|
343
289
|
|
|
344
|
-
To allow arbitrary HTML5 `data-*` attributes, use the symbol `:data` in place of
|
|
345
|
-
an attribute name.
|
|
290
|
+
To allow arbitrary HTML5 `data-*` attributes, use the symbol `:data` in place of an attribute name.
|
|
346
291
|
|
|
347
292
|
```ruby
|
|
348
293
|
# Allow arbitrary HTML5 data-* attributes on <div> elements.
|
|
349
|
-
:
|
|
294
|
+
attributes: {
|
|
350
295
|
'div' => [:data]
|
|
351
296
|
}
|
|
352
297
|
```
|
|
353
298
|
|
|
354
299
|
#### :css (Hash)
|
|
355
300
|
|
|
356
|
-
Hash of the following CSS config settings to be used when sanitizing CSS (either
|
|
357
|
-
standalone or embedded in HTML).
|
|
301
|
+
Hash of the following CSS config settings to be used when sanitizing CSS (either standalone or embedded in HTML).
|
|
358
302
|
|
|
359
303
|
##### :css => :allow_comments (boolean)
|
|
360
304
|
|
|
@@ -362,36 +306,27 @@ Whether or not to allow CSS comments. The default value is `false`.
|
|
|
362
306
|
|
|
363
307
|
##### :css => :allow_hacks (boolean)
|
|
364
308
|
|
|
365
|
-
Whether or not to allow browser compatibility hacks such as the IE `*` and `_`
|
|
366
|
-
hacks. These are generally harmless, but technically result in invalid CSS. The
|
|
367
|
-
default is `false`.
|
|
309
|
+
Whether or not to allow browser compatibility hacks such as the IE `*` and `_` hacks. These are generally harmless, but technically result in invalid CSS. The default is `false`.
|
|
368
310
|
|
|
369
311
|
##### :css => :at_rules (Array or Set)
|
|
370
312
|
|
|
371
|
-
Names of CSS [at-rules][at-rules] to allow that may not have associated blocks,
|
|
372
|
-
such as `import` or `charset`. Names should be specified in lowercase.
|
|
313
|
+
Names of CSS [at-rules][at-rules] to allow that may not have associated blocks, such as `import` or `charset`. Names should be specified in lowercase.
|
|
373
314
|
|
|
374
315
|
[at-rules]:https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
|
|
375
316
|
|
|
376
317
|
##### :css => :at_rules_with_properties (Array or Set)
|
|
377
318
|
|
|
378
|
-
Names of CSS [at-rules][at-rules] to allow that may have associated blocks
|
|
379
|
-
containing CSS properties. At-rules like `font-face` and `page` fall into this
|
|
380
|
-
category. Names should be specified in lowercase.
|
|
319
|
+
Names of CSS [at-rules][at-rules] to allow that may have associated blocks containing CSS properties. At-rules like `font-face` and `page` fall into this category. Names should be specified in lowercase.
|
|
381
320
|
|
|
382
321
|
##### :css => :at_rules_with_styles (Array or Set)
|
|
383
322
|
|
|
384
|
-
Names of CSS [at-rules][at-rules] to allow that may have associated blocks
|
|
385
|
-
containing style rules. At-rules like `media` and `keyframes` fall into this
|
|
386
|
-
category. Names should be specified in lowercase.
|
|
323
|
+
Names of CSS [at-rules][at-rules] to allow that may have associated blocks containing style rules. At-rules like `media` and `keyframes` fall into this category. Names should be specified in lowercase.
|
|
387
324
|
|
|
388
325
|
##### :css => :import_url_validator
|
|
389
326
|
|
|
390
|
-
This is a `Proc` (or other callable object) that will be called and passed
|
|
391
|
-
the URL specified for any `@import` [at-rules][at-rules].
|
|
327
|
+
This is a `Proc` (or other callable object) that will be called and passed the URL specified for any `@import` [at-rules][at-rules].
|
|
392
328
|
|
|
393
|
-
You can use this to limit what can be imported, for example something
|
|
394
|
-
like the following to limit `@import` to Google Fonts URLs:
|
|
329
|
+
You can use this to limit what can be imported, for example something like the following to limit `@import` to Google Fonts URLs:
|
|
395
330
|
|
|
396
331
|
```ruby
|
|
397
332
|
Proc.new { |url| url.start_with?("https://fonts.googleapis.com") }
|
|
@@ -405,28 +340,26 @@ List of CSS property names to allow. Names should be specified in lowercase.
|
|
|
405
340
|
|
|
406
341
|
URL protocols to allow in CSS URLs. Should be specified in lowercase.
|
|
407
342
|
|
|
408
|
-
If you'd like to allow the use of relative URLs which don't have a protocol,
|
|
409
|
-
include the symbol `:relative` in the protocol array.
|
|
343
|
+
If you'd like to allow the use of relative URLs which don't have a protocol, include the symbol `:relative` in the protocol array.
|
|
410
344
|
|
|
411
345
|
#### :elements (Array or Set)
|
|
412
346
|
|
|
413
|
-
Array of HTML element names to allow. Specify all names in lowercase. Any
|
|
414
|
-
elements not in this array will be removed.
|
|
347
|
+
Array of HTML element names to allow. Specify all names in lowercase. Any elements not in this array will be removed.
|
|
415
348
|
|
|
416
349
|
```ruby
|
|
417
|
-
:
|
|
350
|
+
elements: %w[
|
|
418
351
|
a abbr b blockquote br cite code dd dfn dl dt em i kbd li mark ol p pre
|
|
419
352
|
q s samp small strike strong sub sup time u ul var
|
|
420
353
|
]
|
|
421
354
|
```
|
|
422
355
|
|
|
423
|
-
>
|
|
356
|
+
> [!WARNING]
|
|
424
357
|
>
|
|
425
358
|
> Sanitize cannot fully sanitize the contents of `<math>` or `<svg>` elements. MathML and SVG elements are [foreign elements](https://html.spec.whatwg.org/multipage/syntax.html#foreign-elements) that don't follow normal HTML parsing rules.
|
|
426
359
|
>
|
|
427
360
|
> By default, Sanitize will remove all MathML and SVG elements. If you add MathML or SVG elements to a custom element allowlist, you must assume that any content inside them will be allowed, even if that content would otherwise be removed or escaped by Sanitize. This may create a security vulnerability in your application.
|
|
428
361
|
|
|
429
|
-
>
|
|
362
|
+
> [!NOTE]
|
|
430
363
|
>
|
|
431
364
|
> Sanitize always removes `<noscript>` elements and their contents, even if `noscript` is in the allowlist.
|
|
432
365
|
>
|
|
@@ -434,10 +367,10 @@ elements not in this array will be removed.
|
|
|
434
367
|
|
|
435
368
|
#### :parser_options (Hash)
|
|
436
369
|
|
|
437
|
-
[Parsing options](https://
|
|
370
|
+
[Parsing options](https://nokogiri.org/tutorials/parsing_an_html5_document.html?h=parsing+options#parsing-options) to be supplied to Nokogiri.
|
|
438
371
|
|
|
439
372
|
```ruby
|
|
440
|
-
:
|
|
373
|
+
parser_options: {
|
|
441
374
|
max_errors: -1,
|
|
442
375
|
max_tree_depth: -1
|
|
443
376
|
}
|
|
@@ -445,81 +378,59 @@ elements not in this array will be removed.
|
|
|
445
378
|
|
|
446
379
|
#### :protocols (Hash)
|
|
447
380
|
|
|
448
|
-
URL protocols to allow in specific attributes. If an attribute is listed here
|
|
449
|
-
and contains a protocol other than those specified (or if it contains no
|
|
450
|
-
protocol at all), it will be removed.
|
|
381
|
+
URL protocols to allow in specific attributes. If an attribute is listed here and contains a protocol other than those specified (or if it contains no protocol at all), it will be removed.
|
|
451
382
|
|
|
452
383
|
```ruby
|
|
453
|
-
:
|
|
454
|
-
'a'
|
|
455
|
-
'img' => {'src'
|
|
384
|
+
protocols: {
|
|
385
|
+
'a' => {'href' => ['ftp', 'http', 'https', 'mailto']},
|
|
386
|
+
'img' => {'src' => ['http', 'https']}
|
|
456
387
|
}
|
|
457
388
|
```
|
|
458
389
|
|
|
459
|
-
If you'd like to allow the use of relative URLs which don't have a protocol,
|
|
460
|
-
include the symbol `:relative` in the protocol array:
|
|
390
|
+
If you'd like to allow the use of relative URLs which don't have a protocol, include the symbol `:relative` in the protocol array:
|
|
461
391
|
|
|
462
392
|
```ruby
|
|
463
|
-
:
|
|
393
|
+
protocols: {
|
|
464
394
|
'a' => {'href' => ['http', 'https', :relative]}
|
|
465
395
|
}
|
|
466
396
|
```
|
|
467
397
|
|
|
468
398
|
#### :remove_contents (boolean or Array or Set)
|
|
469
399
|
|
|
470
|
-
If this is `true`, Sanitize will remove the contents of any non-allowlisted
|
|
471
|
-
elements in addition to the elements themselves. By default, Sanitize leaves the
|
|
472
|
-
safe parts of an element's contents behind when the element is removed.
|
|
400
|
+
If this is `true`, Sanitize will remove the contents of any non-allowlisted elements in addition to the elements themselves. By default, Sanitize leaves the safe parts of an element's contents behind when the element is removed.
|
|
473
401
|
|
|
474
|
-
If this is an Array or Set of element names, then only the contents of the
|
|
475
|
-
specified elements (when filtered) will be removed, and the contents of all
|
|
476
|
-
other filtered elements will be left behind.
|
|
402
|
+
If this is an Array or Set of element names, then only the contents of the specified elements (when filtered) will be removed, and the contents of all other filtered elements will be left behind.
|
|
477
403
|
|
|
478
|
-
The default value
|
|
404
|
+
The default value can be seen in the [default config](lib/sanitize/config/default.rb).
|
|
479
405
|
|
|
480
406
|
#### :transformers (Array or callable)
|
|
481
407
|
|
|
482
|
-
Custom HTML transformer or array of custom transformers. See the Transformers
|
|
483
|
-
section below for details.
|
|
408
|
+
Custom HTML transformer or array of custom transformers. See the Transformers section below for details.
|
|
484
409
|
|
|
485
410
|
#### :whitespace_elements (Hash)
|
|
486
411
|
|
|
487
|
-
Hash of element names which, when removed, should have their contents surrounded
|
|
488
|
-
by whitespace to preserve readability.
|
|
412
|
+
Hash of element names which, when removed, should have their contents surrounded by whitespace to preserve readability.
|
|
489
413
|
|
|
490
|
-
Each element name is a key pointing to another Hash, which provides the specific
|
|
491
|
-
whitespace that should be inserted `:before` and `:after` the removed element's
|
|
492
|
-
position. The `:after` value will only be inserted if the removed element has
|
|
493
|
-
children, in which case it will be inserted after those children.
|
|
414
|
+
Each element name is a key pointing to another Hash, which provides the specific whitespace that should be inserted `:before` and `:after` the removed element's position. The `:after` value will only be inserted if the removed element has children, in which case it will be inserted after those children.
|
|
494
415
|
|
|
495
416
|
```ruby
|
|
496
|
-
:
|
|
497
|
-
'br'
|
|
498
|
-
'div' => { :
|
|
499
|
-
'p'
|
|
417
|
+
whitespace_elements: {
|
|
418
|
+
'br' => { before: "\n", after: "" },
|
|
419
|
+
'div' => { before: "\n", after: "\n" },
|
|
420
|
+
'p' => { before: "\n", after: "\n" }
|
|
500
421
|
}
|
|
501
422
|
```
|
|
502
423
|
|
|
503
|
-
The default elements with whitespace added before and after
|
|
504
|
-
|
|
505
|
-
```
|
|
506
|
-
address article aside blockquote br dd div dl dt
|
|
507
|
-
footer h1 h2 h3 h4 h5 h6 header hgroup hr li nav
|
|
508
|
-
ol p pre section ul
|
|
509
|
-
|
|
510
|
-
```
|
|
424
|
+
The default elements with whitespace added before and after can be seen in [the default config](lib/sanitize/config/default.rb).
|
|
511
425
|
|
|
512
426
|
## Transformers
|
|
513
427
|
|
|
514
|
-
Transformers allow you to filter and modify HTML nodes using your own custom
|
|
515
|
-
logic, on top of (or instead of) Sanitize's core filter. A transformer is any
|
|
516
|
-
object that responds to `call()` (such as a lambda or proc).
|
|
428
|
+
Transformers allow you to filter and modify HTML nodes using your own custom logic, on top of (or instead of) Sanitize's core filter. A transformer is any object that responds to `call()` (such as a lambda or proc).
|
|
517
429
|
|
|
518
|
-
To use one or more transformers, pass them to the `:transformers` config
|
|
519
|
-
setting. You may pass a single transformer or an array of transformers.
|
|
430
|
+
To use one or more transformers, pass them to the `:transformers` config setting. You may pass a single transformer or an array of transformers.
|
|
520
431
|
|
|
521
432
|
```ruby
|
|
522
|
-
Sanitize.fragment(html, :
|
|
433
|
+
Sanitize.fragment(html, transformers: [
|
|
523
434
|
transformer_one,
|
|
524
435
|
transformer_two
|
|
525
436
|
])
|
|
@@ -527,56 +438,31 @@ Sanitize.fragment(html, :transformers => [
|
|
|
527
438
|
|
|
528
439
|
### Input
|
|
529
440
|
|
|
530
|
-
Each transformer's `call()` method will be called once for each node in the HTML
|
|
531
|
-
(including elements, text nodes, comments, etc.), and will receive as an
|
|
532
|
-
argument a Hash that contains the following items:
|
|
441
|
+
Each transformer's `call()` method will be called once for each node in the HTML (including elements, text nodes, comments, etc.), and will receive as an argument a Hash that contains the following items:
|
|
533
442
|
|
|
534
443
|
* **:config** - The current Sanitize configuration Hash.
|
|
535
444
|
|
|
536
|
-
* **:is_allowlisted** - `true` if the current node has been allowlisted by a
|
|
537
|
-
previous transformer, `false` otherwise. It's generally bad form to remove
|
|
538
|
-
a node that a previous transformer has allowlisted.
|
|
445
|
+
* **:is_allowlisted** - `true` if the current node has been allowlisted by a previous transformer, `false` otherwise. It's generally bad form to remove a node that a previous transformer has allowlisted.
|
|
539
446
|
|
|
540
|
-
* **:node** - A `Nokogiri::XML::Node` object representing an HTML node. The
|
|
541
|
-
node may be an element, a text node, a comment, a CDATA node, or a document
|
|
542
|
-
fragment. Use Nokogiri's inspection methods (`element?`, `text?`, etc.) to
|
|
543
|
-
selectively ignore node types you aren't interested in.
|
|
447
|
+
* **:node** - A `Nokogiri::XML::Node` object representing an HTML node. The node may be an element, a text node, a comment, a CDATA node, or a document fragment. Use Nokogiri's inspection methods (`element?`, `text?`, etc.) to selectively ignore node types you aren't interested in.
|
|
544
448
|
|
|
545
|
-
* **:node_allowlist** - Set of `Nokogiri::XML::Node` objects in the current
|
|
546
|
-
document that have been allowlisted by previous transformers, if any. It's
|
|
547
|
-
generally bad form to remove a node that a previous transformer has
|
|
548
|
-
allowlisted.
|
|
449
|
+
* **:node_allowlist** - Set of `Nokogiri::XML::Node` objects in the current document that have been allowlisted by previous transformers, if any. It's generally bad form to remove a node that a previous transformer has allowlisted.
|
|
549
450
|
|
|
550
|
-
* **:node_name** - The name of the current HTML node, always lowercase (e.g.
|
|
551
|
-
"div" or "span"). For non-element nodes, the name will be something like
|
|
552
|
-
"text", "comment", "#cdata-section", "#document-fragment", etc.
|
|
451
|
+
* **:node_name** - The name of the current HTML node, always lowercase (e.g. "div" or "span"). For non-element nodes, the name will be something like "text", "comment", "#cdata-section", "#document-fragment", etc.
|
|
553
452
|
|
|
554
453
|
### Output
|
|
555
454
|
|
|
556
|
-
A transformer doesn't have to return anything, but may optionally return a Hash,
|
|
557
|
-
which may contain the following items:
|
|
455
|
+
A transformer doesn't have to return anything, but may optionally return a Hash, which may contain the following items:
|
|
558
456
|
|
|
559
|
-
* **:node_allowlist** - Array or Set of specific `Nokogiri::XML::Node`
|
|
560
|
-
objects to add to the document's allowlist, bypassing the current Sanitize
|
|
561
|
-
config. These specific nodes and all their attributes will be allowlisted,
|
|
562
|
-
but their children will not be.
|
|
457
|
+
* **:node_allowlist** - Array or Set of specific `Nokogiri::XML::Node` objects to add to the document's allowlist, bypassing the current Sanitize config. These specific nodes and all their attributes will be allowlisted, but their children will not be.
|
|
563
458
|
|
|
564
|
-
If a transformer returns anything other than a Hash, the return value will be
|
|
565
|
-
ignored.
|
|
459
|
+
If a transformer returns anything other than a Hash, the return value will be ignored.
|
|
566
460
|
|
|
567
461
|
### Processing
|
|
568
462
|
|
|
569
|
-
Each transformer has full access to the `Nokogiri::XML::Node` that's passed into
|
|
570
|
-
it and to the rest of the document via the node's `document()` method. Any
|
|
571
|
-
changes made to the current node or to the document will be reflected instantly
|
|
572
|
-
in the document and passed on to subsequently called transformers and to
|
|
573
|
-
Sanitize itself. A transformer may even call Sanitize internally to perform
|
|
574
|
-
custom sanitization if needed.
|
|
463
|
+
Each transformer has full access to the `Nokogiri::XML::Node` that's passed into it and to the rest of the document via the node's `document()` method. Any changes made to the current node or to the document will be reflected instantly in the document and passed on to subsequently called transformers and to Sanitize itself. A transformer may even call Sanitize internally to perform custom sanitization if needed.
|
|
575
464
|
|
|
576
|
-
Nodes are passed into transformers in the order in which they're traversed.
|
|
577
|
-
Sanitize performs top-down traversal, meaning that nodes are traversed in the
|
|
578
|
-
same order you'd read them in the HTML, starting at the top node, then its first
|
|
579
|
-
child, and so on.
|
|
465
|
+
Nodes are passed into transformers in the order in which they're traversed. Sanitize performs top-down traversal, meaning that nodes are traversed in the same order you'd read them in the HTML, starting at the top node, then its first child, and so on.
|
|
580
466
|
|
|
581
467
|
```ruby
|
|
582
468
|
html = %[
|
|
@@ -595,81 +481,76 @@ transformer = lambda do |env|
|
|
|
595
481
|
end
|
|
596
482
|
|
|
597
483
|
# Prints "header", "span", "strong", "p", "footer".
|
|
598
|
-
Sanitize.fragment(html, :
|
|
484
|
+
Sanitize.fragment(html, transformers: transformer)
|
|
599
485
|
```
|
|
600
486
|
|
|
601
|
-
Transformers have a tremendous amount of power, including the power to
|
|
602
|
-
completely bypass Sanitize's built-in filtering. Be careful! Your safety is in
|
|
603
|
-
your own hands.
|
|
487
|
+
Transformers have a tremendous amount of power, including the power to completely bypass Sanitize's built-in filtering. Be careful! Your safety is in your own hands.
|
|
604
488
|
|
|
605
489
|
### Example: Transformer to allow image URLs by domain
|
|
606
490
|
|
|
607
|
-
The following example demonstrates how to remove image elements unless they use
|
|
608
|
-
a relative URL or are hosted on a specific domain. It assumes that the `<img>`
|
|
609
|
-
element and its `src` attribute are already allowlisted.
|
|
491
|
+
The following example demonstrates how to remove image elements unless they use a relative URL or are hosted on a specific domain. It assumes that the `<img>` element and its `src` attribute are already allowlisted.
|
|
610
492
|
|
|
611
493
|
```ruby
|
|
612
|
-
require
|
|
494
|
+
require "uri"
|
|
613
495
|
|
|
614
496
|
image_allowlist_transformer = lambda do |env|
|
|
615
497
|
# Ignore everything except <img> elements.
|
|
616
|
-
return unless env[:node_name] ==
|
|
498
|
+
return unless env[:node_name] == "img"
|
|
617
499
|
|
|
618
|
-
node
|
|
619
|
-
image_uri = URI.parse(node[
|
|
500
|
+
node = env[:node]
|
|
501
|
+
image_uri = URI.parse(node["src"])
|
|
620
502
|
|
|
621
503
|
# Only allow relative URLs or URLs with the example.com domain. The
|
|
622
504
|
# image_uri.host.nil? check ensures that protocol-relative URLs like
|
|
623
|
-
# "//evil.com/foo.jpg".
|
|
624
|
-
unless image_uri.host ==
|
|
625
|
-
|
|
505
|
+
# "//evil.com/foo.jpg" are not allowed.
|
|
506
|
+
unless image_uri.host == "example.com"
|
|
507
|
+
unless image_uri.host.nil? && image_uri.relative?
|
|
508
|
+
node.unlink # `Nokogiri::XML::Node#unlink` removes a node from the document
|
|
509
|
+
end
|
|
626
510
|
end
|
|
627
511
|
end
|
|
628
512
|
```
|
|
629
513
|
|
|
630
514
|
### Example: Transformer to allow YouTube video embeds
|
|
631
515
|
|
|
632
|
-
The following example demonstrates how to create a transformer that will safely
|
|
633
|
-
allow valid YouTube video embeds without having to allow other kinds of embedded
|
|
634
|
-
content, which would be the case if you tried to do this by just allowing all
|
|
635
|
-
`<iframe>` elements:
|
|
516
|
+
The following example demonstrates how to create a transformer that will safely allow valid YouTube video embeds without having to allow other kinds of embedded content, which would be the case if you tried to do this by just allowing all `<iframe>` elements:
|
|
636
517
|
|
|
637
518
|
```ruby
|
|
638
519
|
youtube_transformer = lambda do |env|
|
|
639
|
-
node
|
|
520
|
+
node = env[:node]
|
|
640
521
|
node_name = env[:node_name]
|
|
641
522
|
|
|
642
523
|
# Don't continue if this node is already allowlisted or is not an element.
|
|
643
524
|
return if env[:is_allowlisted] || !node.element?
|
|
644
525
|
|
|
645
526
|
# Don't continue unless the node is an iframe.
|
|
646
|
-
return unless node_name ==
|
|
527
|
+
return unless node_name == "iframe"
|
|
647
528
|
|
|
648
529
|
# Verify that the video URL is actually a valid YouTube video URL.
|
|
649
|
-
return unless
|
|
530
|
+
return unless %r{\A(?:https?:)?//(?:www\.)?youtube(?:-nocookie)?\.com/}.match?(node["src"])
|
|
650
531
|
|
|
651
532
|
# We're now certain that this is a YouTube embed, but we still need to run
|
|
652
533
|
# it through a special Sanitize step to ensure that no unwanted elements or
|
|
653
534
|
# attributes that don't belong in a YouTube embed can sneak in.
|
|
654
535
|
Sanitize.node!(node, {
|
|
655
|
-
:
|
|
536
|
+
elements: %w[iframe],
|
|
656
537
|
|
|
657
|
-
:
|
|
658
|
-
|
|
538
|
+
attributes: {
|
|
539
|
+
"iframe" => %w[allowfullscreen frameborder height src width]
|
|
659
540
|
}
|
|
660
541
|
})
|
|
661
542
|
|
|
662
543
|
# Now that we're sure that this is a valid YouTube embed and that there are
|
|
663
544
|
# no unwanted elements or attributes hidden inside it, we can tell Sanitize
|
|
664
545
|
# to allowlist the current node.
|
|
665
|
-
{:
|
|
546
|
+
{node_allowlist: [node]}
|
|
666
547
|
end
|
|
667
548
|
|
|
668
549
|
html = %[
|
|
669
550
|
<iframe width="420" height="315" src="//www.youtube.com/embed/dQw4w9WgXcQ"
|
|
670
551
|
frameborder="0" allowfullscreen></iframe>
|
|
671
|
-
]
|
|
552
|
+
].strip
|
|
672
553
|
|
|
673
|
-
Sanitize.fragment(html, :
|
|
554
|
+
Sanitize.fragment(html, transformers: youtube_transformer)
|
|
674
555
|
# => '<iframe width="420" height="315" src="//www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen=""></iframe>'
|
|
675
556
|
```
|