Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/cloudinary/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def self.download(source, options={})

if url.port == 443
http.use_ssl=true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

res = http.start{|agent|
Expand Down
95 changes: 95 additions & 0 deletions spec/downloader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require 'spec_helper'
require 'cloudinary'
require 'net/http'
require 'openssl'
require 'socket'

describe Cloudinary::Downloader do
DOWNLOADER_TEST_BODY = "downloaded-content"

# Builds a certificate for the given SAN, signed by issuer (self-signed when nil)
def build_cert(subject, san, key, issuer = nil, issuer_key = nil, ca = false)
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = rand(1 << 32)
cert.subject = OpenSSL::X509::Name.parse("/CN=#{subject}")
cert.issuer = issuer ? issuer.subject : cert.subject
cert.public_key = key.public_key
cert.not_before = Time.now - 60
cert.not_after = Time.now + 3600
ext = OpenSSL::X509::ExtensionFactory.new
ext.subject_certificate = cert
ext.issuer_certificate = issuer || cert
cert.add_extension(ext.create_extension("basicConstraints", ca ? "CA:TRUE" : "CA:FALSE", true))
cert.add_extension(ext.create_extension("keyUsage", "keyCertSign,cRLSign", true)) if ca
cert.add_extension(ext.create_extension("subjectAltName", san, false)) if san
cert.sign(issuer_key || key, OpenSSL::Digest::SHA256.new)
cert
end

# Serves DOWNLOADER_TEST_BODY over TLS on an ephemeral local port with the given certificate
def start_tls_server(cert, key)
ctx = OpenSSL::SSL::SSLContext.new
ctx.cert = cert
ctx.key = key
tcp = TCPServer.new("127.0.0.1", 0)
server = OpenSSL::SSL::SSLServer.new(tcp, ctx)
thread = Thread.new do
loop do
begin
socket = server.accept
socket.readpartial(4096)
socket.write("HTTP/1.1 200 OK\r\nContent-Length: #{DOWNLOADER_TEST_BODY.bytesize}\r\nConnection: close\r\n\r\n#{DOWNLOADER_TEST_BODY}")
socket.close
rescue OpenSSL::SSL::SSLError, Errno::ECONNRESET, EOFError
next
end
end
end
[tcp, thread]
end

let(:ca_key) { OpenSSL::PKey::RSA.new(2048) }
let(:ca_cert) { build_cert("Test CA", nil, ca_key, nil, nil, true) }
let(:leaf_key) { OpenSSL::PKey::RSA.new(2048) }
let(:trusted_store) { OpenSSL::X509::Store.new.tap { |store| store.add_cert(ca_cert) } }

before do
@tcp, @thread = start_tls_server(server_cert, leaf_key)
port = @tcp.addr[1]
# Downloader enables TLS only for port 443 URLs; connect those to the local server instead
allow(Net::HTTP).to receive(:new).and_wrap_original { |original, host, _port| original.call(host, port) }
stub_const("OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE", trusted_store)
end

after do
@thread.kill
@tcp.close
end

context "when the server certificate is valid for the host" do
let(:server_cert) { build_cert("127.0.0.1", "IP:127.0.0.1", leaf_key, ca_cert, ca_key) }

it "downloads the content" do
expect(Cloudinary::Downloader.download("https://127.0.0.1/image/upload/sample.jpg")).to eq(DOWNLOADER_TEST_BODY)
end
end

context "when the server certificate is self-signed" do
let(:server_cert) { build_cert("127.0.0.1", "IP:127.0.0.1", leaf_key) }

it "raises a certificate verification error" do
expect { Cloudinary::Downloader.download("https://127.0.0.1/image/upload/sample.jpg") }
.to raise_error(OpenSSL::SSL::SSLError, /certificate verify failed/)
end
end

context "when the server certificate is for a different host" do
let(:server_cert) { build_cert("not-cloudinary.invalid", "DNS:not-cloudinary.invalid", leaf_key, ca_cert, ca_key) }

it "raises a hostname verification error" do
expect { Cloudinary::Downloader.download("https://127.0.0.1/image/upload/sample.jpg") }
.to raise_error(OpenSSL::SSL::SSLError, /hostname/)
end
end
end
Loading