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
10 changes: 9 additions & 1 deletion src/Symfony/Component/Security/Core/Util/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private function __construct() {}
* Compares two strings.
*
* This method implements a constant-time algorithm to compare strings.
* Regardless of the used implementation, it will leak length information.
*
* @param string $knownString The string of known length to compare against
* @param string $userInput The string that the user can control
Expand All @@ -35,6 +36,13 @@ private function __construct() {}
*/
public static function equals($knownString, $userInput)
{
$knownString = (string) $knownString;
$userInput = (string) $userInput;

if (function_exists('hash_equals')) {
return hash_equals($knownString, $userInput);
}

$knownLen = strlen($knownString);
$userLen = strlen($userInput);

Expand All @@ -45,7 +53,7 @@ public static function equals($knownString, $userInput)
$result = $knownLen - $userLen;

// Note that we ALWAYS iterate over the user-supplied length
// This is to prevent leaking length information
// This is to mitigate leaking length information
for ($i = 0; $i < $userLen; $i++) {
$result |= (ord($knownString[$i]) ^ ord($userInput[$i]));
}
Expand Down
44 changes: 41 additions & 3 deletions src/Symfony/Component/Security/Tests/Core/Util/StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,49 @@

use Symfony\Component\Security\Core\Util\StringUtils;

/**
* Data from PHP.net's hash_equals tests
*/
class StringUtilsTest extends \PHPUnit_Framework_TestCase
{
public function testEquals()
public function dataProviderTrue()
{
return array(
array('same', 'same'),
array('', ''),
array(123, 123),
array(null, ''),
array(null, null),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about null and false

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just copied data of the php.net implementation. I'll add it.

);
}

public function dataProviderFalse()
{
return array(
array('not1same', 'not2same'),
array('short', 'longer'),
array('longer', 'short'),
array('', 'notempty'),
array('notempty', ''),
array(123, 'NaN'),
array('NaN', 123),
array(null, 123),
);
}

/**
* @dataProvider dataProviderTrue
*/
public function testEqualsTrue($known, $user)
{
$this->assertTrue(StringUtils::equals($known, $user));
}

/**
* @dataProvider dataProviderFalse
*/
public function testEqualsFalse($known, $user)
{
$this->assertTrue(StringUtils::equals('password', 'password'));
$this->assertFalse(StringUtils::equals('password', 'foo'));
$this->assertFalse(StringUtils::equals($known, $user));
}
}