Sanitize URL before parsing (#17559)

URL strings that have leading or trailing spaces raise exceptions when
parsing.
This commit is contained in:
Jamie Gaskins 2022-05-03 14:28:20 -04:00 committed by GitHub
parent 3fc6108651
commit c568557ead
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View file

@ -52,6 +52,7 @@ module ArticlesHelper
end
def get_host_without_www(url)
url = url.strip
url = "http://#{url}" if Addressable::URI.parse(url).scheme.nil?
host = Addressable::URI.parse(url).host.downcase
host.gsub!("medium.com", "Medium")

View file

@ -21,6 +21,11 @@ describe ArticlesHelper do
host = helper.get_host_without_www("www.example.com")
expect(host).to eq "example.com"
end
it "can handle URLs with leading and trailing whitespace" do
host = helper.get_host_without_www(" www.example.com ")
expect(host).to eq "example.com"
end
end
describe "#image_tag_or_inline_svg_tag" do