loofah 2.20.0 → 2.21.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b06da35026008830bdcee25d54ccaf1cafb06cbb7ed92cd4fbbfd147b022c693
4
- data.tar.gz: 67dd118c7d21f3bcb80b27b3c4e125907eacfc14f26c5ee1232b3f238eea9dbf
3
+ metadata.gz: d72ad662816af09936024ca82a2b98a8fcd06c4328b8dfa434fc1a9166cdcce6
4
+ data.tar.gz: 91484fe22c738402d3336a3e88562fb8adaac96d4536a94136b57ac1f0885de1
5
5
  SHA512:
6
- metadata.gz: 66a34115418e430e251bd599dd7529eeac2efd7d0c8795ac8001f810451fc871ef0a42fd1c767b78a90dc64d62323dadb1d9093d352e024785608ddafd43ec00
7
- data.tar.gz: 31d10913e97432fb5a511019b22b6b9325b5c9e7e2cfc28aaf175fdb0ad62e9449c7879ff47f1fbca82ab1f36eec05751b7051c1eefcbdf0c6d04b60b592a45f
6
+ metadata.gz: da8b034a54f89a9b9d18a9865ba9d67afd1442970af25cf14ac894c9cf9859d1d65e476f3953284fc24f22766b963ca9d481df2540cc6c65fb8d100b43847090
7
+ data.tar.gz: 6f30d4337334c7e2cf69da88827ba92df868e1c3a5aac66ba0b8416cd9df7afa01ddb6c84c30a8c6cabeb6a6a7d26f621415c6f5633d75909d0e61e97118089c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.21.0.rc1 / 2023-04-02
4
+
5
+ ### HTML5 Support
6
+
7
+ Classes `Loofah::HTML5::Document` and `Loofah::HTML5::DocumentFragment` are introduced, along with helper methods:
8
+
9
+ - `Loofah.html5_document`
10
+ - `Loofah.html5_fragment`
11
+ - `Loofah.scrub_html5_document`
12
+ - `Loofah.scrub_html5_fragment`
13
+
14
+ These classes and methods use Nokogiri's HTML5 parser to ensure modern web standards are used.
15
+
16
+ ⚠ HTML5 functionality is only available with Nokogiri v1.14.0 and higher.
17
+
18
+ ⚠ HTML5 functionality is not available for JRuby. Please see [this upstream Nokogiri issue](https://github.com/sparklemotion/nokogiri/issues/2227) if you're interested in helping implement and support HTML5 support.
19
+
20
+
21
+ ### `Loofah::HTML4` module and namespace
22
+
23
+ `Loofah::HTML` has been renamed to `Loofah::HTML4`, and `Loofah::HTML` is aliased to preserve backwards-compatibility. `Nokogiri::HTML` and `Nokogiri::HTML4` parse methods still use libxml2's (or NekoHTML's) HTML4 parser.
24
+
25
+ Take special note that if you rely on the class name of an object in your code, objects will now report a class of `Loofah::HTML4::Foo` where they previously reported `Loofah::HTML::Foo`. Instead of relying on the string returned by `Object#class`, prefer `Class#===` or `Object#is_a?` or `Object#instance_of?`.
26
+
27
+ Future releases of Nokogiri may deprecate `HTML` classes and methods or otherwise change this behavior, so please start using `HTML4` in place of `HTML`.
28
+
29
+
30
+ ### Official support for JRuby
31
+
32
+ This version introduces official support for JRuby. Previously, the test suite had never been green due to differences in behavior in the underlying HTML parser used by Nokogiri. We've updated the test suite to accommodate those differences, and have added JRuby to the CI suite.
33
+
34
+
3
35
  ## 2.20.0 / 2023-04-01
4
36
 
5
37
  ### Features
data/README.md CHANGED
@@ -14,59 +14,61 @@
14
14
 
15
15
  Loofah is a general library for manipulating and transforming HTML/XML documents and fragments, built on top of Nokogiri.
16
16
 
17
- Loofah excels at HTML sanitization (XSS prevention). It includes some nice HTML sanitizers, which are based on HTML5lib's safelist, so it most likely won't make your codes less secure. (These statements have not been evaluated by Netexperts.)
17
+ Loofah also includes some HTML sanitizers based on `html5lib`'s safelist, which are a specific application of the general transformation functionality.
18
18
 
