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

Add option for :whitelist_headers in V4 signer #1228

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions aws-sdk-core/lib/aws-sdk-core/signers/v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ def self.sign(context)
# the endpoint prefix.
# @param [String] region The region (e.g. 'us-west-1') the request
# will be made to.
def initialize(credentials, service_name, region)
# @param [Array] whitelist headers provided to escape blacklist check
def initialize(credentials, service_name, region, whitelist_headers = [])
@service_name = service_name
@credentials = credentials.credentials
@region = EndpointProvider.signing_region(region, service_name)
if whitelist_headers.any? && (BLACKLIST_HEADERS & whitelist_headers).any?
Copy link
Member

Choose a reason for hiding this comment

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

I might be missing something, but why not just remove this whole block and use:

@blacklist = BLACKLIST_HEADERS - whitelist_headers

You already set whitelist headers to be an array by default, so once we get to this point, I'm not sure the if-else block is doing anything.

Copy link
Contributor Author

@cjyclaire cjyclaire Jul 12, 2016

Choose a reason for hiding this comment

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

That's true, I was thinking checking the intersection of the 2 arrays before getting the result. It seems redundant, I'll remove that.

@blacklist = BLACKLIST_HEADERS - whitelist_headers
else
@blacklist = BLACKLIST_HEADERS
end
end

# @param [Seahorse::Client::Http::Request] req
Expand Down Expand Up @@ -182,7 +188,7 @@ def normalized_querystring(querystring)
def signed_headers(request)
request.headers.keys.inject([]) do |signed_headers, header_key|
header_key = header_key.downcase
unless BLACKLIST_HEADERS.include?(header_key)
unless @blacklist.include?(header_key)
signed_headers << header_key
end
signed_headers
Expand All @@ -193,7 +199,7 @@ def canonical_headers(request)
headers = []
request.headers.each_pair do |k,v|
k = k.downcase
headers << [k,v] unless BLACKLIST_HEADERS.include?(k)
headers << [k,v] unless @blacklist.include?(k)
end
headers = headers.sort_by(&:first)
headers.map{|k,v| "#{k}:#{canonical_header_value(v.to_s)}" }.join("\n")
Expand Down
17 changes: 17 additions & 0 deletions aws-sdk-core/spec/aws/signers/v4_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Signers
let(:service_name) { 'SERVICE' }
let(:region) { 'REGION' }
let(:endpoint) { URI.parse('https://domain.com') }
let(:whitelist) { ['cache-control', 'mno'] }
let(:signer) { V4.new(credentials, service_name, region) }
let(:sign) { signer.sign(http_request) }
let(:http_request) do
Expand Down Expand Up @@ -126,6 +127,15 @@ module Signers
expect(signer.signed_headers(http_request)).to eq('abc;mno;xyz')
end

it 'ignores certain headers unless providing headers via :whitelist_headers' do
http_request.headers = {}
http_request.headers['Mno'] = '3'
http_request.headers['Cache-Control'] = '4'
http_request.headers['User-Agent'] = '5'
new_signer = V4.new(credentials, service_name, region, whitelist)
expect(new_signer.signed_headers(http_request)).to eq('cache-control;mno');
end

end

context '#canonical_headers' do
Expand Down Expand Up @@ -153,6 +163,13 @@ module Signers
expect(signer.canonical_headers(http_request)).to eq('abc:"a b c"')
end

it 'ignores certain headers unless providing headers via :whitelist_headers' do
http_request.headers = {}
http_request.headers['Cache-Control'] = '4'
new_signer = V4.new(credentials, service_name, region, whitelist)
expect(new_signer.canonical_headers(http_request)).to eq('cache-control:4');
end

end

context '#normalized_querystring' do
Expand Down