Skip to content

Commit 2b0dcb5

Browse files
authored
Merge pull request #162 from rails/flavorjones-html5-sanitizer-vendor
add an HTML5 sanitizer vendor for Rails to integrate with
2 parents 50644ff + 8ea2500 commit 2b0dcb5

File tree

3 files changed

+112
-25
lines changed

3 files changed

+112
-25
lines changed

CHANGELOG.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
## next / unreleased
22

3-
* `SafeListSanitizer` allows `time` tag and `lang` attribute by default.
3+
* Sanitizers that use an HTML5 parser are now available on platforms supported by
4+
Nokogiri::HTML5. These are available as:
5+
6+
- `Rails::HTML5::FullSanitizer`
7+
- `Rails::HTML5::LinkSanitizer`
8+
- `Rails::HTML5::SafeListSanitizer`
9+
10+
And a new "vendor" is provided at `Rails::HTML5::Sanitizer` that can be used in a future version
11+
of Rails.
12+
13+
Note that for symmetry `Rails::HTML4::Sanitizer` is also added, though its behavior is identical
14+
to the vendor class methods on `Rails::HTML::Sanitizer`.
415

516
*Mike Dalessio*
617

7-
* `Rails::Html::XPATHS_TO_REMOVE` has been removed. It's not necessary with the existing sanitizers,
8-
and should have been a private constant all along anyway.
18+
* Module namespaces have changed, but backwards compatibility is provided by aliases.
19+
20+
The library defines three additional modules:
21+
22+
- `Rails::HTML` for general functionality (replacing `Rails::Html`)
23+
- `Rails::HTML4` containing sanitizers that parse content as HTML4
24+
- `Rails::HTML5` containing sanitizers that parse content as HTML5
25+
26+
The following aliases are maintained for backwards compatibility:
27+
28+
- `Rails::Html` points to `Rails::HTML`
29+
- `Rails::HTML::FullSanitizer` points to `Rails::HTML4::FullSanitizer`
30+
- `Rails::HTML::LinkSanitizer` points to `Rails::HTML4::LinkSanitizer`
31+
- `Rails::HTML::SafeListSanitizer` points to `Rails::HTML4::SafeListSanitizer`
932

1033
*Mike Dalessio*
1134

12-
* `Rails::Html` has been renamed to `Rails::HTML`, but this module is aliased to `Rails::Html` for
13-
backwards compatibility.
35+
* `SafeListSanitizer` allows `time` tag and `lang` attribute by default.
36+
37+
*Mike Dalessio*
38+
39+
* `Rails::Html::XPATHS_TO_REMOVE` has been removed. It's not necessary with the existing sanitizers,
40+
and should have been a private constant all along anyway.
1441

1542
*Mike Dalessio*
1643

@@ -24,6 +51,7 @@
2451

2552
*seyerian*
2653

54+
2755
## 1.4.4 / 2022-12-13
2856

2957
* Address inefficient regular expression complexity with certain configurations of Rails::Html::Sanitizer.
@@ -69,6 +97,7 @@
6997

7098
*Mike Dalessio*
7199

100+
72101
## 1.4.2 / 2021-08-23
73102

74103
* Slightly improve performance.
@@ -77,6 +106,7 @@
77106

78107
*Mike Dalessio*
79108

109+
80110
## 1.4.1 / 2021-08-18
81111

82112
* Fix regression in v1.4.0 that did not pass comment nodes to the scrubber.
@@ -89,6 +119,7 @@
89119

90120
*Mike Dalessio*
91121

122+
92123
## 1.4.0 / 2021-08-18
93124

94125
* Processing Instructions are no longer allowed by Rails::Html::PermitScrubber
@@ -101,12 +132,14 @@
101132

102133
*Mike Dalessio*
103134

135+
104136
## 1.3.0
105137

106138
* Address deprecations in Loofah 2.3.0.
107139

108140
*Josh Goodall*
109141

142+
110143
## 1.2.0
111144

112145
* Remove needless `white_list_sanitizer` deprecation.
@@ -121,6 +154,7 @@
121154

122155
*Kasper Timm Hansen*
123156

157+
124158
## 1.1.0
125159

126160
* Add `safe_list_sanitizer` and deprecate `white_list_sanitizer` to be removed
@@ -138,10 +172,12 @@
138172

139173
*Kasper Timm Hansen*
140174

175+
141176
## 1.0.1
142177

143178
* Added support for Rails 4.2.0.beta2 and above
144179

180+
145181
## 1.0.0
146182

147183
* First release.

lib/rails/html/sanitizer.rb

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ module Rails
44
module HTML
55
class Sanitizer
66
class << self
7-
def full_sanitizer
8-
Rails::HTML4::FullSanitizer
9-
end
10-
11-
def link_sanitizer
12-
Rails::HTML4::LinkSanitizer
13-
end
14-
15-
def safe_list_sanitizer
16-
Rails::HTML4::SafeListSanitizer
17-
end
18-
19-
def white_list_sanitizer # :nodoc:
20-
safe_list_sanitizer
21-
end
22-
237
def html5_support?
248
return @html5_support if defined?(@html5_support)
259

