FIX: Always allow downloads from CDN

This commit is contained in:
Robin Ward 2017-05-23 16:32:54 -04:00
parent f101e052e6
commit 3b0cbf7013
3 changed files with 13 additions and 4 deletions

View File

@ -11,13 +11,14 @@ class FileHelper
url = "https:" + url if url.start_with?("//") url = "https:" + url if url.start_with?("//")
raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\// raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\//
# uri = FinalDestination.new(url).resolve uri = FinalDestination.new(url, max_redirects: follow_redirect ? 5 : 1).resolve
uri = URI.parse(url) return unless uri.present?
extension = File.extname(uri.path) extension = File.extname(uri.path)
tmp = Tempfile.new([tmp_file_name, extension]) tmp = Tempfile.new([tmp_file_name, extension])
File.open(tmp.path, "wb") do |f| File.open(tmp.path, "wb") do |f|
downloaded = uri.open("rb", read_timeout: read_timeout, redirect: follow_redirect, allow_redirections: :all) downloaded = uri.open("rb", read_timeout: read_timeout)
while f.size <= max_file_size && data = downloaded.read(512.kilobytes) while f.size <= max_file_size && data = downloaded.read(512.kilobytes)
f.write(data) f.write(data)
end end

View File

@ -77,6 +77,15 @@ class FinalDestination
end end
def is_dest_valid? def is_dest_valid?
# CDNs are always allowed
return true if SiteSetting.s3_cdn_url.present? &&
@uri.hostname == URI(SiteSetting.s3_cdn_url).hostname
global_cdn = GlobalSetting.try(:cdn_url)
return true if global_cdn.present? &&
@uri.hostname == URI(global_cdn).hostname
return false unless @uri && @uri.host return false unless @uri && @uri.host
address_s = @opts[:lookup_ip].call(@uri.hostname) address_s = @opts[:lookup_ip].call(@uri.hostname)

View File

@ -57,7 +57,6 @@ describe FinalDestination do
expect(final.redirected?).to eq(false) expect(final.redirected?).to eq(false)
expect(final.status).to eq(:resolved) expect(final.status).to eq(:resolved)
end end
end end
context "underscores in URLs" do context "underscores in URLs" do