Skip to content

Commit

Permalink
Merge pull request #658 from stripe/ob-fix-657
Browse files Browse the repository at this point in the history
Use absolute value when checking timestamp tolerance
  • Loading branch information
ob-stripe authored May 20, 2019
2 parents a5f6d81 + 322f697 commit 6cec639
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ abstract class Webhook
*/
public static function constructEvent($payload, $sigHeader, $secret, $tolerance = self::DEFAULT_TOLERANCE)
{
WebhookSignature::verifyHeader($payload, $sigHeader, $secret, $tolerance);

$data = json_decode($payload, true);
$jsonError = json_last_error();
if ($data === null && $jsonError !== JSON_ERROR_NONE) {
Expand All @@ -33,8 +35,6 @@ public static function constructEvent($payload, $sigHeader, $secret, $tolerance
}
$event = Event::constructFrom($data);

WebhookSignature::verifyHeader($payload, $sigHeader, $secret, $tolerance);

return $event;
}
}
2 changes: 1 addition & 1 deletion lib/WebhookSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function verifyHeader($payload, $header, $secret, $tolerance = nul
}

// Check if timestamp is within tolerance
if (($tolerance > 0) && ((time() - $timestamp) > $tolerance)) {
if (($tolerance > 0) && (abs(time() - $timestamp) > $tolerance)) {
throw new Error\SignatureVerification(
"Timestamp outside the tolerance zone",
$header,
Expand Down
12 changes: 11 additions & 1 deletion tests/Stripe/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,22 @@ public function testNoValidSignatureForPayload()
* @expectedException \Stripe\Error\SignatureVerification
* @expectedExceptionMessage Timestamp outside the tolerance zone
*/
public function testTimestampOutsideTolerance()
public function testTimestampTooOld()
{
$sigHeader = $this->generateHeader(["timestamp" => time() - 15]);
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET, 10);
}

/**
* @expectedException \Stripe\Error\SignatureVerification
* @expectedExceptionMessage Timestamp outside the tolerance zone
*/
public function testTimestampTooRecent()
{
$sigHeader = $this->generateHeader(["timestamp" => time() + 15]);
WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, $sigHeader, self::SECRET, 10);
}

public function testValidHeaderAndSignature()
{
$sigHeader = $this->generateHeader();
Expand Down

0 comments on commit 6cec639

Please sign in to comment.