Skip to content

Commit

Permalink
Merge pull request #156 from rails/flavorjones-add-scrubber-test-cove…
Browse files Browse the repository at this point in the history
…rage

allow `time` tag and `lang` attr, remove `XPATHS_TO_REMOVE`, add test coverage, get JRuby green
  • Loading branch information
flavorjones committed May 11, 2023
2 parents 0c567b4 + 57c8015 commit 5a1006f
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 85 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -47,7 +47,6 @@ jobs:
- run: bundle exec rake

jruby:
continue-on-error: true # nokogiri on jruby has different behavior
strategy:
fail-fast: false
matrix:
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -343,3 +343,6 @@ Minitest/SkipEnsure:

Minitest/UnreachableAssertion:
Enabled: true

Minitest/NoAssertions:
Enabled: true
12 changes: 12 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,15 @@
## next / unreleased

* `SafeListSanitizer` allows `time` tag and `lang` attribute by default.

*Mike Dalessio*

* `Rails::Html::XPATHS_TO_REMOVE` has been removed. It's not necessary with the existing sanitizers,
and should have been a private constant all along anyway.

*Mike Dalessio*


## 1.5.0 / 2023-01-20

* `SafeListSanitizer`, `PermitScrubber`, and `TargetScrubber` now all support pruning of unsafe tags.
Expand Down
67 changes: 59 additions & 8 deletions lib/rails/html/sanitizer.rb
Expand Up @@ -2,8 +2,6 @@

module Rails
module Html
XPATHS_TO_REMOVE = %w{.//script .//form comment()}

class Sanitizer # :nodoc:
def sanitize(html, options = {})
raise NotImplementedError, "subclasses must implement sanitize method."
Expand Down Expand Up @@ -33,7 +31,6 @@ def sanitize(html, options = {})

loofah_fragment = Loofah.fragment(html)

remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
loofah_fragment.scrub!(TextOnlyScrubber.new)

properly_encode(loofah_fragment, encoding: "UTF-8")
Expand Down Expand Up @@ -106,10 +103,65 @@ class << self
attr_accessor :allowed_tags
attr_accessor :allowed_attributes
end
self.allowed_tags = Set.new(%w(strong em b i p code pre tt samp kbd var sub
sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dl dt dd abbr
acronym a img blockquote del ins))
self.allowed_attributes = Set.new(%w(href src width height alt cite datetime title class name xml:lang abbr))
self.allowed_tags = Set.new([
"a",
"abbr",
"acronym",
"address",
"b",
"big",
"blockquote",
"br",
"cite",
"code",
"dd",
"del",
"dfn",
"div",
"dl",
"dt",
"em",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"hr",
"i",
"img",
"ins",
"kbd",
"li",
"ol",
"p",
"pre",
"samp",
"small",
"span",
"strong",
"sub",
"sup",
"time",
"tt",
"ul",
"var",
])
self.allowed_attributes = Set.new([
"abbr",
"alt",
"cite",
"class",
"datetime",
"height",
"href",
"lang",
"name",
"src",
"title",
"width",
"xml:lang",
])

def initialize(prune: false)
@permit_scrubber = PermitScrubber.new(prune: prune)
Expand All @@ -129,7 +181,6 @@ def sanitize(html, options = {})
@permit_scrubber.attributes = allowed_attributes(options)
loofah_fragment.scrub!(@permit_scrubber)
else
remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
loofah_fragment.scrub!(:strip)
end

Expand Down

0 comments on commit 5a1006f

Please sign in to comment.