Skip to content

Commit

Permalink
[CloudFront] Add exception when signature generation fails (#2590)
Browse files Browse the repository at this point in the history
* Add exception when signature generation fails

* Add fallback error message when policy signing fail
  • Loading branch information
DocLM authored Jan 10, 2024
1 parent 8a2710f commit 46ac1da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit 46ac1da

Please sign in to comment.