Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .changes/nextrelease/chore-sort-presigned-headers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "",
"description": "Sorts presigned headers alphabetically."
}
]
3 changes: 3 additions & 0 deletions src/Signature/SignatureV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ private function getPresignHeaders(array $headers)
$presignHeaders[] = $lName;
}
}

sort($presignHeaders);

return $presignHeaders;
}

Expand Down
21 changes: 20 additions & 1 deletion tests/Signature/SignatureV4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function testPresignSpecificHeaders()
'x-amz-content-sha256' => 'abc',
]);
$presigned = $sig->presign($req, $creds, '+5 minutes');
$this->assertStringContainsString(urlencode('host;x-amz-foo;content-md5;x-amz-meta-foo'), (string)$presigned->getUri());
$this->assertStringContainsString(urlencode('content-md5;host;x-amz-foo;x-amz-meta-foo'), (string)$presigned->getUri());
}

public function testPresignBlacklistedHeaders()
Expand Down Expand Up @@ -613,4 +613,23 @@ public function testRemovesIllegalV4aHeadersBeforeSigning()
$this->assertEquals($value, $req->getHeaderLine($key));
}
}

public function testSortHeadersAlphabetically()
{
$signature = new SignatureV4('foo', 'bar');
$credentials = new Credentials('a', 'b');
$headers = [
'z-header-1' => 'foo',
'x-header-2' => 'bar',
'b-header-3' => 'baz',
'a-header-3' => 'baz',
'r-header-4' => 'bar',
];
$req = new Request('PUT', 'http://foo.com', $headers);
$presigned = $signature->presign($req, $credentials, '+5 minutes');
$this->assertStringContainsString(
urlencode('a-header-3;b-header-3;host;r-header-4;x-header-2;z-header-1'),
(string)$presigned->getUri()
);
}
}