sanitize 6.1.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sanitize.rb CHANGED
@@ -1,20 +1,19 @@
1
- # encoding: utf-8
2
-
3
- require 'nokogiri'
4
- require 'set'
5
-
6
- require_relative 'sanitize/version'
7
- require_relative 'sanitize/config'
8
- require_relative 'sanitize/config/default'
9
- require_relative 'sanitize/config/restricted'
10
- require_relative 'sanitize/config/basic'
11
- require_relative 'sanitize/config/relaxed'
12
- require_relative 'sanitize/css'
13
- require_relative 'sanitize/transformers/clean_cdata'
14
- require_relative 'sanitize/transformers/clean_comment'
15
- require_relative 'sanitize/transformers/clean_css'
16
- require_relative 'sanitize/transformers/clean_doctype'
17
- require_relative 'sanitize/transformers/clean_element'
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ require_relative "sanitize/version"
6
+ require_relative "sanitize/config"
7
+ require_relative "sanitize/config/default"
8
+ require_relative "sanitize/config/restricted"
9
+ require_relative "sanitize/config/basic"
10
+ require_relative "sanitize/config/relaxed"
11
+ require_relative "sanitize/css"
12
+ require_relative "sanitize/transformers/clean_cdata"
13
+ require_relative "sanitize/transformers/clean_comment"
14
+ require_relative "sanitize/transformers/clean_css"
15
+ require_relative "sanitize/transformers/clean_doctype"
16
+ require_relative "sanitize/transformers/clean_element"
18
17
 
19
18
  class Sanitize
20
19
  attr_reader :config
@@ -33,12 +32,12 @@ class Sanitize
33
32
  # - https://infra.spec.whatwg.org/#noncharacter
34
33
  REGEX_HTML_NON_CHARACTERS = /[\ufdd0-\ufdef\ufffe\uffff\u{1fffe}\u{1ffff}\u{2fffe}\u{2ffff}\u{3fffe}\u{3ffff}\u{4fffe}\u{4ffff}\u{5fffe}\u{5ffff}\u{6fffe}\u{6ffff}\u{7fffe}\u{7ffff}\u{8fffe}\u{8ffff}\u{9fffe}\u{9ffff}\u{afffe}\u{affff}\u{bfffe}\u{bffff}\u{cfffe}\u{cffff}\u{dfffe}\u{dffff}\u{efffe}\u{effff}\u{ffffe}\u{fffff}\u{10fffe}\u{10ffff}]+/u
35
34
 
