loofah 2.19.1 → 2.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +102 -0
- data/README.md +161 -115
- data/lib/loofah/concerns.rb +207 -0
- data/lib/loofah/elements.rb +78 -76
- data/lib/loofah/helpers.rb +21 -15
- data/lib/loofah/{html → html4}/document.rb +5 -7
- data/lib/loofah/html4/document_fragment.rb +15 -0
- data/lib/loofah/html5/document.rb +17 -0
- data/lib/loofah/html5/document_fragment.rb +15 -0
- data/lib/loofah/html5/libxml2_workarounds.rb +7 -6
- data/lib/loofah/html5/safelist.rb +940 -924
- data/lib/loofah/html5/scrub.rb +36 -35
- data/lib/loofah/metahelpers.rb +10 -6
- data/lib/loofah/scrubber.rb +10 -8
- data/lib/loofah/scrubbers.rb +174 -43
- data/lib/loofah/version.rb +2 -1
- data/lib/loofah/xml/document.rb +1 -0
- data/lib/loofah/xml/document_fragment.rb +2 -6
- data/lib/loofah.rb +116 -43
- metadata +18 -122
- data/lib/loofah/html/document_fragment.rb +0 -42
- data/lib/loofah/instance_methods.rb +0 -133
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e003942bbd3be7a5d576d4b48002964664c844b3aa66aa24a6b55e536ecd0662
|
4
|
+
data.tar.gz: f959fadbc762a26167cc46563d92e8be0d085372467b774dcbb3f18dabc6d8c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 254caef30657885b063ae85c0ac01ba386aa387571a7f5587dfee57f4e803047f56579d11425bb01a6aa50209b3b1755d7e2c613412f4783f3ce5e6d4fd70b70
|
7
|
+
data.tar.gz: 4a531c380c0ce45a9c3e9ea1a87c08da487331bf56db20e6bf42b903ca1e1f3e043f72ddfc1ad82076fffcad6bd08055131952b62c40f0b6067fcac3a552cfd7
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,107 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 2.24.0 / 2024-12-24
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
* Built-in scrubber `:double_breakpoint` which sees `<br><br>` and wraps the surrounding content in `<p>` tags. #279, #284 @josecolella @torihuang
|
8
|
+
|
9
|
+
### Improved
|
10
|
+
|
11
|
+
* Built-in scrubber `:targetblank` now skips `a` tags whose `href` attribute is an anchor link. Previously, all `a` tags were modified to have `target='_blank'`. #291 @fnando
|
12
|
+
|
13
|
+
|
14
|
+
## 2.23.1 / 2024-10-25
|
15
|
+
|
16
|
+
### Added
|
17
|
+
|
18
|
+
* Allow CSS properties `min-height` and `max-height`. [#288] @lazyatom
|
19
|
+
|
20
|
+
|
21
|
+
## 2.23.0 / 2024-10-24
|
22
|
+
|
23
|
+
### Added
|
24
|
+
|
25
|
+
* Allow CSS property `min-width`. [#287] @lazyatom
|
26
|
+
|
27
|
+
|
28
|
+
## 2.22.0 / 2023-11-13
|
29
|
+
|
30
|
+
### Added
|
31
|
+
|
32
|
+
* A `:targetblank` HTML scrubber which ensures all hyperlinks have `target="_blank"`. [#275] @stefannibrasil and @thdaraujo
|
33
|
+
* A `:noreferrer` HTML scrubber which ensures all hyperlinks have `rel=noreferrer`, similar to the `:nofollow` and `:noopener` scrubbers. [#277] @wynksaiddestroy
|
34
|
+
|
35
|
+
|
36
|
+
## 2.21.4 / 2023-10-10
|
37
|
+
|
38
|
+
### Fixed
|
39
|
+
|
40
|
+
* `Loofah::HTML5::Scrub.scrub_css` is more consistent in preserving whitespace (and lack of whitespace) in CSS property values. In particular, `.scrub_css` no longer inserts whitespace between tokens that did not already have whitespace between them. [[#273](https://github.com/flavorjones/loofah/issues/273), fixes [#271](https://github.com/flavorjones/loofah/issues/271)]
|
41
|
+
|
42
|
+
|
43
|
+
## 2.21.3 / 2023-05-15
|
44
|
+
|
45
|
+
### Fixed
|
46
|
+
|
47
|
+
* Quash "instance variable not initialized" warning in Ruby < 3.0. [[#268](https://github.com/flavorjones/loofah/issues/268)] (Thanks, [@dharamgollapudi](https://github.com/dharamgollapudi)!)
|
48
|
+
|
49
|
+
|
50
|
+
## 2.21.2 / 2023-05-11
|
51
|
+
|
52
|
+
### Dependencies
|
53
|
+
|
54
|
+
* Update the dependency on Nokogiri to be `>= 1.12.0`. The dependency in 2.21.0 and 2.21.1 was left at `>= 1.5.9` but versions before 1.12 would result in a `NameError` exception. [[#266](https://github.com/flavorjones/loofah/issues/266)]
|
55
|
+
|
56
|
+
|
57
|
+
## 2.21.1 / 2023-05-10
|
58
|
+
|
59
|
+
### Fixed
|
60
|
+
|
61
|
+
* Don't define `HTML5::Document` and `HTML5::DocumentFragment` when Nokogiri is `< 1.14`. In 2.21.0 these classes were defined whenever `Nokogiri::HTML5` was defined, but Nokogiri v1.12 and v1.13 do not support Loofah subclassing properly.
|
62
|
+
|
63
|
+
|
64
|
+
## 2.21.0 / 2023-05-10
|
65
|
+
|
66
|
+
### HTML5 Support
|
67
|
+
|
68
|
+
Classes `Loofah::HTML5::Document` and `Loofah::HTML5::DocumentFragment` are introduced, along with helper methods:
|
69
|
+
|
70
|
+
- `Loofah.html5_document`
|
71
|
+
- `Loofah.html5_fragment`
|
72
|
+
- `Loofah.scrub_html5_document`
|
73
|
+
- `Loofah.scrub_html5_fragment`
|
74
|
+
|
75
|
+
These classes and methods use Nokogiri's HTML5 parser to ensure modern web standards are used.
|
76
|
+
|
77
|
+
⚠ HTML5 functionality is only available with Nokogiri v1.14.0 and higher.
|
78
|
+
|
79
|
+
⚠ 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.
|
80
|
+
|
81
|
+
|
82
|
+
### `Loofah::HTML4` module and namespace
|
83
|
+
|
84
|
+
`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.
|
85
|
+
|
86
|
+
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?`.
|
87
|
+
|
88
|
+
Future releases of Nokogiri may deprecate `HTML` classes and methods or otherwise change this behavior, so please start using `HTML4` in place of `HTML`.
|
89
|
+
|
90
|
+
|
91
|
+
### Official support for JRuby
|
92
|
+
|
93
|
+
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.
|
94
|
+
|
95
|
+
|
96
|
+
## 2.20.0 / 2023-04-01
|
97
|
+
|
98
|
+
### Features
|
99
|
+
|
100
|
+
* Allow SVG attributes `color-profile`, `cursor`, `filter`, `marker`, and `mask`. [[#246](https://github.com/flavorjones/loofah/issues/246)]
|
101
|
+
* Allow SVG elements `altGlyph`, `cursor`, `feImage`, `pattern`, and `tref`. [[#246](https://github.com/flavorjones/loofah/issues/246)]
|
102
|
+
* Allow protocols `fax` and `modem`. [[#255](https://github.com/flavorjones/loofah/issues/255)] (Thanks, [@cjba7](https://github.com/cjba7)!)
|
103
|
+
|
104
|
+
|
3
105
|
## 2.19.1 / 2022-12-13
|
4
106
|
|
5
107
|
### Security
|
data/README.md
CHANGED
@@ -14,59 +14,64 @@
|
|
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
|
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
|
-
|
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
|
25
|
-
* Common HTML sanitizing
|
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><</tt> and <tt>></tt> entities.
|
29
29
|
* _Whitewash_ the markup, removing all attributes and namespaced nodes.
|
30
|
-
*
|
30
|
+
* Other common HTML transformations are built-in:
|
31
31
|
* Add the _nofollow_ attribute to all hyperlinks.
|
32
|
-
*
|
32
|
+
* Add the _target=\_blank_ attribute to all hyperlinks.
|
33
|
+
* Remove _unprintable_ characters from text nodes.
|
34
|
+
* Some specialized HTML transformations are also built-in:
|
35
|
+
* Where `<br><br>` exists inside a `p` tag, close the `p` and open a new one.
|
36
|
+
* Format markup as plain text, with (or without) sensible whitespace handling around block elements.
|
33
37
|
* Replace Rails's `strip_tags` and `sanitize` view helper methods.
|
34
38
|
|
35
39
|
|
36
40
|
## Compare and Contrast
|
37
41
|
|
38
|
-
Loofah is
|
39
|
-
guarantees well-formed and valid markup (the other is Sanitize, which
|
40
|
-
also uses Nokogiri).
|
42
|
+
Loofah is both:
|
41
43
|
|
42
|
-
|
44
|
+
- a general framework for transforming XML, XHTML, and HTML documents
|
45
|
+
- a specific toolkit for HTML sanitization
|
43
46
|
|
44
|
-
|
45
|
-
commonly-used libraries (ActionView, Sanitize, HTML5lib and HTMLfilter):
|
47
|
+
### General document transformation
|
46
48
|
|
47
|
-
|
49
|
+
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
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
|
52
|
+
### HTML sanitization
|
53
|
+
|
54
|
+
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.
|
55
|
+
|
56
|
+
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
57
|
|
53
58
|
|
54
59
|
## The Basics
|
55
60
|
|
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.
|
61
|
+
Loofah wraps [Nokogiri](http://nokogiri.org) in a loving embrace. Nokogiri is a stable, well-maintained parser for XML, HTML4, and HTML5.
|
60
62
|
|
61
|
-
Loofah
|
63
|
+
Loofah implements the following classes:
|
62
64
|
|
63
|
-
* `Loofah::
|
64
|
-
* `Loofah::
|
65
|
-
* `Loofah::
|
65
|
+
* `Loofah::HTML5::Document`
|
66
|
+
* `Loofah::HTML5::DocumentFragment`
|
67
|
+
* `Loofah::HTML4::Document` (aliased as `Loofah::HTML::Document` for now)
|
68
|
+
* `Loofah::HTML4::DocumentFragment` (aliased as `Loofah::HTML::DocumentFragment` for now)
|
69
|
+
* `Loofah::XML::Document`
|
70
|
+
* `Loofah::XML::DocumentFragment`
|
66
71
|
|
67
|
-
|
72
|
+
These document and fragment classes are subclasses of the similarly-named Nokogiri classes `Nokogiri::HTML5::Document` et al.
|
68
73
|
|
69
|
-
|
74
|
+
Loofah also implements `Loofah::Scrubber`, which represents the document transformation, either by wrapping
|
70
75
|
a block,
|
71
76
|
|
72
77
|
``` ruby
|
@@ -80,50 +85,49 @@ or by implementing a method.
|
|
80
85
|
|
81
86
|
### Side Note: Fragments vs Documents
|
82
87
|
|
83
|
-
Generally speaking, unless you expect to have a DOCTYPE and a single
|
84
|
-
|
85
|
-
HTML
|
86
|
-
|
88
|
+
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.
|
89
|
+
|
90
|
+
**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.
|
91
|
+
|
92
|
+
**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.
|
93
|
+
|
94
|
+
**XML fragments** should be parsed with `Loofah.xml_fragment`. The result won't have a DOCTYPE declaration, and multiple root nodes are allowed.
|
95
|
+
|
96
|
+
**XML documents** should be parsed with `Loofah.xml_document`. The result will have a DOCTYPE declaration and a single root node.
|
87
97
|
|
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
98
|
|
93
|
-
|
94
|
-
won't have a DOCTYPE declaration, and multiple root nodes are allowed.
|
99
|
+
### Side Note: HTML4 vs HTML5
|
95
100
|
|
96
|
-
|
97
|
-
have a DOCTYPE declaration, along with `html`, `head` and `body` tags.
|
101
|
+
⚠ _HTML5 functionality is not available on JRuby, or with versions of Nokogiri `< 1.14.0`._
|
98
102
|
|
99
|
-
|
100
|
-
will have a DOCTYPE declaration and a single root node.
|
103
|
+
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`.)
|
101
104
|
|
105
|
+
**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].
|
102
106
|
|
103
|
-
|
107
|
+
**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.
|
104
108
|
|
105
|
-
|
106
|
-
Nokogiri::HTML::DocumentFragment, so you get all the markup
|
107
|
-
fixer-uppery and API goodness of Nokogiri.
|
109
|
+
[1]: [[feature request] HTML5 parser for JRuby implementation · Issue #2227 · sparklemotion/nokogiri](https://github.com/sparklemotion/nokogiri/issues/2227)
|
108
110
|
|
109
|
-
|
110
|
-
|
111
|
+
|
112
|
+
### `Loofah::HTML5::Document` and `Loofah::HTML5::DocumentFragment`
|
113
|
+
|
114
|
+
These classes are subclasses of `Nokogiri::HTML5::Document` and `Nokogiri::HTML5::DocumentFragment`.
|
115
|
+
|
116
|
+
The module methods `Loofah.html5_document` and `Loofah.html5_fragment` will parse either an HTML document and an HTML fragment, respectively.
|
111
117
|
|
112
118
|
``` ruby
|
113
|
-
Loofah.
|
114
|
-
Loofah.
|
119
|
+
Loofah.html5_document(unsafe_html).is_a?(Nokogiri::HTML5::Document) # => true
|
120
|
+
Loofah.html5_fragment(unsafe_html).is_a?(Nokogiri::HTML5::DocumentFragment) # => true
|
115
121
|
```
|
116
122
|
|
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.
|
123
|
+
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
124
|
|
121
125
|
Loofah overrides `to_s` to return HTML:
|
122
126
|
|
123
127
|
``` ruby
|
124
128
|
unsafe_html = "ohai! <div>div is safe</div> <script>but script is not</script>"
|
125
129
|
|
126
|
-
doc = Loofah.
|
130
|
+
doc = Loofah.html5_fragment(unsafe_html).scrub!(:prune)
|
127
131
|
doc.to_s # => "ohai! <div>div is safe</div> "
|
128
132
|
```
|
129
133
|
|
@@ -136,32 +140,38 @@ doc.text # => "ohai! div is safe "
|
|
136
140
|
Also, `to_text` is available, which does the right thing with whitespace around block-level and line break elements.
|
137
141
|
|
138
142
|
``` ruby
|
139
|
-
doc = Loofah.
|
143
|
+
doc = Loofah.html5_fragment("<h1>Title</h1><div>Content<br>Next line</div>")
|
140
144
|
doc.text # => "TitleContentNext line" # probably not what you want
|
141
145
|
doc.to_text # => "\nTitle\n\nContent\nNext line\n" # better
|
142
146
|
```
|
143
147
|
|
144
|
-
### Loofah::
|
148
|
+
### `Loofah::HTML4::Document` and `Loofah::HTML4::DocumentFragment`
|
149
|
+
|
150
|
+
These classes are subclasses of `Nokogiri::HTML4::Document` and `Nokogiri::HTML4::DocumentFragment`.
|
151
|
+
|
152
|
+
The module methods `Loofah.html4_document` and `Loofah.html4_fragment` will parse either an HTML document and an HTML fragment, respectively.
|
153
|
+
|
154
|
+
``` ruby
|
155
|
+
Loofah.html4_document(unsafe_html).is_a?(Nokogiri::HTML4::Document) # => true
|
156
|
+
Loofah.html4_fragment(unsafe_html).is_a?(Nokogiri::HTML4::DocumentFragment) # => true
|
157
|
+
```
|
158
|
+
|
159
|
+
### `Loofah::XML::Document` and `Loofah::XML::DocumentFragment`
|
145
160
|
|
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.
|
161
|
+
These classes are subclasses of `Nokogiri::XML::Document` and `Nokogiri::XML::DocumentFragment`.
|
149
162
|
|
150
|
-
The module methods Loofah.xml_document and Loofah.xml_fragment will
|
151
|
-
parse an XML document and an XML fragment, respectively.
|
163
|
+
The module methods `Loofah.xml_document` and `Loofah.xml_fragment` will parse an XML document and an XML fragment, respectively.
|
152
164
|
|
153
165
|
``` ruby
|
154
166
|
Loofah.xml_document(bad_xml).is_a?(Nokogiri::XML::Document) # => true
|
155
167
|
Loofah.xml_fragment(bad_xml).is_a?(Nokogiri::XML::DocumentFragment) # => true
|
156
168
|
```
|
157
169
|
|
158
|
-
### Nodes and
|
170
|
+
### Nodes and Node Sets
|
159
171
|
|
160
|
-
Nokogiri
|
161
|
-
method, which makes it easy to scrub subtrees.
|
172
|
+
Nokogiri's `Node` and `NodeSet` classes also get a `scrub!` method, which makes it easy to scrub subtrees.
|
162
173
|
|
163
|
-
The following code will apply the `employee_scrubber` only to the
|
164
|
-
`employee` nodes (and their subtrees) in the document:
|
174
|
+
The following code will apply the `employee_scrubber` only to the `employee` nodes (and their subtrees) in the document:
|
165
175
|
|
166
176
|
``` ruby
|
167
177
|
Loofah.xml_document(bad_xml).xpath("//employee").scrub!(employee_scrubber)
|
@@ -173,7 +183,7 @@ And this code will only scrub the first `employee` node and its subtree:
|
|
173
183
|
Loofah.xml_document(bad_xml).at_xpath("//employee").scrub!(employee_scrubber)
|
174
184
|
```
|
175
185
|
|
176
|
-
### Loofah::Scrubber
|
186
|
+
### `Loofah::Scrubber`
|
177
187
|
|
178
188
|
A Scrubber wraps up a block (or method) that is run on a document node:
|
179
189
|
|
@@ -187,14 +197,11 @@ end
|
|
187
197
|
This can then be run on a document:
|
188
198
|
|
189
199
|
``` ruby
|
190
|
-
Loofah.
|
200
|
+
Loofah.html5_fragment("<span>foo</span><p>bar</p>").scrub!(span2div).to_s
|
191
201
|
# => "<div>foo</div><p>bar</p>"
|
192
202
|
```
|
193
203
|
|
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.
|
204
|
+
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
205
|
|
199
206
|
Here's an XML example:
|
200
207
|
|
@@ -211,10 +218,10 @@ Loofah.xml_document(File.read('plague.xml')).scrub!(bring_out_your_dead)
|
|
211
218
|
|
212
219
|
### Built-In HTML Scrubbers
|
213
220
|
|
214
|
-
Loofah comes with a set of sanitizing scrubbers that use
|
215
|
-
safelist algorithm:
|
221
|
+
Loofah comes with a set of sanitizing scrubbers that use `html5lib`'s safelist algorithm:
|
216
222
|
|
217
223
|
``` ruby
|
224
|
+
doc = Loofah.html5_document(input)
|
218
225
|
doc.scrub!(:strip) # replaces unknown/unsafe tags with their inner text
|
219
226
|
doc.scrub!(:prune) # removes unknown/unsafe tags and their children
|
220
227
|
doc.scrub!(:escape) # escapes unknown/unsafe tags, like this: <script>
|
@@ -222,14 +229,18 @@ doc.scrub!(:whitewash) # removes unknown/unsafe/namespaced tags and their chi
|
|
222
229
|
# and strips all node attributes
|
223
230
|
```
|
224
231
|
|
225
|
-
Loofah also comes with some common transformation tasks:
|
232
|
+
Loofah also comes with built-in scrubers for some common transformation tasks:
|
226
233
|
|
227
234
|
``` ruby
|
228
|
-
doc.scrub!(:nofollow)
|
229
|
-
doc.scrub!(:
|
235
|
+
doc.scrub!(:nofollow) # adds rel="nofollow" attribute to links
|
236
|
+
doc.scrub!(:noopener) # adds rel="noopener" attribute to links
|
237
|
+
doc.scrub!(:noreferrer) # adds rel="noreferrer" attribute to links
|
238
|
+
doc.scrub!(:unprintable) # removes unprintable characters from text nodes
|
239
|
+
doc.scrub!(:targetblank) # adds target="_blank" attribute to links
|
240
|
+
doc.scrub!(:double_breakpoint) # where `<br><br>` appears in a `p` tag, close the `p` and open a new one
|
230
241
|
```
|
231
242
|
|
232
|
-
See Loofah::Scrubbers for more details and example usage.
|
243
|
+
See `Loofah::Scrubbers` for more details and example usage.
|
233
244
|
|
234
245
|
|
235
246
|
### Chaining Scrubbers
|
@@ -237,7 +248,7 @@ See Loofah::Scrubbers for more details and example usage.
|
|
237
248
|
You can chain scrubbers:
|
238
249
|
|
239
250
|
``` ruby
|
240
|
-
Loofah.
|
251
|
+
Loofah.html5_fragment("<span>hello</span> <script>alert('OHAI')</script>") \
|
241
252
|
.scrub!(:prune) \
|
242
253
|
.scrub!(span2div).to_s
|
243
254
|
# => "<div>hello</div> "
|
@@ -245,21 +256,26 @@ Loofah.fragment("<span>hello</span> <script>alert('OHAI')</script>") \
|
|
245
256
|
|
246
257
|
### Shorthand
|
247
258
|
|
248
|
-
The class methods Loofah.
|
249
|
-
|
259
|
+
The class methods `Loofah.scrub_html5_fragment` and `Loofah.scrub_html5_document` (and the corresponding HTML4 methods) are shorthand.
|
260
|
+
|
261
|
+
These methods:
|
250
262
|
|
251
263
|
``` ruby
|
252
|
-
Loofah.
|
253
|
-
Loofah.
|
264
|
+
Loofah.scrub_html5_fragment(unsafe_html, :prune)
|
265
|
+
Loofah.scrub_html5_document(unsafe_html, :prune)
|
266
|
+
Loofah.scrub_html4_fragment(unsafe_html, :prune)
|
267
|
+
Loofah.scrub_html4_document(unsafe_html, :prune)
|
254
268
|
Loofah.scrub_xml_fragment(bad_xml, custom_scrubber)
|
255
269
|
Loofah.scrub_xml_document(bad_xml, custom_scrubber)
|
256
270
|
```
|
257
271
|
|
258
|
-
|
272
|
+
do the same thing as (and arguably semantically clearer than):
|
259
273
|
|
260
274
|
``` ruby
|
261
|
-
Loofah.
|
262
|
-
Loofah.
|
275
|
+
Loofah.html5_fragment(unsafe_html).scrub!(:prune)
|
276
|
+
Loofah.html5_document(unsafe_html).scrub!(:prune)
|
277
|
+
Loofah.html4_fragment(unsafe_html).scrub!(:prune)
|
278
|
+
Loofah.html4_document(unsafe_html).scrub!(:prune)
|
263
279
|
Loofah.xml_fragment(bad_xml).scrub!(custom_scrubber)
|
264
280
|
Loofah.xml_document(bad_xml).scrub!(custom_scrubber)
|
265
281
|
```
|
@@ -267,10 +283,9 @@ Loofah.xml_document(bad_xml).scrub!(custom_scrubber)
|
|
267
283
|
|
268
284
|
### View Helpers
|
269
285
|
|
270
|
-
Loofah has two "view helpers": Loofah::Helpers.sanitize and
|
271
|
-
|
272
|
-
|
273
|
-
These are no longer required automatically. You must require `loofah/helpers`.
|
286
|
+
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.
|
287
|
+
|
288
|
+
These are not required automatically. You must require `loofah/helpers` to use them.
|
274
289
|
|
275
290
|
|
276
291
|
## Requirements
|
@@ -282,7 +297,11 @@ These are no longer required automatically. You must require `loofah/helpers`.
|
|
282
297
|
|
283
298
|
Unsurprisingly:
|
284
299
|
|
285
|
-
|
300
|
+
> gem install loofah
|
301
|
+
|
302
|
+
Requirements:
|
303
|
+
|
304
|
+
* Ruby >= 2.5
|
286
305
|
|
287
306
|
|
288
307
|
## Support
|
@@ -296,8 +315,6 @@ And the mailing list is on Google Groups:
|
|
296
315
|
* Mail: loofah-talk@googlegroups.com
|
297
316
|
* Archive: https://groups.google.com/forum/#!forum/loofah-talk
|
298
317
|
|
299
|
-
And the IRC channel is \#loofah on freenode.
|
300
|
-
|
301
318
|
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
319
|
|
303
320
|
[tidelift]: https://tidelift.com/subscription/pkg/rubygems-loofah?utm_source=undefined&utm_medium=referral&utm_campaign=enterprise
|
@@ -308,26 +325,12 @@ Consider subscribing to [Tidelift][tidelift] which provides license assurances a
|
|
308
325
|
See [`SECURITY.md`](SECURITY.md) for vulnerability reporting details.
|
309
326
|
|
310
327
|
|
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
328
|
## Related Links
|
327
329
|
|
330
|
+
* loofah-activerecord: https://github.com/flavorjones/loofah-activerecord
|
328
331
|
* Nokogiri: http://nokogiri.org
|
329
332
|
* libxml2: http://xmlsoft.org
|
330
|
-
* html5lib: https://
|
333
|
+
* html5lib: https://github.com/html5lib/
|
331
334
|
|
332
335
|
|
333
336
|
## Authors
|
@@ -337,26 +340,69 @@ you by default from this XXE vulnerability.
|
|
337
340
|
|
338
341
|
Featuring code contributed by:
|
339
342
|
|
340
|
-
*
|
341
|
-
*
|
342
|
-
*
|
343
|
-
*
|
344
|
-
*
|
343
|
+
* [@flavorjones](https://github.com/flavorjones)
|
344
|
+
* [@brynary](https://github.com/brynary)
|
345
|
+
* [@olleolleolle](https://github.com/olleolleolle)
|
346
|
+
* [@JuanitoFatas](https://github.com/JuanitoFatas)
|
347
|
+
* [@kaspth](https://github.com/kaspth)
|
348
|
+
* [@tenderlove](https://github.com/tenderlove)
|
349
|
+
* [@ktdreyer](https://github.com/ktdreyer)
|
350
|
+
* [@orien](https://github.com/orien)
|
351
|
+
* [@asok](https://github.com/asok)
|
352
|
+
* [@junaruga](https://github.com/junaruga)
|
353
|
+
* [@MothOnMars](https://github.com/MothOnMars)
|
354
|
+
* [@nick-desteffen](https://github.com/nick-desteffen)
|
355
|
+
* [@NikoRoberts](https://github.com/NikoRoberts)
|
356
|
+
* [@trans](https://github.com/trans)
|
357
|
+
* [@andreynering](https://github.com/andreynering)
|
358
|
+
* [@aried3r](https://github.com/aried3r)
|
359
|
+
* [@baopham](https://github.com/baopham)
|
360
|
+
* [@batter](https://github.com/batter)
|
361
|
+
* [@brendon](https://github.com/brendon)
|
362
|
+
* [@cjba7](https://github.com/cjba7)
|
363
|
+
* [@christiankisssner](https://github.com/christiankisssner)
|
364
|
+
* [@dacort](https://github.com/dacort)
|
365
|
+
* [@danfstucky](https://github.com/danfstucky)
|
366
|
+
* [@david-a-wheeler](https://github.com/david-a-wheeler)
|
367
|
+
* [@dharamgollapudi](https://github.com/dharamgollapudi)
|
368
|
+
* [@georgeclaghorn](https://github.com/georgeclaghorn)
|
369
|
+
* [@gogainda](https://github.com/gogainda)
|
370
|
+
* [@jaredbeck](https://github.com/jaredbeck)
|
371
|
+
* [@ThatHurleyGuy](https://github.com/ThatHurleyGuy)
|
372
|
+
* [@jstorimer](https://github.com/jstorimer)
|
373
|
+
* [@jbarnette](https://github.com/jbarnette)
|
374
|
+
* [@queso](https://github.com/queso)
|
375
|
+
* [@technicalpickles](https://github.com/technicalpickles)
|
376
|
+
* [@kyoshidajp](https://github.com/kyoshidajp)
|
377
|
+
* [@kristianfreeman](https://github.com/kristianfreeman)
|
378
|
+
* [@louim](https://github.com/louim)
|
379
|
+
* [@mrpasquini](https://github.com/mrpasquini)
|
380
|
+
* [@olivierlacan](https://github.com/olivierlacan)
|
381
|
+
* [@pauldix](https://github.com/pauldix)
|
382
|
+
* [@sampokuokkanen](https://github.com/sampokuokkanen)
|
383
|
+
* [@stefannibrasil](https://github.com/stefannibrasil)
|
384
|
+
* [@tastycode](https://github.com/tastycode)
|
385
|
+
* [@vipulnsward](https://github.com/vipulnsward)
|
386
|
+
* [@joncalhoun](https://github.com/joncalhoun)
|
387
|
+
* [@ahorek](https://github.com/ahorek)
|
388
|
+
* [@rmacklin](https://github.com/rmacklin)
|
389
|
+
* [@y-yagi](https://github.com/y-yagi)
|
390
|
+
* [@lazyatom](https://github.com/lazyatom)
|
345
391
|
|
346
392
|
And a big shout-out to Corey Innis for the name, and feedback on the API.
|
347
393
|
|
348
394
|
|
349
395
|
## Thank You
|
350
396
|
|
351
|
-
The following people have generously funded Loofah:
|
397
|
+
The following people have generously funded Loofah with financial sponsorship:
|
352
398
|
|
353
399
|
* Bill Harding
|
400
|
+
* [Sentry](https://sentry.io/) @getsentry
|
354
401
|
|
355
402
|
|
356
403
|
## Historical Note
|
357
404
|
|
358
|
-
This library was
|
359
|
-
name that nobody could spell properly.
|
405
|
+
This library was once named "Dryopteris", which was a very bad name that nobody could spell properly.
|
360
406
|
|
361
407
|
|
362
408
|
## License
|