@@ -209,6 +193,28 @@ def serialize(fragment)
209193
end
210194

211195
module HTML4
196+
module Sanitizer
197+
module VendorMethods
198+
def full_sanitizer
199+
Rails::HTML4::FullSanitizer
200+
end
201+
202+
def link_sanitizer
203+
Rails::HTML4::LinkSanitizer
204+
end
205+
206+
def safe_list_sanitizer
207+
Rails::HTML4::SafeListSanitizer
208+
end
209+
210+
def white_list_sanitizer # :nodoc:
211+
safe_list_sanitizer
212+
end
213+
end
214+
215+
extend VendorMethods
216+
end
217+
212218
# == Rails::HTML4::FullSanitizer
213219
#
214220
# Removes all tags from HTML4 but strips out scripts, forms and comments.
@@ -299,6 +305,26 @@ class SafeListSanitizer < Rails::HTML::Sanitizer
299305
end
300306

301307
module HTML5
308+
class Sanitizer
309+
class << self
310+
def full_sanitizer
311+
Rails::HTML5::FullSanitizer
312+
end
313+
314+
def link_sanitizer
315+
Rails::HTML5::LinkSanitizer
316+
end
317+
318+
def safe_list_sanitizer
319+
Rails::HTML5::SafeListSanitizer
320+
end
321+
322+
def white_list_sanitizer # :nodoc:
323+
safe_list_sanitizer
324+
end
325+
end
326+
end
327+
302328
# == Rails::HTML5::FullSanitizer
303329
#
304330
# Removes all tags from HTML5 but strips out scripts, forms and comments.
@@ -389,6 +415,7 @@ class SafeListSanitizer < Rails::HTML::Sanitizer
389415
end if Rails::HTML::Sanitizer.html5_support?
390416

391417
module HTML
418+
Sanitizer.extend(HTML4::Sanitizer::VendorMethods) # :nodoc:
392419
FullSanitizer = HTML4::FullSanitizer # :nodoc:
393420
LinkSanitizer = HTML4::LinkSanitizer # :nodoc:
394421
SafeListSanitizer = HTML4::SafeListSanitizer # :nodoc:

test/rails_api_test.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,43 @@ def test_html4_sanitizer_alias_safe_list
3232
assert_equal("Rails::HTML4::SafeListSanitizer", Rails::HTML::SafeListSanitizer.name)
3333
end
3434

35-
def test_full_sanitizer_returns_a_full_sanitizer
35+
def test_html4_full_sanitizer
3636
assert_equal(Rails::HTML4::FullSanitizer, Rails::HTML::Sanitizer.full_sanitizer)
37+
assert_equal(Rails::HTML4::FullSanitizer, Rails::HTML4::Sanitizer.full_sanitizer)
3738
end
3839

39-
def test_link_sanitizer_returns_a_link_sanitizer
40+
def test_html4_link_sanitizer
4041
assert_equal(Rails::HTML4::LinkSanitizer, Rails::HTML::Sanitizer.link_sanitizer)
42+
assert_equal(Rails::HTML4::LinkSanitizer, Rails::HTML4::Sanitizer.link_sanitizer)
4143
end
4244

43-
def test_safe_list_sanitizer_returns_a_safe_list_sanitizer
45+
def test_html4_safe_list_sanitizer
4446
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::Sanitizer.safe_list_sanitizer)
47+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML4::Sanitizer.safe_list_sanitizer)
4548
end
4649

47-
def test_white_list_sanitizer_returns_a_safe_list_sanitizer
50+
def test_html4_white_list_sanitizer
4851
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML::Sanitizer.white_list_sanitizer)
52+
assert_equal(Rails::HTML4::SafeListSanitizer, Rails::HTML4::Sanitizer.white_list_sanitizer)
53+
end
54+
55+
def test_html5_full_sanitizer
56+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
57+
assert_equal(Rails::HTML5::FullSanitizer, Rails::HTML5::Sanitizer.full_sanitizer)
58+
end
59+
60+
def test_html5_link_sanitizer
61+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
62+
assert_equal(Rails::HTML5::LinkSanitizer, Rails::HTML5::Sanitizer.link_sanitizer)
63+
end
64+
65+
def test_html5_safe_list_sanitizer
66+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
67+
assert_equal(Rails::HTML5::SafeListSanitizer, Rails::HTML5::Sanitizer.safe_list_sanitizer)
68+
end
69+
70+
def test_html5_white_list_sanitizer
71+
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
72+
assert_equal(Rails::HTML5::SafeListSanitizer, Rails::HTML5::Sanitizer.white_list_sanitizer)
4973
end
5074
end

0 commit comments

Comments
 (0)