From c952bae52c0e43ea8f33a6d0f59eb5ea1d0036e9 Mon Sep 17 00:00:00 2001 From: Ransom Briggs Date: Thu, 20 Dec 2018 15:49:23 -0600 Subject: [PATCH] Fix issue with subdomains and buckets with periods --- lib/carrierwave/storage/fog.rb | 6 +++++- spec/storage/fog_helper.rb | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/carrierwave/storage/fog.rb b/lib/carrierwave/storage/fog.rb index 074409a8e..92a3c8b8b 100644 --- a/lib/carrierwave/storage/fog.rb +++ b/lib/carrierwave/storage/fog.rb @@ -359,8 +359,12 @@ def public_url "#{@uploader.fog_credentials[:endpoint]}/#{@uploader.fog_directory}/#{encoded_path}" else protocol = @uploader.fog_use_ssl_for_aws ? "https" : "http" + + subdomain_regex = /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/ + valid_subdomain = @uploader.fog_directory.to_s =~ subdomain_regex && !(protocol == 'https' && @uploader.fog_directory =~ /\./) + # if directory is a valid subdomain, use that style for access - if @uploader.fog_directory.to_s =~ /^(?:[a-z]|\d(?!\d{0,2}(?:\d{1,3}){3}$))(?:[a-z0-9\.]|(?![\-])|\-(?![\.])){1,61}[a-z0-9]$/ + if valid_subdomain s3_subdomain = @uploader.fog_aws_accelerate ? "s3-accelerate" : "s3" "#{protocol}://#{@uploader.fog_directory}.#{s3_subdomain}.amazonaws.com/#{encoded_path}" else diff --git a/spec/storage/fog_helper.rb b/spec/storage/fog_helper.rb index 1e89bbbf7..68a6dd826 100644 --- a/spec/storage/fog_helper.rb +++ b/spec/storage/fog_helper.rb @@ -88,19 +88,19 @@ class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base context "directory is a valid subdomain" do before do - allow(@uploader).to receive(:fog_directory).and_return('assets.site.com') + allow(@uploader).to receive(:fog_directory).and_return('assets-site-com') end it "should use a subdomain URL for AWS" do if @provider == 'AWS' - expect(@fog_file.public_url).to include('https://assets.site.com.s3.amazonaws.com') + expect(@fog_file.public_url).to include('https://assets-site-com.s3.amazonaws.com') end end it "should use accelerate domain if fog_aws_accelerate is true" do if @provider == 'AWS' allow(@uploader).to receive(:fog_aws_accelerate).and_return(true) - expect(@fog_file.public_url).to include('https://assets.site.com.s3-accelerate.amazonaws.com') + expect(@fog_file.public_url).to include('https://assets-site-com.s3-accelerate.amazonaws.com') end end end @@ -112,6 +112,22 @@ class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base end end + it "should not use a subdomain URL for AWS if https && the directory is not accessible over https as a virtual hosted bucket" do + if @provider == 'AWS' + allow(@uploader).to receive(:fog_use_ssl_for_aws).and_return(true) + allow(@uploader).to receive(:fog_directory).and_return('foo.bar') + expect(@fog_file.public_url).to include('https://s3.amazonaws.com/foo.bar') + end + end + + it "should use a subdomain URL for AWS if http && the directory is not accessible over https as a virtual hosted bucket" do + if @provider == 'AWS' + allow(@uploader).to receive(:fog_use_ssl_for_aws).and_return(false) + allow(@uploader).to receive(:fog_directory).and_return('foo.bar') + expect(@fog_file.public_url).to include('http://foo.bar.s3.amazonaws.com/') + end + end + it "should use https as a default protocol" do if @provider == 'AWS' expect(@fog_file.public_url).to start_with 'https://'