loofah 1.0.0 → 2.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +489 -0
- data/MIT-LICENSE.txt +3 -1
- data/README.md +364 -0
- data/SECURITY.md +18 -0
- data/lib/loofah/elements.rb +88 -11
- data/lib/loofah/helpers.rb +76 -2
- data/lib/loofah/html/document.rb +1 -0
- data/lib/loofah/html/document_fragment.rb +9 -2
- data/lib/loofah/html5/libxml2_workarounds.rb +27 -0
- data/lib/loofah/html5/safelist.rb +1042 -0
- data/lib/loofah/html5/scrub.rb +198 -40
- data/lib/loofah/instance_methods.rb +16 -10
- data/lib/loofah/metahelpers.rb +9 -10
- data/lib/loofah/scrubber.rb +22 -6
- data/lib/loofah/scrubbers.rb +96 -16
- data/lib/loofah/version.rb +5 -0
- data/lib/loofah/xml/document.rb +1 -0
- data/lib/loofah/xml/document_fragment.rb +5 -2
- data/lib/loofah.rb +38 -25
- metadata +159 -172
- data/CHANGELOG.rdoc +0 -134
- data/Gemfile +0 -1
- data/Manifest.txt +0 -34
- data/README.rdoc +0 -312
- data/Rakefile +0 -53
- data/benchmark/benchmark.rb +0 -149
- data/benchmark/fragment.html +0 -96
- data/benchmark/helper.rb +0 -73
- data/benchmark/www.slashdot.com.html +0 -2560
- data/lib/loofah/html5/whitelist.rb +0 -168
- data/test/helper.rb +0 -7
- data/test/html5/test_sanitizer.rb +0 -248
- data/test/integration/test_ad_hoc.rb +0 -176
- data/test/integration/test_helpers.rb +0 -33
- data/test/integration/test_html.rb +0 -51
- data/test/integration/test_scrubbers.rb +0 -331
- data/test/integration/test_xml.rb +0 -55
- data/test/unit/test_api.rb +0 -138
- data/test/unit/test_helpers.rb +0 -27
- data/test/unit/test_scrubber.rb +0 -229
- data/test/unit/test_scrubbers.rb +0 -14
data/README.md
ADDED
@@ -0,0 +1,364 @@
|
|
1
|
+
# Loofah
|
2
|
+
|
3
|
+
* https://github.com/flavorjones/loofah
|
4
|
+
* Docs: http://rubydoc.info/github/flavorjones/loofah/main/frames
|
5
|
+
* Mailing list: [loofah-talk@googlegroups.com](https://groups.google.com/forum/#!forum/loofah-talk)
|
6
|
+
|
7
|
+
## Status
|
8
|
+
|
9
|
+
[![ci](https://github.com/flavorjones/loofah/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/flavorjones/loofah/actions/workflows/ci.yml)
|
10
|
+
[![Tidelift dependencies](https://tidelift.com/badges/package/rubygems/loofah)](https://tidelift.com/subscription/pkg/rubygems-loofah?utm_source=rubygems-loofah&utm_medium=referral&utm_campaign=readme)
|
11
|
+
|
12
|
+
|
13
|
+
## Description
|
14
|
+
|
15
|
+
Loofah is a general library for manipulating and transforming HTML/XML documents and fragments, built on top of Nokogiri.
|
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.)
|
18
|
+
|
19
|
+
ActiveRecord extensions for sanitization are available in the [`loofah-activerecord` gem](https://github.com/flavorjones/loofah-activerecord).
|
20
|
+
|
21
|
+
|
22
|
+
## Features
|
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:
|
26
|
+
* _Strip_ unsafe tags, leaving behind only the inner text.
|
27
|
+
* _Prune_ unsafe tags and their subtrees, removing all traces that they ever existed.
|
28
|
+
* _Escape_ unsafe tags and their subtrees, leaving behind lots of <tt><</tt> and <tt>></tt> entities.
|
29
|
+
* _Whitewash_ the markup, removing all attributes and namespaced nodes.
|
30
|
+
* Common HTML transformation tasks are built-in:
|
31
|
+
* Add the _nofollow_ attribute to all hyperlinks.
|
32
|
+
* Format markup as plain text, with or without sensible whitespace handling around block elements.
|
33
|
+
* Replace Rails's `strip_tags` and `sanitize` view helper methods.
|
34
|
+
|
35
|
+
|
36
|
+
## Compare and Contrast
|
37
|
+
|
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).
|
41
|
+
|
42
|
+
Loofah works on XML, XHTML and HTML documents.
|
43
|
+
|
44
|
+
Also, it's pretty fast. Here is a benchmark comparing Loofah to other
|
45
|
+
commonly-used libraries (ActionView, Sanitize, HTML5lib and HTMLfilter):
|
46
|
+
|
47
|
+
* https://gist.github.com/170193
|
48
|
+
|
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.
|
52
|
+
|
53
|
+
|
54
|
+
## The Basics
|
55
|
+
|
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.
|
60
|
+
|
61
|
+
Loofah presents the following classes:
|
62
|
+
|
63
|
+
* `Loofah::HTML::Document` and `Loofah::HTML::DocumentFragment`
|
64
|
+
* `Loofah::XML::Document` and `Loofah::XML::DocumentFragment`
|
65
|
+
* `Loofah::Scrubber`
|
66
|
+
|
67
|
+
The documents and fragments are subclasses of the similar Nokogiri classes.
|
68
|
+
|
69
|
+
The Scrubber represents the document manipulation, either by wrapping
|
70
|
+
a block,
|
71
|
+
|
72
|
+
``` ruby
|
73
|
+
span2div = Loofah::Scrubber.new do |node|
|
74
|
+
node.name = "div" if node.name == "span"
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
or by implementing a method.
|
79
|
+
|
80
|
+
|
81
|
+
### Side Note: Fragments vs Documents
|
82
|
+
|
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.
|
87
|
+
|
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.
|
92
|
+
|
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
|
+
|
96
|
+
HTML documents should be parsed with Loofah.document. The result will
|
97
|
+
have a DOCTYPE declaration, along with `html`, `head` and `body` tags.
|
98
|
+
|
99
|
+
XML documents should be parsed with Loofah.xml_document. The result
|
100
|
+
will have a DOCTYPE declaration and a single root node.
|
101
|
+
|
102
|
+
|
103
|
+
### Loofah::HTML::Document and Loofah::HTML::DocumentFragment
|
104
|
+
|
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.
|
108
|
+
|
109
|
+
The module methods Loofah.document and Loofah.fragment will parse an
|
110
|
+
HTML document and an HTML fragment, respectively.
|
111
|
+
|
112
|
+
``` ruby
|
113
|
+
Loofah.document(unsafe_html).is_a?(Nokogiri::HTML::Document) # => true
|
114
|
+
Loofah.fragment(unsafe_html).is_a?(Nokogiri::HTML::DocumentFragment) # => true
|
115
|
+
```
|
116
|
+
|
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
|
+
|
121
|
+
Loofah overrides `to_s` to return HTML:
|
122
|
+
|
123
|
+
``` ruby
|
124
|
+
unsafe_html = "ohai! <div>div is safe</div> <script>but script is not</script>"
|
125
|
+
|
126
|
+
doc = Loofah.fragment(unsafe_html).scrub!(:prune)
|
127
|
+
doc.to_s # => "ohai! <div>div is safe</div> "
|
128
|
+
```
|
129
|
+
|
130
|
+
and `text` to return plain text:
|
131
|
+
|
132
|
+
``` ruby
|
133
|
+
doc.text # => "ohai! div is safe "
|
134
|
+
```
|
135
|
+
|
136
|
+
Also, `to_text` is available, which does the right thing with whitespace around block-level and line break elements.
|
137
|
+
|
138
|
+
``` ruby
|
139
|
+
doc = Loofah.fragment("<h1>Title</h1><div>Content<br>Next line</div>")
|
140
|
+
doc.text # => "TitleContentNext line" # probably not what you want
|
141
|
+
doc.to_text # => "\nTitle\n\nContent\nNext line\n" # better
|
142
|
+
```
|
143
|
+
|
144
|
+
### Loofah::XML::Document and Loofah::XML::DocumentFragment
|
145
|
+
|
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.
|
149
|
+
|
150
|
+
The module methods Loofah.xml_document and Loofah.xml_fragment will
|
151
|
+
parse an XML document and an XML fragment, respectively.
|
152
|
+
|
153
|
+
``` ruby
|
154
|
+
Loofah.xml_document(bad_xml).is_a?(Nokogiri::XML::Document) # => true
|
155
|
+
Loofah.xml_fragment(bad_xml).is_a?(Nokogiri::XML::DocumentFragment) # => true
|
156
|
+
```
|
157
|
+
|
158
|
+
### Nodes and NodeSets
|
159
|
+
|
160
|
+
Nokogiri::XML::Node and Nokogiri::XML::NodeSet also get a `scrub!`
|
161
|
+
method, which makes it easy to scrub subtrees.
|
162
|
+
|
163
|
+
The following code will apply the `employee_scrubber` only to the
|
164
|
+
`employee` nodes (and their subtrees) in the document:
|
165
|
+
|
166
|
+
``` ruby
|
167
|
+
Loofah.xml_document(bad_xml).xpath("//employee").scrub!(employee_scrubber)
|
168
|
+
```
|
169
|
+
|
170
|
+
And this code will only scrub the first `employee` node and its subtree:
|
171
|
+
|
172
|
+
``` ruby
|
173
|
+
Loofah.xml_document(bad_xml).at_xpath("//employee").scrub!(employee_scrubber)
|
174
|
+
```
|
175
|
+
|
176
|
+
### Loofah::Scrubber
|
177
|
+
|
178
|
+
A Scrubber wraps up a block (or method) that is run on a document node:
|
179
|
+
|
180
|
+
``` ruby
|
181
|
+
# change all <span> tags to <div> tags
|
182
|
+
span2div = Loofah::Scrubber.new do |node|
|
183
|
+
node.name = "div" if node.name == "span"
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
This can then be run on a document:
|
188
|
+
|
189
|
+
``` ruby
|
190
|
+
Loofah.fragment("<span>foo</span><p>bar</p>").scrub!(span2div).to_s
|
191
|
+
# => "<div>foo</div><p>bar</p>"
|
192
|
+
```
|
193
|
+
|
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.
|
198
|
+
|
199
|
+
Here's an XML example:
|
200
|
+
|
201
|
+
``` ruby
|
202
|
+
# remove all <employee> tags that have a "deceased" attribute set to true
|
203
|
+
bring_out_your_dead = Loofah::Scrubber.new do |node|
|
204
|
+
if node.name == "employee" and node["deceased"] == "true"
|
205
|
+
node.remove
|
206
|
+
Loofah::Scrubber::STOP # don't bother with the rest of the subtree
|
207
|
+
end
|
208
|
+
end
|
209
|
+
Loofah.xml_document(File.read('plague.xml')).scrub!(bring_out_your_dead)
|
210
|
+
```
|
211
|
+
|
212
|
+
### Built-In HTML Scrubbers
|
213
|
+
|
214
|
+
Loofah comes with a set of sanitizing scrubbers that use HTML5lib's
|
215
|
+
safelist algorithm:
|
216
|
+
|
217
|
+
``` ruby
|
218
|
+
doc.scrub!(:strip) # replaces unknown/unsafe tags with their inner text
|
219
|
+
doc.scrub!(:prune) # removes unknown/unsafe tags and their children
|
220
|
+
doc.scrub!(:escape) # escapes unknown/unsafe tags, like this: <script>
|
221
|
+
doc.scrub!(:whitewash) # removes unknown/unsafe/namespaced tags and their children,
|
222
|
+
# and strips all node attributes
|
223
|
+
```
|
224
|
+
|
225
|
+
Loofah also comes with some common transformation tasks:
|
226
|
+
|
227
|
+
``` ruby
|
228
|
+
doc.scrub!(:nofollow) # adds rel="nofollow" attribute to links
|
229
|
+
doc.scrub!(:unprintable) # removes unprintable characters from text nodes
|
230
|
+
```
|
231
|
+
|
232
|
+
See Loofah::Scrubbers for more details and example usage.
|
233
|
+
|
234
|
+
|
235
|
+
### Chaining Scrubbers
|
236
|
+
|
237
|
+
You can chain scrubbers:
|
238
|
+
|
239
|
+
``` ruby
|
240
|
+
Loofah.fragment("<span>hello</span> <script>alert('OHAI')</script>") \
|
241
|
+
.scrub!(:prune) \
|
242
|
+
.scrub!(span2div).to_s
|
243
|
+
# => "<div>hello</div> "
|
244
|
+
```
|
245
|
+
|
246
|
+
### Shorthand
|
247
|
+
|
248
|
+
The class methods Loofah.scrub_fragment and Loofah.scrub_document are
|
249
|
+
shorthand.
|
250
|
+
|
251
|
+
``` ruby
|
252
|
+
Loofah.scrub_fragment(unsafe_html, :prune)
|
253
|
+
Loofah.scrub_document(unsafe_html, :prune)
|
254
|
+
Loofah.scrub_xml_fragment(bad_xml, custom_scrubber)
|
255
|
+
Loofah.scrub_xml_document(bad_xml, custom_scrubber)
|
256
|
+
```
|
257
|
+
|
258
|
+
are the same thing as (and arguably semantically clearer than):
|
259
|
+
|
260
|
+
``` ruby
|
261
|
+
Loofah.fragment(unsafe_html).scrub!(:prune)
|
262
|
+
Loofah.document(unsafe_html).scrub!(:prune)
|
263
|
+
Loofah.xml_fragment(bad_xml).scrub!(custom_scrubber)
|
264
|
+
Loofah.xml_document(bad_xml).scrub!(custom_scrubber)
|
265
|
+
```
|
266
|
+
|
267
|
+
|
268
|
+
### View Helpers
|
269
|
+
|
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`.
|
274
|
+
|
275
|
+
|
276
|
+
## Requirements
|
277
|
+
|
278
|
+
* Nokogiri >= 1.5.9
|
279
|
+
|
280
|
+
|
281
|
+
## Installation
|
282
|
+
|
283
|
+
Unsurprisingly:
|
284
|
+
|
285
|
+
* gem install loofah
|
286
|
+
|
287
|
+
|
288
|
+
## Support
|
289
|
+
|
290
|
+
The bug tracker is available here:
|
291
|
+
|
292
|
+
* https://github.com/flavorjones/loofah/issues
|
293
|
+
|
294
|
+
And the mailing list is on Google Groups:
|
295
|
+
|
296
|
+
* Mail: loofah-talk@googlegroups.com
|
297
|
+
* Archive: https://groups.google.com/forum/#!forum/loofah-talk
|
298
|
+
|
299
|
+
And the IRC channel is \#loofah on freenode.
|
300
|
+
|
301
|
+
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
|
+
|
303
|
+
[tidelift]: https://tidelift.com/subscription/pkg/rubygems-loofah?utm_source=undefined&utm_medium=referral&utm_campaign=enterprise
|
304
|
+
|
305
|
+
|
306
|
+
## Security
|
307
|
+
|
308
|
+
See [`SECURITY.md`](SECURITY.md) for vulnerability reporting details.
|
309
|
+
|
310
|
+
|
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
|
+
## Related Links
|
327
|
+
|
328
|
+
* Nokogiri: http://nokogiri.org
|
329
|
+
* libxml2: http://xmlsoft.org
|
330
|
+
* html5lib: https://code.google.com/p/html5lib
|
331
|
+
|
332
|
+
|
333
|
+
## Authors
|
334
|
+
|
335
|
+
* [Mike Dalessio](http://mike.daless.io) ([@flavorjones](https://twitter.com/flavorjones))
|
336
|
+
* Bryan Helmkamp
|
337
|
+
|
338
|
+
Featuring code contributed by:
|
339
|
+
|
340
|
+
* Aaron Patterson
|
341
|
+
* John Barnette
|
342
|
+
* Josh Owens
|
343
|
+
* Paul Dix
|
344
|
+
* Luke Melia
|
345
|
+
|
346
|
+
And a big shout-out to Corey Innis for the name, and feedback on the API.
|
347
|
+
|
348
|
+
|
349
|
+
## Thank You
|
350
|
+
|
351
|
+
The following people have generously funded Loofah:
|
352
|
+
|
353
|
+
* Bill Harding
|
354
|
+
|
355
|
+
|
356
|
+
## Historical Note
|
357
|
+
|
358
|
+
This library was formerly known as Dryopteris, which was a very bad
|
359
|
+
name that nobody could spell properly.
|
360
|
+
|
361
|
+
|
362
|
+
## License
|
363
|
+
|
364
|
+
Distributed under the MIT License. See `MIT-LICENSE.txt` for details.
|
data/SECURITY.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Security and Vulnerability Reporting
|
2
|
+
|
3
|
+
The Loofah core contributors take security very seriously and investigate all reported vulnerabilities.
|
4
|
+
|
5
|
+
If you would like to report a vulnerablity or have a security concern regarding Loofah, please [report it via HackerOne](https://hackerone.com/loofah/reports/new).
|
6
|
+
|
7
|
+
Your report will be acknowledged within 24 hours, and you'll receive a more detailed response within 72 hours indicating next steps in handling your report.
|
8
|
+
|
9
|
+
If you have not received a reply to your submission within 48 hours, there are a few steps you can take:
|
10
|
+
|
11
|
+
* Contact the current security coordinator (Mike Dalessio <mike.dalessio@gmail.com>)
|
12
|
+
* Email the Loofah user group at loofah-talk@googlegroups.com (archive at https://groups.google.com/forum/#!forum/loofah-talk)
|
13
|
+
|
14
|
+
Please note, the user group list is a public area. When escalating in that venue, please do not discuss your issue. Simply say that you're trying to get a hold of someone from the core team.
|
15
|
+
|
16
|
+
The information you share with the Loofah core contributors as part of this process will be kept confidential within the team, unless or until we need to share information upstream with our dependent libraries' core teams, at which point we will notify you.
|
17
|
+
|
18
|
+
If a vulnerability is first reported by you, we will credit you with the discovery in the public disclosure.
|
data/lib/loofah/elements.rb
CHANGED
@@ -1,19 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "set"
|
3
|
+
|
1
4
|
module Loofah
|
2
5
|
module Elements
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
STRICT_BLOCK_LEVEL_HTML4 = Set.new %w[
|
7
|
+
address
|
8
|
+
blockquote
|
9
|
+
center
|
10
|
+
dir
|
11
|
+
div
|
12
|
+
dl
|
13
|
+
fieldset
|
14
|
+
form
|
15
|
+
h1
|
16
|
+
h2
|
17
|
+
h3
|
18
|
+
h4
|
19
|
+
h5
|
20
|
+
h6
|
21
|
+
hr
|
22
|
+
isindex
|
23
|
+
menu
|
24
|
+
noframes
|
25
|
+
noscript
|
26
|
+
ol
|
27
|
+
p
|
28
|
+
pre
|
29
|
+
table
|
30
|
+
ul
|
31
|
+
]
|
32
|
+
|
33
|
+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
|
34
|
+
STRICT_BLOCK_LEVEL_HTML5 = Set.new %w[
|
35
|
+
address
|
36
|
+
article
|
37
|
+
aside
|
38
|
+
blockquote
|
39
|
+
canvas
|
40
|
+
dd
|
41
|
+
div
|
42
|
+
dl
|
43
|
+
dt
|
44
|
+
fieldset
|
45
|
+
figcaption
|
46
|
+
figure
|
47
|
+
footer
|
48
|
+
form
|
49
|
+
h1
|
50
|
+
h2
|
51
|
+
h3
|
52
|
+
h4
|
53
|
+
h5
|
54
|
+
h6
|
55
|
+
header
|
56
|
+
hgroup
|
57
|
+
hr
|
58
|
+
li
|
59
|
+
main
|
60
|
+
nav
|
61
|
+
noscript
|
62
|
+
ol
|
63
|
+
output
|
64
|
+
p
|
65
|
+
pre
|
66
|
+
section
|
67
|
+
table
|
68
|
+
tfoot
|
69
|
+
ul
|
70
|
+
video
|
71
|
+
]
|
72
|
+
|
73
|
+
# The following elements may also be considered block-level
|
74
|
+
# elements since they may contain block-level elements
|
75
|
+
LOOSE_BLOCK_LEVEL = Set.new %w[dd
|
76
|
+
dt
|
77
|
+
frameset
|
78
|
+
li
|
79
|
+
tbody
|
80
|
+
td
|
81
|
+
tfoot
|
82
|
+
th
|
83
|
+
thead
|
84
|
+
tr
|
85
|
+
]
|
7
86
|
|
8
|
-
#
|
9
|
-
|
87
|
+
# Elements that aren't block but should generate a newline in #to_text
|
88
|
+
INLINE_LINE_BREAK = Set.new(["br"])
|
10
89
|
|
90
|
+
STRICT_BLOCK_LEVEL = STRICT_BLOCK_LEVEL_HTML4 + STRICT_BLOCK_LEVEL_HTML5
|
11
91
|
BLOCK_LEVEL = STRICT_BLOCK_LEVEL + LOOSE_BLOCK_LEVEL
|
92
|
+
LINEBREAKERS = BLOCK_LEVEL + INLINE_LINE_BREAK
|
12
93
|
end
|
13
94
|
|
14
|
-
|
15
|
-
include Loofah::MetaHelpers::HashifiedConstants(Elements)
|
16
|
-
end
|
95
|
+
::Loofah::MetaHelpers.add_downcased_set_members_to_all_set_constants ::Loofah::Elements
|
17
96
|
end
|
18
|
-
|
19
|
-
|
data/lib/loofah/helpers.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Loofah
|
2
3
|
module Helpers
|
3
4
|
class << self
|
@@ -16,14 +17,87 @@ module Loofah
|
|
16
17
|
# Loofah::Helpers.sanitize("<script src=http://ha.ckers.org/xss.js></script>") # => "<script src=\"http://ha.ckers.org/xss.js\"></script>"
|
17
18
|
#
|
18
19
|
def sanitize(string_or_io)
|
19
|
-
Loofah.
|
20
|
+
loofah_fragment = Loofah.fragment(string_or_io)
|
21
|
+
loofah_fragment.scrub!(:strip)
|
22
|
+
loofah_fragment.xpath("./form").each { |form| form.remove }
|
23
|
+
loofah_fragment.to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# A replacement for Rails's built-in +sanitize_css+ helper.
|
28
|
+
#
|
29
|
+
# Loofah::Helpers.sanitize_css("display:block;background-image:url(http://www.ragingplatypus.com/i/cam-full.jpg)") # => "display: block;"
|
30
|
+
#
|
31
|
+
def sanitize_css(style_string)
|
32
|
+
::Loofah::HTML5::Scrub.scrub_css style_string
|
20
33
|
end
|
21
34
|
|
22
35
|
#
|
23
36
|
# A helper to remove extraneous whitespace from text-ified HTML
|
37
|
+
# TODO: remove this in a future major-point-release.
|
24
38
|
#
|
25
39
|
def remove_extraneous_whitespace(string)
|
26
|
-
string
|
40
|
+
Loofah.remove_extraneous_whitespace string
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module ActionView
|
45
|
+
module ClassMethods # :nodoc:
|
46
|
+
def full_sanitizer
|
47
|
+
@full_sanitizer ||= ::Loofah::Helpers::ActionView::FullSanitizer.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def safe_list_sanitizer
|
51
|
+
@safe_list_sanitizer ||= ::Loofah::Helpers::ActionView::SafeListSanitizer.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def white_list_sanitizer
|
55
|
+
warn "warning: white_list_sanitizer is deprecated, please use safe_list_sanitizer instead."
|
56
|
+
safe_list_sanitizer
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Replacement class for Rails's HTML::FullSanitizer.
|
62
|
+
#
|
63
|
+
# To use by default, call this in an application initializer:
|
64
|
+
#
|
65
|
+
# ActionView::Helpers::SanitizeHelper.full_sanitizer = ::Loofah::Helpers::ActionView::FullSanitizer.new
|
66
|
+
#
|
67
|
+
# Or, to generally opt-in to Loofah's view sanitizers:
|
68
|
+
#
|
69
|
+
# Loofah::Helpers::ActionView.set_as_default_sanitizer
|
70
|
+
#
|
71
|
+
class FullSanitizer
|
72
|
+
def sanitize(html, *args)
|
73
|
+
Loofah::Helpers.strip_tags html
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Replacement class for Rails's HTML::WhiteListSanitizer.
|
79
|
+
#
|
80
|
+
# To use by default, call this in an application initializer:
|
81
|
+
#
|
82
|
+
# ActionView::Helpers::SanitizeHelper.safe_list_sanitizer = ::Loofah::Helpers::ActionView::SafeListSanitizer.new
|
83
|
+
#
|
84
|
+
# Or, to generally opt-in to Loofah's view sanitizers:
|
85
|
+
#
|
86
|
+
# Loofah::Helpers::ActionView.set_as_default_sanitizer
|
87
|
+
#
|
88
|
+
class SafeListSanitizer
|
89
|
+
def sanitize(html, *args)
|
90
|
+
Loofah::Helpers.sanitize html
|
91
|
+
end
|
92
|
+
|
93
|
+
def sanitize_css(style_string, *args)
|
94
|
+
Loofah::Helpers.sanitize_css style_string
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
WhiteListSanitizer = SafeListSanitizer
|
99
|
+
if Object.respond_to?(:deprecate_constant)
|
100
|
+
deprecate_constant :WhiteListSanitizer
|
27
101
|
end
|
28
102
|
end
|
29
103
|
end
|
data/lib/loofah/html/document.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Loofah
|
2
3
|
module HTML # :nodoc:
|
3
4
|
#
|
@@ -14,8 +15,13 @@ module Loofah
|
|
14
15
|
# constructor. Applications should use Loofah.fragment to
|
15
16
|
# parse a fragment.
|
16
17
|
#
|
17
|
-
def parse
|
18
|
-
|
18
|
+
def parse(tags, encoding = nil)
|
19
|
+
doc = Loofah::HTML::Document.new
|
20
|
+
|
21
|
+
encoding ||= tags.respond_to?(:encoding) ? tags.encoding.name : "UTF-8"
|
22
|
+
doc.encoding = encoding
|
23
|
+
|
24
|
+
new(doc, tags)
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
@@ -25,6 +31,7 @@ module Loofah
|
|
25
31
|
def to_s
|
26
32
|
serialize_root.children.to_s
|
27
33
|
end
|
34
|
+
|
28
35
|
alias :serialize :to_s
|
29
36
|
|
30
37
|
def serialize_root
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
require "set"
|
4
|
+
|
5
|
+
module Loofah
|
6
|
+
#
|
7
|
+
# constants related to working around unhelpful libxml2 behavior
|
8
|
+
#
|
9
|
+
# ಠ_ಠ
|
10
|
+
#
|
11
|
+
module LibxmlWorkarounds
|
12
|
+
#
|
13
|
+
# these attributes and qualifying parent tags are determined by the code at:
|
14
|
+
#
|
15
|
+
# https://git.gnome.org/browse/libxml2/tree/HTMLtree.c?h=v2.9.2#n714
|
16
|
+
#
|
17
|
+
# see comments about CVE-2018-8048 within the tests for more information
|
18
|
+
#
|
19
|
+
BROKEN_ESCAPING_ATTRIBUTES = Set.new %w[
|
20
|
+
href
|
21
|
+
action
|
22
|
+
src
|
23
|
+
name
|
24
|
+
]
|
25
|
+
BROKEN_ESCAPING_ATTRIBUTES_QUALIFYING_TAG = { "name" => "a" }
|
26
|
+
end
|
27
|
+
end
|