FIX: make get_hostname more lenient to user input
This commit is contained in:
parent
f042a9529b
commit
a0a06492d8
|
@ -252,13 +252,17 @@ module SiteSettingExtension
|
||||||
refresh_settings.include?(name.to_sym)
|
refresh_settings.include?(name.to_sym)
|
||||||
end
|
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)
|
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
|
if HOSTNAME_SETTINGS.include?(name)
|
||||||
domain_array = []
|
value.split("|").map { |url| get_hostname(url) }.compact.uniq.join("|")
|
||||||
value.split('|').each { |url| domain_array << get_hostname(url) }
|
else
|
||||||
value = domain_array.join("|")
|
value
|
||||||
end
|
end
|
||||||
value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(name, value)
|
def set(name, value)
|
||||||
|
@ -339,16 +343,21 @@ module SiteSettingExtension
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_hostname(url)
|
def get_hostname(url)
|
||||||
uri = begin
|
url.strip!
|
||||||
URI.parse(url)
|
|
||||||
|
host = begin
|
||||||
|
URI.parse(url)&.host
|
||||||
rescue URI::InvalidURIError
|
rescue URI::InvalidURIError
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
unless uri.scheme.nil?
|
host ||= begin
|
||||||
url = uri.host
|
URI.parse("http://#{url}")&.host
|
||||||
|
rescue URI::InvalidURIError
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
url
|
host.presence || url
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -586,4 +586,15 @@ describe SiteSettingExtension do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue