Skip to content

Commit 5a1006f

Browse files
authoredMay 11, 2023
Merge pull request #156 from rails/flavorjones-add-scrubber-test-coverage
allow `time` tag and `lang` attr, remove `XPATHS_TO_REMOVE`, add test coverage, get JRuby green
2 parents 0c567b4 + 57c8015 commit 5a1006f

File tree

6 files changed

+335
-85
lines changed

6 files changed

+335
-85
lines changed
 

‎.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ jobs:
4747
- run: bundle exec rake
4848

4949
jruby:
50-
continue-on-error: true # nokogiri on jruby has different behavior
5150
strategy:
5251
fail-fast: false
5352
matrix:

‎.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,6 @@ Minitest/SkipEnsure:
343343

344344
Minitest/UnreachableAssertion:
345345
Enabled: true
346+
347+
Minitest/NoAssertions:
348+
Enabled: true

‎CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## next / unreleased
2+
3+
* `SafeListSanitizer` allows `time` tag and `lang` attribute by default.
4+
5+
*Mike Dalessio*
6+
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.
9+
10+
*Mike Dalessio*
11+
12+
113
## 1.5.0 / 2023-01-20
214

315
* `SafeListSanitizer`, `PermitScrubber`, and `TargetScrubber` now all support pruning of unsafe tags.

‎lib/rails/html/sanitizer.rb

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
module Rails
44
module Html
5-
XPATHS_TO_REMOVE = %w{.//script .//form comment()}
6-
75
class Sanitizer # :nodoc:
86
def sanitize(html, options = {})
97
raise NotImplementedError, "subclasses must implement sanitize method."
@@ -33,7 +31,6 @@ def sanitize(html, options = {})
3331

3432
loofah_fragment = Loofah.fragment(html)
3533

36-
remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
3734
loofah_fragment.scrub!(TextOnlyScrubber.new)
3835

3936
properly_encode(loofah_fragment, encoding: "UTF-8")
@@ -106,10 +103,65 @@ class << self
106103
attr_accessor :allowed_tags
107104
attr_accessor :allowed_attributes
108105
end
109-
self.allowed_tags = Set.new(%w(strong em b i p code pre tt samp kbd var sub
110-
sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dl dt dd abbr
111-
acronym a img blockquote del ins))
112-
self.allowed_attributes = Set.new(%w(href src width height alt cite datetime title class name xml:lang abbr))
106+
self.allowed_tags = Set.new([
107+
"a",
108+
"abbr",
109+
"acronym",
110+
"address",
111+
"b",
112+
"big",
113+
"blockquote",
114+
"br",
115+
"cite",
116+
"code",
117+
"dd",
118+
"del",
119+
"dfn",
120+
"div",
121+
"dl",
122+
"dt",
123+
"em",
124+
"h1",
125+
"h2",
126+
"h3",
127+
"h4",
128+
"h5",
129+
"h6",
130+
"hr",
131+
"i",
132+
"img",
133+
"ins",
134+
"kbd",
135+
"li",
136+
"ol",
137+
"p",
138+
"pre",
139+
"samp",
140+
"small",
141+
"span",
142+
"strong",
143+
"sub",
144+
"sup",
145+
"time",
146+
"tt",
147+
"ul",
148+
"var",
149+
])
150+
self.allowed_attributes = Set.new([
151+
"abbr",
152+
"alt",
153+
"cite",
154+
"class",
155+
"datetime",
156+
"height",
157+
"href",
158+
"lang",
159+
"name",
160+
"src",
161+
"title",
162+
"width",
163+
"xml:lang",
164+
])
113165

114166
def initialize(prune: false)
115167
@permit_scrubber = PermitScrubber.new(prune: prune)
@@ -129,7 +181,6 @@ def sanitize(html, options = {})
129181
@permit_scrubber.attributes = allowed_attributes(options)
130182
loofah_fragment.scrub!(@permit_scrubber)
131183
else
132-
remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
133184
loofah_fragment.scrub!(:strip)
134185
end
135186

0 commit comments

Comments
 (0)