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

[11.x] Prevents Bcrypt hashing from spilling over and creating insecure hashes. #54494

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 9 additions & 0 deletions src/Illuminate/Hashing/BcryptHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class BcryptHasher extends AbstractHasher implements HasherContract
{
/**
* The maximum length of the string that can be hashed by bcrypt.
*/
const BCRYPT_STR_LIMIT = 72;

/**
* The default cost factor.
*
Expand Down Expand Up @@ -46,6 +51,10 @@ public function __construct(array $options = [])
public function make(#[\SensitiveParameter] $value, array $options = [])
{
try {
if (strlen($value) > self::BCRYPT_STR_LIMIT) {
throw new BcryptValueTooLongException;
}

$hash = password_hash($value, PASSWORD_BCRYPT, [
'cost' => $this->cost($options),
]);
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Hashing/BcryptValueTooLongException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Illuminate\Hashing;

use RuntimeException;

class BcryptValueTooLongException extends RuntimeException
{
public function __construct(int $maxLength = BcryptHasher::BCRYPT_STR_LIMIT)
{
parent::__construct("Value to hash is too long. Bcrypt only allows for a maximum length of {$maxLength} characters. Shorten your value or use a different hashing algorithm.");
}
}
8 changes: 8 additions & 0 deletions tests/Hashing/HasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Hashing\Argon2IdHasher;
use Illuminate\Hashing\ArgonHasher;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Hashing\BcryptValueTooLongException;
use Illuminate\Hashing\HashManager;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -59,6 +60,13 @@ public function testBasicBcryptHashing()
$this->assertTrue($this->hashManager->isHashed($value));
}

public function testBcryptTooLongValue()
{
$this->expectException(BcryptValueTooLongException::class);

(new BcryptHasher())->make(str_repeat('a', 73));
}

public function testBasicArgon2iHashing()
{
$hasher = new ArgonHasher;
Expand Down