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

forces request query param values to be escaped #162

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 24 additions & 2 deletions lib/api_auth/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,31 @@ def sign_header(header)
def parse_uri(uri)
parsed_uri = URI.parse(uri)

return parsed_uri.request_uri if parsed_uri.respond_to?(:request_uri)
uri_without_host = parsed_uri.respond_to?(:request_uri) ? parsed_uri.request_uri : uri
return '/' if uri_without_host.empty?
escape_params(uri_without_host)
end

uri.empty? ? '/' : uri
# Different versions of request parsers escape/unescape the param values
# Examples:
# Rails 5.1.3 ApiAuth canonical_string:
# 'GET,application/json,,/api/v1/employees?select=epulse_id%2Cfirst_name%2Clast_name,Thu, 14 Dec 2017 16:19:48 GMT'
# Rails 5.1.4 ApiAuth canonical_string:
# 'GET,application/json,,/api/v1/employees?select=epulse_id,first_name,last_name,Thu, 14 Dec 2017 16:20:57 GMT'
# This will force param values to escaped and fixes issue #123
def escape_params(uri)
unescaped_uri = CGI.unescape(uri)
uri_array = unescaped_uri.split('?')
return uri unless uri_array.length > 1
params = uri_array[1].split('&')
encoded_params = ''
params.each do |param|
next unless param.include?('=')
encoded_params += '&' unless encoded_params.empty?
split_param = param.split('=')
encoded_params += split_param[0] + '=' + CGI.escape(split_param[1])
end
uri_array[0] + '?' + encoded_params
end
end
end
28 changes: 27 additions & 1 deletion spec/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,39 @@
let(:uri) { 'http://google.com/?redirect_to=https://www.example.com'.freeze }

it 'return /?redirect_to=https://www.example.com as canonical string path' do
expect(subject.canonical_string).to eq('GET,,,/?redirect_to=https://www.example.com,')
expect(subject.canonical_string).to eq('GET,,,/?redirect_to=https%3A%2F%2Fwww.example.com,')
end

it 'does not change request url (by removing host)' do
expect(request.url).to eq(uri)
end
end

if RUBY_VERSION.to_f > 2.1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you need this ?

Copy link
Author

Choose a reason for hiding this comment

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

The rest-client will throw an exception for an invalid URI in ruby versions 2.1 and below, I needed this so that all the CI tests would pass. I believe, in later versions the URI is properly escaped rather than throwing the exception but I did not verify in the rest-client code.

context 'uri param values are not escaped' do
let(:uri) do
'http://www.google.com/search/advanced?redirect_to=https://www.example.com&account=a12dd334/3444\:23'.freeze
end

it 'returns correct anonical string' do
expect(subject.canonical_string).to(
eq('GET,,,/search/advanced?redirect_to=https%3A%2F%2Fwww.example.com&account=a12dd334%2F3444%5C%3A23,')
)
end
end
end

context 'uri param values are escaped' do
let(:uri) do
'http://www.google.com/search/advanced?redirect_to=https%3A%2F%2Fwww.example.com&account=a12dd334%2F3444%5C%3A23'.freeze
end

it 'returns correct anonical string' do
expect(subject.canonical_string).to(
eq('GET,,,/search/advanced?redirect_to=https%3A%2F%2Fwww.example.com&account=a12dd334%2F3444%5C%3A23,')
)
end
end
end

context 'string construction' do
Expand Down