Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ COMMENT_MODERATION_THRESHOLD: 10
EDITED_COMMENT_SPAM_CHECK_THRESHOLD: 10

# SANITIZER VERSION
SANITIZER_VERSION: 4
SANITIZER_VERSION: 5

# parameters that must be natural integers and their default values
NONZERO_INTEGER_PARAMETERS:
Expand Down
11 changes: 11 additions & 0 deletions config/initializers/gem-plugin_config/sanitizer_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,16 @@ module Config
rescue URI::InvalidURIError
# do nothing, the sanitizer will handle it
end

# On a elements, blank out hrefs that aren't valid URLs
INVALID_HREF_TRANSFORMER = lambda do |env|
return unless env[:node_name] == "a" && env[:node]["href"]

host = Addressable::URI.parse(env[:node]["href"]).host

env[:node]["href"] = "" if host&.start_with?("[") && !host&.end_with?("]")
rescue Addressable::URI::InvalidURIError
env[:node]["href"] = ""
end
end
end
3 changes: 2 additions & 1 deletion lib/html_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def sanitize_value(field, value)
# We're allowing users to use HTML in this field
transformers = [
Sanitize::Config::OPEN_ATTRIBUTE_TRANSFORMER,
Sanitize::Config::RELATIVE_IMAGE_PATH_TRANSFORMER
Sanitize::Config::RELATIVE_IMAGE_PATH_TRANSFORMER,
Sanitize::Config::INVALID_HREF_TRANSFORMER
]
if ArchiveConfig.FIELDS_ALLOWING_MEDIA_EMBEDS.include?(field.to_s)
transformers << OtwSanitize::EmbedSanitizer.transformer
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/html_cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,23 @@ def one_cell_table(content)
end
end

context "when given an <a> tag with an invalid href" do
it "should clean up an invalid href while preserving the link text" do
result = sanitize_value(field, "<a href='https://[https://test.archiveofourown.org/users/testy]' rel='nofollow'>testy</a>")
expect(result).to eq("<p><a href=\"\" rel=\"nofollow\">testy</a></p>")
end

it "should not clean up a valid href" do
result = sanitize_value(field, "<a href='https://test.archiveofourown.org/users/testy' rel='nofollow'>testy</a>")
expect(result).to eq("<p><a href=\"https://test.archiveofourown.org/users/testy\" rel=\"nofollow\">testy</a></p>")
end

it "should not clean up a valid href with non-ASCII characters" do
result = sanitize_value(field, "<a href='https://test.archiveofourown.org/users/café' rel='nofollow'>testy</a>")
expect(result).to eq("<p><a href=\"https://test.archiveofourown.org/users/café\" rel=\"nofollow\">testy</a></p>")
end
end

# These are from https://github.com/rgrove/sanitize/commit/a11498de9e283cd457b35ee252983662f7452aa9
it 'should not preserve the content of removed `math` elements' do
content = sanitize_value(field, '<math>hello! <script>alert(0)</script></math>')
Expand Down
Loading