FIX: Always allow downloads from CDN
This commit is contained in:
parent
f101e052e6
commit
3b0cbf7013
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue