Skip to content

Commit

Permalink
AO3-5744 Fix following relative redirects in work importing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilka2 committed Jun 23, 2024
1 parent f47e0f5 commit 5b38ddd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/story_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------")
Expand Down
23 changes: 23 additions & 0 deletions spec/models/story_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down

0 comments on commit 5b38ddd

Please sign in to comment.