dandruff 0.8.1 → 0.9.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/CHANGELOG.md +44 -0
- data/Gemfile.lock +56 -44
- data/README.md +63 -46
- data/lib/dandruff/config.rb +22 -7
- data/lib/dandruff/css.rb +157 -0
- data/lib/dandruff/expressions.rb +19 -6
- data/lib/dandruff/version.rb +1 -1
- data/lib/dandruff.rb +187 -102
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8ce2a761d6a76b28a6e5e0ce85e980cfcb1a7afe127d2dbe282759eef095f348
|
|
4
|
+
data.tar.gz: 2e0e3c65aebc44f4b5a8cd1fcbcb839f1bb0eb96b2cccdbd14725e2d208eadff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2528c90ea2673cef83a3ce6078eabe1471e0e7a33e5923eb406efdc1b50039d46f706fada4faf4133c4f4ac17465c04333d621879327450bd7188cabc79e3f6c
|
|
7
|
+
data.tar.gz: d96a2e120ccd3c99afab5d6c414c9aeaabc07faaf055804e378c1de01f26aebdbe977db54d9b9f732c8526bb1ed6015cc93d347356dc35dae4fdf9737635584f
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.9.0] - 2026-11-26
|
|
4
|
+
|
|
5
|
+
### Security
|
|
6
|
+
- `<style>` elements now go through the normal element/attribute pipeline. Previously they skipped
|
|
7
|
+
`sanitize_element`, so event handlers on `<style>` (e.g. `<div><style onload=...>`) survived with the default
|
|
8
|
+
config and anywhere in the `html_email` profile. Also, `forbidden_tags`/`allowed_tags`, hooks and the
|
|
9
|
+
SVG/MathML mXSS check never applied to `<style>`.
|
|
10
|
+
- `<style>` content is now sanitized (it was passed through unchanged even though the docs said otherwise):
|
|
11
|
+
`@import` rules, `expression()`, `behavior`/`-moz-binding`, `javascript:`/`vbscript:` and `url()`/`src()`/
|
|
12
|
+
`image-set()` URLs that fail the URI rules are removed per declaration/rule, after decoding CSS escapes and
|
|
13
|
+
ignoring comments. The rest of the stylesheet is kept.
|
|
14
|
+
- Disallowed elements whose children were hoisted (`keep_content`) are now always removed. Before, the emptied
|
|
15
|
+
element stayed in the output with unchecked attributes whenever only a single pass ran
|
|
16
|
+
(`mutation_max_passes`/`pass_limit` = 1 or `sanitize_until_stable = false`).
|
|
17
|
+
- `upon_sanitize_attribute` hooks: `data[:keep_attr]` and changes to `data[:value]` are now honoured. Rewritten
|
|
18
|
+
values are validated, and `keep_attr = true` never keeps `on*` handlers, `forbidden_attributes` or values that
|
|
19
|
+
fail URI/style checks.
|
|
20
|
+
- `srcset` is validated per candidate URL (including `allowed_uri_regexp`) instead of as a single URI.
|
|
21
|
+
- `IS_ALLOWED_URI` now matches DOMPurify: it adds `cid:`, `ftps:`, `sms:`, `callto:`, `xmpp:` and `matrix:`,
|
|
22
|
+
fixes an unescaped `-` that created the character range `.-:` (so relative URLs such as `images/logo.png` or
|
|
23
|
+
`img2.png` were rejected), and anchors with `\A`/`\z` instead of line anchors.
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
- `allow_remote_style_urls` (default `true`): set to `false` to remove remote (`http(s)`, `ftp(s)`, `//host`) CSS URLs
|
|
27
|
+
from `<style>` content and `style` attributes, e.g. to block tracking or CSS data exfiltration in HTML email.
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
- `<style>` is only kept where the tag rules allow it: the `html` profile (whose allowlist has no `style`) and
|
|
31
|
+
explicit `allowed_tags` lists without `style` now remove `<style>`. `html_email` and the default config still keep it.
|
|
32
|
+
- `<style>` inside SVG/MathML is now removed, like `<title>` and `<mglyph>` (stricter than DOMPurify).
|
|
33
|
+
- Options passed as a hash now apply `use_profiles` first, whatever the key order. Previously
|
|
34
|
+
`Dandruff.sanitize(html, forbidden_tags: ['b'], use_profiles: { html_email: true })` dropped `forbidden_tags`.
|
|
35
|
+
- `html`/`head`/`body` are implicitly allowed when parsing whole documents (`whole_document`, `return_dom`,
|
|
36
|
+
`allow_document_elements`), as with DOMPurify's `WHOLE_DOCUMENT`.
|
|
37
|
+
- Removed the unused `sanitize_style_value`/`unsafe_style_block?` private methods.
|
|
38
|
+
|
|
39
|
+
### Fixed
|
|
40
|
+
- `html_email` (or any whole-document mode) combined with an `allowed_tags` list lacking `html`/`body` no longer
|
|
41
|
+
raises `ArgumentError: A document may not have multiple root nodes`.
|
|
42
|
+
- A value written by a hook directly to the attribute node is no longer reverted, and assigning a frozen string to
|
|
43
|
+
`data[:value]` no longer raises.
|
|
44
|
+
- README: corrected the hook example (it raised `NoMethodError`), replaced the non-existent `scrub_until_stable`
|
|
45
|
+
option with `sanitize_until_stable`, and stopped recommending a single pass for performance.
|
|
46
|
+
|
|
3
47
|
## [0.8.1] - 2025-11-27
|
|
4
48
|
|
|
5
49
|
### Fixed
|
data/Gemfile.lock
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
GIT
|
|
2
2
|
remote: https://github.com/kuyio/kuyio-rubocop.git
|
|
3
|
-
revision:
|
|
3
|
+
revision: f7774235c8fddd1855cda03ea0415b5dad005621
|
|
4
4
|
specs:
|
|
5
|
-
kuyio-rubocop (0.
|
|
6
|
-
rubocop (~> 1.
|
|
7
|
-
rubocop-performance (~> 1.
|
|
8
|
-
rubocop-rails (~> 2.
|
|
9
|
-
rubocop-rspec (~> 3.
|
|
5
|
+
kuyio-rubocop (0.4.1)
|
|
6
|
+
rubocop (~> 1.85.0)
|
|
7
|
+
rubocop-performance (~> 1.26.0)
|
|
8
|
+
rubocop-rails (~> 2.34.0)
|
|
9
|
+
rubocop-rspec (~> 3.9.0)
|
|
10
10
|
|
|
11
11
|
PATH
|
|
12
12
|
remote: .
|
|
13
13
|
specs:
|
|
14
|
-
dandruff (0.
|
|
14
|
+
dandruff (0.9.0)
|
|
15
15
|
nokogiri (~> 1.10)
|
|
16
16
|
|
|
17
17
|
GEM
|
|
18
18
|
remote: https://rubygems.org/
|
|
19
19
|
specs:
|
|
20
|
-
activesupport (8.1.
|
|
20
|
+
activesupport (8.1.3)
|
|
21
21
|
base64
|
|
22
22
|
bigdecimal
|
|
23
23
|
concurrent-ruby (~> 1.0, >= 1.3.1)
|
|
@@ -33,44 +33,54 @@ GEM
|
|
|
33
33
|
ast (2.4.3)
|
|
34
34
|
base64 (0.3.0)
|
|
35
35
|
benchmark (0.5.0)
|
|
36
|
-
bigdecimal (
|
|
37
|
-
concurrent-ruby (1.3.
|
|
38
|
-
connection_pool (
|
|
36
|
+
bigdecimal (4.1.2)
|
|
37
|
+
concurrent-ruby (1.3.7)
|
|
38
|
+
connection_pool (3.0.2)
|
|
39
39
|
diff-lcs (1.6.2)
|
|
40
40
|
drb (2.2.3)
|
|
41
|
-
|
|
41
|
+
hana (1.3.7)
|
|
42
|
+
i18n (1.15.2)
|
|
42
43
|
concurrent-ruby (~> 1.0)
|
|
43
|
-
json (2.
|
|
44
|
-
|
|
44
|
+
json (2.21.1)
|
|
45
|
+
json_schemer (2.5.0)
|
|
46
|
+
bigdecimal
|
|
47
|
+
hana (~> 1.3)
|
|
48
|
+
regexp_parser (~> 2.0)
|
|
49
|
+
simpleidn (~> 0.2)
|
|
50
|
+
language_server-protocol (3.17.0.6)
|
|
45
51
|
lint_roller (1.1.0)
|
|
46
52
|
logger (1.7.0)
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
mcp (0.24.0)
|
|
54
|
+
json_schemer (>= 2.4)
|
|
55
|
+
minitest (6.0.6)
|
|
56
|
+
drb (~> 2.0)
|
|
57
|
+
prism (~> 1.5)
|
|
58
|
+
nokogiri (1.19.4-aarch64-linux-gnu)
|
|
49
59
|
racc (~> 1.4)
|
|
50
|
-
nokogiri (1.
|
|
60
|
+
nokogiri (1.19.4-aarch64-linux-musl)
|
|
51
61
|
racc (~> 1.4)
|
|
52
|
-
nokogiri (1.
|
|
62
|
+
nokogiri (1.19.4-arm-linux-gnu)
|
|
53
63
|
racc (~> 1.4)
|
|
54
|
-
nokogiri (1.
|
|
64
|
+
nokogiri (1.19.4-arm-linux-musl)
|
|
55
65
|
racc (~> 1.4)
|
|
56
|
-
nokogiri (1.
|
|
66
|
+
nokogiri (1.19.4-arm64-darwin)
|
|
57
67
|
racc (~> 1.4)
|
|
58
|
-
nokogiri (1.
|
|
68
|
+
nokogiri (1.19.4-x86_64-darwin)
|
|
59
69
|
racc (~> 1.4)
|
|
60
|
-
nokogiri (1.
|
|
70
|
+
nokogiri (1.19.4-x86_64-linux-gnu)
|
|
61
71
|
racc (~> 1.4)
|
|
62
|
-
nokogiri (1.
|
|
72
|
+
nokogiri (1.19.4-x86_64-linux-musl)
|
|
63
73
|
racc (~> 1.4)
|
|
64
|
-
parallel (1.
|
|
65
|
-
parser (3.3.
|
|
74
|
+
parallel (1.28.0)
|
|
75
|
+
parser (3.3.11.1)
|
|
66
76
|
ast (~> 2.4.1)
|
|
67
77
|
racc
|
|
68
|
-
prism (1.
|
|
78
|
+
prism (1.9.0)
|
|
69
79
|
racc (1.8.1)
|
|
70
|
-
rack (3.2.
|
|
80
|
+
rack (3.2.6)
|
|
71
81
|
rainbow (3.1.1)
|
|
72
|
-
rake (13.
|
|
73
|
-
regexp_parser (2.
|
|
82
|
+
rake (13.4.2)
|
|
83
|
+
regexp_parser (2.12.0)
|
|
74
84
|
rspec (3.13.2)
|
|
75
85
|
rspec-core (~> 3.13.0)
|
|
76
86
|
rspec-expectations (~> 3.13.0)
|
|
@@ -80,44 +90,46 @@ GEM
|
|
|
80
90
|
rspec-expectations (3.13.5)
|
|
81
91
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
82
92
|
rspec-support (~> 3.13.0)
|
|
83
|
-
rspec-mocks (3.13.
|
|
93
|
+
rspec-mocks (3.13.8)
|
|
84
94
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
85
95
|
rspec-support (~> 3.13.0)
|
|
86
|
-
rspec-support (3.13.
|
|
87
|
-
rubocop (1.
|
|
96
|
+
rspec-support (3.13.7)
|
|
97
|
+
rubocop (1.85.1)
|
|
88
98
|
json (~> 2.3)
|
|
89
99
|
language_server-protocol (~> 3.17.0.2)
|
|
90
100
|
lint_roller (~> 1.1.0)
|
|
101
|
+
mcp (~> 0.6)
|
|
91
102
|
parallel (~> 1.10)
|
|
92
103
|
parser (>= 3.3.0.2)
|
|
93
104
|
rainbow (>= 2.2.2, < 4.0)
|
|
94
105
|
regexp_parser (>= 2.9.3, < 3.0)
|
|
95
|
-
rubocop-ast (>= 1.
|
|
106
|
+
rubocop-ast (>= 1.49.0, < 2.0)
|
|
96
107
|
ruby-progressbar (~> 1.7)
|
|
97
108
|
unicode-display_width (>= 2.4.0, < 4.0)
|
|
98
|
-
rubocop-ast (1.
|
|
109
|
+
rubocop-ast (1.50.0)
|
|
99
110
|
parser (>= 3.3.7.2)
|
|
100
|
-
prism (~> 1.
|
|
101
|
-
rubocop-performance (1.
|
|
111
|
+
prism (~> 1.7)
|
|
112
|
+
rubocop-performance (1.26.1)
|
|
102
113
|
lint_roller (~> 1.1)
|
|
103
|
-
rubocop (>= 1.
|
|
104
|
-
rubocop-ast (>= 1.
|
|
105
|
-
rubocop-rails (2.
|
|
114
|
+
rubocop (>= 1.75.0, < 2.0)
|
|
115
|
+
rubocop-ast (>= 1.47.1, < 2.0)
|
|
116
|
+
rubocop-rails (2.34.3)
|
|
106
117
|
activesupport (>= 4.2.0)
|
|
107
118
|
lint_roller (~> 1.1)
|
|
108
119
|
rack (>= 1.1)
|
|
109
|
-
rubocop (>= 1.
|
|
110
|
-
rubocop-ast (>= 1.
|
|
111
|
-
rubocop-rspec (3.
|
|
120
|
+
rubocop (>= 1.75.0, < 2.0)
|
|
121
|
+
rubocop-ast (>= 1.44.0, < 2.0)
|
|
122
|
+
rubocop-rspec (3.9.0)
|
|
112
123
|
lint_roller (~> 1.1)
|
|
113
|
-
rubocop (~> 1.
|
|
124
|
+
rubocop (~> 1.81)
|
|
114
125
|
ruby-progressbar (1.13.0)
|
|
115
126
|
securerandom (0.4.1)
|
|
127
|
+
simpleidn (0.2.3)
|
|
116
128
|
tzinfo (2.0.6)
|
|
117
129
|
concurrent-ruby (~> 1.0)
|
|
118
130
|
unicode-display_width (3.2.0)
|
|
119
131
|
unicode-emoji (~> 4.1)
|
|
120
|
-
unicode-emoji (4.
|
|
132
|
+
unicode-emoji (4.2.0)
|
|
121
133
|
uri (1.1.1)
|
|
122
134
|
|
|
123
135
|
PLATFORMS
|
data/README.md
CHANGED
|
@@ -204,9 +204,16 @@ The `html_email` profile:
|
|
|
204
204
|
- Allows document structure tags (`head`, `meta`, `style`)
|
|
205
205
|
- Permits legacy presentation attributes (`bgcolor`, `cellpadding`, `align`, etc.)
|
|
206
206
|
- Uses per-tag attribute restrictions for security
|
|
207
|
-
- Allows style tags with content sanitization
|
|
207
|
+
- Allows style tags with content sanitization (`@import`, `expression()`, `behavior`/`-moz-binding`
|
|
208
|
+
and disallowed `url()` values are removed; see [CSS Injection](#css-injection))
|
|
208
209
|
- Excludes form elements and scripts
|
|
209
210
|
|
|
211
|
+
To stop remote resources referenced from CSS (tracking pixels, web fonts, CSS-based data
|
|
212
|
+
exfiltration) from loading, also set `config.allow_remote_style_urls = false`. This removes
|
|
213
|
+
declarations with remote `url()`, `src()` or `image-set()` URLs from `<style>` and `style`
|
|
214
|
+
attributes. It does not touch HTML attributes such as `<img src>` or `background`; use
|
|
215
|
+
`allowed_uri_regexp` or an `upon_sanitize_attribute` hook for those.
|
|
216
|
+
|
|
210
217
|
#### Per-Tag Attribute Control
|
|
211
218
|
|
|
212
219
|
Restrict which attributes are allowed on specific tags for maximum security:
|
|
@@ -241,7 +248,7 @@ dandruff.scrub(html)
|
|
|
241
248
|
```ruby
|
|
242
249
|
# Only allow HTTPS URLs from your domain
|
|
243
250
|
dandruff = Dandruff.new do |config|
|
|
244
|
-
config.allowed_uri_regexp =
|
|
251
|
+
config.allowed_uri_regexp = /\Ahttps:\/\/(www\.)?example\.com\//
|
|
245
252
|
end
|
|
246
253
|
|
|
247
254
|
html = '<a href="https://example.com/safe">OK</a><a href="http://evil.com">Bad</a>'
|
|
@@ -254,22 +261,18 @@ dandruff.scrub(html)
|
|
|
254
261
|
Hooks allow you to extend Dandruff's behavior:
|
|
255
262
|
|
|
256
263
|
```ruby
|
|
257
|
-
dandruff = Dandruff.new
|
|
264
|
+
dandruff = Dandruff.new do |config|
|
|
265
|
+
config.allow_data_attributes = false # only data-* attributes the hook keeps survive
|
|
266
|
+
end
|
|
258
267
|
|
|
259
|
-
# Custom attribute handling
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
# Force lowercase on certain attributes
|
|
270
|
-
if attr_name == 'id'
|
|
271
|
-
node[attr_name] = node[attr_name].downcase
|
|
272
|
-
end
|
|
268
|
+
# Custom attribute handling. `attr` is the Nokogiri::XML::Attr being checked
|
|
269
|
+
# (use `attr.parent` for the element).
|
|
270
|
+
dandruff.add_hook(:upon_sanitize_attribute) do |attr, data, config|
|
|
271
|
+
# Keep attributes the allowlists would otherwise remove
|
|
272
|
+
data[:keep_attr] = true if data[:attr_name].start_with?('data-safe-')
|
|
273
|
+
|
|
274
|
+
# Rewrite the value; the new value is still validated
|
|
275
|
+
data[:value] = data[:value].downcase if data[:attr_name] == 'id'
|
|
273
276
|
end
|
|
274
277
|
|
|
275
278
|
# Element processing
|
|
@@ -278,11 +281,20 @@ dandruff.add_hook(:upon_sanitize_element) do |node, data, config|
|
|
|
278
281
|
puts "Processing #{data[:tag_name]} element"
|
|
279
282
|
end
|
|
280
283
|
|
|
281
|
-
html = '<div data-safe-user-id="123"
|
|
284
|
+
html = '<div data-safe-user-id="123" data-key="ABC" id="MyID">Content</div>'
|
|
282
285
|
dandruff.scrub(html)
|
|
283
286
|
# => '<div data-safe-user-id="123" id="myid">Content</div>'
|
|
284
287
|
```
|
|
285
288
|
|
|
289
|
+
`upon_sanitize_attribute` receives `data` with `:tag_name`, `:attr_name`, `:value` and `:keep_attr`:
|
|
290
|
+
- Set `data[:value]` to rewrite the attribute (for example to route image URLs through a proxy).
|
|
291
|
+
The rewritten value goes through the same checks (URI, style, DOM clobbering) as the original.
|
|
292
|
+
- Set `data[:keep_attr] = false` to remove the attribute.
|
|
293
|
+
- Set `data[:keep_attr] = true` to keep an attribute that the tag/attribute allowlists would
|
|
294
|
+
remove. This never keeps event handlers (`on*`), attributes in `forbidden_attributes`, or
|
|
295
|
+
values that fail the URI/style checks.
|
|
296
|
+
- Hooks run once per sanitization pass (twice by default), so make rewrites idempotent.
|
|
297
|
+
|
|
286
298
|
Available hooks:
|
|
287
299
|
- `:before_sanitize_elements` - Before processing elements
|
|
288
300
|
- `:after_sanitize_elements` - After processing elements
|
|
@@ -316,8 +328,8 @@ Protect against mutation-based XSS (mXSS):
|
|
|
316
328
|
|
|
317
329
|
```ruby
|
|
318
330
|
dandruff = Dandruff.new do |config|
|
|
319
|
-
config.
|
|
320
|
-
config.mutation_max_passes = 2 # default
|
|
331
|
+
config.sanitize_until_stable = true # default
|
|
332
|
+
config.mutation_max_passes = 2 # default (alias: pass_limit)
|
|
321
333
|
end
|
|
322
334
|
|
|
323
335
|
# Dandruff will re-sanitize until output is stable
|
|
@@ -364,9 +376,10 @@ Complete list of configuration options with defaults and security implications:
|
|
|
364
376
|
| `allow_aria_attributes` | `Boolean` | `true` | Controls `aria-*` attributes for accessibility. |
|
|
365
377
|
| `allow_data_uri` | `Boolean` | `true` | Blocks `data:` URIs by default for safety. |
|
|
366
378
|
| `allow_unknown_protocols` | `Boolean` | `false` | If true, permits non-standard schemes (⚠️ security risk). |
|
|
367
|
-
| `allowed_uri_regexp` | `Regexp` | `nil` | Custom regexp to validate URI attributes. |
|
|
379
|
+
| `allowed_uri_regexp` | `Regexp` | `nil` | Custom regexp to validate URI attributes (anchor with `\A`, not `^`). Default allows `http(s)`, `ftp(s)`, `mailto`, `tel`, `callto`, `sms`, `cid`, `xmpp`, `matrix` and relative URLs. |
|
|
368
380
|
| `additional_uri_safe_attributes` | `Array<String>` | `[]` | Extra attributes treated as URI-like. |
|
|
369
|
-
| `allow_style_tags` | `Boolean` | `true` | `<style>`
|
|
381
|
+
| `allow_style_tags` | `Boolean` | `true` | Allow `<style>` elements (they must also pass `forbidden_tags`/`allowed_tags`); their content is sanitized. |
|
|
382
|
+
| `allow_remote_style_urls` | `Boolean` | `true` | If false, removes CSS declarations/rules with remote `url()`/`src()`/`image-set()` URLs (`http(s)`, `ftp(s)`, `//host`) from `<style>` and `style` attributes. |
|
|
370
383
|
| `sanitize_dom` | `Boolean` | `true` | Removes DOM clobbering `id`/`name` values. |
|
|
371
384
|
| `safe_for_templates` | `Boolean` | `false` | Strips template expressions (`{{ }}`, `<%= %>`, `${ }`). |
|
|
372
385
|
| `safe_for_xml` | `Boolean` | `true` | Removes comments/PI in XML-ish content. |
|
|
@@ -380,7 +393,7 @@ Complete list of configuration options with defaults and security implications:
|
|
|
380
393
|
| `mutation_max_passes` | `Integer` | `2` | Max passes for stabilization. Higher = more secure, slower. |
|
|
381
394
|
| `keep_content` | `Boolean` | `true` | If false, removes contents of stripped elements. |
|
|
382
395
|
| `in_place` | `Boolean` | `false` | Attempts to sanitize in place (experimental). |
|
|
383
|
-
| `use_profiles` | `Hash` | `{}` | Enable content type profiles: `:html`, `:svg`, `:svg_filters`, `:math_ml`, `:html_email`. |
|
|
396
|
+
| `use_profiles` | `Hash` | `{}` | Enable content type profiles: `:html`, `:svg`, `:svg_filters`, `:math_ml`, `:html_email`. Setting it resets profile-dependent options (`allowed_tags`, `allowed_attributes`, `forbidden_tags`, ...). In a hash it is always applied first; in a block, set it before customising other options. |
|
|
384
397
|
| `namespace` | `String` | `'http://www.w3.org/1999/xhtml'` | Namespace for XHTML handling. |
|
|
385
398
|
| `parser_media_type` | `String` | `'text/html'` | Parser media type; set to `application/xhtml+xml` for XHTML. |
|
|
386
399
|
|
|
@@ -470,11 +483,11 @@ Adds a hook function.
|
|
|
470
483
|
|
|
471
484
|
**Parameters:**
|
|
472
485
|
- `entry_point` (Symbol) - Hook name (`:before_sanitize_elements`, `:upon_sanitize_attribute`, etc.)
|
|
473
|
-
- `block` (Proc) - Hook function receiving `(node, data, config)`
|
|
486
|
+
- `block` (Proc) - Hook function receiving `(node, data, config)`; for `:upon_sanitize_attribute` the first argument is the `Nokogiri::XML::Attr`
|
|
474
487
|
|
|
475
488
|
**Example:**
|
|
476
489
|
```ruby
|
|
477
|
-
dandruff.add_hook(:upon_sanitize_attribute) do |
|
|
490
|
+
dandruff.add_hook(:upon_sanitize_attribute) do |attr, data, config|
|
|
478
491
|
data[:keep_attr] = true if data[:attr_name] == 'data-safe'
|
|
479
492
|
end
|
|
480
493
|
```
|
|
@@ -621,7 +634,7 @@ dandruff.scrub('<a onclick="alert(1)">Click</a>')
|
|
|
621
634
|
```ruby
|
|
622
635
|
# mXSS prevented through multi-pass sanitization
|
|
623
636
|
dandruff = Dandruff.new do |config|
|
|
624
|
-
config.
|
|
637
|
+
config.sanitize_until_stable = true # default
|
|
625
638
|
config.mutation_max_passes = 2
|
|
626
639
|
end
|
|
627
640
|
```
|
|
@@ -667,7 +680,11 @@ dandruff.scrub('<link href="vbscript:msgbox(1)">')
|
|
|
667
680
|
**Protection:**
|
|
668
681
|
- Parses and validates inline `style` attributes
|
|
669
682
|
- Removes dangerous CSS properties and values
|
|
670
|
-
-
|
|
683
|
+
- Sanitizes `<style>` content rule by rule instead of dropping whole blocks: removes `@import`,
|
|
684
|
+
`expression()`, `behavior`/`-moz-binding`, `javascript:`/`vbscript:` and any `url()`, `src()` or
|
|
685
|
+
`image-set()` URL that fails the URI rules (`data:` only for images and only with
|
|
686
|
+
`allow_data_uri`; `allowed_uri_regexp` applies). CSS escapes and comments are decoded before checking.
|
|
687
|
+
- Remote CSS URLs are kept by default (like remote `<img>`); set `allow_remote_style_urls = false` to remove them
|
|
671
688
|
|
|
672
689
|
```ruby
|
|
673
690
|
dandruff.scrub('<div style="expression(alert(1))"></div>')
|
|
@@ -693,7 +710,7 @@ config.forbidden_tags = ['script', 'iframe', ...] # incomplete!
|
|
|
693
710
|
|
|
694
711
|
```ruby
|
|
695
712
|
# ✅ Good - only allow HTTPS
|
|
696
|
-
config.allowed_uri_regexp =
|
|
713
|
+
config.allowed_uri_regexp = /\Ahttps:/
|
|
697
714
|
|
|
698
715
|
# ⚠️ Caution - allowing unknown protocols is risky
|
|
699
716
|
config.allow_unknown_protocols = true # avoid unless necessary
|
|
@@ -776,7 +793,7 @@ dandruff = Dandruff.new do |config|
|
|
|
776
793
|
]
|
|
777
794
|
config.allowed_attributes = ['href', 'src', 'alt', 'title', 'class', 'id']
|
|
778
795
|
config.allow_data_uri = true # for embedded images
|
|
779
|
-
config.allowed_uri_regexp =
|
|
796
|
+
config.allowed_uri_regexp = /\A(?:https?:|\/)/ # only https and relative
|
|
780
797
|
end
|
|
781
798
|
```
|
|
782
799
|
|
|
@@ -859,16 +876,15 @@ end
|
|
|
859
876
|
cleaned_docs = documents.map { |doc| dandruff.scrub(doc) }
|
|
860
877
|
```
|
|
861
878
|
|
|
862
|
-
#### 4.
|
|
879
|
+
#### 4. Multi-Pass Limit
|
|
863
880
|
|
|
864
|
-
|
|
881
|
+
A single pass already removes disallowed elements and attributes. The second pass
|
|
882
|
+
re-parses the output to catch parser mutations (mXSS), so keep the default of 2 for
|
|
883
|
+
untrusted input. Only lower it for content that is already trusted:
|
|
865
884
|
|
|
866
885
|
```ruby
|
|
867
|
-
#
|
|
868
|
-
config.
|
|
869
|
-
|
|
870
|
-
# Or reduce max passes
|
|
871
|
-
config.mutation_max_passes = 1 # default is 2
|
|
886
|
+
# Single pass: skips the mXSS re-parse check, use only for trusted content
|
|
887
|
+
config.sanitize_until_stable = false # equivalent to mutation_max_passes = 1
|
|
872
888
|
```
|
|
873
889
|
|
|
874
890
|
#### 5. Return DOM for Further Processing
|
|
@@ -1073,13 +1089,17 @@ config.allowed_attributes_per_tag = {
|
|
|
1073
1089
|
|
|
1074
1090
|
### Style tags are removed
|
|
1075
1091
|
|
|
1076
|
-
|
|
1092
|
+
`<style>` is kept when `allow_style_tags` is true (the default), it is not in `forbidden_tags`, and,
|
|
1093
|
+
if you set `allowed_tags` or use a profile, the allowlist contains `style`. The `html` profile's allowlist
|
|
1094
|
+
does not include `style`; `html_email` does. In fragments, a `<style>` at the top level or inside
|
|
1095
|
+
SVG/MathML is removed.
|
|
1077
1096
|
|
|
1078
1097
|
```ruby
|
|
1079
|
-
config.
|
|
1098
|
+
config.allowed_tags = %w[p div style] # explicit allowlists must list style
|
|
1099
|
+
config.allow_style_tags = true # default
|
|
1080
1100
|
|
|
1081
1101
|
# For whole documents (like emails)
|
|
1082
|
-
config.
|
|
1102
|
+
config.use_profiles = { html_email: true }
|
|
1083
1103
|
```
|
|
1084
1104
|
|
|
1085
1105
|
### URI validation is too strict
|
|
@@ -1088,7 +1108,7 @@ config.whole_document = true
|
|
|
1088
1108
|
|
|
1089
1109
|
```ruby
|
|
1090
1110
|
# Allow more protocols
|
|
1091
|
-
config.allowed_uri_regexp =
|
|
1111
|
+
config.allowed_uri_regexp = /\A(?:https?|ftp|mailto):/
|
|
1092
1112
|
|
|
1093
1113
|
# Or allow unknown protocols (⚠️ less secure)
|
|
1094
1114
|
config.allow_unknown_protocols = true
|
|
@@ -1101,14 +1121,11 @@ config.allow_unknown_protocols = true
|
|
|
1101
1121
|
```ruby
|
|
1102
1122
|
# Use specific allowlists
|
|
1103
1123
|
config.allowed_tags = ['p', 'strong', 'em'] # faster than nil/defaults
|
|
1104
|
-
|
|
1105
|
-
# Reduce multi-pass iterations for trusted content
|
|
1106
|
-
config.mutation_max_passes = 1 # default is 2
|
|
1107
|
-
|
|
1108
|
-
# Disable multi-pass for pre-validated content
|
|
1109
|
-
config.scrub_until_stable = false # use with caution
|
|
1110
1124
|
```
|
|
1111
1125
|
|
|
1126
|
+
Avoid lowering `mutation_max_passes` / disabling `sanitize_until_stable` for untrusted
|
|
1127
|
+
input: the second pass is the mXSS check, not a performance knob.
|
|
1128
|
+
|
|
1112
1129
|
## 🤝 Contributing
|
|
1113
1130
|
|
|
1114
1131
|
We welcome contributions! Here's how to get involved:
|
data/lib/dandruff/config.rb
CHANGED
|
@@ -90,7 +90,7 @@ module Dandruff
|
|
|
90
90
|
# Custom regexp for validating URI attributes
|
|
91
91
|
# @return [Regexp, nil] custom URI validation pattern or nil for default
|
|
92
92
|
# @example Only allow HTTPS
|
|
93
|
-
# config.allowed_uri_regexp =
|
|
93
|
+
# config.allowed_uri_regexp = /\Ahttps:/
|
|
94
94
|
|
|
95
95
|
# @!attribute [rw] forbidden_attributes
|
|
96
96
|
# Attributes that are always removed (takes precedence over allowed)
|
|
@@ -107,7 +107,15 @@ module Dandruff
|
|
|
107
107
|
# @!attribute [rw] allow_style_tags
|
|
108
108
|
# Allow <style> tags with content sanitization
|
|
109
109
|
# @return [Boolean] true to allow style tags (default: true)
|
|
110
|
-
# @note
|
|
110
|
+
# @note <style> must also pass forbidden_tags/allowed_tags. Its content is sanitized:
|
|
111
|
+
# @import rules, expression(), behavior/-moz-binding, script URLs and disallowed
|
|
112
|
+
# url() values are removed (see {Dandruff::Css})
|
|
113
|
+
|
|
114
|
+
# @!attribute [rw] allow_remote_style_urls
|
|
115
|
+
# Allow remote (http, https, ftp, protocol-relative) URLs in CSS: url(), src() and
|
|
116
|
+
# image-set() in <style> content and style attributes. Set to false to strip them,
|
|
117
|
+
# e.g. to stop remote tracking or data exfiltration through CSS in HTML email.
|
|
118
|
+
# @return [Boolean] (default: true)
|
|
111
119
|
|
|
112
120
|
# @!attribute [rw] allow_document_elements
|
|
113
121
|
# Allow html/head/body document structure elements
|
|
@@ -187,7 +195,7 @@ module Dandruff
|
|
|
187
195
|
:mathml_text_integration_points, :namespace, :parser_media_type,
|
|
188
196
|
:return_dom_fragment, :return_dom,
|
|
189
197
|
:safe_for_templates, :safe_for_xml, :sanitize_dom, :sanitize_until_stable, :mutation_max_passes,
|
|
190
|
-
:sanitize_named_props, :trusted_types_policy, :allow_style_tags, :minimal_profile,
|
|
198
|
+
:sanitize_named_props, :trusted_types_policy, :allow_style_tags, :allow_remote_style_urls, :minimal_profile,
|
|
191
199
|
:whole_document, :allow_document_elements
|
|
192
200
|
|
|
193
201
|
# Initializes a new configuration instance
|
|
@@ -221,6 +229,7 @@ module Dandruff
|
|
|
221
229
|
@in_place = false
|
|
222
230
|
@minimal_profile = false
|
|
223
231
|
@allow_style_tags = true
|
|
232
|
+
@allow_remote_style_urls = true
|
|
224
233
|
|
|
225
234
|
# Profiles / namespaces
|
|
226
235
|
@use_profiles = {}
|
|
@@ -397,8 +406,10 @@ module Dandruff
|
|
|
397
406
|
# @example Email profile
|
|
398
407
|
# config.use_profiles = { html_email: true }
|
|
399
408
|
#
|
|
400
|
-
# @note Setting profiles resets allowed_tags and
|
|
401
|
-
#
|
|
409
|
+
# @note Setting profiles resets allowed_tags, allowed_attributes, forbidden_tags and the other
|
|
410
|
+
# profile-dependent options. In a hash (`Dandruff.new(use_profiles: ..., forbidden_tags: ...)`)
|
|
411
|
+
# profiles are always applied first; in a configuration block, set `use_profiles` before
|
|
412
|
+
# the options you want to customise.
|
|
402
413
|
def use_profiles=(profiles)
|
|
403
414
|
@use_profiles = profiles || {}
|
|
404
415
|
reset_profile_dependent_settings
|
|
@@ -409,10 +420,14 @@ module Dandruff
|
|
|
409
420
|
|
|
410
421
|
# Applies configuration options from a hash
|
|
411
422
|
#
|
|
423
|
+
# `use_profiles` is applied first, whatever its position in the hash: setting a profile
|
|
424
|
+
# resets profile-dependent options, which would otherwise discard options given before it.
|
|
425
|
+
#
|
|
412
426
|
# @param cfg [Hash] configuration hash
|
|
413
427
|
def apply_config(cfg)
|
|
414
|
-
cfg.
|
|
415
|
-
|
|
428
|
+
entries = cfg.map { |key, value| [key.to_s.downcase, value] }
|
|
429
|
+
profiles, options = entries.partition { |key, _| key == 'use_profiles' }
|
|
430
|
+
(profiles + options).each do |normalized, value|
|
|
416
431
|
setter = CONFIG_MAPPING[normalized] || :"#{normalized}="
|
|
417
432
|
send(setter, value) if respond_to?(setter)
|
|
418
433
|
end
|
data/lib/dandruff/css.rb
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dandruff
|
|
4
|
+
# Sanitizes CSS text, i.e. the content of `<style>` elements
|
|
5
|
+
#
|
|
6
|
+
# The stylesheet is split into segments at the structural characters `;`, `{` and `}`
|
|
7
|
+
# (outside strings, comments and unquoted `url()`). Each segment (a declaration, a selector or
|
|
8
|
+
# an at-rule prelude) is checked on its own, with comments removed and CSS escapes decoded, so
|
|
9
|
+
# obfuscations such as `@\69mport`, `u\rl(` or `exp/**/ression(` are recognised. Unsafe
|
|
10
|
+
# segments are dropped; everything else, including the delimiters, is kept verbatim, so the
|
|
11
|
+
# rest of the stylesheet keeps working and dropping text cannot join neighbouring tokens.
|
|
12
|
+
#
|
|
13
|
+
# A segment is dropped when it contains:
|
|
14
|
+
# - an `@import` rule
|
|
15
|
+
# - `expression(`, `behavior:` or `-moz-binding:` (legacy script-in-CSS vectors)
|
|
16
|
+
# - `javascript:`, `vbscript:` or `livescript:`
|
|
17
|
+
# - a `url()`, `src()` or `image-set()` URL that is not allowed: `data:` URLs are only allowed
|
|
18
|
+
# for images and only when `allow_data_uri` is set, other URLs must pass
|
|
19
|
+
# `allowed_uri_regexp` (when set) and the default URI allowlist, and remote URLs
|
|
20
|
+
# (`http(s):`, `ftp(s):`, `//host`) are dropped when `allow_remote_style_urls` is false
|
|
21
|
+
#
|
|
22
|
+
# @api private
|
|
23
|
+
class Css
|
|
24
|
+
ESCAPE = /\\(?:([0-9a-fA-F]{1,6})(?:\r\n|[ \t\r\n\f])?|(\r\n|[\r\n\f])|(.)|\z)/m
|
|
25
|
+
DANGEROUS = /@import|expression\(|(?:behavior|binding):|(?:java|vb|live)script:/i
|
|
26
|
+
URL_FUNCTION = /(?:url|src)\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*))/i
|
|
27
|
+
IMAGE_SET = /image-set\((.*)/im
|
|
28
|
+
QUOTED = /"([^"]*)"|'([^']*)'/
|
|
29
|
+
DATA_IMAGE = %r{\Adata:image/[a-z0-9.+-]+[;,]}i
|
|
30
|
+
REMOTE = %r{\A(?:(?:f|ht)tps?:|[/\\]{2})}i
|
|
31
|
+
SPACE = /[\s\u0085\u00a0\u1680\u180e\u2000-\u200b\u2028\u2029\u205f\u3000]+/
|
|
32
|
+
CONTROL = /[\x00-\x1f\x7f]/
|
|
33
|
+
DELIMITERS = %w[; { }].freeze
|
|
34
|
+
|
|
35
|
+
# @param allow_data_uri [Boolean] allow `data:image/...` URLs
|
|
36
|
+
# @param allowed_uri_regexp [Regexp, nil] custom URI allowlist
|
|
37
|
+
# @param allow_remote_urls [Boolean] allow absolute remote URLs (http, https, ftp, //host)
|
|
38
|
+
def initialize(allow_data_uri: true, allowed_uri_regexp: nil, allow_remote_urls: true)
|
|
39
|
+
@allow_data_uri = allow_data_uri
|
|
40
|
+
@allowed_uri_regexp = allowed_uri_regexp
|
|
41
|
+
@allow_remote_urls = allow_remote_urls
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Returns the CSS with unsafe segments removed
|
|
45
|
+
#
|
|
46
|
+
# @param css [String] CSS text
|
|
47
|
+
# @return [String] sanitized CSS text
|
|
48
|
+
def sanitize(css)
|
|
49
|
+
segments(css.to_s.scrub).map { |raw, code, delimiter| (safe?(code) ? raw : '') + delimiter }.join
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def safe?(code)
|
|
55
|
+
code.strip.empty? || !dangerous?(decode(code))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Splits CSS into [raw text, text without comments, delimiter] triples.
|
|
59
|
+
#
|
|
60
|
+
# Delimiters inside strings, comments and unquoted url() do not split; escapes are kept
|
|
61
|
+
# together with the escaped character.
|
|
62
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
63
|
+
def segments(css)
|
|
64
|
+
result = []
|
|
65
|
+
raw = +''
|
|
66
|
+
code = +''
|
|
67
|
+
state = :normal
|
|
68
|
+
i = 0
|
|
69
|
+
while i < css.length
|
|
70
|
+
ch = css[i]
|
|
71
|
+
if state == :comment
|
|
72
|
+
raw << ch
|
|
73
|
+
if ch == '*' && css[i + 1] == '/'
|
|
74
|
+
raw << '/'
|
|
75
|
+
i += 1
|
|
76
|
+
state = :normal
|
|
77
|
+
end
|
|
78
|
+
elsif ch == '\\'
|
|
79
|
+
escaped = css[i, 2]
|
|
80
|
+
raw << escaped
|
|
81
|
+
code << escaped
|
|
82
|
+
i += 1
|
|
83
|
+
elsif state.is_a?(String) # inside a quoted string; the state is the quote character
|
|
84
|
+
raw << ch
|
|
85
|
+
code << ch
|
|
86
|
+
state = :normal if ch == state || ch == "\n"
|
|
87
|
+
elsif state == :url
|
|
88
|
+
raw << ch
|
|
89
|
+
code << ch
|
|
90
|
+
state = :normal if ch == ')'
|
|
91
|
+
elsif ch == '/' && css[i + 1] == '*'
|
|
92
|
+
raw << '/*'
|
|
93
|
+
i += 1
|
|
94
|
+
state = :comment
|
|
95
|
+
elsif DELIMITERS.include?(ch)
|
|
96
|
+
result << [raw, code, ch]
|
|
97
|
+
raw = +''
|
|
98
|
+
code = +''
|
|
99
|
+
else
|
|
100
|
+
raw << ch
|
|
101
|
+
code << ch
|
|
102
|
+
if ['"', "'"].include?(ch)
|
|
103
|
+
state = ch
|
|
104
|
+
elsif ch == '(' && code.match?(/url\(\z/i) && !css[(i + 1)..].match?(/\A\s*["']/)
|
|
105
|
+
state = :url
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
i += 1
|
|
109
|
+
end
|
|
110
|
+
result << [raw, code, '']
|
|
111
|
+
end
|
|
112
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
113
|
+
|
|
114
|
+
def dangerous?(text)
|
|
115
|
+
return true if text.gsub(SPACE, '').gsub(CONTROL, '').match?(DANGEROUS)
|
|
116
|
+
|
|
117
|
+
urls(text).any? { |url| !allowed_url?(url) }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def urls(text)
|
|
121
|
+
found = text.scan(URL_FUNCTION).flat_map(&:compact)
|
|
122
|
+
text.scan(IMAGE_SET).flatten.each { |rest| found.concat(rest.scan(QUOTED).flat_map(&:compact)) }
|
|
123
|
+
found
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def allowed_url?(url)
|
|
127
|
+
url = url.gsub(/\A#{SPACE}|#{SPACE}\z/o, '')
|
|
128
|
+
return true if url.empty?
|
|
129
|
+
return false if url.match?(CONTROL)
|
|
130
|
+
return @allow_data_uri && url.match?(DATA_IMAGE) if url.match?(/\Adata:/i)
|
|
131
|
+
return false if @allowed_uri_regexp && !url.match?(@allowed_uri_regexp)
|
|
132
|
+
return false unless url.match?(Expressions::IS_ALLOWED_URI)
|
|
133
|
+
|
|
134
|
+
@allow_remote_urls || !url.match?(REMOTE)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Decodes CSS escapes (CSS Syntax Level 3, "consume an escaped code point")
|
|
138
|
+
def decode(text)
|
|
139
|
+
text.gsub(ESCAPE) do
|
|
140
|
+
hex, newline, char = Regexp.last_match.captures
|
|
141
|
+
if hex
|
|
142
|
+
code_point(hex.to_i(16))
|
|
143
|
+
elsif newline
|
|
144
|
+
''
|
|
145
|
+
else
|
|
146
|
+
char.to_s
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def code_point(value)
|
|
152
|
+
return "�" if value.zero? || value > 0x10FFFF || (0xD800..0xDFFF).cover?(value)
|
|
153
|
+
|
|
154
|
+
[value].pack('U')
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
data/lib/dandruff/expressions.rb
CHANGED
|
@@ -71,21 +71,34 @@ module Dandruff
|
|
|
71
71
|
|
|
72
72
|
# Validates allowed URI protocols and relative URLs
|
|
73
73
|
#
|
|
74
|
-
# This is the default URI validation pattern matching DOMPurify's
|
|
75
|
-
# Allows
|
|
76
|
-
#
|
|
74
|
+
# This is the default URI validation pattern, matching DOMPurify's IS_ALLOWED_URI.
|
|
75
|
+
# Allows the http, https, ftp, ftps, mailto, tel, callto, sms, cid, xmpp and matrix
|
|
76
|
+
# schemes plus relative URLs. A value without one of those schemes is only accepted
|
|
77
|
+
# when it cannot be parsed as a scheme: it starts with a non-letter, or its leading
|
|
78
|
+
# run of scheme characters (letters, `+`, `.`, `-`) is followed by anything but `:`.
|
|
77
79
|
#
|
|
78
|
-
# **Allowed
|
|
79
|
-
#
|
|
80
|
+
# **Allowed:** the schemes above, relative URLs (`/`, `./`, `../`, `#`, `?`, `page2.html`,
|
|
81
|
+
# `images/logo.png`), `cid:` references to inline email parts
|
|
82
|
+
# **Blocked:** javascript, vbscript, data (handled separately) and other unknown schemes
|
|
83
|
+
#
|
|
84
|
+
# The pattern is anchored with `\A` (not `^`) so that only the start of the whole value counts.
|
|
80
85
|
#
|
|
81
86
|
# @example Valid URIs
|
|
82
87
|
# 'https://example.com' =~ Expressions::IS_ALLOWED_URI # matches
|
|
83
88
|
# 'mailto:user@example.com' =~ Expressions::IS_ALLOWED_URI # matches
|
|
89
|
+
# 'cid:image001.png@01D9C2A0' =~ Expressions::IS_ALLOWED_URI # matches
|
|
84
90
|
# '/path/to/page' =~ Expressions::IS_ALLOWED_URI # matches
|
|
91
|
+
# 'images/logo.png' =~ Expressions::IS_ALLOWED_URI # matches
|
|
85
92
|
# 'javascript:alert(1)' =~ Expressions::IS_ALLOWED_URI # does not match
|
|
86
93
|
#
|
|
87
94
|
# @see Config#allowed_uri_regexp Custom URI pattern override
|
|
88
|
-
IS_ALLOWED_URI =
|
|
95
|
+
IS_ALLOWED_URI = /
|
|
96
|
+
\A(?:
|
|
97
|
+
(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|matrix): # allowed schemes
|
|
98
|
+
| [^a-z] # starts with a non-letter
|
|
99
|
+
| [a-z+.-]+(?:[^a-z+.\-:]|\z) # scheme characters not followed by ':'
|
|
100
|
+
)
|
|
101
|
+
/ix
|
|
89
102
|
|
|
90
103
|
# Detects dangerous JavaScript and data:text/html URIs
|
|
91
104
|
#
|
data/lib/dandruff/version.rb
CHANGED
data/lib/dandruff.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'nokogiri'
|
|
4
|
-
require 'set'
|
|
5
4
|
require 'uri'
|
|
6
5
|
|
|
7
6
|
require_relative 'dandruff/version'
|
|
@@ -10,6 +9,7 @@ require_relative 'dandruff/tags'
|
|
|
10
9
|
require_relative 'dandruff/attributes'
|
|
11
10
|
require_relative 'dandruff/expressions'
|
|
12
11
|
require_relative 'dandruff/utils'
|
|
12
|
+
require_relative 'dandruff/css'
|
|
13
13
|
|
|
14
14
|
# Dandruff - A robust HTML sanitizer for Ruby
|
|
15
15
|
#
|
|
@@ -60,7 +60,7 @@ require_relative 'dandruff/utils'
|
|
|
60
60
|
#
|
|
61
61
|
# @example Custom hooks
|
|
62
62
|
# dandruff = Dandruff.new
|
|
63
|
-
# dandruff.add_hook(:upon_sanitize_attribute) do |
|
|
63
|
+
# dandruff.add_hook(:upon_sanitize_attribute) do |attr, data, config|
|
|
64
64
|
# # Custom attribute processing
|
|
65
65
|
# if data[:attr_name] == 'data-safe'
|
|
66
66
|
# data[:keep_attr] = true
|
|
@@ -90,6 +90,7 @@ module Dandruff
|
|
|
90
90
|
# It parses HTML, removes dangerous elements and attributes, and serializes the result.
|
|
91
91
|
class Sanitizer
|
|
92
92
|
MATH_SVG_TAGS = %w[math svg].freeze
|
|
93
|
+
DOCUMENT_TAGS = %w[html head body].freeze
|
|
93
94
|
attr_reader :removed, :config, :hooks
|
|
94
95
|
|
|
95
96
|
# Initializes a new sanitizer instance
|
|
@@ -174,6 +175,7 @@ module Dandruff
|
|
|
174
175
|
|
|
175
176
|
cfg.empty? ? ensure_config : set_config(cfg)
|
|
176
177
|
@removed = []
|
|
178
|
+
@css_sanitizer = nil # rebuilt from the current config
|
|
177
179
|
return '' if dirty.nil?
|
|
178
180
|
|
|
179
181
|
# Handle empty strings - still process through DOM if return_dom is enabled
|
|
@@ -278,13 +280,6 @@ module Dandruff
|
|
|
278
280
|
node.remove
|
|
279
281
|
end
|
|
280
282
|
next
|
|
281
|
-
elsif node.element? && node.name == 'style'
|
|
282
|
-
node.remove && next unless @config.allow_style_tags
|
|
283
|
-
|
|
284
|
-
if unsafe_style_node?(node)
|
|
285
|
-
node.remove
|
|
286
|
-
next
|
|
287
|
-
end
|
|
288
283
|
elsif node.element?
|
|
289
284
|
sanitize_element(node)
|
|
290
285
|
elsif node.text? && @config.safe_for_templates
|
|
@@ -312,11 +307,44 @@ module Dandruff
|
|
|
312
307
|
handle_disallowed_element(node, tag_name)
|
|
313
308
|
return
|
|
314
309
|
end
|
|
310
|
+
return if tag_name == 'style' && remove_unsafe_style_node(node)
|
|
315
311
|
|
|
316
312
|
sanitize_attributes(node)
|
|
313
|
+
sanitize_style_content(node) if tag_name == 'style'
|
|
317
314
|
handle_vml_namespace(node)
|
|
318
315
|
end
|
|
319
316
|
|
|
317
|
+
# Removes unsafe rules and declarations from the text of a <style> element
|
|
318
|
+
#
|
|
319
|
+
# @param node [Nokogiri::XML::Element] the style element
|
|
320
|
+
# @see Css
|
|
321
|
+
def sanitize_style_content(node)
|
|
322
|
+
css = node.content
|
|
323
|
+
clean = css_sanitizer.sanitize(css)
|
|
324
|
+
node.content = clean unless clean == css
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
def css_sanitizer
|
|
328
|
+
@css_sanitizer ||= Css.new(
|
|
329
|
+
allow_data_uri: @config.allow_data_uri,
|
|
330
|
+
allowed_uri_regexp: @config.allowed_uri_regexp,
|
|
331
|
+
allow_remote_urls: @config.allow_remote_style_urls
|
|
332
|
+
)
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# Removes a <style> element that is allowed by tag rules but sits in an unsafe position
|
|
336
|
+
# (e.g. top level of a fragment, inside option/select, or containing markup).
|
|
337
|
+
#
|
|
338
|
+
# @param node [Nokogiri::XML::Element] the style element
|
|
339
|
+
# @return [Boolean] true if the node was removed
|
|
340
|
+
def remove_unsafe_style_node(node)
|
|
341
|
+
return false unless unsafe_style_node?(node)
|
|
342
|
+
|
|
343
|
+
@removed << { element: node }
|
|
344
|
+
node.remove
|
|
345
|
+
true
|
|
346
|
+
end
|
|
347
|
+
|
|
320
348
|
# Sanitizes attributes of an element
|
|
321
349
|
#
|
|
322
350
|
# @param node [Nokogiri::XML::Element] element to sanitize attributes for
|
|
@@ -339,9 +367,10 @@ module Dandruff
|
|
|
339
367
|
had_xlink_href ||= (lc_name == 'xlink:href')
|
|
340
368
|
had_xlink_href ||= (attr.namespace&.href == 'http://www.w3.org/1999/xlink')
|
|
341
369
|
|
|
342
|
-
|
|
370
|
+
value, keep_attr = run_attribute_hooks(attr, tag_name, lc_name, value)
|
|
371
|
+
value, keep_attr = strip_remote_style_urls(lc_name, value, keep_attr)
|
|
343
372
|
|
|
344
|
-
if
|
|
373
|
+
if attribute_permitted?(tag_name, lc_name, value, keep_attr)
|
|
345
374
|
attr.value = value if value != attr.value
|
|
346
375
|
else
|
|
347
376
|
to_remove << name
|
|
@@ -364,6 +393,58 @@ module Dandruff
|
|
|
364
393
|
execute_hooks(:after_sanitize_attributes, node)
|
|
365
394
|
end
|
|
366
395
|
|
|
396
|
+
# Runs upon_sanitize_attribute hooks for an attribute
|
|
397
|
+
#
|
|
398
|
+
# @return [Array(String, Boolean)] the value to validate and the hook's keep_attr decision
|
|
399
|
+
def run_attribute_hooks(attr, tag_name, attr_name, value)
|
|
400
|
+
hook_data = { tag_name: tag_name, attr_name: attr_name, value: value.dup, keep_attr: nil }
|
|
401
|
+
execute_hooks(:upon_sanitize_attribute, attr, hook_data)
|
|
402
|
+
[attribute_value_after_hooks(attr, hook_data, value), hook_data[:keep_attr]]
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# Removes remote CSS URLs from a style attribute when allow_remote_style_urls is false
|
|
406
|
+
#
|
|
407
|
+
# @return [Array(String, Boolean)] the value and keep_attr (false if nothing is left)
|
|
408
|
+
def strip_remote_style_urls(attr_name, value, keep_attr)
|
|
409
|
+
return [value, keep_attr] if attr_name != 'style' || @config.allow_remote_style_urls
|
|
410
|
+
|
|
411
|
+
value = css_sanitizer.sanitize(value)
|
|
412
|
+
[value, value.strip.empty? ? false : keep_attr]
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
# Returns the attribute value to validate after upon_sanitize_attribute hooks ran.
|
|
416
|
+
#
|
|
417
|
+
# Hooks may replace or mutate data[:value], or write the Nokogiri::XML::Attr directly.
|
|
418
|
+
#
|
|
419
|
+
# @param attr [Nokogiri::XML::Attr] the attribute passed to the hooks
|
|
420
|
+
# @param data [Hash] the hook data hash
|
|
421
|
+
# @param original [String] the value before the hooks ran
|
|
422
|
+
# @return [String] the (unvalidated) value
|
|
423
|
+
def attribute_value_after_hooks(attr, data, original)
|
|
424
|
+
value = data[:value].to_s
|
|
425
|
+
value = attr.value.to_s if value == original && attr.value != original
|
|
426
|
+
value.dup
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
# Decides whether an attribute is kept, taking a hook's data[:keep_attr] into account.
|
|
430
|
+
#
|
|
431
|
+
# keep_attr = false always removes the attribute. keep_attr = true only bypasses the
|
|
432
|
+
# tag/attribute allowlists: forbidden and event-handler attributes are still removed, and
|
|
433
|
+
# URI, style and DOM clobbering checks still apply to the (possibly rewritten) value.
|
|
434
|
+
#
|
|
435
|
+
# @param tag_name [String] tag name
|
|
436
|
+
# @param attr_name [String] normalized attribute name
|
|
437
|
+
# @param value [String] attribute value after hooks
|
|
438
|
+
# @param keep_attr [Boolean, nil] the hook decision, nil if no hook decided
|
|
439
|
+
# @return [Boolean] true if the attribute is kept
|
|
440
|
+
def attribute_permitted?(tag_name, attr_name, value, keep_attr)
|
|
441
|
+
return false if keep_attr == false
|
|
442
|
+
return valid_attribute?(tag_name, attr_name, value) unless keep_attr == true
|
|
443
|
+
return false if attr_name.match?(/\Aon/i)
|
|
444
|
+
|
|
445
|
+
valid_attribute?(tag_name, attr_name, value, force_allowed: true)
|
|
446
|
+
end
|
|
447
|
+
|
|
367
448
|
def build_isindex_replacement(node)
|
|
368
449
|
doc = node.document
|
|
369
450
|
form = Nokogiri::XML::Node.new('form', doc)
|
|
@@ -392,45 +473,56 @@ module Dandruff
|
|
|
392
473
|
#
|
|
393
474
|
# @param tag_name [String] the tag name to check
|
|
394
475
|
# @return [Boolean] true if the tag is allowed, false otherwise
|
|
395
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
|
396
476
|
def allowed_element?(tag_name)
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return false if @config.forbidden_tags&.include?(tag_name)
|
|
477
|
+
return false if element_denied?(tag_name)
|
|
478
|
+
# When parsing a whole document the parser always creates html/head/body; like DOMPurify's
|
|
479
|
+
# WHOLE_DOCUMENT they are implicitly allowed (removing the root would discard everything).
|
|
480
|
+
return true if document_mode? && DOCUMENT_TAGS.include?(tag_name)
|
|
403
481
|
|
|
404
482
|
unless @config.allowed_tags.nil?
|
|
405
483
|
allowed = @config.allowed_tags.dup.map { |t| transform_case(t) }
|
|
406
484
|
allowed.concat(@config.additional_tags) if @config.additional_tags
|
|
407
|
-
|
|
408
|
-
return is_included
|
|
485
|
+
return allowed.include?(tag_name)
|
|
409
486
|
end
|
|
410
487
|
return true if @config.additional_tags&.map { |t| transform_case(t) }&.include?(tag_name)
|
|
488
|
+
# With the default allowlist, <style> is governed by allow_style_tags (see element_denied?)
|
|
489
|
+
return true if tag_name == 'style'
|
|
411
490
|
|
|
412
491
|
default_allowed_tags.include?(tag_name)
|
|
413
492
|
end
|
|
414
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
|
415
493
|
|
|
416
|
-
#
|
|
494
|
+
# Whether the input is parsed and kept as a full document (html/head/body)
|
|
417
495
|
#
|
|
418
|
-
# @
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
496
|
+
# @return [Boolean]
|
|
497
|
+
def document_mode?
|
|
498
|
+
@config.whole_document || @config.allow_document_elements || @config.return_dom
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
# Checks the rules that remove an element regardless of the allowlist
|
|
502
|
+
#
|
|
503
|
+
# @param tag_name [String] the tag name to check
|
|
504
|
+
# @return [Boolean] true if the element must be removed
|
|
505
|
+
def element_denied?(tag_name)
|
|
506
|
+
return true if !document_mode? && DOCUMENT_TAGS.include?(tag_name)
|
|
507
|
+
return true if @config.forbidden_tags&.include?(tag_name)
|
|
508
|
+
|
|
509
|
+
# <style> is only ever kept when style tags are enabled, whatever the allowlist says
|
|
510
|
+
tag_name == 'style' && !@config.allow_style_tags
|
|
511
|
+
end
|
|
512
|
+
|
|
422
513
|
# Checks if an attribute is valid for a given tag
|
|
423
514
|
#
|
|
424
515
|
# @param tag_name [String] the element tag name
|
|
425
516
|
# @param attr_name [String] the attribute name
|
|
426
517
|
# @param value [String] the attribute value
|
|
518
|
+
# @param force_allowed [Boolean] treat the attribute as allow-listed (hook keep_attr = true)
|
|
427
519
|
# @return [Boolean] true if the attribute is valid, false otherwise
|
|
428
520
|
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
429
|
-
def valid_attribute?(tag_name, attr_name, value)
|
|
521
|
+
def valid_attribute?(tag_name, attr_name, value, force_allowed: false)
|
|
430
522
|
return false if forbidden_attribute?(attr_name)
|
|
431
523
|
return false if dangerous_attribute?(attr_name)
|
|
432
524
|
|
|
433
|
-
attr_allowed = attribute_allowed?(tag_name, attr_name)
|
|
525
|
+
attr_allowed = force_allowed || attribute_allowed?(tag_name, attr_name)
|
|
434
526
|
|
|
435
527
|
return true if data_attribute_allowed?(attr_name)
|
|
436
528
|
return true if aria_attribute_allowed?(attr_name)
|
|
@@ -444,6 +536,7 @@ module Dandruff
|
|
|
444
536
|
|
|
445
537
|
return false if @config.sanitize_dom && dom_clobbering_attribute?(attr_name, value)
|
|
446
538
|
|
|
539
|
+
return valid_srcset?(tag_name, value, attr_allowed) if attr_name == 'srcset' && value
|
|
447
540
|
return valid_uri_attribute?(tag_name, value, attr_allowed) if uri_like?(attr_name) && value
|
|
448
541
|
|
|
449
542
|
return attr_allowed if [true, false].include?(attr_allowed)
|
|
@@ -495,63 +588,6 @@ module Dandruff
|
|
|
495
588
|
false
|
|
496
589
|
end
|
|
497
590
|
|
|
498
|
-
def sanitize_style_value(value)
|
|
499
|
-
return nil if unsafe_inline_style?(value)
|
|
500
|
-
return nil if value.match?(/\\[0-9a-f]{1,6}/i)
|
|
501
|
-
|
|
502
|
-
allowed_props = Set.new(%w[
|
|
503
|
-
align-content align-items align-self all animation animation-delay animation-direction animation-duration
|
|
504
|
-
animation-fill-mode animation-iteration-count animation-name animation-play-state animation-timing-function
|
|
505
|
-
background background-clip background-color background-image background-origin background-position
|
|
506
|
-
background-repeat background-size border border-bottom border-bottom-color border-bottom-style
|
|
507
|
-
border-bottom-width
|
|
508
|
-
border-collapse border-color border-image border-left border-left-color border-left-style border-left-width
|
|
509
|
-
border-radius border-right border-right-color border-right-style border-right-width border-spacing border-style
|
|
510
|
-
border-top border-top-color border-top-style border-top-width border-width bottom box-shadow box-sizing
|
|
511
|
-
caption-side clear clip color column-count column-fill column-gap column-rule column-rule-color
|
|
512
|
-
column-rule-style
|
|
513
|
-
column-rule-width column-span column-width columns content cursor direction display empty-cells filter flex
|
|
514
|
-
flex-basis flex-direction flex-flow flex-grow flex-shrink flex-wrap float font font-family font-size
|
|
515
|
-
font-size-adjust font-stretch font-style font-variant font-weight gap grid grid-area grid-auto-columns
|
|
516
|
-
grid-auto-flow grid-auto-rows grid-column grid-column-end grid-column-gap grid-column-start grid-gap grid-row
|
|
517
|
-
grid-row-end grid-row-gap grid-row-start grid-template grid-template-areas grid-template-columns
|
|
518
|
-
grid-template-rows
|
|
519
|
-
height justify-content left letter-spacing line-height list-style list-style-image
|
|
520
|
-
list-style-position list-style-type
|
|
521
|
-
margin margin-bottom margin-left margin-right margin-top max-height max-width min-height min-width opacity order
|
|
522
|
-
outline outline-color outline-offset outline-style outline-width overflow overflow-x overflow-y padding
|
|
523
|
-
padding-bottom padding-left padding-right padding-top page-break-after page-break-before page-break-inside
|
|
524
|
-
perspective perspective-origin pointer-events position quotes resize right row-gap table-layout text-align
|
|
525
|
-
text-align-last text-decoration text-decoration-color text-decoration-line text-decoration-style text-indent
|
|
526
|
-
text-justify text-overflow text-shadow text-transform top transform transform-origin transition transition-delay
|
|
527
|
-
transition-duration transition-property transition-timing-function unicode-bidi vertical-align visibility
|
|
528
|
-
white-space width word-break word-spacing word-wrap writing-mode z-index
|
|
529
|
-
])
|
|
530
|
-
|
|
531
|
-
declarations = value.split(';').map(&:strip).reject(&:empty?)
|
|
532
|
-
sanitized = declarations.filter_map do |decl|
|
|
533
|
-
prop, val = decl.split(':', 2).map { |p| p&.strip }
|
|
534
|
-
next nil unless prop && val
|
|
535
|
-
|
|
536
|
-
lc_prop = prop.downcase
|
|
537
|
-
next nil unless allowed_props.include?(lc_prop)
|
|
538
|
-
# reject dangerous urls/protocols in values
|
|
539
|
-
next nil if unsafe_inline_style?(val)
|
|
540
|
-
|
|
541
|
-
"#{lc_prop}:#{val}"
|
|
542
|
-
end
|
|
543
|
-
|
|
544
|
-
return nil if sanitized.empty?
|
|
545
|
-
|
|
546
|
-
sanitized.join('; ')
|
|
547
|
-
end
|
|
548
|
-
|
|
549
|
-
def unsafe_style_block?(content)
|
|
550
|
-
return false if content.nil? || content.strip.empty?
|
|
551
|
-
|
|
552
|
-
unsafe_inline_style?(content)
|
|
553
|
-
end
|
|
554
|
-
|
|
555
591
|
# Checks if a node is within a MathML or SVG context
|
|
556
592
|
#
|
|
557
593
|
# @param node [Nokogiri::XML::Element] element to check
|
|
@@ -818,19 +854,19 @@ module Dandruff
|
|
|
818
854
|
# @param node [Nokogiri::XML::Element] the element node
|
|
819
855
|
# @param tag_name [String] the tag name
|
|
820
856
|
def handle_disallowed_element(node, tag_name)
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
node.remove
|
|
828
|
-
end
|
|
857
|
+
if node.parent.is_a?(Nokogiri::XML::Document)
|
|
858
|
+
# The document root (e.g. a forbidden <html>) cannot have siblings: remove it with its content.
|
|
859
|
+
nil
|
|
860
|
+
elsif @config.keep_content && !forbidden_content?(tag_name) && !@config.allowed_tags
|
|
861
|
+
# Children were already sanitized (traverse is post-order); hoist them before removing the node.
|
|
862
|
+
node.children.to_a.each { |child| node.add_previous_sibling(child) }
|
|
829
863
|
elsif @config.allowed_tags && node.children.any?
|
|
830
864
|
node.add_next_sibling(Nokogiri::XML::Text.new(' ', node.document))
|
|
831
865
|
end
|
|
832
866
|
@removed << { element: node }
|
|
833
|
-
|
|
867
|
+
# Always remove the element itself: leaving the emptied shell behind kept its unchecked
|
|
868
|
+
# attributes (event handlers, javascript: URLs) whenever only a single pass ran.
|
|
869
|
+
node.remove
|
|
834
870
|
end
|
|
835
871
|
|
|
836
872
|
# Removes elements with VML namespace
|
|
@@ -967,13 +1003,13 @@ module Dandruff
|
|
|
967
1003
|
# @return [Boolean, nil] true if allowed, nil otherwise
|
|
968
1004
|
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
969
1005
|
def check_default_allowed_attributes(tag_name, attr_name)
|
|
970
|
-
html_attrs = @html_attrs ||= Attributes::HTML.
|
|
971
|
-
svg_attrs = @svg_attrs ||= (Attributes::SVG + Attributes::XML).
|
|
972
|
-
math_attrs = @math_attrs ||= (Attributes::MATH_ML + Attributes::XML).
|
|
1006
|
+
html_attrs = @html_attrs ||= Attributes::HTML.to_set { |a| transform_case(a) }
|
|
1007
|
+
svg_attrs = @svg_attrs ||= (Attributes::SVG + Attributes::XML).to_set { |a| transform_case(a) }
|
|
1008
|
+
math_attrs = @math_attrs ||= (Attributes::MATH_ML + Attributes::XML).to_set { |a| transform_case(a) }
|
|
973
1009
|
|
|
974
|
-
@html_tags_set ||= Tags::HTML.
|
|
975
|
-
@svg_tags_set ||= (Tags::SVG + Tags::SVG_FILTERS).
|
|
976
|
-
@math_tags_set ||= Tags::MATH_ML.
|
|
1010
|
+
@html_tags_set ||= Tags::HTML.to_set { |t| transform_case(t) }
|
|
1011
|
+
@svg_tags_set ||= (Tags::SVG + Tags::SVG_FILTERS).to_set { |t| transform_case(t) }
|
|
1012
|
+
@math_tags_set ||= Tags::MATH_ML.to_set { |t| transform_case(t) }
|
|
977
1013
|
|
|
978
1014
|
is_svg = @svg_tags_set.include?(tag_name)
|
|
979
1015
|
is_math = @math_tags_set.include?(tag_name)
|
|
@@ -1066,6 +1102,55 @@ module Dandruff
|
|
|
1066
1102
|
end
|
|
1067
1103
|
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
1068
1104
|
|
|
1105
|
+
# Validates every image candidate URL of a srcset attribute
|
|
1106
|
+
#
|
|
1107
|
+
# @param tag_name [String] tag name
|
|
1108
|
+
# @param value [String] srcset value
|
|
1109
|
+
# @param attr_allowed [Boolean, nil] whether the attribute is allowed
|
|
1110
|
+
# @return [Boolean] true if all candidate URLs are valid
|
|
1111
|
+
def valid_srcset?(tag_name, value, attr_allowed)
|
|
1112
|
+
srcset_urls(value.to_s).all? { |url| valid_uri_attribute?(tag_name, url, attr_allowed) }
|
|
1113
|
+
end
|
|
1114
|
+
|
|
1115
|
+
# Extracts candidate URLs from a srcset value
|
|
1116
|
+
#
|
|
1117
|
+
# Follows the HTML "parse a srcset attribute" algorithm: a URL is a run of non-whitespace
|
|
1118
|
+
# characters (trailing commas end the candidate), and descriptors run up to the next comma
|
|
1119
|
+
# outside parentheses. Commas inside URLs (e.g. data: URLs) are kept.
|
|
1120
|
+
#
|
|
1121
|
+
# @param value [String] srcset value
|
|
1122
|
+
# @return [Array<String>] candidate URLs
|
|
1123
|
+
def srcset_urls(value)
|
|
1124
|
+
urls = []
|
|
1125
|
+
start = value.index(/[^\s,]/)
|
|
1126
|
+
while start
|
|
1127
|
+
url = value[start..][/\A\S+/]
|
|
1128
|
+
pos = start + url.length
|
|
1129
|
+
if url.end_with?(',')
|
|
1130
|
+
url = url.sub(/,+\z/, '')
|
|
1131
|
+
else
|
|
1132
|
+
pos = skip_srcset_descriptors(value, pos)
|
|
1133
|
+
end
|
|
1134
|
+
urls << url
|
|
1135
|
+
start = value.index(/[^\s,]/, pos)
|
|
1136
|
+
end
|
|
1137
|
+
urls
|
|
1138
|
+
end
|
|
1139
|
+
|
|
1140
|
+
# @return [Integer] the position after the descriptors of the current srcset candidate
|
|
1141
|
+
def skip_srcset_descriptors(value, pos)
|
|
1142
|
+
depth = 0
|
|
1143
|
+
while pos < value.length
|
|
1144
|
+
ch = value[pos]
|
|
1145
|
+
depth += 1 if ch == '('
|
|
1146
|
+
depth -= 1 if ch == ')' && depth.positive?
|
|
1147
|
+
return pos + 1 if ch == ',' && depth.zero?
|
|
1148
|
+
|
|
1149
|
+
pos += 1
|
|
1150
|
+
end
|
|
1151
|
+
pos
|
|
1152
|
+
end
|
|
1153
|
+
|
|
1069
1154
|
# Fallback check for unknown protocols
|
|
1070
1155
|
#
|
|
1071
1156
|
# @param value [String] attribute value
|
|
@@ -1086,8 +1171,8 @@ module Dandruff
|
|
|
1086
1171
|
# @param cfg [Hash, Config] optional configuration to initialize with
|
|
1087
1172
|
# @yield [config] optional block to mutate configuration before use
|
|
1088
1173
|
# @return [Sanitizer] a new sanitizer instance
|
|
1089
|
-
def self.new(cfg = {}, &
|
|
1090
|
-
Sanitizer.new(cfg, &
|
|
1174
|
+
def self.new(cfg = {}, &)
|
|
1175
|
+
Sanitizer.new(cfg, &)
|
|
1091
1176
|
end
|
|
1092
1177
|
|
|
1093
1178
|
# Convenience helper to sanitize with a fresh, default-configured instance.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dandruff
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- KUY.io Inc.
|
|
@@ -103,6 +103,7 @@ files:
|
|
|
103
103
|
- lib/dandruff.rb
|
|
104
104
|
- lib/dandruff/attributes.rb
|
|
105
105
|
- lib/dandruff/config.rb
|
|
106
|
+
- lib/dandruff/css.rb
|
|
106
107
|
- lib/dandruff/expressions.rb
|
|
107
108
|
- lib/dandruff/tags.rb
|
|
108
109
|
- lib/dandruff/utils.rb
|
|
@@ -128,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
128
129
|
- !ruby/object:Gem::Version
|
|
129
130
|
version: '0'
|
|
130
131
|
requirements: []
|
|
131
|
-
rubygems_version: 3.
|
|
132
|
+
rubygems_version: 3.6.9
|
|
132
133
|
specification_version: 4
|
|
133
134
|
summary: Medicated shampoo for your markup
|
|
134
135
|
test_files: []
|