From 6190cd896f96745f691559938c0c8d2623798e66 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 23 Jun 2020 15:32:27 -0400 Subject: [PATCH 1/6] When signing requests with sigv4, don't overwrite the Host header if it was given --- gems/aws-sigv4/lib/aws-sigv4/signer.rb | 4 ++-- gems/aws-sigv4/spec/signer_spec.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gems/aws-sigv4/lib/aws-sigv4/signer.rb b/gems/aws-sigv4/lib/aws-sigv4/signer.rb index 78f01a6db55..799d1398adf 100644 --- a/gems/aws-sigv4/lib/aws-sigv4/signer.rb +++ b/gems/aws-sigv4/lib/aws-sigv4/signer.rb @@ -219,7 +219,7 @@ def sign_request(request) content_sha256 ||= sha256_hexdigest(request[:body] || '') sigv4_headers = {} - sigv4_headers['host'] = host(url) + sigv4_headers['host'] = headers['host'] || host(url) sigv4_headers['x-amz-date'] = datetime sigv4_headers['x-amz-security-token'] = creds.session_token if creds.session_token sigv4_headers['x-amz-content-sha256'] ||= content_sha256 if @apply_checksum_header @@ -376,7 +376,7 @@ def presign_url(options) url = extract_url(options) headers = downcase_headers(options[:headers]) - headers['host'] = host(url) + headers['host'] ||= host(url) datetime = headers['x-amz-date'] datetime ||= (options[:time] || Time.now).utc.strftime("%Y%m%dT%H%M%SZ") diff --git a/gems/aws-sigv4/spec/signer_spec.rb b/gems/aws-sigv4/spec/signer_spec.rb index 96d5549fbcb..b6139f41d56 100644 --- a/gems/aws-sigv4/spec/signer_spec.rb +++ b/gems/aws-sigv4/spec/signer_spec.rb @@ -144,6 +144,17 @@ module Sigv4 expect(signature.headers['host']).to eq('domain.com') end + context 'when a Host header is provided' do + + let(:options) { options.merge('host' => 'custom-host-value') } + + it 'uses the original Host header' do + signature = Signer.new(options).sign_request(request) + + expect(signature.headers['host']).to eql(custom-host-value) + end + end + context 'when credentials are not set' do let(:creds) do Credentials.new(access_key_id: '', secret_access_key: '') From 0e6e1f7e13210ab1a9c27ab0f82d8e9dd03d75ad Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 23 Jun 2020 15:57:02 -0400 Subject: [PATCH 2/6] Add changelog entry --- gems/aws-sigv4/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gems/aws-sigv4/CHANGELOG.md b/gems/aws-sigv4/CHANGELOG.md index bf1132754da..ea5e2c5eda3 100644 --- a/gems/aws-sigv4/CHANGELOG.md +++ b/gems/aws-sigv4/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased Changes ------------------ +* Issue - Don't overwrite `host` header in sigv4 signer if given. + 1.2.0 (2020-06-17) ------------------ From 9c38541ff82656caa6ef31cdc66abda2806e00f8 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 23 Jun 2020 16:34:11 -0400 Subject: [PATCH 3/6] Fix tests --- gems/aws-sigv4/spec/signer_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gems/aws-sigv4/spec/signer_spec.rb b/gems/aws-sigv4/spec/signer_spec.rb index b6139f41d56..4b1091b467d 100644 --- a/gems/aws-sigv4/spec/signer_spec.rb +++ b/gems/aws-sigv4/spec/signer_spec.rb @@ -146,10 +146,10 @@ module Sigv4 context 'when a Host header is provided' do - let(:options) { options.merge('host' => 'custom-host-value') } + let(:headers) { 'host' => 'custom-host-value' } it 'uses the original Host header' do - signature = Signer.new(options).sign_request(request) + signature = Signer.new(options.merge(headers: headers)).sign_request(request) expect(signature.headers['host']).to eql(custom-host-value) end From 1366bad8eff8152491cff1df9b89bcda3a099e33 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 23 Jun 2020 17:16:01 -0400 Subject: [PATCH 4/6] Actually fix tests --- gems/aws-sigv4/spec/signer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gems/aws-sigv4/spec/signer_spec.rb b/gems/aws-sigv4/spec/signer_spec.rb index 4b1091b467d..0d0669ae1b8 100644 --- a/gems/aws-sigv4/spec/signer_spec.rb +++ b/gems/aws-sigv4/spec/signer_spec.rb @@ -146,7 +146,7 @@ module Sigv4 context 'when a Host header is provided' do - let(:headers) { 'host' => 'custom-host-value' } + let(:headers) { {'host' => 'custom-host-value'} } it 'uses the original Host header' do signature = Signer.new(options.merge(headers: headers)).sign_request(request) From 166bd09e3583bbc800b12217a0f36c3e1a176e53 Mon Sep 17 00:00:00 2001 From: Matt Muller Date: Tue, 23 Jun 2020 15:06:10 -0700 Subject: [PATCH 5/6] Actually actually fix tests --- gems/aws-sigv4/spec/signer_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gems/aws-sigv4/spec/signer_spec.rb b/gems/aws-sigv4/spec/signer_spec.rb index 0d0669ae1b8..9d468734d6b 100644 --- a/gems/aws-sigv4/spec/signer_spec.rb +++ b/gems/aws-sigv4/spec/signer_spec.rb @@ -146,12 +146,12 @@ module Sigv4 context 'when a Host header is provided' do - let(:headers) { {'host' => 'custom-host-value'} } + let(:headers) { { headers: { 'host' => 'custom-host-value' } } } it 'uses the original Host header' do - signature = Signer.new(options.merge(headers: headers)).sign_request(request) + signature = Signer.new(options).sign_request(request.merge(headers)) - expect(signature.headers['host']).to eql(custom-host-value) + expect(signature.headers['host']).to eql('custom-host-value') end end From 3915fddf7c12deaf67170ef55dbdefcae9420acf Mon Sep 17 00:00:00 2001 From: Matt Muller Date: Tue, 23 Jun 2020 15:16:42 -0700 Subject: [PATCH 6/6] Clean up test --- gems/aws-sigv4/spec/signer_spec.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/gems/aws-sigv4/spec/signer_spec.rb b/gems/aws-sigv4/spec/signer_spec.rb index 9d468734d6b..9d4640e699c 100644 --- a/gems/aws-sigv4/spec/signer_spec.rb +++ b/gems/aws-sigv4/spec/signer_spec.rb @@ -145,13 +145,12 @@ module Sigv4 end context 'when a Host header is provided' do - - let(:headers) { { headers: { 'host' => 'custom-host-value' } } } - - it 'uses the original Host header' do - signature = Signer.new(options).sign_request(request.merge(headers)) + it 'uses the provided Host header' do + signature = Signer.new(options).sign_request( + request.merge(headers: { 'host' => 'otherdomain.com' }) + ) - expect(signature.headers['host']).to eql('custom-host-value') + expect(signature.headers['host']).to eql('otherdomain.com') end end