From c568557eadd14fd536d3dab3610fdbe696564ca0 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Tue, 3 May 2022 14:28:20 -0400 Subject: [PATCH] Sanitize URL before parsing (#17559) URL strings that have leading or trailing spaces raise exceptions when parsing. --- app/helpers/articles_helper.rb | 1 + spec/helpers/articles_helper_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb index de8d8b560..dbbdb5732 100644 --- a/app/helpers/articles_helper.rb +++ b/app/helpers/articles_helper.rb @@ -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") diff --git a/spec/helpers/articles_helper_spec.rb b/spec/helpers/articles_helper_spec.rb index fbbc7d86f..6baa3ed75 100644 --- a/spec/helpers/articles_helper_spec.rb +++ b/spec/helpers/articles_helper_spec.rb @@ -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