sanitize 7.0.2 → 7.0.3
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 +12 -0
- data/README.md +8 -1
- data/lib/sanitize/css.rb +54 -17
- data/lib/sanitize/transformers/clean_css.rb +5 -1
- data/lib/sanitize/transformers/clean_element.rb +10 -1
- data/lib/sanitize/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0a93325511ec2929036ce3e0736fd3de9344f000e177e8699f8baf785f096f71
|
|
4
|
+
data.tar.gz: 70a4b6bec7a667347bdeff21aac84ea7ab4d48b654ef4dfe621b22a1e36553d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ff96afaa502b8275ccefac4171cdfb5da41d9190a206a0838f5e3979cdd63bd1635e3db7ee670c3eee4798e00493f17889e05a645dbc9022a9588a0384a7250b
|
|
7
|
+
data.tar.gz: fb3f80a7d2ff91605d008004edaecda1d30452226a9a4798f70b5b92d7d8f224127bb959dd6936091ae6d38ebdce12d790f250caeaad5b096245e73b3409b748
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to Sanitize are documented in this file. The format (since version 7.0.0) is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
4
4
|
|
|
5
|
+
## 7.0.3 (2026-12-01)
|
|
6
|
+
|
|
7
|
+
### Security
|
|
8
|
+
|
|
9
|
+
- CSS properties that contain an image function like `image-set()` or `image()` are now removed if the image function contains a disallowed URL or a substitution function like `var()` at any depth. Previously, a value like `image-set(var(--x, "http://example.com/a.png") 1x)` could be used to load an image from a URL that the `:protocols` allowlist didn't allow. (GHSA-c48c-gjq9-vcq5)
|
|
10
|
+
|
|
11
|
+
- URLs in CSS `src()` functions are now validated against the `:protocols` allowlist just like URLs in `url()` functions, and CSS properties that contain a `src()` function with a disallowed URL or with anything other than a single string are removed. No browsers currently support `src()`, so this is a defense in depth measure. (GHSA-c48c-gjq9-vcq5)
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- Fixed a regression in 7.0.2 that could cause an infinite loop when a transformer added elements inside a removed `<style>`, `<script>`, or similar element. When Sanitize removes an element along with its contents, it now discards the contents immediately, so transformers are no longer called for them. [#255](https://github.com/rgrove/sanitize/issues/255)
|
|
16
|
+
|
|
5
17
|
## 7.0.2 (2026-11-02)
|
|
6
18
|
|
|
7
19
|
### Security
|
data/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all HTML and/o
|
|
|
4
4
|
|
|
5
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.
|
|
6
6
|
|
|
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.
|
|
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.
|
|
8
8
|
|
|
9
9
|
[](http://badge.fury.io/rb/sanitize)
|
|
10
10
|
[](https://github.com/rgrove/sanitize/actions?query=workflow%3ATests)
|
|
@@ -51,6 +51,9 @@ Sanitize::CSS.stylesheet(css, Sanitize::Config::RELAXED)
|
|
|
51
51
|
Sanitize::CSS.properties(css, Sanitize::Config::RELAXED)
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
> [!IMPORTANT]
|
|
55
|
+
> Input that can't be parsed as HTML or CSS may cause an exception to be raised. How you handle exceptions (or whether you allow them to crash your application) is up to you, but be sure to keep this in mind if you're passing untrusted content to Sanitize.
|
|
56
|
+
|
|
54
57
|
## Usage
|
|
55
58
|
|
|
56
59
|
Sanitize can sanitize the following types of input:
|
|
@@ -410,6 +413,8 @@ If this is `true`, Sanitize will remove the contents of any non-allowlisted elem
|
|
|
410
413
|
|
|
411
414
|
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.
|
|
412
415
|
|
|
416
|
+
Removed contents are discarded before they're traversed, so transformers won't be called for them.
|
|
417
|
+
|
|
413
418
|
The default value can be seen in the [default config](lib/sanitize/config/default.rb).
|
|
414
419
|
|
|
415
420
|
#### :transformers (Array or callable)
|
|
@@ -495,6 +500,8 @@ Sanitize.fragment(html, transformers: transformer)
|
|
|
495
500
|
|
|
496
501
|
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.
|
|
497
502
|
|
|
503
|
+
Be especially careful with transformers that add nodes. If a transformer adds something that Sanitize then removes, such as an element that isn't allowed by the config or isn't allowed where it was added (like an `<a>` element inside a `<style>`, `<textarea>`, `<title>`, or `<option>` element), Sanitize may keep whatever was inside it and call the transformer again for those nodes. If the transformer adds the same thing again, Sanitize will be stuck in an infinite loop. Since the input determines where content appears, an attacker may be able to trigger this deliberately. Sanitize can't protect against this, so make sure your transformers only add things that will be allowed where they're added. For example, a transformer that turns URLs into links should check that `env[:config][:elements]` includes `a`, and should skip text inside elements that can't contain links.
|
|
504
|
+
|
|
498
505
|
### Example: Transformer to allow image URLs by domain
|
|
499
506
|
|
|
500
507
|
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.
|
data/lib/sanitize/css.rb
CHANGED
|
@@ -298,7 +298,7 @@ class Sanitize
|
|
|
298
298
|
if child.key?(:name)
|
|
299
299
|
name = child[:name].downcase
|
|
300
300
|
|
|
301
|
-
if name
|
|
301
|
+
if url_function?(name)
|
|
302
302
|
return nil unless valid_url?(child)
|
|
303
303
|
end
|
|
304
304
|
|
|
@@ -401,14 +401,38 @@ class Sanitize
|
|
|
401
401
|
tree
|
|
402
402
|
end
|
|
403
403
|
|
|
404
|
+
# Returns `true` if the given CSS function name is a substitution function
|
|
405
|
+
# whose value is determined by the browser at computed-value time. This
|
|
406
|
+
# includes calls to custom functions defined with `@function`, whose names
|
|
407
|
+
# begin with `--`.
|
|
408
|
+
def substitution_function?(name)
|
|
409
|
+
name.start_with?("--") || ["attr", "env", "if", "inherit", "var"].include?(name)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# Returns `true` if the given CSS function name is a URL function like
|
|
413
|
+
# `url()` or `src()`.
|
|
414
|
+
def url_function?(name)
|
|
415
|
+
["src", "url"].include?(name)
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# Returns `true` if the given URL string uses an allowlisted protocol, or is
|
|
419
|
+
# relative and relative URLs are allowlisted.
|
|
420
|
+
def url_protocol_allowed?(url)
|
|
421
|
+
if url =~ Sanitize::REGEX_PROTOCOL
|
|
422
|
+
@config[:protocols].include?($1.downcase)
|
|
423
|
+
else
|
|
424
|
+
@config[:protocols].include?(:relative)
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
|
|
404
428
|
# Returns `true` if the given node (which may be of type `:url` or
|
|
405
429
|
# `:function`, since the CSS syntax can produce both) uses an allowlisted
|
|
406
|
-
# protocol.
|
|
430
|
+
# protocol. A `:function` node may be either a `url()` or `src()` function.
|
|
407
431
|
def valid_url?(node)
|
|
408
432
|
type = node[:node]
|
|
409
433
|
|
|
410
434
|
if type == :function
|
|
411
|
-
return false unless node.key?(:name) && node[:name].downcase
|
|
435
|
+
return false unless node.key?(:name) && url_function?(node[:name].downcase)
|
|
412
436
|
return false unless Array === node[:value]
|
|
413
437
|
|
|
414
438
|
# A URL function's `:value` should be an array containing no more than
|
|
@@ -442,34 +466,47 @@ class Sanitize
|
|
|
442
466
|
return false
|
|
443
467
|
end
|
|
444
468
|
|
|
445
|
-
|
|
446
|
-
@config[:protocols].include?($1.downcase)
|
|
447
|
-
else
|
|
448
|
-
@config[:protocols].include?(:relative)
|
|
449
|
-
end
|
|
469
|
+
url_protocol_allowed?(url)
|
|
450
470
|
end
|
|
451
471
|
|
|
452
|
-
# Returns `true` if the given node is an image-related function
|
|
453
|
-
#
|
|
472
|
+
# Returns `true` if the given node is an image-related function in which
|
|
473
|
+
# every string at any depth (other than a MIME type in a `type()` function)
|
|
474
|
+
# uses an allowlisted protocol, and which contains no substitution functions
|
|
475
|
+
# (see {#substitution_function?}) at any depth.
|
|
476
|
+
#
|
|
477
|
+
# Substitution functions are rejected because their values aren't known
|
|
478
|
+
# until a browser computes them and could be URLs that use a disallowed
|
|
479
|
+
# protocol. For example, `image-set(var(--x) 1x)` would load an image from
|
|
480
|
+
# `http://example.com` if `--x` were set to `"http://example.com"`.
|
|
454
481
|
def valid_image?(node)
|
|
455
482
|
return false unless node[:node] == :function
|
|
456
483
|
return false unless node.key?(:name) && image_function?(node[:name].downcase)
|
|
457
484
|
return false unless Array === node[:value]
|
|
458
485
|
|
|
459
|
-
node[:value].
|
|
486
|
+
stack = node[:value].map { |token| [token, nil] }
|
|
487
|
+
|
|
488
|
+
until stack.empty?
|
|
489
|
+
token, parent_function_name = stack.pop
|
|
460
490
|
return false unless Hash === token
|
|
461
491
|
|
|
492
|
+
function_name = nil
|
|
493
|
+
|
|
462
494
|
case token[:node]
|
|
495
|
+
when :function
|
|
496
|
+
function_name = token[:name].to_s.downcase
|
|
497
|
+
return false if substitution_function?(function_name)
|
|
498
|
+
|
|
463
499
|
when :string
|
|
464
|
-
|
|
465
|
-
return false unless
|
|
466
|
-
else
|
|
467
|
-
return false unless @config[:protocols].include?(:relative)
|
|
500
|
+
unless parent_function_name == "type"
|
|
501
|
+
return false unless url_protocol_allowed?(token[:value])
|
|
468
502
|
end
|
|
469
|
-
else
|
|
470
|
-
next
|
|
471
503
|
end
|
|
504
|
+
|
|
505
|
+
value = token[:value]
|
|
506
|
+
stack.concat(value.map { |child| [child, function_name] }) if Array === value
|
|
472
507
|
end
|
|
508
|
+
|
|
509
|
+
true
|
|
473
510
|
end
|
|
474
511
|
end
|
|
475
512
|
end
|
|
@@ -48,11 +48,15 @@ class Sanitize
|
|
|
48
48
|
|
|
49
49
|
css = @scss.stylesheet(node.content)
|
|
50
50
|
|
|
51
|
+
# The original contents are removed even if the element is removed so
|
|
52
|
+
# that they won't be traversed, since they'll never be part of the
|
|
53
|
+
# output.
|
|
54
|
+
node.children.unlink
|
|
55
|
+
|
|
51
56
|
if css.strip.empty?
|
|
52
57
|
node.unlink
|
|
53
58
|
else
|
|
54
59
|
css.gsub!("</", '<\/')
|
|
55
|
-
node.children.unlink
|
|
56
60
|
node << Nokogiri::XML::Text.new(css, node.document)
|
|
57
61
|
end
|
|
58
62
|
end
|
|
@@ -134,7 +134,12 @@ class Sanitize
|
|
|
134
134
|
end
|
|
135
135
|
|
|
136
136
|
unless node.children.empty?
|
|
137
|
-
|
|
137
|
+
if @remove_all_contents || @remove_element_contents.include?(name)
|
|
138
|
+
# Remove the contents now rather than leaving them in the removed
|
|
139
|
+
# element so that they won't be traversed, since they'll never be
|
|
140
|
+
# part of the output.
|
|
141
|
+
node.children.unlink
|
|
142
|
+
else
|
|
138
143
|
node.add_previous_sibling(node.children)
|
|
139
144
|
end
|
|
140
145
|
end
|
|
@@ -263,7 +268,11 @@ class Sanitize
|
|
|
263
268
|
# because Nokogiri can't fully replicate the parsing behavior of a
|
|
264
269
|
# scripting-enabled browser. The safest thing to do is to simply remove
|
|
265
270
|
# all `<noscript>` elements.
|
|
271
|
+
#
|
|
272
|
+
# The contents are removed first so that they won't be traversed, since
|
|
273
|
+
# they'll never be part of the output.
|
|
266
274
|
when "noscript"
|
|
275
|
+
node.children.unlink
|
|
267
276
|
node.unlink
|
|
268
277
|
end
|
|
269
278
|
end
|
data/lib/sanitize/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sanitize
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 7.0.
|
|
4
|
+
version: 7.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryan Grove
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 1.0.
|
|
18
|
+
version: 1.0.9
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 1.0.
|
|
25
|
+
version: 1.0.9
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: nokogiri
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|