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

Fixed timing attack vulnerability #8

Merged
merged 3 commits into from
Aug 8, 2016
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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Barzahlen Payment Module PHP SDK (v2.0.0)
# Barzahlen Payment Module PHP SDK (v2.0.1)

[![Build Status](https://travis-ci.org/Barzahlen/Barzahlen-PHP.svg?branch=master)](https://travis-ci.org/Barzahlen/Barzahlen-PHP)
[![Total Downloads](https://poser.pugx.org/barzahlen/barzahlen-php/downloads)](https://packagist.org/packages/barzahlen/barzahlen-php)
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Client
/**
* @var string
*/
private $userAgent = 'PHP SDK v2.0.0';
private $userAgent = 'PHP SDK v2.0.1';


/**
Expand Down
26 changes: 26 additions & 0 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,30 @@ public static function generateSignature($host, $method, $path, $query, $date, $

return hash_hmac('sha256', $signatureString, $key);
}

/**
* @param string $first
* @param string $second
* @return boolean
*
* Workaround for PHP < 5.6 by: asphp at dsgml dot com
* Source: https://php.net/manual/en/function.hash-equals.php#115635
*/
public static function stringsEqual($first, $second)
{
if (function_exists('hash_equals')) {
return hash_equals($first, $second);
}

if (strlen($first) != strlen($second)) {
return false;
}

$res = $first ^ $second;
$ret = 0;
for ($i = strlen($res) - 1; $i >= 0; $i--) {
$ret |= ord($res[$i]);
}
return !$ret;
}
}
2 changes: 1 addition & 1 deletion src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function verify($header, $body)
$this->paymentKey
);

return $header['HTTP_BZ_SIGNATURE'] == 'BZ1-HMAC-SHA256 ' . $signature;
return Middleware::stringsEqual($header['HTTP_BZ_SIGNATURE'], 'BZ1-HMAC-SHA256 ' . $signature);
}
}
6 changes: 3 additions & 3 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp()

public function testDefaultUserAgent()
{
$this->assertAttributeEquals('PHP SDK v2.0.0', 'userAgent', $this->client);
$this->assertAttributeEquals('PHP SDK v2.0.1', 'userAgent', $this->client);
}

public function testSetUserAgent()
Expand All @@ -41,7 +41,7 @@ public function testBuildHeaderWithIdempotency()
$header = $this->client->buildHeader($request);
$this->assertEquals('Host: api.barzahlen.de', $header[0]);
$this->assertContains('Date: ', $header[1]);
$this->assertEquals('User-Agent: PHP SDK v2.0.0', $header[2]);
$this->assertEquals('User-Agent: PHP SDK v2.0.1', $header[2]);
$this->assertRegExp('/^Authorization: BZ1-HMAC-SHA256 DivisionId=12345, Signature=[a-f0-9]{64}$/', $header[3]);
$this->assertRegExp('/^Idempotency-Key: [a-f0-9]{32}$/', $header[4]);
}
Expand All @@ -54,7 +54,7 @@ public function testBuildHeaderWithoutIdempotencyForSandbox()
$header = $client->buildHeader($request);
$this->assertEquals('Host: api-sandbox.barzahlen.de', $header[0]);
$this->assertContains('Date: ', $header[1]);
$this->assertEquals('User-Agent: PHP SDK v2.0.0', $header[2]);
$this->assertEquals('User-Agent: PHP SDK v2.0.1', $header[2]);
$this->assertRegExp('/^Authorization: BZ1-HMAC-SHA256 DivisionId=12345, Signature=[a-f0-9]{64}$/', $header[3]);
$this->assertArrayNotHasKey(4, $header);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ public function testGenerateSignature()

$this->assertEquals('35764655afcf2121602a5493b58020d3b6b9d75b4150c7395acf6114ae0ba49c', $signature);
}

public function testStringsEqualInvalidLength()
{
$first = 'thisisarandomstring123';
$second = 'thisisanotherrandomstring123';

$this->assertFalse(Middleware::stringsEqual($first, $second));
}

public function testStringsEqualInvalidContent()
{
$first = 'thisisarandomstring123';
$second = 'thisisarandomstring124';

$this->assertFalse(Middleware::stringsEqual($first, $second));
}

public function testStringsEqualValid()
{
$first = 'thismustbeavalidhash';
$second = 'thismustbeavalidhash';

$this->assertTrue(Middleware::stringsEqual($first, $second));
}
}