From 0617945f0dbdda430d28368a6200ff1d5db3ee1b Mon Sep 17 00:00:00 2001 From: Constantine Nathanson Date: Sun, 6 Dec 2026 11:20:38 +0200 Subject: [PATCH] Verify TLS certificates in `Cloudinary::Downloader` Co-Authored-By: Claude Opus 5.5 --- lib/cloudinary/downloader.rb | 1 - spec/downloader_spec.rb | 95 ++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 spec/downloader_spec.rb diff --git a/lib/cloudinary/downloader.rb b/lib/cloudinary/downloader.rb index 712acdd0..bdaaa042 100644 --- a/lib/cloudinary/downloader.rb +++ b/lib/cloudinary/downloader.rb @@ -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| diff --git a/spec/downloader_spec.rb b/spec/downloader_spec.rb new file mode 100644 index 00000000..2c51b203 --- /dev/null +++ b/spec/downloader_spec.rb @@ -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