Skip to content

Commit

Permalink
[Security] limited the password length passed to encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 23, 2013
1 parent b1542f0 commit f7d0ec6
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 0 deletions.
Expand Up @@ -64,6 +64,8 @@ public function __construct($cost)
*/
public function encodePassword($raw, $salt)
{
$this->checkPasswordLength($raw);

$options = array('cost' => $this->cost);

if ($salt) {
Expand All @@ -78,6 +80,8 @@ public function encodePassword($raw, $salt)
*/
public function isPasswordValid($encoded, $raw, $salt)
{
$this->checkPasswordLength($raw);

return password_verify($raw, $encoded);
}
}
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Core\Encoder;

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

/**
* BasePasswordEncoder is the base class for all password encoders.
Expand All @@ -20,6 +21,8 @@
*/
abstract class BasePasswordEncoder implements PasswordEncoderInterface
{
const MAX_PASSWORD_LENGTH = 4096;

/**
* Demerges a merge password and salt string.
*
Expand Down Expand Up @@ -83,4 +86,11 @@ protected function comparePasswords($password1, $password2)
{
return StringUtils::equals($password1, $password2);
}

protected function checkPasswordLength($password)
{
if (strlen($password) > self::MAX_PASSWORD_LENGTH) {
throw new BadCredentialsException('Invalid password.');
}
}
}
Expand Up @@ -41,6 +41,8 @@ public function __construct($algorithm = 'sha512', $encodeHashAsBase64 = true, $
*/
public function encodePassword($raw, $salt)
{
$this->checkPasswordLength($raw);

if (!in_array($this->algorithm, hash_algos(), true)) {
throw new \LogicException(sprintf('The algorithm "%s" is not supported.', $this->algorithm));
}
Expand All @@ -61,6 +63,8 @@ public function encodePassword($raw, $salt)
*/
public function isPasswordValid($encoded, $raw, $salt)
{
$this->checkPasswordLength($raw);

return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
}
}
Expand Up @@ -54,6 +54,8 @@ public function __construct($algorithm = 'sha512', $encodeHashAsBase64 = true, $
*/
public function encodePassword($raw, $salt)
{
$this->checkPasswordLength($raw);

if (!in_array($this->algorithm, hash_algos(), true)) {
throw new \LogicException(sprintf('The algorithm "%s" is not supported.', $this->algorithm));
}
Expand All @@ -72,6 +74,8 @@ public function encodePassword($raw, $salt)
*/
public function isPasswordValid($encoded, $raw, $salt)
{
$this->checkPasswordLength($raw);

return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
}

Expand Down
Expand Up @@ -35,6 +35,8 @@ public function __construct($ignorePasswordCase = false)
*/
public function encodePassword($raw, $salt)
{
$this->checkPasswordLength($raw);

return $this->mergePasswordAndSalt($raw, $salt);
}

Expand All @@ -43,6 +45,8 @@ public function encodePassword($raw, $salt)
*/
public function isPasswordValid($encoded, $raw, $salt)
{
$this->checkPasswordLength($raw);

$pass2 = $this->mergePasswordAndSalt($raw, $salt);

if (!$this->ignorePasswordCase) {
Expand Down
Expand Up @@ -64,6 +64,26 @@ public function testValidation()
$this->assertFalse($encoder->isPasswordValid($result, 'anotherPassword', null));
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testEncodePasswordLength()
{
$encoder = new BCryptPasswordEncoder(4);

$encoder->encodePassword(str_repeat('a', 5000), 'salt');
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testCheckPasswordLength()
{
$encoder = new BCryptPasswordEncoder(4);

$encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt');
}

private function skipIfPhpVersionIsNotSupported()
{
if (version_compare(phpversion(), '5.3.7', '<')) {
Expand Down
Expand Up @@ -42,4 +42,24 @@ public function testEncodePasswordAlgorithmDoesNotExist()
$encoder = new MessageDigestPasswordEncoder('foobar');
$encoder->encodePassword('password', '');
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testEncodePasswordLength()
{
$encoder = new MessageDigestPasswordEncoder();

$encoder->encodePassword(str_repeat('a', 5000), 'salt');
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testCheckPasswordLength()
{
$encoder = new MessageDigestPasswordEncoder();

$encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt');
}
}
Expand Up @@ -42,4 +42,24 @@ public function testEncodePasswordAlgorithmDoesNotExist()
$encoder = new Pbkdf2PasswordEncoder('foobar');
$encoder->encodePassword('password', '');
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testEncodePasswordLength()
{
$encoder = new Pbkdf2PasswordEncoder();

$encoder->encodePassword(str_repeat('a', 5000), 'salt');
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testCheckPasswordLength()
{
$encoder = new Pbkdf2PasswordEncoder();

$encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt');
}
}
Expand Up @@ -36,4 +36,24 @@ public function testEncodePassword()

$this->assertSame('foo', $encoder->encodePassword('foo', ''));
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testEncodePasswordLength()
{
$encoder = new PlaintextPasswordEncoder();

$encoder->encodePassword(str_repeat('a', 5000), 'salt');
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
*/
public function testCheckPasswordLength()
{
$encoder = new PlaintextPasswordEncoder();

$encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt');
}
}

0 comments on commit f7d0ec6

Please sign in to comment.