36
- # Matches an attribute value that could be treated by a browser as a URL
37
- # with a protocol prefix, such as "http:" or "javascript:". Any string of zero
38
- # or more characters followed by a colon is considered a match, even if the
39
- # colon is encoded as an entity and even if it's an incomplete entity (which
40
- # IE6 and Opera will still parse).
41
- REGEX_PROTOCOL = /\A\s*([^\/#]*?)(?:\:|&#0*58|&#x0*3a)/i
35
+ # Matches an attribute value that could be treated by a browser as a URL with
36
+ # a protocol prefix, such as "http:" or "javascript:". Any string of zero or
37
+ # more characters followed by a colon is considered a match, even if the colon
38
+ # is encoded as an entity and even if it's an incomplete entity (which IE6 and
39
+ # Opera will still parse).
40
+ REGEX_PROTOCOL = /\A\s*([^\/#]*?)(?::|&#0*58|&#x0*3a)/i
42
41
 
43
42
  # Matches one or more characters that should be stripped from HTML before
44
43
  # parsing. This is a combination of `REGEX_HTML_CONTROL_CHARACTERS` and
@@ -97,14 +96,14 @@ class Sanitize
97
96
  # Default transformers always run at the end of the chain, after any custom
98
97
  # transformers.
99
98
  @transformers << Transformers::CleanElement.new(@config)
100
- @transformers << Transformers::CleanComment unless @config[:allow_comments]
99
+ @transformers << Transformers::CleanComment
101
100
 
102
- if @config[:elements].include?('style')
101
+ if @config[:elements].include?("style")
103
102
  scss = Sanitize::CSS.new(config)
104
103
  @transformers << Transformers::CSS::CleanElement.new(scss)
105
104
  end
106
105
 
107
- if @config[:attributes].values.any? {|attr| attr.include?('style') }
106
+ if @config[:attributes].values.any? { |attr| attr.include?("style") }
108
107
  scss ||= Sanitize::CSS.new(config)
109
108
  @transformers << Transformers::CSS::CleanAttribute.new(scss)
110
109
  end
@@ -112,7 +111,7 @@ class Sanitize
112
111
  @transformers << Transformers::CleanDoctype
113
112
  @transformers << Transformers::CleanCDATA
114
113
 
115
- @transformer_config = { config: @config }
114
+ @transformer_config = {config: @config}
116
115
  end
117
116
 
118
117
  # Returns a sanitized copy of the given _html_ document.
@@ -121,7 +120,7 @@ class Sanitize
121
120
  # error will be raised. If this is undesirable, you should probably use
122
121
  # {#fragment} instead.
123
122
  def document(html)
124
- return '' unless html
123
+ return "" unless html
125
124
 
126
125
  doc = Nokogiri::HTML5.parse(preprocess(html), **@config[:parser_options])
127
126
  node!(doc)
@@ -133,7 +132,7 @@ class Sanitize
133
132
 
134
133
  # Returns a sanitized copy of the given _html_ fragment.
135
134
  def fragment(html)
136
- return '' unless html
135
+ return "" unless html
137
136
 
138
137
  frag = Nokogiri::HTML5.fragment(preprocess(html), **@config[:parser_options])
139
138
  node!(frag)
@@ -152,7 +151,7 @@ class Sanitize
152
151
  raise ArgumentError unless node.is_a?(Nokogiri::XML::Node)
153
152
 
154
153
  if node.is_a?(Nokogiri::XML::Document)
155
- unless @config[:elements].include?('html')
154
+ unless @config[:elements].include?("html")
156
155
  raise Error, 'When sanitizing a document, "<html>" must be allowlisted.'
157
156
  end
158
157
  end
@@ -175,13 +174,13 @@ class Sanitize
175
174
  def preprocess(html)
176
175
  html = html.to_s.dup
177
176
 
178
- unless html.encoding.name == 'UTF-8'
179
- html.encode!('UTF-8',
180
- :invalid => :replace,
181
- :undef => :replace)
177
+ unless html.encoding.name == "UTF-8"
178
+ html.encode!("UTF-8",
179
+ invalid: :replace,
180
+ undef: :replace)
182
181
  end
183
182
 
184
- html.gsub!(REGEX_UNSUITABLE_CHARS, '')
183
+ html.gsub!(REGEX_UNSUITABLE_CHARS, "")
185
184
  html
186
185
  end
187
186
 
@@ -225,17 +224,17 @@ class Sanitize
225
224
 
226
225
  child = node.child
227
226
 
228
- while child do
227
+ while child
229
228
  prev = child.previous_sibling
230
229
  traverse(child, &block)
231
230
 
232
- if child.parent == node
233
- child = child.next_sibling
231
+ child = if child.parent == node
232
+ child.next_sibling
234
233
  else
235
234
  # The child was unlinked or reparented, so traverse the previous node's
236
235
  # next sibling, or the parent's first child if there is no previous
237
236
  # node.
238
- child = prev ? prev.next_sibling : node.child
237
+ prev ? prev.next_sibling : node.child
239
238
  end
240
239
  end
241
240
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanitize
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 7.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Grove
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-09-14 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: crass
@@ -16,65 +15,38 @@ dependencies:
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: 1.0.2
18
+ version: 1.0.8
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: 1.0.2
25
+ version: 1.0.8
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: nokogiri
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: 1.12.0
32
+ version: 1.19.7
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: 1.12.0
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 5.14.4
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 5.14.4
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: 13.0.6
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: 13.0.6
69
- description: Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all
70
- HTML and/or CSS from a string except the elements, attributes, and properties you
39
+ version: 1.19.7
40
+ description: |
41
+ Sanitize is an allowlist-based HTML and CSS sanitizer. It removes all HTML
42
+ and/or CSS from a string except the elements, attributes, and properties you
71
43
  choose to allow.
72
44
  email: ryan@wonko.com
73
45
  executables: []
74
46
  extensions: []
75
47
  extra_rdoc_files: []
76
48
  files:
77
- - HISTORY.md
49
+ - CHANGELOG.md
78
50
  - LICENSE
79
51
  - README.md
80
52
  - lib/sanitize.rb
@@ -90,23 +62,13 @@ files:
90
62
  - lib/sanitize/transformers/clean_doctype.rb
91
63
  - lib/sanitize/transformers/clean_element.rb
92
64
  - lib/sanitize/version.rb
93
- - test/common.rb
94
- - test/test_clean_comment.rb
95
- - test/test_clean_css.rb
96
- - test/test_clean_doctype.rb
97
- - test/test_clean_element.rb
98
- - test/test_config.rb
99
- - test/test_malicious_css.rb
100
- - test/test_malicious_html.rb
101
- - test/test_parser.rb
102
- - test/test_sanitize.rb
103
- - test/test_sanitize_css.rb
104
- - test/test_transformers.rb
105
65
  homepage: https://github.com/rgrove/sanitize/
106
66
  licenses:
107
67
  - MIT
108
- metadata: {}
109
- post_install_message:
68
+ metadata:
69
+ changelog_uri: https://github.com/rgrove/sanitize/blob/main/CHANGELOG.md
70
+ documentation_uri: https://rubydoc.info/github/rgrove/sanitize
71
+ rubygems_mfa_required: 'true'
110
72
  rdoc_options: []
111
73
  require_paths:
112
74
  - lib
@@ -114,15 +76,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
76
  requirements:
115
77
  - - ">="
116
78
  - !ruby/object:Gem::Version
117
- version: 2.5.0
79
+ version: 3.2.0
118
80
  required_rubygems_version: !ruby/object:Gem::Requirement
119
81
  requirements:
120
82
  - - ">="
121
83
  - !ruby/object:Gem::Version
122
- version: 1.2.0
84
+ version: '0'
123
85
  requirements: []
124
- rubygems_version: 3.4.10
125
- signing_key:
86
+ rubygems_version: 4.0.20
126
87
  specification_version: 4
127
88
  summary: Allowlist-based HTML and CSS sanitizer.
128
89
  test_files: []
data/test/common.rb DELETED
@@ -1,3 +0,0 @@
1
- # encoding: utf-8
2
- require 'minitest/autorun'
3
- require_relative '../lib/sanitize'
@@ -1,47 +0,0 @@
1
- # encoding: utf-8
2
- require_relative 'common'
3
-
4
- describe 'Sanitize::Transformers::CleanComment' do
5
- make_my_diffs_pretty!
6
- parallelize_me!
7
-
8
- describe 'when :allow_comments is false' do
9
- before do
10
- @s = Sanitize.new(:allow_comments => false, :elements => ['div'])
11
- end
12
-
13
- it 'should remove comments' do
14
- _(@s.fragment('foo <!-- comment --> bar')).must_equal 'foo bar'
15
- _(@s.fragment('foo <!-- ')).must_equal 'foo '
16
- _(@s.fragment('foo <!-- - -> bar')).must_equal 'foo '
17
- _(@s.fragment("foo <!--\n\n\n\n-->bar")).must_equal 'foo bar'
18
- _(@s.fragment("foo <!-- <!-- <!-- --> --> -->bar")).must_equal 'foo --&gt; --&gt;bar'
19
- _(@s.fragment("foo <div <!-- comment -->>bar</div>")).must_equal 'foo <div>&gt;bar</div>'
20
-
21
- # Special case: the comment markup is inside a <script>, which makes it
22
- # text content and not an actual HTML comment.
23
- _(@s.fragment("<script><!-- comment --></script>")).must_equal ''
24
-
25
- _(Sanitize.fragment("<script><!-- comment --></script>", :allow_comments => false, :elements => ['script']))
26
- .must_equal '<script><!-- comment --></script>'
27
- end
28
- end
29
-
30
- describe 'when :allow_comments is true' do
31
- before do
32
- @s = Sanitize.new(:allow_comments => true, :elements => ['div'])
33
- end
34
-
35
- it 'should allow comments' do
36
- _(@s.fragment('foo <!-- comment --> bar')).must_equal 'foo <!-- comment --> bar'
37
- _(@s.fragment('foo <!-- ')).must_equal 'foo <!-- -->'
38
- _(@s.fragment('foo <!-- - -> bar')).must_equal 'foo <!-- - -> bar-->'
39
- _(@s.fragment("foo <!--\n\n\n\n-->bar")).must_equal "foo <!--\n\n\n\n-->bar"
40
- _(@s.fragment("foo <!-- <!-- <!-- --> --> -->bar")).must_equal 'foo <!-- <!-- <!-- --> --&gt; --&gt;bar'
41
- _(@s.fragment("foo <div <!-- comment -->>bar</div>")).must_equal 'foo <div>&gt;bar</div>'
42
-
43
- _(Sanitize.fragment("<script><!-- comment --></script>", :allow_comments => true, :elements => ['script']))
44
- .must_equal '<script><!-- comment --></script>'
45
- end
46
- end
47
- end
@@ -1,67 +0,0 @@
1
- # encoding: utf-8
2
- require_relative 'common'
3
-
4
- describe 'Sanitize::Transformers::CSS::CleanAttribute' do
5
- make_my_diffs_pretty!
6
- parallelize_me!
7
-
8
- before do
9
- @s = Sanitize.new(Sanitize::Config::RELAXED)
10
- end
11
-
12
- it 'should sanitize CSS properties in style attributes' do
13
- _(@s.fragment(%[
14
- <div style="color: #fff; width: expression(alert(1)); /* <-- evil! */"></div>
15
- ].strip)).must_equal %[
16
- <div style="color: #fff; /* <-- evil! */"></div>
17
- ].strip
18
- end
19
-
20
- it 'should remove the style attribute if the sanitized CSS is empty' do
21
- _(@s.fragment('<div style="width: expression(alert(1))"></div>')).
22
- must_equal '<div></div>'
23
- end
24
- end
25
-
26
- describe 'Sanitize::Transformers::CSS::CleanElement' do
27
- make_my_diffs_pretty!
28
- parallelize_me!
29
-
30
- before do
31
- @s = Sanitize.new(Sanitize::Config::RELAXED)
32
- end
33
-
34
- it 'should sanitize CSS stylesheets in <style> elements' do
35
- html = %[
36
- <style>@import url(evil.css);
37
- /* Yay CSS! */
38
- .foo { color: #fff; }
39
- #bar { background: url(yay.jpg); bogus: wtf; }
40
- .evil { width: expression(xss()); }
41
-
42
- @media screen (max-width:480px) {
43
- .foo { width: 400px; }
44
- #bar:not(.baz) { height: 100px; }
45
- }
46
- </style>
47
- ].strip
48
-
49
- _(@s.fragment(html)).must_equal %[
50
- <style>
51
- /* Yay CSS! */
52
- .foo { color: #fff; }
53
- #bar { background: url(yay.jpg); }
54
- .evil { }
55
-
56
- @media screen (max-width:480px) {
57
- .foo { width: 400px; }
58
- #bar:not(.baz) { height: 100px; }
59
- }
60
- </style>
61
- ].strip
62
- end
63
-
64
- it 'should remove the <style> element if the sanitized CSS is empty' do
65
- _(@s.fragment('<style></style>')).must_equal ''
66
- end
67
- end
@@ -1,71 +0,0 @@
1
- # encoding: utf-8
2
- require_relative 'common'
3
-
4
- describe 'Sanitize::Transformers::CleanDoctype' do
5
- make_my_diffs_pretty!
6
- parallelize_me!
7
-
8
- describe 'when :allow_doctype is false' do
9
- before do
10
- @s = Sanitize.new(:allow_doctype => false, :elements => ['html'])
11
- end
12
-
13
- it 'should remove doctype declarations' do
14
- _(@s.document('<!DOCTYPE html><html>foo</html>')).must_equal "<html>foo</html>"
15
- _(@s.fragment('<!DOCTYPE html>foo')).must_equal 'foo'
16
- end
17
-
18
- it 'should not allow doctype definitions in fragments' do
19
- _(@s.fragment('<!DOCTYPE html><html>foo</html>'))
20
- .must_equal "foo"
21
-
22
- _(@s.fragment('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
23
- .must_equal "foo"
24
-
25
- _(@s.fragment("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html>foo</html>"))
26
- .must_equal "foo"
27
- end
28
- end
29
-
30
- describe 'when :allow_doctype is true' do
31
- before do
32
- @s = Sanitize.new(:allow_doctype => true, :elements => ['html'])
33
- end
34
-
35
- it 'should allow doctype declarations in documents' do
36
- _(@s.document('<!DOCTYPE html><html>foo</html>'))
37
- .must_equal "<!DOCTYPE html><html>foo</html>"
38
-
39
- _(@s.document('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
40
- .must_equal "<!DOCTYPE html><html>foo</html>"
41
-
42
- _(@s.document("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html>foo</html>"))
43
- .must_equal "<!DOCTYPE html><html>foo</html>"
44
- end
45
-
46
- it 'should not allow obviously invalid doctype declarations in documents' do
47
- _(@s.document('<!DOCTYPE blah blah blah><html>foo</html>'))
48
- .must_equal "<!DOCTYPE html><html>foo</html>"
49
-
50
- _(@s.document('<!DOCTYPE blah><html>foo</html>'))
51
- .must_equal "<!DOCTYPE html><html>foo</html>"
52
-
53
- _(@s.document('<!DOCTYPE html BLAH "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
54
- .must_equal "<!DOCTYPE html><html>foo</html>"
55
-
56
- _(@s.document('<!whatever><html>foo</html>'))
57
- .must_equal "<html>foo</html>"
58
- end
59
-
60
- it 'should not allow doctype definitions in fragments' do
61
- _(@s.fragment('<!DOCTYPE html><html>foo</html>'))
62
- .must_equal "foo"
63
-
64
- _(@s.fragment('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
65
- .must_equal "foo"
66
-
67
- _(@s.fragment("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html>foo</html>"))
68
- .must_equal "foo"
69
- end
70
- end
71
- end