Commit 559e7a5c authored by Pascal Fautré's avatar Pascal Fautré Committed by Robert May

Skip LFS fragment on redirect

Fixes issue #335495 : LFS download service not skipping body fragment on redirect

Changelog: fixed
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65751
parent 9bedc00e
......@@ -61,8 +61,10 @@ module Projects
def download_and_save_file!(file)
digester = Digest::SHA256.new
fetch_file do |fragment|
digester << fragment
file.write(fragment)
if digest_fragment?(fragment)
digester << fragment
file.write(fragment)
end
raise_size_error! if file.size > lfs_size
end
......@@ -71,6 +73,10 @@ module Projects
raise_oid_error! if digester.hexdigest != lfs_oid
end
def digest_fragment?(fragment)
fragment.http_response.is_a?(Net::HTTPSuccess)
end
def download_options
http_options = { headers: lfs_headers, stream_body: true }
......
......@@ -106,6 +106,26 @@ RSpec.describe Projects::LfsPointers::LfsDownloadService do
end
end
context 'when file download returns a redirect' do
let(:redirect_link) { 'http://external-link' }
before do
stub_full_request(download_link).to_return(status: 301, body: 'You are being redirected', headers: { 'Location' => redirect_link } )
stub_full_request(redirect_link).to_return(body: lfs_content)
end
it_behaves_like 'lfs object is created'
it 'correctly stores lfs object' do
subject.execute
new_lfs_object = LfsObject.first
expect(new_lfs_object).to have_attributes(oid: oid, size: size)
expect(File.binread(new_lfs_object.file.file.file)).to eq lfs_content
end
end
context 'when downloaded lfs file has a different size' do
let(:size) { 1 }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment