From 59640bae3b700cff763e2316383b81fdd5709f34 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 5 Sep 2016 15:57:46 +1000 Subject: [PATCH] FIX: absolute URL for CDN should always be rooted with a protocol --- lib/url_helper.rb | 1 + spec/components/url_helper_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/url_helper.rb b/lib/url_helper.rb index fe9e62d28..df25e5463 100644 --- a/lib/url_helper.rb +++ b/lib/url_helper.rb @@ -9,6 +9,7 @@ class UrlHelper end def self.absolute(url, cdn = Discourse.asset_host) + cdn = "https:" << cdn if cdn && cdn =~ /^\/\// url =~ /^\/[^\/]/ ? (cdn || Discourse.base_url_no_prefix) + url : url end diff --git a/spec/components/url_helper_spec.rb b/spec/components/url_helper_spec.rb index e9f580f61..972f3daa5 100644 --- a/spec/components/url_helper_spec.rb +++ b/spec/components/url_helper_spec.rb @@ -30,6 +30,21 @@ describe UrlHelper 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 expect(UrlHelper.absolute("http://www.discourse.org")).to eq("http://www.discourse.org") end