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

Re-parse the virtual_host URI in presigned_url after changing the scheme. Fixes #1400, #1401 #1403

Closed

Conversation

kjwierenga
Copy link

Presigner#presigned_url should not mutate params hash when it processes paramaters such as virtual_host and secure. This is fixed by duplicating the hash. Fixes #1400

@kjwierenga
Copy link
Author

This PR is dependent on PR #1402

@awood45
Copy link
Member

awood45 commented Jan 31, 2017

Update on this - I'm trying to get an environment set up to properly test this. I believe that the port 80 explicit mention may have been for a specific bug, and I would want to avoid a regression of the same. We only have an integration test around this currently.

@awood45
Copy link
Member

awood45 commented Feb 1, 2017

Okay, there does appear to be a problem which makes this issue a bit more complicated than the included PR. Consider the original purpose of this code snippet, which is to switch the URI to http. For your use case of a non-standard port, your PR is fine:

uri = URI("https://foo.bar:3000") #=> #<URI::HTTPS https://foo.bar:3000>
uri.port #=> 3000
uri.scheme = "http"
uri #=> #<URI::HTTPS http://foo.bar:3000>
uri.port #=> 3000

However, it doesn't work for the standard HTTP/HTTPS ports - that explicit setter is needed:

uri = URI("https://foo.bar") #=> #<URI::HTTPS https://foo.bar>
uri.port #=> 443
uri.scheme = "http"
uri #=> #<URI::HTTPS http://foo.bar>
uri.port #=> 443 - wrong

That doesn't mean we can't fix this issue - I'd like to support your ability to set ports, but we need to do a bit more. I'll copy this comment to the PR.

@kjwierenga kjwierenga force-pushed the feature/fix/presigner-params-mutation branch from 9b765ea to d2ea458 Compare February 1, 2017 16:27
@kjwierenga
Copy link
Author

You can change the scheme of a URI object, but that doesn't change the class of the object. This is what is causing the default port to be appended.

uri = URI("https://foo.bar") #=> #<URI::HTTPS https://foo.bar> 
uri.to_s # => "https://foo.bar" 
uri.scheme='http'
uri.port=80
uri.to_s # => "http://foo.bar:80" 
URI.parse(uri.to_s).to_s
# Fix like this
URI(uri.to_s) # => #<URI::HTTP http://foo.bar> 

Same goes the other way around:

uri=URI("http://foo.bar") # => #<URI::HTTP http://foo.bar> 
uri.to_s # => "http://foo.bar" 
uri.scheme='https'
uri.port=443
uri.to_s # => "https://foo.bar:443" 
# Fix like this
URI(uri.to_s) # => #<URI::HTTPS https://foo.bar> 

Note how the URI subclass is determined at first parse of the input URL.

Fix is to re-parse the URI after changing the scheme to a URI object with the appropriate subclass gets created.

@kjwierenga kjwierenga changed the title Dup params hash in presigned_url so it doesn't get mutated. Fixes #1400 Re-parse the virtual_host URI in presigned_url after changing the scheme. Fixes #1400, #1401 Feb 1, 2017
@kjwierenga
Copy link
Author

This is a better way of fixing this issue I think. The solution was there all along in sign_but_dont_send which applies exactly the same technique.

@kjwierenga kjwierenga force-pushed the feature/fix/presigner-params-mutation branch from d2ea458 to 3dd0404 Compare February 1, 2017 16:37
@awood45
Copy link
Member

awood45 commented Feb 1, 2017 via email

@cjyclaire
Copy link
Contributor

PR #1477 has opened including support for this PR, closing, tracking in #1477 instead.
The spec test has been included as well.
Feel free to chime in and add comment in that PR, thanks : )

@cjyclaire cjyclaire closed this Apr 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Presigner#presigned_url mutates params hash
3 participants