diff --git a/app/models/story_parser.rb b/app/models/story_parser.rb index 9647707480..b6d48316ca 100644 --- a/app/models/story_parser.rb +++ b/app/models/story_parser.rb @@ -805,7 +805,9 @@ def download_with_timeout(location, limit = 10) story = response.body when Net::HTTPRedirection if limit.positive? - story = download_with_timeout(response['location'], limit - 1) + new_uri = URI.parse(response["location"]) + new_uri = URI.join(uri, new_uri) if new_uri.relative? + story = download_with_timeout(new_uri.to_s, limit - 1) end else Rails.logger.error("------- STORY PARSER: download_with_timeout: response is not success or redirection ------") diff --git a/spec/models/story_parser_spec.rb b/spec/models/story_parser_spec.rb index 15ddfbe03e..108950a12b 100644 --- a/spec/models/story_parser_spec.rb +++ b/spec/models/story_parser_spec.rb @@ -155,6 +155,29 @@ class StoryParser end end + describe "#download_text" do + before do + WebMock.stub_request(:get, "http://example.org/foo") + .to_return(status: 200, body: "the response of the redirect target", headers: {}) + end + + it "follows relative redirects" do + input_url = "http://example.org/bar" + WebMock.stub_request(:get, input_url) + .to_return(status: 302, headers: { "Location" => "/foo" }) + + expect(@sp.send(:download_text, input_url)).to eq("the response of the redirect target") + end + + it "follows absolute redirects" do + input_url = "http://foo.com/" + WebMock.stub_request(:get, input_url) + .to_return(status: 302, headers: { "Location" => "http://example.org/foo" }) + + expect(@sp.send(:download_text, input_url)).to eq("the response of the redirect target") + end + end + describe "#parse_common" do it "converts relative to absolute links" do # This one doesn't work because the sanitizer is converting the & to &