From 45521bfd3e050fe680f05dd33a4a606d7ae6e871 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Thu, 18 Jun 2020 11:56:03 +0800 Subject: [PATCH] Fix handling of redirect response without Location header --- lib/fastimage.rb | 5 ++++- test/test.rb | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/fastimage.rb b/lib/fastimage.rb index fe630c8..96c7222 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -296,7 +296,10 @@ def fetch_using_http_from_parsed_uri if res.is_a?(Net::HTTPRedirection) && @redirect_count < 4 @redirect_count += 1 begin - @parsed_uri = URI.join(@parsed_uri, escaped_location(res['Location'])) + location = res['Location'] + raise ImageFetchFailure if location.nil? || location.empty? + + @parsed_uri = URI.join(@parsed_uri, escaped_location(location)) rescue URI::InvalidURIError else fetch_using_http_from_parsed_uri diff --git a/test/test.rb b/test/test.rb index 6a4a5fc..3d3f9d3 100644 --- a/test/test.rb +++ b/test/test.rb @@ -300,6 +300,13 @@ def test_should_handle_permanent_redirect_with_encoded_url assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(TestUrl, :raise_on_failure=>true) end + def test_should_handle_permanent_redirect_with_missing_location + register_redirect(TestUrl, nil) + assert_raises(FastImage::ImageFetchFailure) do + FastImage.size(TestUrl, :raise_on_failure=>true) + end + end + def register_redirect(from, to) resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved") resp['Location'] = to