loofah 2.3.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of loofah might be problematic. Click here for more details.

Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.md +336 -0
  4. data/Gemfile +22 -0
  5. data/MIT-LICENSE.txt +23 -0
  6. data/Manifest.txt +41 -0
  7. data/README.md +363 -0
  8. data/Rakefile +81 -0
  9. data/SECURITY.md +18 -0
  10. data/benchmark/benchmark.rb +149 -0
  11. data/benchmark/fragment.html +96 -0
  12. data/benchmark/helper.rb +73 -0
  13. data/benchmark/www.slashdot.com.html +2560 -0
  14. data/lib/loofah.rb +83 -0
  15. data/lib/loofah/elements.rb +92 -0
  16. data/lib/loofah/helpers.rb +103 -0
  17. data/lib/loofah/html/document.rb +18 -0
  18. data/lib/loofah/html/document_fragment.rb +40 -0
  19. data/lib/loofah/html5/libxml2_workarounds.rb +26 -0
  20. data/lib/loofah/html5/safelist.rb +796 -0
  21. data/lib/loofah/html5/scrub.rb +133 -0
  22. data/lib/loofah/instance_methods.rb +127 -0
  23. data/lib/loofah/metahelpers.rb +13 -0
  24. data/lib/loofah/scrubber.rb +133 -0
  25. data/lib/loofah/scrubbers.rb +297 -0
  26. data/lib/loofah/xml/document.rb +13 -0
  27. data/lib/loofah/xml/document_fragment.rb +23 -0
  28. data/test/assets/msword.html +63 -0
  29. data/test/assets/testdata_sanitizer_tests1.dat +502 -0
  30. data/test/helper.rb +18 -0
  31. data/test/html5/test_sanitizer.rb +401 -0
  32. data/test/html5/test_scrub.rb +10 -0
  33. data/test/integration/test_ad_hoc.rb +220 -0
  34. data/test/integration/test_helpers.rb +43 -0
  35. data/test/integration/test_html.rb +72 -0
  36. data/test/integration/test_scrubbers.rb +400 -0
  37. data/test/integration/test_xml.rb +55 -0
  38. data/test/unit/test_api.rb +142 -0
  39. data/test/unit/test_encoding.rb +20 -0
  40. data/test/unit/test_helpers.rb +62 -0
  41. data/test/unit/test_scrubber.rb +229 -0
  42. data/test/unit/test_scrubbers.rb +14 -0
  43. metadata +287 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1196afab25d29644d1961e4516ac317a2c38dee3295f35354c468e6a9318fa55
