Skip to content

Commit

Permalink
Fix downloading from redirects
Browse files Browse the repository at this point in the history
Depending on your region inkbunny can serve you urls
that redirect to a different content server. The downloaded file
would contain the redirect text from the initial request and fall
apart during analyzation.
  • Loading branch information
Earlopain committed Sep 18, 2023
1 parent ec64533 commit 4fab876
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/logical/sites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ def download_headers_for_image_uri(uri)
def download_file(outfile, url)
fixed_uri = fix_url(url)
headers = download_headers_for_image_uri(fixed_uri)
response = HTTParty.get(fixed_uri, { headers: headers }) do |chunk|
outfile.write(chunk)
response = HTTParty.get(fixed_uri, headers: headers) do |fragment|
next if [301, 302].include?(fragment.code)

outfile.write(fragment)
end
outfile.rewind
response
Expand Down
15 changes: 15 additions & 0 deletions test/logical/sites_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,19 @@ def assert_extracted(url:, site:, username:, valid: true)
)
end
end

describe "download_file" do
it "properly handles redirects" do
redirect_url = "https://example.com/redirected"
initial_request = stub_request_once(:get, "https://example.com", body: "Your are being redirected", status: 302, headers: { Location: redirect_url })
redirect_request = stub_request_once(:get, redirect_url, body: "Actual content")

file = Tempfile.new(binmode: true)
Sites.download_file(file, "https://example.com")

assert_requested(initial_request)
assert_requested(redirect_request)
assert_equal("Actual content", file.read)
end
end
end

0 comments on commit 4fab876

Please sign in to comment.