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

[CloudFront] Add exception when signature generation fails #2590

Merged
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/cloudfront-signed-urls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "CloudFront",
"description": "Throw exception when an empty signature for signed url is generated."
}
]
16 changes: 15 additions & 1 deletion src/CloudFront/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function __destruct()
* @return array The values needed to construct a signed URL or cookie
* @throws \InvalidArgumentException when not provided either a policy or a
* resource and a expires
* @throws \RuntimeException when generated signature is empty
*
* @link http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-cookies.html
*/
Expand Down Expand Up @@ -113,7 +114,20 @@ private function createCannedPolicy($resource, $expiration)
private function sign($policy)
{
$signature = '';
openssl_sign($policy, $signature, $this->pkHandle);

if(!openssl_sign($policy, $signature, $this->pkHandle)) {
$errorMessages = [];
while(($newMessage = openssl_error_string()) !== false) {
$errorMessages[] = $newMessage;
}

$exceptionMessage = "An error has occurred when signing the policy";
if (count($errorMessages) > 0) {
$exceptionMessage = implode("\n", $errorMessages);
}

throw new \RuntimeException($exceptionMessage);
}

return $signature;
}
Expand Down