A platform for community discussion. Free, open, simple.
FIX: make get_hostname more lenient to user input
Régis Hanola0a06492d855
diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rbindex 1a26e82204c..20cd574a58a 100644--- a/lib/site_setting_extension.rb+++ b/lib/site_setting_extension.rb@@ -252,13 +252,17 @@ module SiteSettingExtensionrefresh_settings.include?(name.to_sym)end+ HOSTNAME_SETTINGS ||= %w{+ disabled_image_download_domains onebox_domains_blacklist exclude_rel_nofollow_domains+ email_domains_blacklist email_domains_whitelist white_listed_spam_host_domains+ }+def filter_value(name, value)- if %w[disabled_image_download_domains onebox_domains_blacklist exclude_rel_nofollow_domains email_domains_blacklist email_domains_whitelist white_listed_spam_host_domains].include? name- domain_array = []- value.split('|').each { |url| domain_array << get_hostname(url) }- value = domain_array.join("|")+ if HOSTNAME_SETTINGS.include?(name)+ value.split("|").map { |url| get_hostname(url) }.compact.uniq.join("|")+ else+ valueend- valueenddef set(name, value)@@ -339,16 +343,21 @@ module SiteSettingExtensionenddef get_hostname(url)- uri = begin- URI.parse(url)+ url.strip!++ host = begin+ URI.parse(url)&.hostrescue URI::InvalidURIError+ nilend- unless uri.scheme.nil?- url = uri.host+ host ||= begin+ URI.parse("http://#{url}")&.host+ rescue URI::InvalidURIError+ nilend- url+ host.presence || urlendprivatediff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rbindex 8adf3c15567..77e9b735d6a 100644--- a/spec/components/site_setting_extension_spec.rb+++ b/spec/components/site_setting_extension_spec.rb@@ -586,4 +586,15 @@ describe SiteSettingExtension doendend+ describe "get_hostname" do++ it "properly extracts the hostname" do+ expect(settings.send(:get_hostname, "discourse.org")).to eq("discourse.org")+ expect(settings.send(:get_hostname, " discourse.org ")).to eq("discourse.org")+ expect(settings.send(:get_hostname, "@discourse.org")).to eq("discourse.org")+ expect(settings.send(:get_hostname, "https://discourse.org")).to eq("discourse.org")+ end++ end+end