19
- ActiveRecord extensions for sanitization are available in the [`loofah-activerecord` gem](https://github.com/flavorjones/loofah-activerecord).
19
+ Active Record extensions for HTML sanitization are available in the [`loofah-activerecord` gem](https://github.com/flavorjones/loofah-activerecord).
20
20
 
21
21
 
22
22
  ## Features
23
23
 
24
- * Easily write custom scrubbers for HTML/XML leveraging the sweetness of Nokogiri (and HTML5lib's safelists).
25
- * Common HTML sanitizing tasks are built-in:
24
+ * Easily write custom transformations for HTML and XML
25
+ * Common HTML sanitizing transformations are built-in:
26
26
  * _Strip_ unsafe tags, leaving behind only the inner text.
27
27
  * _Prune_ unsafe tags and their subtrees, removing all traces that they ever existed.
28
28
  * _Escape_ unsafe tags and their subtrees, leaving behind lots of <tt>&lt;</tt> and <tt>&gt;</tt> entities.
29
29
  * _Whitewash_ the markup, removing all attributes and namespaced nodes.
30
- * Common HTML transformation tasks are built-in:
30
+ * Other common HTML transformations are built-in:
31
31
  * Add the _nofollow_ attribute to all hyperlinks.
32
- * Format markup as plain text, with or without sensible whitespace handling around block elements.
32
+ * Remove _unprintable_ characters from text nodes.
33
+ * Format markup as plain text, with (or without) sensible whitespace handling around block elements.
33
34
  * Replace Rails's `strip_tags` and `sanitize` view helper methods.
34
35
 
35
36
 
36
37
  ## Compare and Contrast
37
38
 
38
- Loofah is one of two known Ruby XSS/sanitization solutions that
39
- guarantees well-formed and valid markup (the other is Sanitize, which
40
- also uses Nokogiri).
39
+ Loofah is both:
41
40
 
42
- Loofah works on XML, XHTML and HTML documents.
41
+ - a general framework for transforming XML, XHTML, and HTML documents
42
+ - a specific toolkit for HTML sanitization
43
43
 
44
- Also, it's pretty fast. Here is a benchmark comparing Loofah to other
45
- commonly-used libraries (ActionView, Sanitize, HTML5lib and HTMLfilter):
44
+ ### General document transformation
46
45
 
47
- * https://gist.github.com/170193
46
+ Loofah tries to make it easy to write your own custom scrubbers for whatever document transformation you need. You don't like the built-in scrubbers? Build your own, like a boss.
48
47
 
49
- Lastly, Loofah is extensible. It's super-easy to write your own custom
50
- scrubbers for whatever document manipulation you need. You don't like
51
- the built-in scrubbers? Build your own, like a boss.
48
+
49
+ ### HTML sanitization
50
+
51
+ Another Ruby library that provides HTML sanitization is [`rgrove/sanitize`](https://github.com/rgrove/sanitize), another library built on top of Nokogiri, which provides a bit more flexibility on the tags and attributes being scrubbed.
52
+
53
+ You may also want to look at [`rails/rails-html-sanitizer`](https://github.com/rails/rails-html-sanitizer) which is built on top of Loofah and provides some useful extensions and additional flexibility in the HTML sanitization.
52
54
 
53
55
 
54
56
  ## The Basics
55
57
 
56
- Loofah wraps [Nokogiri](http://nokogiri.org) in a loving
57
- embrace. Nokogiri is an excellent HTML/XML parser. If you don't know
58
- how Nokogiri works, you might want to pause for a moment and go check
59
- it out. I'll wait.
58
+ Loofah wraps [Nokogiri](http://nokogiri.org) in a loving embrace. Nokogiri is a stable, well-maintained parser for XML, HTML4, and HTML5.
60
59
 
61
- Loofah presents the following classes:
60
+ Loofah implements the following classes:
62
61
 
63
- * `Loofah::HTML::Document` and `Loofah::HTML::DocumentFragment`
64
- * `Loofah::XML::Document` and `Loofah::XML::DocumentFragment`
65
- * `Loofah::Scrubber`
62
+ * `Loofah::HTML5::Document`
63
+ * `Loofah::HTML5::DocumentFragment`
64
+ * `Loofah::HTML4::Document` (aliased as `Loofah::HTML::Document` for now)
65
+ * `Loofah::HTML4::DocumentFragment` (aliased as `Loofah::HTML::DocumentFragment` for now)
66
+ * `Loofah::XML::Document`
67
+ * `Loofah::XML::DocumentFragment`
66
68
 
67
- The documents and fragments are subclasses of the similar Nokogiri classes.
69
+ These document and fragment classes are subclasses of the similarly-named Nokogiri classes `Nokogiri::HTML5::Document` et al.
68
70
 
69
- The Scrubber represents the document manipulation, either by wrapping
71
+ Loofah also implements `Loofah::Scrubber`, which represents the document transformation, either by wrapping
70
72
  a block,
71
73
 
72
74
  ``` ruby
@@ -80,50 +82,49 @@ or by implementing a method.
80
82
 
81
83
  ### Side Note: Fragments vs Documents
82
84
 
83
- Generally speaking, unless you expect to have a DOCTYPE and a single
84
- root node, you don't have a *document*, you have a *fragment*. For
85
- HTML, another rule of thumb is that *documents* have `html` and `body`
86
- tags, and *fragments* usually do not.
85
+ Generally speaking, unless you expect to have a DOCTYPE and a single root node, you don't have a *document*, you have a *fragment*. For HTML, another rule of thumb is that *documents* have `html` and `body` tags, and *fragments* usually do not.
86
+
87
+ **HTML fragments** should be parsed with `Loofah.html5_fragment` or `Loofah.html4_fragment`. The result won't be wrapped in `html` or `body` tags, won't have a DOCTYPE declaration, `head` elements will be silently ignored, and multiple root nodes are allowed.
88
+
89
+ **HTML documents** should be parsed with `Loofah.html5_document` or `Loofah.html4_document`. The result will have a DOCTYPE declaration, along with `html`, `head` and `body` tags.
90
+
91
+ **XML fragments** should be parsed with `Loofah.xml_fragment`. The result won't have a DOCTYPE declaration, and multiple root nodes are allowed.
87
92
 
88
- HTML fragments should be parsed with Loofah.fragment. The result won't
89
- be wrapped in `html` or `body` tags, won't have a DOCTYPE declaration,
90
- `head` elements will be silently ignored, and multiple root nodes are
91
- allowed.
93
+ **XML documents** should be parsed with `Loofah.xml_document`. The result will have a DOCTYPE declaration and a single root node.
92
94
 
93
- XML fragments should be parsed with Loofah.xml_fragment. The result
94
- won't have a DOCTYPE declaration, and multiple root nodes are allowed.
95
95
 
96
- HTML documents should be parsed with Loofah.document. The result will
97
- have a DOCTYPE declaration, along with `html`, `head` and `body` tags.
96
+ ### Side Note: HTML4 vs HTML5
98
97
 
99
- XML documents should be parsed with Loofah.xml_document. The result
100
- will have a DOCTYPE declaration and a single root node.
98
+ _HTML5 functionality is not available on JRuby, or with versions of Nokogiri `< 1.14.0`._
101
99
 
100
+ Currently, Loofah's methods `Loofah.document` and `Loofah.fragment` are aliases to `.html4_document` and `.html4_fragment`, which use Nokogiri's HTML4 parser. (Similarly, `Loofah::HTML::Document` and `Loofah::HTML::DocumentFragment` are aliased to `Loofah::HTML4::Document` and `Loofah::HTML4::DocumentFragment`.)
102
101
 
103
- ### Loofah::HTML::Document and Loofah::HTML::DocumentFragment
102
+ **Please note** that in a future version of Loofah, these methods and classes may switch to using Nokogiri's HTML5 parser and classes on platforms that support it [1].
104
103
 
105
- These classes are subclasses of Nokogiri::HTML::Document and
106
- Nokogiri::HTML::DocumentFragment, so you get all the markup
107
- fixer-uppery and API goodness of Nokogiri.
104
+ **We strongly recommend that you explicitly use `.html5_document` or `.html5_fragment`** unless you know of a compelling reason not to. If you are sure that you need to use the HTML4 parser, you should explicitly call `.html4_document` or `.html4_fragment` to avoid breakage in a future version.
108
105
 
109
- The module methods Loofah.document and Loofah.fragment will parse an
110
- HTML document and an HTML fragment, respectively.
106
+ [1]: [[feature request] HTML5 parser for JRuby implementation · Issue #2227 · sparklemotion/nokogiri](https://github.com/sparklemotion/nokogiri/issues/2227)
107
+
108
+
109
+ ### `Loofah::HTML5::Document` and `Loofah::HTML5::DocumentFragment`
110
+
111
+ These classes are subclasses of `Nokogiri::HTML5::Document` and `Nokogiri::HTML5::DocumentFragment`.
112
+
113
+ The module methods `Loofah.html5_document` and `Loofah.html5_fragment` will parse either an HTML document and an HTML fragment, respectively.
111
114
 
112
115
  ``` ruby
113
- Loofah.document(unsafe_html).is_a?(Nokogiri::HTML::Document) # => true
114
- Loofah.fragment(unsafe_html).is_a?(Nokogiri::HTML::DocumentFragment) # => true
116
+ Loofah.html5_document(unsafe_html).is_a?(Nokogiri::HTML5::Document) # => true
117
+ Loofah.html5_fragment(unsafe_html).is_a?(Nokogiri::HTML5::DocumentFragment) # => true
115
118
  ```
116
119
 
117
- Loofah injects a `scrub!` method, which takes either a symbol (for
118
- built-in scrubbers) or a Loofah::Scrubber object (for custom
119
- scrubbers), and modifies the document in-place.
120
+ Loofah injects a `scrub!` method, which takes either a symbol (for built-in scrubbers) or a `Loofah::Scrubber` object (for custom scrubbers), and modifies the document in-place.
120
121
 
121
122
  Loofah overrides `to_s` to return HTML:
122
123
 
123
124
  ``` ruby
124
125
  unsafe_html = "ohai! <div>div is safe</div> <script>but script is not</script>"
125
126
 
126
- doc = Loofah.fragment(unsafe_html).scrub!(:prune)
127
+ doc = Loofah.html5_fragment(unsafe_html).scrub!(:prune)
127
128
  doc.to_s # => "ohai! <div>div is safe</div> "
128
129
  ```
129
130
 
@@ -136,32 +137,38 @@ doc.text # => "ohai! div is safe "
136
137
  Also, `to_text` is available, which does the right thing with whitespace around block-level and line break elements.
137
138
 
138
139
  ``` ruby
139
- doc = Loofah.fragment("<h1>Title</h1><div>Content<br>Next line</div>")
140
+ doc = Loofah.html5_fragment("<h1>Title</h1><div>Content<br>Next line</div>")
140
141
  doc.text # => "TitleContentNext line" # probably not what you want
141
142
  doc.to_text # => "\nTitle\n\nContent\nNext line\n" # better
142
143
  ```
143
144
 
144
- ### Loofah::XML::Document and Loofah::XML::DocumentFragment
145
+ ### `Loofah::HTML4::Document` and `Loofah::HTML4::DocumentFragment`
146
+
147
+ These classes are subclasses of `Nokogiri::HTML4::Document` and `Nokogiri::HTML4::DocumentFragment`.
148
+
149
+ The module methods `Loofah.html4_document` and `Loofah.html4_fragment` will parse either an HTML document and an HTML fragment, respectively.
150
+
151
+ ``` ruby
152
+ Loofah.html4_document(unsafe_html).is_a?(Nokogiri::HTML4::Document) # => true
153
+ Loofah.html4_fragment(unsafe_html).is_a?(Nokogiri::HTML4::DocumentFragment) # => true
154
+ ```
155
+
156
+ ### `Loofah::XML::Document` and `Loofah::XML::DocumentFragment`
145
157
 
146
- These classes are subclasses of Nokogiri::XML::Document and
147
- Nokogiri::XML::DocumentFragment, so you get all the markup
148
- fixer-uppery and API goodness of Nokogiri.
158
+ These classes are subclasses of `Nokogiri::XML::Document` and `Nokogiri::XML::DocumentFragment`.
149
159
 
150
- The module methods Loofah.xml_document and Loofah.xml_fragment will
151
- parse an XML document and an XML fragment, respectively.
160
+ The module methods `Loofah.xml_document` and `Loofah.xml_fragment` will parse an XML document and an XML fragment, respectively.
152
161
 
153
162
  ``` ruby
154
163
  Loofah.xml_document(bad_xml).is_a?(Nokogiri::XML::Document) # => true
155
164
  Loofah.xml_fragment(bad_xml).is_a?(Nokogiri::XML::DocumentFragment) # => true
156
165
  ```
157
166
 
158
- ### Nodes and NodeSets
167
+ ### Nodes and Node Sets
159
168
 
160
- Nokogiri::XML::Node and Nokogiri::XML::NodeSet also get a `scrub!`
161
- method, which makes it easy to scrub subtrees.
169
+ Nokogiri's `Node` and `NodeSet` classes also get a `scrub!` method, which makes it easy to scrub subtrees.
162
170
 
163
- The following code will apply the `employee_scrubber` only to the
164
- `employee` nodes (and their subtrees) in the document:
171
+ The following code will apply the `employee_scrubber` only to the `employee` nodes (and their subtrees) in the document:
165
172
 
166
173
  ``` ruby
167
174
  Loofah.xml_document(bad_xml).xpath("//employee").scrub!(employee_scrubber)
@@ -173,7 +180,7 @@ And this code will only scrub the first `employee` node and its subtree:
173
180
  Loofah.xml_document(bad_xml).at_xpath("//employee").scrub!(employee_scrubber)
174
181
  ```
175
182
 
176
- ### Loofah::Scrubber
183
+ ### `Loofah::Scrubber`
177
184
 
178
185
  A Scrubber wraps up a block (or method) that is run on a document node:
179
186
 
@@ -187,14 +194,11 @@ end
187
194
  This can then be run on a document:
188
195
 
189
196
  ``` ruby
190
- Loofah.fragment("<span>foo</span><p>bar</p>").scrub!(span2div).to_s
197
+ Loofah.html5_fragment("<span>foo</span><p>bar</p>").scrub!(span2div).to_s
191
198
  # => "<div>foo</div><p>bar</p>"
192
199
  ```
193
200
 
194
- Scrubbers can be run on a document in either a top-down traversal (the
195
- default) or bottom-up. Top-down scrubbers can optionally return
196
- Scrubber::STOP to terminate the traversal of a subtree. Read below and
197
- in the Loofah::Scrubber class for more detailed usage.
201
+ Scrubbers can be run on a document in either a top-down traversal (the default) or bottom-up. Top-down scrubbers can optionally return `Scrubber::STOP` to terminate the traversal of a subtree. Read below and in the `Loofah::Scrubber` class for more detailed usage.
198
202
 
199
203
  Here's an XML example:
200
204
 
@@ -211,10 +215,10 @@ Loofah.xml_document(File.read('plague.xml')).scrub!(bring_out_your_dead)
211
215
 
212
216
  ### Built-In HTML Scrubbers
213
217
 
214
- Loofah comes with a set of sanitizing scrubbers that use HTML5lib's
215
- safelist algorithm:
218
+ Loofah comes with a set of sanitizing scrubbers that use `html5lib`'s safelist algorithm:
216
219
 
217
220
  ``` ruby
221
+ doc = Loofah.html5_document(input)
218
222
  doc.scrub!(:strip) # replaces unknown/unsafe tags with their inner text
219
223
  doc.scrub!(:prune) # removes unknown/unsafe tags and their children
220
224
  doc.scrub!(:escape) # escapes unknown/unsafe tags, like this: &lt;script&gt;
@@ -222,14 +226,14 @@ doc.scrub!(:whitewash) # removes unknown/unsafe/namespaced tags and their chi
222
226
  # and strips all node attributes
223
227
  ```
224
228
 
225
- Loofah also comes with some common transformation tasks:
229
+ Loofah also comes with some common transformation tasks:
226
230
 
227
231
  ``` ruby
228
232
  doc.scrub!(:nofollow) # adds rel="nofollow" attribute to links
229
233
  doc.scrub!(:unprintable) # removes unprintable characters from text nodes
230
234
  ```
231
235
 
232
- See Loofah::Scrubbers for more details and example usage.
236
+ See `Loofah::Scrubbers` for more details and example usage.
233
237
 
234
238
 
235
239
  ### Chaining Scrubbers
@@ -237,7 +241,7 @@ See Loofah::Scrubbers for more details and example usage.
237
241
  You can chain scrubbers:
238
242
 
239
243
  ``` ruby
240
- Loofah.fragment("<span>hello</span> <script>alert('OHAI')</script>") \
244
+ Loofah.html5_fragment("<span>hello</span> <script>alert('OHAI')</script>") \
241
245
  .scrub!(:prune) \
242
246
  .scrub!(span2div).to_s
243
247
  # => "<div>hello</div> "
@@ -245,21 +249,26 @@ Loofah.fragment("<span>hello</span> <script>alert('OHAI')</script>") \
245
249
 
246
250
  ### Shorthand
247
251
 
248
- The class methods Loofah.scrub_fragment and Loofah.scrub_document are
249
- shorthand.
252
+ The class methods `Loofah.scrub_html5_fragment` and `Loofah.scrub_html5_document` (and the corresponding HTML4 methods) are shorthand.
253
+
254
+ These methods:
250
255
 
251
256
  ``` ruby
252
- Loofah.scrub_fragment(unsafe_html, :prune)
253
- Loofah.scrub_document(unsafe_html, :prune)
257
+ Loofah.scrub_html5_fragment(unsafe_html, :prune)
258
+ Loofah.scrub_html5_document(unsafe_html, :prune)
259
+ Loofah.scrub_html4_fragment(unsafe_html, :prune)
260
+ Loofah.scrub_html4_document(unsafe_html, :prune)
254
261
  Loofah.scrub_xml_fragment(bad_xml, custom_scrubber)
255
262
  Loofah.scrub_xml_document(bad_xml, custom_scrubber)
256
263
  ```
257
264
 
258
- are the same thing as (and arguably semantically clearer than):
265
+ do the same thing as (and arguably semantically clearer than):
259
266
 
260
267
  ``` ruby
261
- Loofah.fragment(unsafe_html).scrub!(:prune)
262
- Loofah.document(unsafe_html).scrub!(:prune)
268
+ Loofah.html5_fragment(unsafe_html).scrub!(:prune)
269
+ Loofah.html5_document(unsafe_html).scrub!(:prune)
270
+ Loofah.html4_fragment(unsafe_html).scrub!(:prune)
271
+ Loofah.html4_document(unsafe_html).scrub!(:prune)
263
272
  Loofah.xml_fragment(bad_xml).scrub!(custom_scrubber)
264
273
  Loofah.xml_document(bad_xml).scrub!(custom_scrubber)
265
274
  ```
@@ -267,10 +276,9 @@ Loofah.xml_document(bad_xml).scrub!(custom_scrubber)
267
276
 
268
277
  ### View Helpers
269
278
 
270
- Loofah has two "view helpers": Loofah::Helpers.sanitize and
271
- Loofah::Helpers.strip_tags, both of which are drop-in replacements for
272
- the Rails ActionView helpers of the same name.
273
- These are no longer required automatically. You must require `loofah/helpers`.
279
+ Loofah has two "view helpers": `Loofah::Helpers.sanitize` and `Loofah::Helpers.strip_tags`, both of which are drop-in replacements for the Rails Action View helpers of the same name.
280
+
281
+ These are not required automatically. You must require `loofah/helpers` to use them.
274
282
 
275
283
 
276
284
  ## Requirements
@@ -296,8 +304,6 @@ And the mailing list is on Google Groups:
296
304
  * Mail: loofah-talk@googlegroups.com
297
305
  * Archive: https://groups.google.com/forum/#!forum/loofah-talk
298
306
 
299
- And the IRC channel is \#loofah on freenode.
300
-
301
307
  Consider subscribing to [Tidelift][tidelift] which provides license assurances and timely security notifications for your open source dependencies, including Loofah. [Tidelift][tidelift] subscriptions also help the Loofah maintainers fund our [automated testing](https://ci.nokogiri.org) which in turn allows us to ship releases, bugfixes, and security updates more often.
302
308
 
303
309
  [tidelift]: https://tidelift.com/subscription/pkg/rubygems-loofah?utm_source=undefined&utm_medium=referral&utm_campaign=enterprise
@@ -308,26 +314,12 @@ Consider subscribing to [Tidelift][tidelift] which provides license assurances a
308
314
  See [`SECURITY.md`](SECURITY.md) for vulnerability reporting details.
309
315
 
310
316
 
311
- ### "Secure by Default"
312
-
313
- Some tools may incorrectly report Loofah as a potential security
314
- vulnerability.
315
-
316
- Loofah depends on Nokogiri, and it's _possible_ to use Nokogiri in a
317
- dangerous way (by enabling its DTDLOAD option and disabling its NONET
318
- option). This specifically allows the opportunity for an XML External
319
- Entity (XXE) vulnerability if the XML data is untrusted.
320
-
321
- However, Loofah __never enables this Nokogiri configuration__; Loofah
322
- never enables DTDLOAD, and it never disables NONET, thereby protecting
323
- you by default from this XXE vulnerability.
324
-
325
-
326
317
  ## Related Links
327
318
 
319
+ * loofah-activerecord: https://github.com/flavorjones/loofah-activerecord
328
320
  * Nokogiri: http://nokogiri.org
329
321
  * libxml2: http://xmlsoft.org
330
- * html5lib: https://code.google.com/p/html5lib
322
+ * html5lib: https://github.com/html5lib/
331
323
 
332
324
 
333
325
  ## Authors
@@ -355,8 +347,7 @@ The following people have generously funded Loofah:
355
347
 
356
348
  ## Historical Note
357
349
 
358
- This library was formerly known as Dryopteris, which was a very bad
359
- name that nobody could spell properly.
350
+ This library was once named "Dryopteris", which was a very bad name that nobody could spell properly.
360
351
 
361
352
 
362
353
  ## License
@@ -0,0 +1,207 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Loofah
4
+ #
5
+ # Mixes +scrub!+ into Document, DocumentFragment, Node and NodeSet.
6
+ #
7
+ # Traverse the document or fragment, invoking the +scrubber+ on each node.
8
+ #
9
+ # +scrubber+ must either be one of the symbols representing the built-in scrubbers (see
10
+ # Scrubbers), or a Scrubber instance.
11
+ #
12
+ # span2div = Loofah::Scrubber.new do |node|
13
+ # node.name = "div" if node.name == "span"
14
+ # end
15
+ # Loofah.html5_fragment("<span>foo</span><p>bar</p>").scrub!(span2div).to_s
16
+ # # => "<div>foo</div><p>bar</p>"
17
+ #
18
+ # or
19
+ #
20
+ # unsafe_html = "ohai! <div>div is safe</div> <script>but script is not</script>"
21
+ # Loofah.html5_fragment(unsafe_html).scrub!(:strip).to_s
22
+ # # => "ohai! <div>div is safe</div> "
23
+ #
24
+ # Note that this method is called implicitly from the shortcuts Loofah.scrub_html5_fragment et
25
+ # al.
26
+ #
27
+ # Please see Scrubber for more information on implementation and traversal, and README.rdoc for
28
+ # more example usage.
29
+ #
30
+ module ScrubBehavior
31
+ module Node # :nodoc:
32
+ def scrub!(scrubber)
33
+ #
34
+ # yes. this should be three separate methods. but nokogiri decorates (or not) based on
35
+ # whether the module name has already been included. and since documents get decorated just
36
+ # like their constituent nodes, we need to jam all the logic into a single module.
37
+ #
38
+ scrubber = ScrubBehavior.resolve_scrubber(scrubber)
39
+ case self
40
+ when Nokogiri::XML::Document
41
+ scrubber.traverse(root) if root
42
+ when Nokogiri::XML::DocumentFragment
43
+ children.scrub!(scrubber)
44
+ else
45
+ scrubber.traverse(self)
46
+ end
47
+ self
48
+ end
49
+ end
50
+
51
+ module NodeSet # :nodoc:
52
+ def scrub!(scrubber)
53
+ each { |node| node.scrub!(scrubber) }
54
+ self
55
+ end
56
+ end
57
+
58
+ class << self
59
+ def resolve_scrubber(scrubber) # :nodoc:
60
+ scrubber = Scrubbers::MAP[scrubber].new if Scrubbers::MAP[scrubber]
61
+ unless scrubber.is_a?(Loofah::Scrubber)
62
+ raise Loofah::ScrubberNotFound, "not a Scrubber or a scrubber name: #{scrubber.inspect}"
63
+ end
64
+
65
+ scrubber
66
+ end
67
+ end
68
+ end
69
+
70
+ #
71
+ # Overrides +text+ in Document and DocumentFragment classes, and mixes in +to_text+.
72
+ #
73
+ module TextBehavior
74
+ #
75
+ # Returns a plain-text version of the markup contained by the document, with HTML entities
76
+ # encoded.
77
+ #
78
+ # This method is significantly faster than #to_text, but isn't clever about whitespace around
79
+ # block elements.
80
+ #
81
+ # Loofah.html5_document("<h1>Title</h1><div>Content</div>").text
82
+ # # => "TitleContent"
83
+ #
84
+ # By default, the returned text will have HTML entities escaped. If you want unescaped
85
+ # entities, and you understand that the result is unsafe to render in a browser, then you can
86
+ # pass an argument as shown:
87
+ #
88
+ # frag = Loofah.html5_fragment("&lt;script&gt;alert('EVIL');&lt;/script&gt;")
89
+ # # ok for browser:
90
+ # frag.text # => "&lt;script&gt;alert('EVIL');&lt;/script&gt;"
91
+ # # decidedly not ok for browser:
92
+ # frag.text(:encode_special_chars => false) # => "<script>alert('EVIL');</script>"
93
+ #
94
+ def text(options = {})
95
+ result = if serialize_root
96
+ serialize_root.children.reject(&:comment?).map(&:inner_text).join("")
97
+ else
98
+ ""
99
+ end
100
+ if options[:encode_special_chars] == false
101
+ result # possibly dangerous if rendered in a browser
102
+ else
103
+ encode_special_chars(result)
104
+ end
105
+ end
106
+
107
+ alias_method :inner_text, :text
108
+ alias_method :to_str, :text
109
+
110
+ #
111
+ # Returns a plain-text version of the markup contained by the fragment, with HTML entities
112
+ # encoded.
113
+ #
114
+ # This method is slower than #text, but is clever about whitespace around block elements and
115
+ # line break elements.
116
+ #
117
+ # Loofah.html5_document("<h1>Title</h1><div>Content<br>Next line</div>").to_text
118
+ # # => "\nTitle\n\nContent\nNext line\n"
119
+ #
120
+ def to_text(options = {})
121
+ Loofah.remove_extraneous_whitespace(dup.scrub!(:newline_block_elements).text(options))
122
+ end
123
+ end
124
+
125
+ module DocumentDecorator # :nodoc:
126
+ def initialize(*args, &block)
127
+ super
128
+ decorators(Nokogiri::XML::Node) << ScrubBehavior::Node
129
+ decorators(Nokogiri::XML::NodeSet) << ScrubBehavior::NodeSet
130
+ end
131
+ end
132
+
133
+ module HtmlDocumentBehavior # :nodoc:
134
+ module ClassMethods
135
+ def parse(*args, &block)
136
+ remove_comments_before_html_element(super)
137
+ end
138
+
139
+ private
140
+
141
+ # remove comments that exist outside of the HTML element.
142
+ #
143
+ # these comments are allowed by the HTML spec:
144
+ #
145
+ # https://www.w3.org/TR/html401/struct/global.html#h-7.1
146
+ #
147
+ # but are not scrubbed by Loofah because these nodes don't meet
148
+ # the contract that scrubbers expect of a node (e.g., it can be
149
+ # replaced, sibling and children nodes can be created).
150
+ def remove_comments_before_html_element(doc)
151
+ doc.children.each do |child|
152
+ child.unlink if child.comment?
153
+ end
154
+ doc
155
+ end
156
+ end
157
+
158
+ class << self
159
+ def included(base)
160
+ base.extend(ClassMethods)
161
+ end
162
+ end
163
+
164
+ def serialize_root
165
+ at_xpath("/html/body")
166
+ end
167
+ end
168
+
169
+ module HtmlFragmentBehavior # :nodoc:
170
+ module ClassMethods
171
+ def parse(tags, encoding = nil)
172
+ doc = document_klass.new
173
+
174
+ encoding ||= tags.respond_to?(:encoding) ? tags.encoding.name : "UTF-8"
175
+ doc.encoding = encoding
176
+
177
+ new(doc, tags)
178
+ end
179
+
180
+ def document_klass
181
+ @document_klass ||= if Loofah.html5_support? && self == Loofah::HTML5::DocumentFragment
182
+ Loofah::HTML5::Document
183
+ elsif self == Loofah::HTML4::DocumentFragment
184
+ Loofah::HTML4::Document
185
+ else
186
+ raise ArgumentError, "unexpected class: #{self}"
187
+ end
188
+ end
189
+ end
190
+
191
+ class << self
192
+ def included(base)
193
+ base.extend(ClassMethods)
194
+ end
195
+ end
196
+
197
+ def to_s
198
+ serialize_root.children.to_s
199
+ end
200
+
201
+ alias_method :serialize, :to_s
202
+
203
+ def serialize_root
204
+ at_xpath("./body") || self
205
+ end
206
+ end
207
+ end