4
+ data.tar.gz: 2e07ff641edb37d2b0dce2933288da4667d4b680a586912af9c171db7dfb0a63
5
+ SHA512:
6
+ metadata.gz: 37ac2cdb0d136da417cff62e3845c5b71769f044d8150c636a549dc9ca4cf98bcef4c6d2b6e653eff56922b95d812ed39310a406c49366c14791456ca905e8fe
7
+ data.tar.gz: 0fa3cdd75a3d2950801a1cfe7f8d4cad6bb73bbec67d24ba25980c09a565f6c95c5d664c1789ccd62486d1917c685a5b0f762cc073a054bbb0f02fb0222688f0
File without changes
@@ -0,0 +1,336 @@
1
+ # Changelog
2
+
3
+ ## 2.3.1 / 2019-10-22
4
+
5
+ ### Security
6
+
7
+ Address CVE-2019-15587: Unsanitized JavaScript may occur in sanitized output when a crafted SVG element is republished.
8
+
9
+ This CVE's public notice is at https://github.com/flavorjones/loofah/issues/171
10
+
11
+
12
+ ## 2.3.0 / 2019-09-28
13
+
14
+ ### Features
15
+
16
+ * Expand set of allowed protocols to include `tel:` and `line:`. [#104, #147]
17
+ * Expand set of allowed CSS functions. [related to #122]
18
+ * Allow greater precision in shorthand CSS values. [#149] (Thanks, @danfstucky!)
19
+ * Allow CSS property `list-style` [#162] (Thanks, @jaredbeck!)
20
+ * Allow CSS keywords `thick` and `thin` [#168] (Thanks, @georgeclaghorn!)
21
+ * Allow HTML property `contenteditable` [#167] (Thanks, @andreynering!)
22
+
23
+
24
+ ### Bug fixes
25
+
26
+ * CSS hex values are no longer limited to lowercase hex. Previously uppercase hex were scrubbed. [#165] (Thanks, @asok!)
27
+
28
+
29
+ ### Deprecations / Name Changes
30
+
31
+ The following method and constants are hereby deprecated, and will be completely removed in a future release:
32
+
33
+ * Deprecate `Loofah::Helpers::ActionView.white_list_sanitizer`, please use `Loofah::Helpers::ActionView.safe_list_sanitizer` instead.
34
+ * Deprecate `Loofah::Helpers::ActionView::WhiteListSanitizer`, please use `Loofah::Helpers::ActionView::SafeListSanitizer` instead.
35
+ * Deprecate `Loofah::HTML5::WhiteList`, please use `Loofah::HTML5::SafeList` instead.
36
+
37
+ Thanks to @JuanitoFatas for submitting these changes in #164 and for making the language used in Loofah more inclusive.
38
+
39
+
40
+ ## 2.2.3 / 2018-10-30
41
+
42
+ ### Security
43
+
44
+ Address CVE-2018-16468: Unsanitized JavaScript may occur in sanitized output when a crafted SVG element is republished.
45
+
46
+ This CVE's public notice is at https://github.com/flavorjones/loofah/issues/154
47
+
48
+
49
+ ## Meta / 2018-10-27
50
+
51
+ The mailing list is now on Google Groups [#146](https://github.com/flavorjones/loofah/issues/146):
52
+
53
+ * Mail: loofah-talk@googlegroups.com
54
+ * Archive: https://groups.google.com/forum/#!forum/loofah-talk
55
+
56
+ This change was made because librelist no longer appears to be maintained.
57
+
58
+
59
+ ## 2.2.2 / 2018-03-22
60
+
61
+ Make public `Loofah::HTML5::Scrub.force_correct_attribute_escaping!`,
62
+ which was previously a private method. This is so that downstream gems
63
+ (like rails-html-sanitizer) can use this logic directly for their own
64
+ attribute scrubbers should they need to address CVE-2018-8048.
65
+
66
+
67
+ ## 2.2.1 / 2018-03-19
68
+
69
+ ### Security
70
+
71
+ Addresses CVE-2018-8048. Loofah allowed non-whitelisted attributes to be present in sanitized output when input with specially-crafted HTML fragments.
72
+
73
+ This CVE's public notice is at https://github.com/flavorjones/loofah/issues/144
74
+
75
+
76
+ ## 2.2.0 / 2018-02-11
77
+
78
+ ### Features:
79
+
80
+ * Support HTML5 `<main>` tag. #133 (Thanks, @MothOnMars!)
81
+ * Recognize HTML5 block elements. #136 (Thanks, @MothOnMars!)
82
+ * Support SVG `<symbol>` tag. #131 (Thanks, @baopham!)
83
+ * Support for whitelisting CSS functions, initially just `calc` and `rgb`. #122/#123/#129 (Thanks, @NikoRoberts!)
84
+ * Whitelist CSS property `list-style-type`. #68/#137/#142 (Thanks, @andela-ysanni and @NikoRoberts!)
85
+
86
+ ### Bugfixes:
87
+
88
+ * Properly handle nested `script` tags. #127.
89
+
90
+
91
+ ## 2.1.1 / 2017-09-24
92
+
93
+ ### Bugfixes:
94
+
95
+ * Removed warning for unused variable. #124 (Thanks, @y-yagi!)
96
+
97
+
98
+ ## 2.1.0 / 2017-09-24
99
+
100
+ ### Notes:
101
+
102
+ * Re-implemented CSS parsing and sanitization using the [crass](https://github.com/rgrove/crass) library. #91
103
+
104
+
105
+ ### Features:
106
+
107
+ * Added :noopener HTML scrubber (Thanks, @tastycode!)
108
+ * Support `data` URIs with the following media types: text/plain, text/css, image/png, image/gif, image/jpeg, image/svg+xml. #101, #120. (Thanks, @mrpasquini!)
109
+
110
+
111
+ ### Bugfixes:
112
+
113
+ * The :unprintable scrubber now scrubs unprintable characters in CDATA nodes (like `<script>`). #124
114
+ * Allow negative values in CSS properties. Restores functionality that was reverted in v2.0.3. #91
115
+
116
+
117
+ ## 2.0.3 / 2015-08-17
118
+
119
+ ### Bug fixes:
120
+
121
+ * Revert support for negative values in CSS properties due to slow performance. #90 (Related to #85.)
122
+
123
+
124
+ ## 2.0.2 / 2015-05-05
125
+
126
+ ### Bug fixes:
127
+
128
+ * Fix error with `#to_text` when Loofah::Helpers hadn't been required. #75
129
+ * Allow multi-word data attributes. #84 (Thanks, @jstorimer!)
130
+ * Allow negative values in CSS properties. #85 (Thanks, @siddhartham!)
131
+
132
+
133
+ ## 2.0.1 / 2014-08-21
134
+
135
+ ### Bug fixes:
136
+
137
+ * Load RR correctly when running test files directly. (Thanks, @ktdreyer!)
138
+
139
+
140
+ ### Notes:
141
+
142
+ * Extracted HTML5::Scrub#scrub_css_attribute to accommodate the Rails integration work. (Thanks, @kaspth!)
143
+
144
+
145
+ ## 2.0.0 / 2014-05-09
146
+
147
+ ### Compatibility notes:
148
+
149
+ * ActionView helpers now must be required explicitly: `require "loofah/helpers"`
150
+ * Support for Ruby 1.8.7 and prior has been dropped
151
+
152
+ ### Enhancements:
153
+
154
+ * HTML5 whitelist allows the following ...
155
+ * tags: `article`, `aside`, `bdi`, `bdo`, `canvas`, `command`, `datalist`, `details`, `figcaption`, `figure`, `footer`, `header`, `mark`, `meter`, `nav`, `output`, `section`, `summary`, `time`
156
+ * attributes: `data-*` (Thanks, Rafael Franca!)
157
+ * URI attributes: `poster` and `preload`
158
+ * Addition of the `:unprintable` scrubber to remove unprintable characters from text nodes. #65 (Thanks, Matt Swanson!)
159
+ * `Loofah.fragment` accepts an optional encoding argument, compatible with `Nokogiri::HTML::DocumentFragment.parse`. #62 (Thanks, Ben Atkins!)
160
+ * HTML5 sanitizers now remove attributes without values. (Thanks, Kasper Timm Hansen!)
161
+
162
+ ### Bug fixes:
163
+
164
+ * HTML5 sanitizers' CSS keyword check now actually works (broken in v2.0). Additional regression tests added. (Thanks, Kasper Timm Hansen!)
165
+ * HTML5 sanitizers now allow negative arguments to CSS. #64 (Thanks, Jon Calhoun!)
166
+
167
+
168
+ ## 1.2.1 (2012-04-14)
169
+
170
+ * Declaring encoding in html5/scrub.rb. Without this, use of the ruby -KU option would cause havoc. (#32)
171
+
172
+
173
+ ## 1.2.0 (2011-08-08)
174
+
175
+ ### Enhancements:
176
+
177
+ * Loofah::Helpers.sanitize_css is a replacement for Rails's built-in sanitize_css helper.
178
+ * Improving ActionView integration.
179
+
180
+
181
+ ## 1.1.0 (2011-08-08)
182
+
183
+ ### Enhancements:
184
+
185
+ * Additional HTML5lib whitelist elements (from html5lib 1524:80b5efe26230).
186
+ Up to date with HTML5lib ruby code as of 1723:7ee6a0331856.
187
+ * Whitelists (which are not part of the public API) are now Sets (were previously Arrays).
188
+ * Don't explode when encountering UTF-8 URIs. (#25, #29)
189
+
190
+
191
+ ## 1.0.0 (2010-10-26)
192
+
193
+ ### Notes:
194
+
195
+ * Moved ActiveRecord functionality into `loofah-activerecord` gem.
196
+ * Removed DEPRECATIONS.rdoc documenting 0.3.0 API changes.
197
+
198
+
199
+ ## 0.4.7 (2010-03-09)
200
+
201
+ ### Enhancements:
202
+
203
+ * New methods Loofah::HTML::Document#to_text and
204
+ Loofah::HTML::DocumentFragment#to_text do the right thing with
205
+ whitespace. Note that these methods are significantly slower than
206
+ #text. GH #12
207
+ * Loofah::Elements::BLOCK_LEVEL contains a canonical list of HTML4 block-level4 elements.
208
+ * Loofah::HTML::Document#text and Loofah::HTML::DocumentFragment#text
209
+ will return unescaped HTML entities by passing :encode_special_chars => false.
210
+
211
+
212
+ ## 0.4.4, 0.4.5, 0.4.6 (2010-02-01)
213
+
214
+ ### Enhancements:
215
+
216
+ * Loofah::HTML::Document#text and Loofah::HTML::DocumentFragment#text now escape HTML entities.
217
+
218
+ ### Bug fixes:
219
+
220
+ * Loofah::XssFoliate was not properly escaping HTML entities when implicitly scrubbing a string attribute. GH #17
221
+
222
+
223
+ ## 0.4.3 (2010-01-29)
224
+
225
+ ### Enhancements:
226
+
227
+ * All built-in scrubbers are accepted by ActiveRecord::Base.xss_foliate
228
+ * Loofah::XssFoliate.xss_foliate_all_models replaces use of the constant LOOFAH_XSS_FOLIATE_ALL_MODELS
229
+
230
+ ### Miscellaneous:
231
+
232
+ * Modified documentation for bootstrapping XssFoliate in a Rails app,
233
+ since the use of Bundler breaks the previously-documented method. To
234
+ be safe, always use an initializer file.
235
+
236
+
237
+ ## 0.4.2 (2010-01-22)
238
+
239
+ ### Enhancements:
240
+
241
+ * Implemented Node#scrub! for scrubbing subtrees.
242
+ * Implemented NodeSet#scrub! for scrubbing a set of subtrees.
243
+ * Document.text now only serializes <body> contents (ignores <head>)
244
+ * <head>, <html> and <body> added to the HTML5lib whitelist.
245
+
246
+ ### Bug fixes:
247
+
248
+ * Supporting Rails apps that aren't loading ActiveRecord. GH #10
249
+
250
+ ### Miscellaneous:
251
+
252
+ * Mailing list is now loofah@librelist.com / http://librelist.com
253
+ * IRC channel is now \#loofah on freenode.
254
+
255
+
256
+ ## 0.4.1 (2009-11-23)
257
+
258
+ ### Bugfix:
259
+
260
+ * Manifest fixed. Whoops.
261
+
262
+
263
+ ## 0.4.0 (2009-11-21)
264
+
265
+ ### Enhancements:
266
+
267
+ * Scrubber class introduced, allowing development of custom scrubbers.
268
+ * Added support for XML documents and fragments.
269
+ * Added :nofollow HTML scrubber (thanks Luke Melia!)
270
+ * Built-in scrubbing methods refactored to use Scrubber.
271
+
272
+
273
+
274
+ ## 0.3.1 (2009-10-12)
275
+
276
+ ### Bug fixes:
277
+
278
+ * Scrubbed Documents properly render html, head and body tags when serialized.
279
+
280
+
281
+ ## 0.3.0 (2009-10-06)
282
+
283
+ ### Enhancements:
284
+
285
+ * New ActiveRecord extension `xss_foliate`, a drop-in replacement for xss_terminate[http://github.com/look/xss_terminate/tree/master].
286
+ * Replacement methods for Rails's helpers, Loofah::Rails.sanitize and Loofah::Rails.strip_tags.
287
+ * Official support (and test coverage) for Rails versions 2.3, 2.2, 2.1, 2.0 and 1.2.
288
+
289
+ ### Deprecations:
290
+
291
+ * The methods strip_tags, whitewash, whitewash_document, sanitize, and
292
+ sanitize_document have been deprecated. See DEPRECATED.rdoc for
293
+ details on the equivalent calls with the post-0.2 API.
294
+
295
+
296
+ ## 0.2.2 (2009-09-30)
297
+
298
+ ### Enhancements:
299
+
300
+ * ActiveRecord extension scrubs fields in a before_validation callback
301
+ (was previously in a before_save)
302
+
303
+
304
+ ## 0.2.1 (2009-09-19)
305
+
306
+ ### Enhancements:
307
+
308
+ * when loaded in a Rails app, automatically extend ActiveRecord::Base
309
+ with html_fragment and html_document. GH #6 (Thanks Josh Nichols!)
310
+
311
+ ### Bugfixes:
312
+
313
+ * ActiveRecord scrubbing should generate strings instead of Document or
314
+ DocumentFragment objects. GH #5
315
+ * init.rb fixed to support installation as a Rails plugin. GH #6
316
+ (Thanks Josh Nichols!)
317
+
318
+
319
+ ## 0.2.0 (2009-09-11)
320
+
321
+ * Swank new API.
322
+ * ActiveRecord extension.
323
+ * Uses Nokogiri's Document and DocumentFragment for parsing.
324
+ * Updated html5lib codes and tests to revision 1384:b9d3153d7be7.
325
+ * Deprecated the Dryopteris sanitization methods. Will be removed in 0.3.0.
326
+ * Documentation! Hey!
327
+
328
+
329
+ ## 0.1.2 (2009-04-30)
330
+
331
+ * Added whitewashing -- removal of all attributes and namespaced nodes. You know, for microsofty HTML.
332
+
333
+
334
+ ## 0.1.0 (2009-02-10)
335
+
336
+ * Birthday!
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ # -*- ruby -*-
2
+
3
+ # DO NOT EDIT THIS FILE. Instead, edit Rakefile, and run `rake bundler:gemfile`.
4
+
5
+ source "https://rubygems.org/"
6
+
7
+ gem "nokogiri", ">=1.5.9"
8
+ gem "crass", "~>1.0.2"
9
+
10
+ gem "rake", ">=0.8", :group => [:development, :test]
11
+ gem "minitest", "~>2.2", :group => [:development, :test]
12
+ gem "rr", "~>1.2.0", :group => [:development, :test]
13
+ gem "json", ">=0", :group => [:development, :test]
14
+ gem "hoe-gemspec", ">=0", :group => [:development, :test]
15
+ gem "hoe-debugging", ">=0", :group => [:development, :test]
16
+ gem "hoe-bundler", ">=0", :group => [:development, :test]
17
+ gem "hoe-git", ">=0", :group => [:development, :test]
18
+ gem "concourse", ">=0.26.0", :group => [:development, :test]
19
+ gem "rdoc", ">=4.0", "<7", :group => [:development, :test]
20
+ gem "hoe", "~>3.17", :group => [:development, :test]
21
+
22
+ # vim: syntax=ruby
@@ -0,0 +1,23 @@
1
+ The MIT License
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2009 -- 2018 by Mike Dalessio, Bryan Helmkamp
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ .gemtest
2
+ CHANGELOG.md
3
+ Gemfile
4
+ MIT-LICENSE.txt
5
+ Manifest.txt
6
+ README.md
7
+ Rakefile
8
+ SECURITY.md
9
+ benchmark/benchmark.rb
10
+ benchmark/fragment.html
11
+ benchmark/helper.rb
12
+ benchmark/www.slashdot.com.html
13
+ lib/loofah.rb
14
+ lib/loofah/elements.rb
15
+ lib/loofah/helpers.rb
16
+ lib/loofah/html/document.rb
17
+ lib/loofah/html/document_fragment.rb
18
+ lib/loofah/html5/libxml2_workarounds.rb
19
+ lib/loofah/html5/safelist.rb
20
+ lib/loofah/html5/scrub.rb
21
+ lib/loofah/instance_methods.rb
22
+ lib/loofah/metahelpers.rb
23
+ lib/loofah/scrubber.rb
24
+ lib/loofah/scrubbers.rb
25
+ lib/loofah/xml/document.rb
26
+ lib/loofah/xml/document_fragment.rb
27
+ test/assets/msword.html
28
+ test/assets/testdata_sanitizer_tests1.dat
29
+ test/helper.rb
30
+ test/html5/test_sanitizer.rb
31
+ test/html5/test_scrub.rb
32
+ test/integration/test_ad_hoc.rb
33
+ test/integration/test_helpers.rb
34
+ test/integration/test_html.rb
35
+ test/integration/test_scrubbers.rb
36
+ test/integration/test_xml.rb
37
+ test/unit/test_api.rb
38
+ test/unit/test_encoding.rb
39
+ test/unit/test_helpers.rb
40
+ test/unit/test_scrubber.rb
41
+ test/unit/test_scrubbers.rb
@@ -0,0 +1,363 @@
1
+ # Loofah
2
+
3
+ * https://github.com/flavorjones/loofah
4
+ * Docs: http://rubydoc.info/github/flavorjones/loofah/master/frames
5
+ * Mailing list: [loofah-talk@googlegroups.com](https://groups.google.com/forum/#!forum/loofah-talk)
6
+
7
+ ## Status
8
+
9
+ |System|Status|
10
+ |--|--|
11
+ | Concourse CI | [![Concourse CI](https://ci.nokogiri.org/api/v1/teams/nokogiri-core/pipelines/loofah/jobs/ruby-2.5/badge)](https://ci.nokogiri.org/teams/nokogiri-core/pipelines/loofah?groups=master) |
12
+ | Code Climate | [![Code Climate](https://codeclimate.com/github/flavorjones/loofah.svg)](https://codeclimate.com/github/flavorjones/loofah) |
13
+
14
+
15
+ ## Description
16
+
17
+ Loofah is a general library for manipulating and transforming HTML/XML documents and fragments, built on top of Nokogiri.
18
+
19
+ 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.)
20
+
21
+ ActiveRecord extensions for sanitization are available in the [`loofah-activerecord` gem](https://github.com/flavorjones/loofah-activerecord).
22
+
23
+
24
+ ## Features
25
+
26
+ * Easily write custom scrubbers for HTML/XML leveraging the sweetness of Nokogiri (and HTML5lib's safelists).
27
+ * Common HTML sanitizing tasks are built-in:
28
+ * _Strip_ unsafe tags, leaving behind only the inner text.
29
+ * _Prune_ unsafe tags and their subtrees, removing all traces that they ever existed.
30
+ * _Escape_ unsafe tags and their subtrees, leaving behind lots of <tt>&lt;</tt> and <tt>&gt;</tt> entities.
31
+ * _Whitewash_ the markup, removing all attributes and namespaced nodes.
32
+ * Common HTML transformation tasks are built-in:
33
+ * Add the _nofollow_ attribute to all hyperlinks.
34
+ * Format markup as plain text, with or without sensible whitespace handling around block elements.
35
+ * Replace Rails's `strip_tags` and `sanitize` view helper methods.
36
+
37
+
38
+ ## Compare and Contrast
39
+
40
+ Loofah is one of two known Ruby XSS/sanitization solutions that
41
+ guarantees well-formed and valid markup (the other is Sanitize, which
42
+ also uses Nokogiri).
43
+
44
+ Loofah works on XML, XHTML and HTML documents.
45
+
46
+ Also, it's pretty fast. Here is a benchmark comparing Loofah to other
47
+ commonly-used libraries (ActionView, Sanitize, HTML5lib and HTMLfilter):
48
+
49
+ * https://gist.github.com/170193
50
+
51
+ Lastly, Loofah is extensible. It's super-easy to write your own custom
52
+ scrubbers for whatever document manipulation you need. You don't like
53
+ the built-in scrubbers? Build your own, like a boss.
54
+
55
+
56
+ ## The Basics
57
+
58
+ Loofah wraps [Nokogiri](http://nokogiri.org) in a loving
59
+ embrace. Nokogiri is an excellent HTML/XML parser. If you don't know
60
+ how Nokogiri works, you might want to pause for a moment and go check
61
+ it out. I'll wait.
62
+
63
+ Loofah presents the following classes:
64
+
65
+ * `Loofah::HTML::Document` and `Loofah::HTML::DocumentFragment`
66
+ * `Loofah::XML::Document` and `Loofah::XML::DocumentFragment`
67
+ * `Loofah::Scrubber`
68
+
69
+ The documents and fragments are subclasses of the similar Nokogiri classes.
70
+
71
+ The Scrubber represents the document manipulation, either by wrapping
72
+ a block,
73
+
74
+ ``` ruby
75
+ span2div = Loofah::Scrubber.new do |node|
76
+ node.name = "div" if node.name == "span"
77
+ end
78
+ ```
79
+
80
+ or by implementing a method.
81
+
82
+
83
+ ### Side Note: Fragments vs Documents
84
+
85
+ Generally speaking, unless you expect to have a DOCTYPE and a single
86
+ root node, you don't have a *document*, you have a *fragment*. For
87
+ HTML, another rule of thumb is that *documents* have `html` and `body`
88
+ tags, and *fragments* usually do not.
89
+
90
+ HTML fragments should be parsed with Loofah.fragment. The result won't
91
+ be wrapped in `html` or `body` tags, won't have a DOCTYPE declaration,
92
+ `head` elements will be silently ignored, and multiple root nodes are
93
+ allowed.
94
+
95
+ XML fragments should be parsed with Loofah.xml_fragment. The result
96
+ won't have a DOCTYPE declaration, and multiple root nodes are allowed.
97
+
98
+ HTML documents should be parsed with Loofah.document. The result will
99
+ have a DOCTYPE declaration, along with `html`, `head` and `body` tags.
100
+
101
+ XML documents should be parsed with Loofah.xml_document. The result
102
+ will have a DOCTYPE declaration and a single root node.
103
+
104
+
105
+ ### Loofah::HTML::Document and Loofah::HTML::DocumentFragment
106
+
107
+ These classes are subclasses of Nokogiri::HTML::Document and
108
+ Nokogiri::HTML::DocumentFragment, so you get all the markup
109
+ fixer-uppery and API goodness of Nokogiri.
110
+
111
+ The module methods Loofah.document and Loofah.fragment will parse an
112
+ HTML document and an HTML fragment, respectively.
113
+
114
+ ``` ruby
115
+ Loofah.document(unsafe_html).is_a?(Nokogiri::HTML::Document) # => true
116
+ Loofah.fragment(unsafe_html).is_a?(Nokogiri::HTML::DocumentFragment) # => true
117
+ ```
118
+
119
+ Loofah injects a `scrub!` method, which takes either a symbol (for
120
+ built-in scrubbers) or a Loofah::Scrubber object (for custom
121
+ scrubbers), and modifies the document in-place.
122
+
123
+ Loofah overrides `to_s` to return HTML:
124
+
125
+ ``` ruby
126
+ unsafe_html = "ohai! <div>div is safe</div> <script>but script is not</script>"
127
+
128
+ doc = Loofah.fragment(unsafe_html).scrub!(:prune)
129
+ doc.to_s # => "ohai! <div>div is safe</div> "
130
+ ```
131
+
132
+ and `text` to return plain text:
133
+
134
+ ``` ruby
135
+ doc.text # => "ohai! div is safe "
136
+ ```
137
+
138
+ Also, `to_text` is available, which does the right thing with
139
+ whitespace around block-level elements.
140
+
141
+ ``` ruby
142
+ doc = Loofah.fragment("<h1>Title</h1><div>Content</div>")
143
+ doc.text # => "TitleContent" # probably not what you want
144
+ doc.to_text # => "\nTitle\n\nContent\n" # better
145
+ ```
146
+
147
+ ### Loofah::XML::Document and Loofah::XML::DocumentFragment
148
+
149
+ These classes are subclasses of Nokogiri::XML::Document and
150
+ Nokogiri::XML::DocumentFragment, so you get all the markup
151
+ fixer-uppery and API goodness of Nokogiri.
152
+
153
+ The module methods Loofah.xml_document and Loofah.xml_fragment will
154
+ parse an XML document and an XML fragment, respectively.
155
+
156
+ ``` ruby
157
+ Loofah.xml_document(bad_xml).is_a?(Nokogiri::XML::Document) # => true
158
+ Loofah.xml_fragment(bad_xml).is_a?(Nokogiri::XML::DocumentFragment) # => true
159
+ ```
160
+
161
+ ### Nodes and NodeSets
162
+
163
+ Nokogiri::XML::Node and Nokogiri::XML::NodeSet also get a `scrub!`
164
+ method, which makes it easy to scrub subtrees.
165
+
166
+ The following code will apply the `employee_scrubber` only to the
167
+ `employee` nodes (and their subtrees) in the document:
168
+
169
+ ``` ruby
170
+ Loofah.xml_document(bad_xml).xpath("//employee").scrub!(employee_scrubber)
171
+ ```
172
+
173
+ And this code will only scrub the first `employee` node and its subtree:
174
+
175
+ ``` ruby
176
+ Loofah.xml_document(bad_xml).at_xpath("//employee").scrub!(employee_scrubber)
177
+ ```
178
+
179
+ ### Loofah::Scrubber
180
+
181
+ A Scrubber wraps up a block (or method) that is run on a document node:
182
+
183
+ ``` ruby
184
+ # change all <span> tags to <div> tags
185
+ span2div = Loofah::Scrubber.new do |node|
186
+ node.name = "div" if node.name == "span"
187
+ end
188
+ ```
189
+
190
+ This can then be run on a document:
191
+
192
+ ``` ruby
193
+ Loofah.fragment("<span>foo</span><p>bar</p>").scrub!(span2div).to_s
194
+ # => "<div>foo</div><p>bar</p>"
195
+ ```
196
+
197
+ Scrubbers can be run on a document in either a top-down traversal (the
198
+ default) or bottom-up. Top-down scrubbers can optionally return
199
+ Scrubber::STOP to terminate the traversal of a subtree. Read below and
200
+ in the Loofah::Scrubber class for more detailed usage.
201
+
202
+ Here's an XML example:
203
+
204
+ ``` ruby
205
+ # remove all <employee> tags that have a "deceased" attribute set to true
206
+ bring_out_your_dead = Loofah::Scrubber.new do |node|
207
+ if node.name == "employee" and node["deceased"] == "true"
208
+ node.remove
209
+ Loofah::Scrubber::STOP # don't bother with the rest of the subtree
210
+ end
211
+ end
212
+ Loofah.xml_document(File.read('plague.xml')).scrub!(bring_out_your_dead)
213
+ ```
214
+
215
+ === Built-In HTML Scrubbers
216
+
217
+ Loofah comes with a set of sanitizing scrubbers that use HTML5lib's
218
+ safelist algorithm:
219
+
220
+ ``` ruby
221
+ doc.scrub!(:strip) # replaces unknown/unsafe tags with their inner text
222
+ doc.scrub!(:prune) # removes unknown/unsafe tags and their children
223
+ doc.scrub!(:escape) # escapes unknown/unsafe tags, like this: &lt;script&gt;
224
+ doc.scrub!(:whitewash) # removes unknown/unsafe/namespaced tags and their children,
225
+ # and strips all node attributes
226
+ ```
227
+
228
+ Loofah also comes with some common transformation tasks:
229
+
230
+ ``` ruby
231
+ doc.scrub!(:nofollow) # adds rel="nofollow" attribute to links
232
+ doc.scrub!(:unprintable) # removes unprintable characters from text nodes
233
+ ```
234
+
235
+ See Loofah::Scrubbers for more details and example usage.
236
+
237
+
238
+ ### Chaining Scrubbers
239
+
240
+ You can chain scrubbers:
241
+
242
+ ``` ruby
243
+ Loofah.fragment("<span>hello</span> <script>alert('OHAI')</script>") \
244
+ .scrub!(:prune) \
245
+ .scrub!(span2div).to_s
246
+ # => "<div>hello</div> "
247
+ ```
248
+
249
+ ### Shorthand
250
+
251
+ The class methods Loofah.scrub_fragment and Loofah.scrub_document are
252
+ shorthand.
253
+
254
+ ``` ruby
255
+ Loofah.scrub_fragment(unsafe_html, :prune)
256
+ Loofah.scrub_document(unsafe_html, :prune)
257
+ Loofah.scrub_xml_fragment(bad_xml, custom_scrubber)
258
+ Loofah.scrub_xml_document(bad_xml, custom_scrubber)
259
+ ```
260
+
261
+ are the same thing as (and arguably semantically clearer than):
262
+
263
+ ``` ruby
264
+ Loofah.fragment(unsafe_html).scrub!(:prune)
265
+ Loofah.document(unsafe_html).scrub!(:prune)
266
+ Loofah.xml_fragment(bad_xml).scrub!(custom_scrubber)
267
+ Loofah.xml_document(bad_xml).scrub!(custom_scrubber)
268
+ ```
269
+
270
+
271
+ ### View Helpers
272
+
273
+ Loofah has two "view helpers": Loofah::Helpers.sanitize and
274
+ Loofah::Helpers.strip_tags, both of which are drop-in replacements for
275
+ the Rails ActionView helpers of the same name.
276
+ These are no longer required automatically. You must require `loofah/helpers`.
277
+
278
+
279
+ ## Requirements
280
+
281
+ * Nokogiri >= 1.5.9
282
+
283
+
284
+ ## Installation
285
+
286
+ Unsurprisingly:
287
+
288
+ * gem install loofah
289
+
290
+
291
+ ## Support
292
+
293
+ The bug tracker is available here:
294
+
295
+ * https://github.com/flavorjones/loofah/issues
296
+
297
+ And the mailing list is on Google Groups:
298
+
299
+ * Mail: loofah-talk@googlegroups.com
300
+ * Archive: https://groups.google.com/forum/#!forum/loofah-talk
301
+
302
+ And the IRC channel is \#loofah on freenode.
303
+
304
+
305
+ ## Security
306
+
307
+ See [`SECURITY.md`](SECURITY.md) for vulnerability reporting details.
308
+
309
+
310
+ ### "Secure by Default"
311
+
312
+ Some tools may incorrectly report Loofah as a potential security
313
+ vulnerability.
314
+
315
+ Loofah depends on Nokogiri, and it's _possible_ to use Nokogiri in a
316
+ dangerous way (by enabling its DTDLOAD option and disabling its NONET
317
+ option). This specifically allows the opportunity for an XML External
318
+ Entity (XXE) vulnerability if the XML data is untrusted.
319
+
320
+ However, Loofah __never enables this Nokogiri configuration__; Loofah
321
+ never enables DTDLOAD, and it never disables NONET, thereby protecting
322
+ you by default from this XXE vulnerability.
323
+
324
+
325
+ ## Related Links
326
+
327
+ * Nokogiri: http://nokogiri.org
328
+ * libxml2: http://xmlsoft.org
329
+ * html5lib: https://code.google.com/p/html5lib
330
+
331
+
332
+ ## Authors
333
+
334
+ * [Mike Dalessio](http://mike.daless.io) ([@flavorjones](https://twitter.com/flavorjones))
335
+ * Bryan Helmkamp
336
+
337
+ Featuring code contributed by:
338
+
339
+ * Aaron Patterson
340
+ * John Barnette
341
+ * Josh Owens
342
+ * Paul Dix
343
+ * Luke Melia
344
+
345
+ And a big shout-out to Corey Innis for the name, and feedback on the API.
346
+
347
+
348
+ ## Thank You
349
+
350
+ The following people have generously donated via the [Pledgie](http://pledgie.com) badge on the [Loofah github page](https://github.com/flavorjones/loofah):
351
+
352
+ * Bill Harding
353
+
354
+
355
+ ## Historical Note
356
+
357
+ This library was formerly known as Dryopteris, which was a very bad
358
+ name that nobody could spell properly.
359
+
360
+
361
+ ## License
362
+
363
+ Distributed under the MIT License. See `MIT-LICENSE.txt` for details.