FIX: absolute URL for CDN should always be rooted with a protocol

This commit is contained in:
Sam 2016-09-05 15:57:46 +10:00
parent e4b75f604c
commit 59640bae3b
2 changed files with 16 additions and 0 deletions

View file

@ -9,6 +9,7 @@ class UrlHelper
end end
def self.absolute(url, cdn = Discourse.asset_host) def self.absolute(url, cdn = Discourse.asset_host)
cdn = "https:" << cdn if cdn && cdn =~ /^\/\//
url =~ /^\/[^\/]/ ? (cdn || Discourse.base_url_no_prefix) + url : url url =~ /^\/[^\/]/ ? (cdn || Discourse.base_url_no_prefix) + url : url
end end

View file

@ -30,6 +30,21 @@ describe UrlHelper do
describe "#absolute" do describe "#absolute" do
it "returns an absolute URL for CDN" do
begin
Rails.configuration.action_controller.asset_host = "//cdn.awesome.com"
expect(UrlHelper.absolute("/test.jpg")).to eq("https://cdn.awesome.com/test.jpg")
Rails.configuration.action_controller.asset_host = "https://cdn.awesome.com"
expect(UrlHelper.absolute("/test.jpg")).to eq("https://cdn.awesome.com/test.jpg")
Rails.configuration.action_controller.asset_host = "http://cdn.awesome.com"
expect(UrlHelper.absolute("/test.jpg")).to eq("http://cdn.awesome.com/test.jpg")
ensure
Rails.configuration.action_controller.asset_host = nil
end
end
it "does not change non-relative url" do it "does not change non-relative url" do
expect(UrlHelper.absolute("http://www.discourse.org")).to eq("http://www.discourse.org") expect(UrlHelper.absolute("http://www.discourse.org")).to eq("http://www.discourse.org")
end end