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

Incorrect S3 presigned_url when :host option passed #592

Open
randaalex opened this issue Jul 19, 2022 · 2 comments
Open

Incorrect S3 presigned_url when :host option passed #592

randaalex opened this issue Jul 19, 2022 · 2 comments

Comments

@randaalex
Copy link

Brief Description

URL with incorrect signature built for the private S3 bucket, when host option passed.

Current Shrine::Storage::S3.url implementation replaces host (line 149) after signing (line 144).
And it leads to SignatureDoesNotMatch because now the signature is invalid (checked on minio, but believe the same behavior will be on S3).
I think the host should be changed before signing.

def url(id, public: self.public, host: nil, **options)
if public || signer
url = object(id).public_url(**options)
else
url = object(id).presigned_url(:get, **options)
end
if host
uri = URI.parse(url)
uri.path = uri.path.match(/^\/#{bucket.name}/).post_match unless uri.host.include?(bucket.name)
url = URI.join(host, uri.request_uri[1..-1]).to_s
end

@janko
Copy link
Member

janko commented Oct 6, 2022

Thanks for reporting. Any idea how to get the AWS SDK to sign the URL with the custom host? I couldn't figure it out, nothing shows up in Google, nor can I see a way in the source code.

@jrochkind
Copy link
Contributor

jrochkind commented Oct 6, 2022

Ugh. It looks like maybe you're supposed to pass the hostname as the argument when you create the Bucket object, instead of the bucket name, and then pass virtual_host: true to presigned_url? That is... not nice.

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#presigned_url-instance_method

bucket = Aws::S3::Bucket.new('my.bucket.com')
bucket.object('key').presigned_url(virtual_host: true) 
#=> "http://my.bucket.com/key?..."

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

No branches or pull requests

3 participants