Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: Capped a media download from a URL while it streams
Damian Legawiec4edc8627157a
diff --git a/spree/core/app/jobs/spree/images/save_from_url_job.rb b/spree/core/app/jobs/spree/images/save_from_url_job.rbindex 33367343f2..ebd9e23f84 100644--- a/spree/core/app/jobs/spree/images/save_from_url_job.rb+++ b/spree/core/app/jobs/spree/images/save_from_url_job.rb@@ -75,6 +75,7 @@ module Spreedef download_and_attach_image(external_url, image)max_size = Spree::Config.max_image_download_size+ streamed_body = nilresponse = SsrfFilter.get(external_url,@@ -90,12 +91,12 @@ module Spreeread_timeout: 60,open_timeout: 30}- )+ ) { |fetched| streamed_body = read_within(fetched, max_size) }- body = response.body- if body.bytesize > max_size- raise StandardError, "Image file size exceeds the maximum allowed size of #{max_size} bytes"- end+ # A fetch that did not stream (a test double, say) is read whole and+ # held to the same limit.+ body = streamed_body || response.body+ raise_too_large(max_size) if body.bytesize > max_sizeuri = URI.parse(external_url)filename = File.basename(uri.path)@@ -114,6 +115,24 @@ module Spreeendend+ # Reads the body as it arrives and stops at the limit, so an oversized+ # or endless response is refused mid-stream rather than held in memory+ # first; a declared length over the limit is refused before any of it.+ def read_within(response, max_size)+ raise_too_large(max_size) if response['Content-Length'].to_i > max_size++ buffer = +''+ response.read_body do |chunk|+ buffer << chunk+ raise_too_large(max_size) if buffer.bytesize > max_size+ end+ buffer+ end++ def raise_too_large(max_size)+ raise StandardError, "Image file size exceeds the maximum allowed size of #{max_size} bytes"+ end+def image_already_saved?(image, external_url)image.persisted? && image.attachment.attached? && image.external_url.present? && external_url == image.external_urlenddiff --git a/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb b/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rbindex 3d5cc9aff1..1a92259104 100644--- a/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb+++ b/spree/core/spec/jobs/spree/images/save_from_url_job_spec.rb@@ -137,6 +137,33 @@ RSpec.describe Spree::Images::SaveFromUrlJob, type: :job doit 'raises an error about file size' doexpect { subject }.to raise_error(StandardError, /exceeds the maximum allowed size/)end++ # The limit applies to what is downloaded, not only to what is attached:+ # an endless response is cut off once it passes the limit.+ it 'stops reading a streamed body once it passes the limit' do+ chunks_read = 0+ streamed = double('response')+ allow(streamed).to receive(:[]).with('Content-Length').and_return(nil)+ allow(streamed).to receive(:read_body) do |&block|+ loop do+ chunks_read += 1+ block.call('x' * 512)+ end+ end+ allow(SsrfFilter).to receive(:get) { |&block| block.call(streamed); response }++ expect { subject }.to raise_error(StandardError, /exceeds the maximum allowed size/)+ expect(chunks_read).to eq(3)+ end++ it 'refuses a declared length over the limit before reading' do+ streamed = double('response')+ allow(streamed).to receive(:[]).with('Content-Length').and_return((max_size + 1).to_s)+ expect(streamed).not_to receive(:read_body)+ allow(SsrfFilter).to receive(:get) { |&block| block.call(streamed); response }++ expect { subject }.to raise_error(StandardError, /exceeds the maximum allowed size/)+ endendcontext 'when link_variant_id is provided for a product-level upload' do