Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove href from LinkSanitizer tags list #92

Merged
merged 2 commits into from May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/rails/html/sanitizer.rb
Expand Up @@ -40,15 +40,16 @@ def sanitize(html, options = {})
end

# === Rails::Html::LinkSanitizer
# Removes a tags and href attributes leaving only the link text
# Removes +a+ tags and +href+ attributes leaving only the link text.
#
# link_sanitizer = Rails::Html::LinkSanitizer.new
# link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
# # => Only the link text will be kept.
# link_sanitizer = Rails::Html::LinkSanitizer.new
# link_sanitizer.sanitize('<a href="example.com">Only the link text will be kept.</a>')
#
# => 'Only the link text will be kept.'
class LinkSanitizer < Sanitizer
def initialize
@link_scrubber = TargetScrubber.new
@link_scrubber.tags = %w(a href)
@link_scrubber.tags = %w(a)
@link_scrubber.attributes = %w(href)
end

Expand Down Expand Up @@ -146,7 +147,7 @@ def allowed_tags(options)

def allowed_attributes(options)
options[:attributes] || self.class.allowed_attributes
end
end
end

WhiteListSanitizer = SafeListSanitizer
Expand Down
4 changes: 0 additions & 4 deletions test/sanitizer_test.rb
Expand Up @@ -154,10 +154,6 @@ def test_strip_links_with_linkception
assert_equal "Magic", link_sanitize("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
end

def test_strip_links_with_a_tag_in_href
assert_equal "FrrFox", link_sanitize("<href onlclick='steal()'>FrrFox</a></href>")
end

def test_sanitize_form
assert_sanitized "<form action=\"/foo/bar\" method=\"post\"><input></form>", ''
end
Expand Down