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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class BCryptPasswordEncoder extends BasePasswordEncoder
{
const MAX_PASSWORD_LENGTH = 72;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could add a comment above this constant to show that this limit is not arbitrary. Something like this:

// When using bcrypt algorithm, PHP's password_hash() truncates the password length to a maximum of 72 chars.
const MAX_PASSWORD_LENGTH = 72;


/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ protected function comparePasswords($password1, $password2)
*/
protected function isPasswordTooLong($password)
{
return strlen($password) > self::MAX_PASSWORD_LENGTH;
return strlen($password) > static::MAX_PASSWORD_LENGTH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,13 @@ public function testCostInRange()
}
}

/**
* @requires PHP 5.3.7
*/
public function testResultLength()
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why these two tests were skipped. They work perfectly fine with all PHP versions, and they're not skipped on 2.7.

Copy link
Member

Choose a reason for hiding this comment

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

PHP before 5.3.7 has a broken bcrypt implementation: https://github.com/ircmaxell/password_compat#requirements

Copy link
Member

Choose a reason for hiding this comment

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

But some <5.3.7 versions have been patched by distribs :)

Copy link
Member

Choose a reason for hiding this comment

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

then use the password_compat check function to check for support (btw, the library used to trigger an error at runtime, which is probably why the skipping was there)

Copy link
Member

Choose a reason for hiding this comment

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

By the way, that's the reason why tests on Appveyor are failing now (see #17073 for the fix).

$encoder = new BCryptPasswordEncoder(self::VALID_COST);
$result = $encoder->encodePassword(self::PASSWORD, null);
$this->assertEquals(60, strlen($result));
}

/**
* @requires PHP 5.3.7
*/
public function testValidation()
{
$encoder = new BCryptPasswordEncoder(self::VALID_COST);
Expand All @@ -73,13 +67,15 @@ public function testEncodePasswordLength()
{
$encoder = new BCryptPasswordEncoder(self::VALID_COST);

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

public function testCheckPasswordLength()
{
$encoder = new BCryptPasswordEncoder(self::VALID_COST);
$result = $encoder->encodePassword(str_repeat('a', 72), null);

$this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), 'salt'));
$this->assertFalse($encoder->isPasswordValid($result, str_repeat('a', 73), 'salt'));
$this->assertTrue($encoder->isPasswordValid($result, str_repeat('a', 72), 'salt'));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We didn't have this assertion before. Without this there could be no length validation in isPasswordValid() and the assertion on the line before would pass anyway.

}
}