rails-html-sanitizer 1.5.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +125 -0
- data/MIT-LICENSE +1 -1
- data/README.md +124 -72
- data/lib/rails/html/sanitizer/version.rb +4 -2
- data/lib/rails/html/sanitizer.rb +372 -104
- data/lib/rails/html/scrubbers.rb +98 -73
- data/lib/rails-html-sanitizer.rb +7 -23
- data/test/rails_api_test.rb +88 -0
- data/test/sanitizer_test.rb +1095 -584
- data/test/scrubbers_test.rb +129 -38
- metadata +68 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5507b24d4d93d6efebf2e327d04980dd114cd491732b5c71a7e1a3294c846a9
|
4
|
+
data.tar.gz: dd7a5070b04bf6a97b96df01d65fce9d52790e62dc6631b31fb72ecc2a6d16ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 912e9d41629bd93de8a14352757add81fde36e394e7907923a88dbebc39da85722fc13a01fa23973421a11e715b41fb78cec3f0379ccf5a97ab0f0ee3ed3dc5a
|
7
|
+
data.tar.gz: e03d92f6289ef71e4039a64b89bce79c5d5a9d628632c84b2b54235fbe69384578de92f7b2db114c13d041c4d589672dd09e23758a043e675eb37f961a858f67
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,119 @@
|
|
1
|
+
## v1.6.2 / 2024-12-12
|
2
|
+
|
3
|
+
* `PermitScrubber` fully supports frozen "allowed tags".
|
4
|
+
|
5
|
+
v1.6.1 introduced safety checks that may remove unsafe tags from the allowed list, which
|
6
|
+
introduced a regression for applications passing a frozen array of allowed tags. Tags and
|
7
|
+
attributes are now properly copied when they are passed to the scrubber.
|
8
|
+
|
9
|
+
Fixes #195.
|
10
|
+
|
11
|
+
*Mike Dalessio*
|
12
|
+
|
13
|
+
|
14
|
+
## 1.6.1 / 2024-12-02
|
15
|
+
|
16
|
+
This is a performance and security release which addresses several possible XSS vulnerabilities.
|
17
|
+
|
18
|
+
* The dependency on Nokogiri is updated to v1.15.7 or >=1.16.8.
|
19
|
+
|
20
|
+
This change addresses CVE-2024-53985 (GHSA-w8gc-x259-rc7x).
|
21
|
+
|
22
|
+
*Mike Dalessio*
|
23
|
+
|
24
|
+
* Disallowed tags will be pruned when they appear in foreign content (i.e. SVG or MathML content),
|
25
|
+
regardless of the `prune:` option value. Previously, disallowed tags were "stripped" unless the
|
26
|
+
gem was configured with the `prune: true` option.
|
27
|
+
|
28
|
+
The CVEs addressed by this change are:
|
29
|
+
|
30
|
+
- CVE-2024-53986 (GHSA-638j-pmjw-jq48)
|
31
|
+
- CVE-2024-53987 (GHSA-2x5m-9ch4-qgrr)
|
32
|
+
|
33
|
+
*Mike Dalessio*
|
34
|
+
|
35
|
+
* The tags "noscript", "mglyph", and "malignmark" will not be allowed, even if explicitly added to
|
36
|
+
the allowlist. If applications try to allow any of these tags, a warning is emitted and the tags
|
37
|
+
are removed from the allow-list.
|
38
|
+
|
39
|
+
The CVEs addressed by this change are:
|
40
|
+
|
41
|
+
- CVE-2024-53988 (GHSA-cfjx-w229-hgx5)
|
42
|
+
- CVE-2024-53989 (GHSA-rxv5-gxqc-xx8g)
|
43
|
+
|
44
|
+
Please note that we _may_ restore support for allowing "noscript" in a future release. We do not
|
45
|
+
expect to ever allow "mglyph" or "malignmark", though, especially since browser support is minimal
|
46
|
+
for these tags.
|
47
|
+
|
48
|
+
*Mike Dalessio*
|
49
|
+
|
50
|
+
* Improve performance by eliminating needless operations on attributes that are being removed. #188
|
51
|
+
|
52
|
+
*Mike Dalessio*
|
53
|
+
|
54
|
+
|
55
|
+
## 1.6.0 / 2023-05-26
|
56
|
+
|
57
|
+
* Dependencies have been updated:
|
58
|
+
|
59
|
+
- Loofah `~>2.21` and Nokogiri `~>1.14` for HTML5 parser support
|
60
|
+
- As a result, required Ruby version is now `>= 2.7.0`
|
61
|
+
|
62
|
+
Security updates will continue to be made on the `1.5.x` release branch as long as Rails 6.1
|
63
|
+
(which supports Ruby 2.5) is still in security support.
|
64
|
+
|
65
|
+
*Mike Dalessio*
|
66
|
+
|
67
|
+
* HTML5 standards-compliant sanitizers are now available on platforms supported by
|
68
|
+
Nokogiri::HTML5. These are available as:
|
69
|
+
|
70
|
+
- `Rails::HTML5::FullSanitizer`
|
71
|
+
- `Rails::HTML5::LinkSanitizer`
|
72
|
+
- `Rails::HTML5::SafeListSanitizer`
|
73
|
+
|
74
|
+
And a new "vendor" is provided at `Rails::HTML5::Sanitizer` that can be used in a future version
|
75
|
+
of Rails.
|
76
|
+
|
77
|
+
Note that for symmetry `Rails::HTML4::Sanitizer` is also added, though its behavior is identical
|
78
|
+
to the vendor class methods on `Rails::HTML::Sanitizer`.
|
79
|
+
|
80
|
+
Users may call `Rails::HTML::Sanitizer.best_supported_vendor` to get back the HTML5 vendor if it's
|
81
|
+
supported, else the legacy HTML4 vendor.
|
82
|
+
|
83
|
+
*Mike Dalessio*
|
84
|
+
|
85
|
+
* Module namespaces have changed, but backwards compatibility is provided by aliases.
|
86
|
+
|
87
|
+
The library defines three additional modules:
|
88
|
+
|
89
|
+
- `Rails::HTML` for general functionality (replacing `Rails::Html`)
|
90
|
+
- `Rails::HTML4` containing sanitizers that parse content as HTML4
|
91
|
+
- `Rails::HTML5` containing sanitizers that parse content as HTML5
|
92
|
+
|
93
|
+
The following aliases are maintained for backwards compatibility:
|
94
|
+
|
95
|
+
- `Rails::Html` points to `Rails::HTML`
|
96
|
+
- `Rails::HTML::FullSanitizer` points to `Rails::HTML4::FullSanitizer`
|
97
|
+
- `Rails::HTML::LinkSanitizer` points to `Rails::HTML4::LinkSanitizer`
|
98
|
+
- `Rails::HTML::SafeListSanitizer` points to `Rails::HTML4::SafeListSanitizer`
|
99
|
+
|
100
|
+
*Mike Dalessio*
|
101
|
+
|
102
|
+
* `LinkSanitizer` always returns UTF-8 encoded strings. `SafeListSanitizer` and `FullSanitizer`
|
103
|
+
already ensured this encoding.
|
104
|
+
|
105
|
+
*Mike Dalessio*
|
106
|
+
|
107
|
+
* `SafeListSanitizer` allows `time` tag and `lang` attribute by default.
|
108
|
+
|
109
|
+
*Mike Dalessio*
|
110
|
+
|
111
|
+
* The constant `Rails::Html::XPATHS_TO_REMOVE` has been removed. It's not necessary with the
|
112
|
+
existing sanitizers, and should have been a private constant all along anyway.
|
113
|
+
|
114
|
+
*Mike Dalessio*
|
115
|
+
|
116
|
+
|
1
117
|
## 1.5.0 / 2023-01-20
|
2
118
|
|
3
119
|
* `SafeListSanitizer`, `PermitScrubber`, and `TargetScrubber` now all support pruning of unsafe tags.
|
@@ -7,6 +123,7 @@
|
|
7
123
|
|
8
124
|
*seyerian*
|
9
125
|
|
126
|
+
|
10
127
|
## 1.4.4 / 2022-12-13
|
11
128
|
|
12
129
|
* Address inefficient regular expression complexity with certain configurations of Rails::Html::Sanitizer.
|
@@ -52,6 +169,7 @@
|
|
52
169
|
|
53
170
|
*Mike Dalessio*
|
54
171
|
|
172
|
+
|
55
173
|
## 1.4.2 / 2021-08-23
|
56
174
|
|
57
175
|
* Slightly improve performance.
|
@@ -60,6 +178,7 @@
|
|
60
178
|
|
61
179
|
*Mike Dalessio*
|
62
180
|
|
181
|
+
|
63
182
|
## 1.4.1 / 2021-08-18
|
64
183
|
|
65
184
|
* Fix regression in v1.4.0 that did not pass comment nodes to the scrubber.
|
@@ -72,6 +191,7 @@
|
|
72
191
|
|
73
192
|
*Mike Dalessio*
|
74
193
|
|
194
|
+
|
75
195
|
## 1.4.0 / 2021-08-18
|
76
196
|
|
77
197
|
* Processing Instructions are no longer allowed by Rails::Html::PermitScrubber
|
@@ -84,12 +204,14 @@
|
|
84
204
|
|
85
205
|
*Mike Dalessio*
|
86
206
|
|
207
|
+
|
87
208
|
## 1.3.0
|
88
209
|
|
89
210
|
* Address deprecations in Loofah 2.3.0.
|
90
211
|
|
91
212
|
*Josh Goodall*
|
92
213
|
|
214
|
+
|
93
215
|
## 1.2.0
|
94
216
|
|
95
217
|
* Remove needless `white_list_sanitizer` deprecation.
|
@@ -104,6 +226,7 @@
|
|
104
226
|
|
105
227
|
*Kasper Timm Hansen*
|
106
228
|
|
229
|
+
|
107
230
|
## 1.1.0
|
108
231
|
|
109
232
|
* Add `safe_list_sanitizer` and deprecate `white_list_sanitizer` to be removed
|
@@ -121,10 +244,12 @@
|
|
121
244
|
|
122
245
|
*Kasper Timm Hansen*
|
123
246
|
|
247
|
+
|
124
248
|
## 1.0.1
|
125
249
|
|
126
250
|
* Added support for Rails 4.2.0.beta2 and above
|
127
251
|
|
252
|
+
|
128
253
|
## 1.0.0
|
129
254
|
|
130
255
|
* First release.
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,63 +1,31 @@
|
|
1
|
-
# Rails
|
1
|
+
# Rails HTML Sanitizers
|
2
2
|
|
3
|
-
|
4
|
-
applications, i.e. in the `sanitize`, `sanitize_css`, `strip_tags` and `strip_links` methods.
|
3
|
+
This gem is responsible for sanitizing HTML fragments in Rails applications. Specifically, this is the set of sanitizers used to implement the Action View `SanitizerHelper` methods `sanitize`, `sanitize_css`, `strip_tags` and `strip_links`.
|
5
4
|
|
6
|
-
Rails
|
5
|
+
Rails HTML Sanitizer is only intended to be used with Rails applications. If you need similar functionality but aren't using Rails, consider using the underlying sanitization library [Loofah](https://github.com/flavorjones/loofah) directly.
|
7
6
|
|
8
|
-
## Installation
|
9
|
-
|
10
|
-
Add this line to your application's Gemfile:
|
11
|
-
|
12
|
-
gem 'rails-html-sanitizer'
|
13
|
-
|
14
|
-
And then execute:
|
15
|
-
|
16
|
-
$ bundle
|
17
|
-
|
18
|
-
Or install it yourself as:
|
19
|
-
|
20
|
-
$ gem install rails-html-sanitizer
|
21
7
|
|
22
8
|
## Usage
|
23
9
|
|
24
|
-
###
|
25
|
-
|
26
|
-
__Rails::HTML sanitizers are intended to be used by the view layer, at page-render time. They are *not* intended to sanitize persisted strings that will sanitized *again* at page-render time.__
|
27
|
-
|
28
|
-
Proper HTML sanitization will replace some characters with HTML entities. For example, `<` will be replaced with `<` to ensure that the markup is well-formed.
|
29
|
-
|
30
|
-
This is important to keep in mind because __HTML entities will render improperly if they are sanitized twice.__
|
31
|
-
|
32
|
-
|
33
|
-
#### A concrete example showing the problem that can arise
|
34
|
-
|
35
|
-
Imagine the user is asked to enter their employer's name, which will appear on their public profile page. Then imagine they enter `JPMorgan Chase & Co.`.
|
36
|
-
|
37
|
-
If you sanitize this before persisting it in the database, the stored string will be `JPMorgan Chase & Co.`
|
38
|
-
|
39
|
-
When the page is rendered, if this string is sanitized a second time by the view layer, the HTML will contain `JPMorgan Chase &amp; Co.` which will render as "JPMorgan Chase &amp; Co.".
|
40
|
-
|
41
|
-
Another problem that can arise is rendering the sanitized string in a non-HTML context (for example, if it ends up being part of an SMS message). In this case, it may contain inappropriate HTML entities.
|
42
|
-
|
43
|
-
|
44
|
-
#### Suggested alternatives
|
10
|
+
### Sanitizers
|
45
11
|
|
46
|
-
|
12
|
+
All sanitizers respond to `sanitize`, and are available in variants that use either HTML4 or HTML5 parsing, under the `Rails::HTML4` and `Rails::HTML5` namespaces, respectively.
|
47
13
|
|
48
|
-
|
14
|
+
NOTE: The HTML5 sanitizers are not supported on JRuby. Users may programmatically check for support by calling `Rails::HTML::Sanitizer.html5_support?`.
|
49
15
|
|
50
|
-
If you really want to sanitize the string that's stored in your database, you may wish to look into [Loofah::ActiveRecord](https://github.com/flavorjones/loofah-activerecord) rather than use the Rails::HTML sanitizers.
|
51
16
|
|
17
|
+
#### FullSanitizer
|
52
18
|
|
53
|
-
|
19
|
+
```ruby
|
20
|
+
full_sanitizer = Rails::HTML5::FullSanitizer.new
|
21
|
+
full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
|
22
|
+
# => Bold no more! See more here...
|
23
|
+
```
|
54
24
|
|
55
|
-
|
56
|
-
|
57
|
-
#### FullSanitizer
|
25
|
+
or, if you insist on parsing the content as HTML4:
|
58
26
|
|
59
27
|
```ruby
|
60
|
-
full_sanitizer = Rails::
|
28
|
+
full_sanitizer = Rails::HTML4::FullSanitizer.new
|
61
29
|
full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
|
62
30
|
# => Bold no more! See more here...
|
63
31
|
```
|
@@ -65,44 +33,55 @@ full_sanitizer.sanitize("<b>Bold</b> no more! <a href='more.html'>See more here
|
|
65
33
|
#### LinkSanitizer
|
66
34
|
|
67
35
|
```ruby
|
68
|
-
link_sanitizer = Rails::
|
36
|
+
link_sanitizer = Rails::HTML5::LinkSanitizer.new
|
37
|
+
link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
|
38
|
+
# => Only the link text will be kept.
|
39
|
+
```
|
40
|
+
|
41
|
+
or, if you insist on parsing the content as HTML4:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
link_sanitizer = Rails::HTML4::LinkSanitizer.new
|
69
45
|
link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
|
70
46
|
# => Only the link text will be kept.
|
71
47
|
```
|
72
48
|
|
49
|
+
|
73
50
|
#### SafeListSanitizer
|
74
51
|
|
52
|
+
This sanitizer is also available as an HTML4 variant, but for simplicity we'll document only the HTML5 variant below.
|
53
|
+
|
75
54
|
```ruby
|
76
|
-
safe_list_sanitizer = Rails::
|
55
|
+
safe_list_sanitizer = Rails::HTML5::SafeListSanitizer.new
|
77
56
|
|
78
57
|
# sanitize via an extensive safe list of allowed elements
|
79
58
|
safe_list_sanitizer.sanitize(@article.body)
|
80
59
|
|
81
|
-
#
|
60
|
+
# sanitize only the supplied tags and attributes
|
82
61
|
safe_list_sanitizer.sanitize(@article.body, tags: %w(table tr td), attributes: %w(id class style))
|
83
62
|
|
84
|
-
#
|
63
|
+
# sanitize via a custom scrubber
|
85
64
|
safe_list_sanitizer.sanitize(@article.body, scrubber: ArticleScrubber.new)
|
86
65
|
|
87
|
-
#
|
88
|
-
safe_list_sanitizer.
|
66
|
+
# prune nodes from the tree instead of stripping tags and leaving inner content
|
67
|
+
safe_list_sanitizer = Rails::HTML5::SafeListSanitizer.new(prune: true)
|
89
68
|
|
90
|
-
#
|
91
|
-
safe_list_sanitizer
|
69
|
+
# the sanitizer can also sanitize css
|
70
|
+
safe_list_sanitizer.sanitize_css('background-color: #000;')
|
92
71
|
```
|
93
72
|
|
94
73
|
### Scrubbers
|
95
74
|
|
96
75
|
Scrubbers are objects responsible for removing nodes or attributes you don't want in your HTML document.
|
97
76
|
|
98
|
-
This gem includes two scrubbers `Rails::
|
77
|
+
This gem includes two scrubbers `Rails::HTML::PermitScrubber` and `Rails::HTML::TargetScrubber`.
|
99
78
|
|
100
|
-
#### `Rails::
|
79
|
+
#### `Rails::HTML::PermitScrubber`
|
101
80
|
|
102
81
|
This scrubber allows you to permit only the tags and attributes you want.
|
103
82
|
|
104
83
|
```ruby
|
105
|
-
scrubber = Rails::
|
84
|
+
scrubber = Rails::HTML::PermitScrubber.new
|
106
85
|
scrubber.tags = ['a']
|
107
86
|
|
108
87
|
html_fragment = Loofah.fragment('<a><img/ ></a>')
|
@@ -113,14 +92,14 @@ html_fragment.to_s # => "<a></a>"
|
|
113
92
|
By default, inner content is left, but it can be removed as well.
|
114
93
|
|
115
94
|
```ruby
|
116
|
-
scrubber = Rails::
|
95
|
+
scrubber = Rails::HTML::PermitScrubber.new
|
117
96
|
scrubber.tags = ['a']
|
118
97
|
|
119
98
|
html_fragment = Loofah.fragment('<a><span>text</span></a>')
|
120
99
|
html_fragment.scrub!(scrubber)
|
121
100
|
html_fragment.to_s # => "<a>text</a>"
|
122
101
|
|
123
|
-
scrubber = Rails::
|
102
|
+
scrubber = Rails::HTML::PermitScrubber.new(prune: true)
|
124
103
|
scrubber.tags = ['a']
|
125
104
|
|
126
105
|
html_fragment = Loofah.fragment('<a><span>text</span></a>')
|
@@ -128,16 +107,16 @@ html_fragment.scrub!(scrubber)
|
|
128
107
|
html_fragment.to_s # => "<a></a>"
|
129
108
|
```
|
130
109
|
|
131
|
-
#### `Rails::
|
110
|
+
#### `Rails::HTML::TargetScrubber`
|
132
111
|
|
133
112
|
Where `PermitScrubber` picks out tags and attributes to permit in sanitization,
|
134
|
-
`Rails::
|
113
|
+
`Rails::HTML::TargetScrubber` targets them for removal. See https://github.com/flavorjones/loofah/blob/main/lib/loofah/html5/safelist.rb for the tag list.
|
135
114
|
|
136
115
|
**Note:** by default, it will scrub anything that is not part of the permitted tags from
|
137
116
|
loofah `HTML5::Scrub.allowed_element?`.
|
138
117
|
|
139
118
|
```ruby
|
140
|
-
scrubber = Rails::
|
119
|
+
scrubber = Rails::HTML::TargetScrubber.new
|
141
120
|
scrubber.tags = ['img']
|
142
121
|
|
143
122
|
html_fragment = Loofah.fragment('<a><img/ ></a>')
|
@@ -148,26 +127,27 @@ html_fragment.to_s # => "<a></a>"
|
|
148
127
|
Similarly to `PermitScrubber`, nodes can be fully pruned.
|
149
128
|
|
150
129
|
```ruby
|
151
|
-
scrubber = Rails::
|
130
|
+
scrubber = Rails::HTML::TargetScrubber.new
|
152
131
|
scrubber.tags = ['span']
|
153
132
|
|
154
133
|
html_fragment = Loofah.fragment('<a><span>text</span></a>')
|
155
134
|
html_fragment.scrub!(scrubber)
|
156
135
|
html_fragment.to_s # => "<a>text</a>"
|
157
136
|
|
158
|
-
scrubber = Rails::
|
137
|
+
scrubber = Rails::HTML::TargetScrubber.new(prune: true)
|
159
138
|
scrubber.tags = ['span']
|
160
139
|
|
161
140
|
html_fragment = Loofah.fragment('<a><span>text</span></a>')
|
162
141
|
html_fragment.scrub!(scrubber)
|
163
142
|
html_fragment.to_s # => "<a></a>"
|
164
143
|
```
|
144
|
+
|
165
145
|
#### Custom Scrubbers
|
166
146
|
|
167
147
|
You can also create custom scrubbers in your application if you want to.
|
168
148
|
|
169
149
|
```ruby
|
170
|
-
class CommentScrubber < Rails::
|
150
|
+
class CommentScrubber < Rails::HTML::PermitScrubber
|
171
151
|
def initialize
|
172
152
|
super
|
173
153
|
self.tags = %w( form script comment blockquote )
|
@@ -180,7 +160,7 @@ class CommentScrubber < Rails::Html::PermitScrubber
|
|
180
160
|
end
|
181
161
|
```
|
182
162
|
|
183
|
-
See `Rails::
|
163
|
+
See `Rails::HTML::PermitScrubber` documentation to learn more about which methods can be overridden.
|
184
164
|
|
185
165
|
#### Custom Scrubber in a Rails app
|
186
166
|
|
@@ -190,26 +170,98 @@ Using the `CommentScrubber` from above, you can use this in a Rails view like so
|
|
190
170
|
<%= sanitize @comment, scrubber: CommentScrubber.new %>
|
191
171
|
```
|
192
172
|
|
173
|
+
### A note on HTML entities
|
174
|
+
|
175
|
+
__Rails HTML sanitizers are intended to be used by the view layer, at page-render time. They are *not* intended to sanitize persisted strings that will be sanitized *again* at page-render time.__
|
176
|
+
|
177
|
+
Proper HTML sanitization will replace some characters with HTML entities. For example, text containing a `<` character will be updated to contain `<` to ensure that the markup is well-formed.
|
178
|
+
|
179
|
+
This is important to keep in mind because __HTML entities will render improperly if they are sanitized twice.__
|
180
|
+
|
181
|
+
|
182
|
+
#### A concrete example showing the problem that can arise
|
183
|
+
|
184
|
+
Imagine the user is asked to enter their employer's name, which will appear on their public profile page. Then imagine they enter `JPMorgan Chase & Co.`.
|
185
|
+
|
186
|
+
If you sanitize this before persisting it in the database, the stored string will be `JPMorgan Chase & Co.`
|
187
|
+
|
188
|
+
When the page is rendered, if this string is sanitized a second time by the view layer, the HTML will contain `JPMorgan Chase &amp; Co.` which will render as "JPMorgan Chase &amp; Co.".
|
189
|
+
|
190
|
+
Another problem that can arise is rendering the sanitized string in a non-HTML context (for example, if it ends up being part of an SMS message). In this case, it may contain inappropriate HTML entities.
|
191
|
+
|
192
|
+
|
193
|
+
#### Suggested alternatives
|
194
|
+
|
195
|
+
You might simply choose to persist the untrusted string as-is (the raw input), and then ensure that the string will be properly sanitized by the view layer.
|
196
|
+
|
197
|
+
That raw string, if rendered in an non-HTML context (like SMS), must also be sanitized by a method appropriate for that context. You may wish to look into using [Loofah](https://github.com/flavorjones/loofah) or [Sanitize](https://github.com/rgrove/sanitize) to customize how this sanitization works, including omitting HTML entities in the final string.
|
198
|
+
|
199
|
+
If you really want to sanitize the string that's stored in your database, you may wish to look into [Loofah::ActiveRecord](https://github.com/flavorjones/loofah-activerecord) rather than use the Rails HTML sanitizers.
|
200
|
+
|
201
|
+
|
202
|
+
### A note on module names
|
203
|
+
|
204
|
+
In versions < 1.6, the only module defined by this library was `Rails::Html`. Starting in 1.6, we define three additional modules:
|
205
|
+
|
206
|
+
- `Rails::HTML` for general functionality (replacing `Rails::Html`)
|
207
|
+
- `Rails::HTML4` containing sanitizers that parse content as HTML4
|
208
|
+
- `Rails::HTML5` containing sanitizers that parse content as HTML5 (if supported)
|
209
|
+
|
210
|
+
The following aliases are maintained for backwards compatibility:
|
211
|
+
|
212
|
+
- `Rails::Html` points to `Rails::HTML`
|
213
|
+
- `Rails::HTML::FullSanitizer` points to `Rails::HTML4::FullSanitizer`
|
214
|
+
- `Rails::HTML::LinkSanitizer` points to `Rails::HTML4::LinkSanitizer`
|
215
|
+
- `Rails::HTML::SafeListSanitizer` points to `Rails::HTML4::SafeListSanitizer`
|
216
|
+
|
217
|
+
|
218
|
+
## Installation
|
219
|
+
|
220
|
+
Add this line to your application's Gemfile:
|
221
|
+
|
222
|
+
gem 'rails-html-sanitizer'
|
223
|
+
|
224
|
+
And then execute:
|
225
|
+
|
226
|
+
$ bundle
|
227
|
+
|
228
|
+
Or install it yourself as:
|
229
|
+
|
230
|
+
$ gem install rails-html-sanitizer
|
231
|
+
|
232
|
+
|
233
|
+
## Support matrix
|
234
|
+
|
235
|
+
| branch | ruby support | actively maintained | security support |
|
236
|
+
|--------|--------------|---------------------|----------------------------------------|
|
237
|
+
| 1.6.x | >= 2.7 | yes | yes |
|
238
|
+
| 1.5.x | >= 2.5 | no | while Rails 6.1 is in security support |
|
239
|
+
| 1.4.x | >= 1.8.7 | no | no |
|
240
|
+
|
241
|
+
|
193
242
|
## Read more
|
194
243
|
|
195
244
|
Loofah is what underlies the sanitizers and scrubbers of rails-html-sanitizer.
|
245
|
+
|
196
246
|
- [Loofah and Loofah Scrubbers](https://github.com/flavorjones/loofah)
|
197
247
|
|
198
248
|
The `node` argument passed to some methods in a custom scrubber is an instance of `Nokogiri::XML::Node`.
|
249
|
+
|
199
250
|
- [`Nokogiri::XML::Node`](https://nokogiri.org/rdoc/Nokogiri/XML/Node.html)
|
200
251
|
- [Nokogiri](http://nokogiri.org)
|
201
252
|
|
202
|
-
## Contributing to Rails Html Sanitizers
|
203
253
|
|
204
|
-
|
254
|
+
## Contributing to Rails HTML Sanitizers
|
255
|
+
|
256
|
+
Rails HTML Sanitizers is work of many contributors. You're encouraged to submit pull requests, propose features and discuss issues.
|
205
257
|
|
206
258
|
See [CONTRIBUTING](CONTRIBUTING.md).
|
207
259
|
|
208
260
|
### Security reports
|
209
261
|
|
210
|
-
Trying to report a possible security vulnerability in this project? Please
|
211
|
-
|
212
|
-
guidelines about how to proceed.
|
262
|
+
Trying to report a possible security vulnerability in this project? Please check out the [Rails project's security policy](https://rubyonrails.org/security) for instructions.
|
263
|
+
|
213
264
|
|
214
265
|
## License
|
215
|
-
|
266
|
+
|
267
|
+
Rails HTML Sanitizers is released under the [MIT License](MIT-LICENSE).
|