Skip to content

Commit

Permalink
Addressing the double encoding issue in SigV4.
Browse files Browse the repository at this point in the history
Adding override for S3 as it is not affected.
Adding test for S3 sigv4.
Updating compliance tests to match reality and strip trailing newlines.
Removing the need to have a test_services.json file.
  • Loading branch information
mtdowling committed Jul 10, 2014
1 parent fd101ae commit c4bf949
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 20 deletions.
9 changes: 8 additions & 1 deletion src/Aws/Common/Signature/SignatureV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ protected function getPresignedPayload(RequestInterface $request)
return $this->getPayload($request);
}

protected function createCanonicalizedPath(RequestInterface $request)
{
$doubleEncoded = rawurlencode(ltrim($request->getPath(), '/'));

return '/' . str_replace('%2F', '/', $doubleEncoded);
}

private function createStringToSign($longDate, $credentialScope, $creq)
{
return "AWS4-HMAC-SHA256\n{$longDate}\n{$credentialScope}\n"
Expand Down Expand Up @@ -299,7 +306,7 @@ private function createSigningContext(RequestInterface $request, $payload)
{
// Normalize the path as required by SigV4 and ensure it's absolute
$canon = $request->getMethod() . "\n"
. '/' . ltrim($request->getPath(), '/') . "\n"
. $this->createCanonicalizedPath($request) . "\n"
. $this->getCanonicalizedQueryString($request) . "\n";

// Create the canonical headers
Expand Down
8 changes: 8 additions & 0 deletions src/Aws/S3/S3SignatureV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ protected function getPresignedPayload(RequestInterface $request)

return $result;
}

/**
* Amazon S3 does not double-encode the path component in the canonical req
*/
protected function createCanonicalizedPath(RequestInterface $request)
{
return '/' . ltrim($request->getPath(), '/');
}
}
4 changes: 2 additions & 2 deletions tests/Aws/Tests/CognitoSync/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function testErrorsAreParsedCorrectly()
{
try {
$this->client->deleteDataset(array(
'IdentityPoolId' => 'abc:abc',
'IdentityId' => 'foo',
'IdentityPoolId' => 'abc:123af',
'IdentityId' => 'foo:123af',
'DatasetName' => 'bar',
));
$this->fail('An exception should have been thrown.');
Expand Down
9 changes: 6 additions & 3 deletions tests/Aws/Tests/Common/Signature/SignatureV4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ public function testSignsRequestsProperly($group)
$context = $request->getParams()->get('aws.signature');

// Test that the canonical request is correct
$this->assertEquals(str_replace("\r", '', file_get_contents($group['creq'])), $context['canonical_request']);
$this->assertEquals(str_replace("\r", '', trim(file_get_contents($group['creq']))), $context['canonical_request']);

// Test that the string to sign is correct
$this->assertEquals(str_replace("\r", '', file_get_contents($group['sts'])), $context['string_to_sign']);
$this->assertEquals(str_replace("\r", '', trim(file_get_contents($group['sts']))), $context['string_to_sign']);

// Test that the authorization header is correct
$this->assertEquals(str_replace("\r", '', file_get_contents($group['authz'])), $request->getHeader('Authorization'));
$this->assertEquals(
str_replace("\r", '', trim(file_get_contents($group['authz']))),
(string) $request->getHeader('Authorization')
);

$parser->setUtf8Support(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east-1/host/aws4_request, SignedHeaders=date;host, Signature=f309cfbd10197a230c42dd17dbf5cca8a0722564cb40a872d25623cfa758e374
AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east-1/host/aws4_request, SignedHeaders=date;host, Signature=2be9e587d1ee1f0fd02128724b86b4358ba3630d39f493b6e06610a19a50779e
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
GET
/%20/foo
/%2520/foo

date:Mon, 09 Sep 2011 23:36:00 GMT
host:host.foo.com

date;host
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
GET /%20/foo http/1.1
GET /%2520/foo http/1.1
Date:Mon, 09 Sep 2011 23:36:00 GMT
Host:host.foo.com
Authorization: AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east-1/host/aws4_request, SignedHeaders=date;host, Signature=f309cfbd10197a230c42dd17dbf5cca8a0722564cb40a872d25623cfa758e374

AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east-1/host/aws4_request, SignedHeaders=date;host, Signature=2be9e587d1ee1f0fd02128724b86b4358ba3630d39f493b6e06610a19a50779e
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AWS4-HMAC-SHA256
20110909T233600Z
20110909/us-east-1/host/aws4_request
69c45fb9fe3fd76442b5086e50b2e9fec8298358da957b293ef26e506fdfb54b
35975885599128f8963ce9be7219598b2b5a344321bae304529556c891e9e6ba
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east-1/host/aws4_request, SignedHeaders=date;host, Signature=8d6634c189aa8c75c2e51e106b6b5121bed103fdb351f7d7d4381c738823af74
AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east-1/host/aws4_request, SignedHeaders=date;host, Signature=57b1eb3df3b16a8479c0097b66c0b261cfd65b829419a3f2c3d05319722490b8
4 changes: 2 additions & 2 deletions tests/Aws/Tests/Common/Signature/aws4_testsuite/get-utf8.creq
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
GET
/%E1%88%B4
/%25E1%2588%25B4

date:Mon, 09 Sep 2011 23:36:00 GMT
host:host.foo.com

date;host
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AWS4-HMAC-SHA256
20110909T233600Z
20110909/us-east-1/host/aws4_request
27ba31df5dbc6e063d8f87d62eb07143f7f271c5330a917840586ac1c85b6f6b
47f4456c465167f4db3490a68903fe057412e6e030de38f71d9006467eb4922a
19 changes: 18 additions & 1 deletion tests/Aws/Tests/S3/Integration/S3_20060301_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Aws\S3\Integration;

use Aws\S3\S3Client;
use Aws\S3\Model\ClearBucket;
use Guzzle\Http\EntityBody;

Expand Down Expand Up @@ -624,10 +625,26 @@ public function testCreatePresignedUrlWithAcl()
curl_exec($ch);
}

/**
* @depends testGetObjectUrlWithSessionCredentials
*/
public function testPutObjectSigV4()
{
$client = $this->getServiceBuilder()->get('s3', array(
'signature' => 'v4'
));
$client->waitUntil('BucketExists', array('Bucket' => $this->bucket));
$client->putObject(array(
'Bucket' => $this->bucket,
'Key' => 'foo:bar.txt',
'Body' => 'Hello!'
));
}

/**
* Clear the contents and delete a bucket
*
* @depends testGetObjectUrlWithSessionCredentials
* @depends testPutObjectSigV4
* @example Aws\S3\S3Client::clearBucket
* @example Aws\S3\S3Client::deleteBucket
* @example Aws\S3\S3Client::waitUntilBucketNotExists
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

// Set the service configuration file if it was not provided from the CLI
if (!isset($_SERVER['CONFIG'])) {
$serviceConfig = $_SERVER['CONFIG'] = dirname(__DIR__) . '/test_services.json';
$serviceConfig = dirname(__DIR__) . '/test_services.json';
if (file_exists($serviceConfig)) {
$_SERVER['CONFIG'] = $serviceConfig;
}
Expand All @@ -54,7 +54,7 @@
}

// Instantiate the service builder
$aws = Aws\Common\Aws::factory(isset($_SERVER['CONFIG']) ? $_SERVER['CONFIG'] : null);
$aws = Aws\Common\Aws::factory(isset($_SERVER['CONFIG']) ? $_SERVER['CONFIG'] : 'test_services.dist.json');

// Turn on wire logging if configured
$aws->getEventDispatcher()->addListener('service_builder.create_client', function (\Guzzle\Common\Event $event) {
Expand Down

0 comments on commit c4bf949

Please sign in to comment.