From 9f25c5c6a32d07753626c5b874dc67450d81ddfb Mon Sep 17 00:00:00 2001 From: Joshua Napoli Date: Sat, 9 Apr 2011 20:16:42 -0400 Subject: [PATCH] Fix a problem with the encoding of the tilde character. AWS needs tilde to be unescaped, or else the signature fails. CGI.escape escapes tilde; don't use it. --- lib/fog/providers/aws.rb | 6 +++++- tests/aws/signed_params_tests.rb | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/aws/signed_params_tests.rb diff --git a/lib/fog/providers/aws.rb b/lib/fog/providers/aws.rb index 9e8733e09..6ce4acb0b 100644 --- a/lib/fog/providers/aws.rb +++ b/lib/fog/providers/aws.rb @@ -41,6 +41,10 @@ module Fog params end + def self.escape(string) + string.gsub( /([^a-zA-Z0-9_.-~]+)/n ) { |match| '%' + match.unpack( 'H2' * match.size ).join( '%' ).upcase } + end + def self.signed_params(params, options = {}) params.merge!({ 'AWSAccessKeyId' => options[:aws_access_key_id], @@ -53,7 +57,7 @@ module Fog body = '' for key in params.keys.sort unless (value = params[key]).nil? - body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&" + body << "#{key}=#{escape(value.to_s)}&" end end string_to_sign = "POST\n#{options[:host]}:#{options[:port]}\n#{options[:path]}\n" << body.chop diff --git a/tests/aws/signed_params_tests.rb b/tests/aws/signed_params_tests.rb new file mode 100644 index 000000000..11a36f132 --- /dev/null +++ b/tests/aws/signed_params_tests.rb @@ -0,0 +1,3 @@ +Shindo.tests('AWS | signed_params', ['aws']) do + returns( Fog::AWS.escape( "'Stop!' said Fred~" ) ) { "%27Stop%21%27%20said%20Fred~" } +end