Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overly aggressive remote image url escaping in specific instances #2457

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/carrierwave/downloader/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def download(url, remote_headers = {})
def process_uri(uri)
uri_parts = uri.split('?')
encoded_uri = Addressable::URI.parse(uri_parts.shift).normalize.to_s
encoded_uri << '?' << Addressable::URI.encode(uri_parts.join('?')).gsub('%5B', '[').gsub('%5D', ']') if uri_parts.any?
encoded_uri << '?' << Addressable::URI.encode(uri_parts.join('?')).
gsub('%5B', '[').gsub('%253A%252F%252F', '%3A%2F%2F').
Copy link
Contributor Author

@courtsimas courtsimas Mar 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specifically targeting for the :// portion of the https:// or http:// or some-protocol:// and removing the double escape. This, coupled with the change in the next line, is what fixes the issue.

gsub('%252F', '%2F').gsub('%5D', ']') if uri_parts.any?
URI.parse(encoded_uri)
rescue URI::InvalidURIError, Addressable::URI::InvalidURIError
raise CarrierWave::DownloadError, "couldn't parse URL: #{uri}"
Expand Down
7 changes: 7 additions & 0 deletions spec/downloader/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@
expect(processed.to_s).to eq('http://example.com/%20%25%5B%5D.jpg')
end

it "parses but not escape uris with query-string characters representing urls not needing escaping" do
uri = 'http://example.com/?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1234_1280x720.jpg'
processed = subject.process_uri(uri)
expect(processed.class).to eq(URI::HTTP)
expect(processed.to_s).to eq(uri)
end

it "escapes and parse brackets in uri paths without harming the query string" do
uri = 'http://example.com/].jpg?test[]'
processed = subject.process_uri(uri)
Expand Down