Skip to content
Merged
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
34 changes: 31 additions & 3 deletions src/Http/Middleware/AuthenticateSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ public function handle(Request $request, Closure $next): Response
$shouldLogout = $guards->filter(
fn ($guard, $driver) => $request->session()->has('password_hash_'.$driver)
)->filter(
fn ($guard, $driver) => $request->session()->get('password_hash_'.$driver) !==
$request->user()->getAuthPassword()
fn ($guard, $driver) => ! $this->validatePasswordHash(
$guard,
$request->user()->getAuthPassword(),
$request->session()->get('password_hash_'.$driver)
)
);

if ($shouldLogout->isNotEmpty()) {
Expand Down Expand Up @@ -94,8 +97,33 @@ protected function getFirstGuardWithUser(Collection $guards)
*/
protected function storePasswordHashInSession($request, string $guard)
{
$guardInstance = $this->auth->guard($guard);

$request->session()->put([
"password_hash_{$guard}" => $this->auth->guard($guard)->user()->getAuthPassword(),
"password_hash_{$guard}" => method_exists($guardInstance, 'hashPasswordForCookie')
? $guardInstance->hashPasswordForCookie($guardInstance->user()->getAuthPassword())
: $guardInstance->user()->getAuthPassword(),
]);
}

/**
* Validate the password hash against the stored value.
*
* @param \Illuminate\Auth\SessionGuard $guard
* @param string $passwordHash
* @param string $storedValue
* @return bool
*/
protected function validatePasswordHash(SessionGuard $guard, string $passwordHash, string $storedValue): bool

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

$passwordHash may be null here.

Laravel\Sanctum\Http\Middleware\AuthenticateSession::validatePasswordHash(): Argument #2 ($passwordHash) must be of type string, null given, called in vendor/laravel/sanctum/src/Http/Middleware/AuthenticateSession.php on line 53

@patrickomeara patrickomeara Jan 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Update: My user factory wasn't setting a password so some of my tests were failing with the above error.

As @rdehnhardt mentioned there are cases where password is null for real world users so perhaps the $passwordHash type should be ?string or have the type removed. Both SessionGuard::hashPasswordForCookie() and hash_equals do not enforce the string type.

@taylorotwell I've opened a PR #581 to address this.

In my case I've just set a password in my factory.

{
// Try new HMAC format first (Laravel 12.45.0+)...
if (method_exists($guard, 'hashPasswordForCookie')) {
if (hash_equals($guard->hashPasswordForCookie($passwordHash), $storedValue)) {
return true;
}
}

// Fall back to raw password hash format for backward compatibility...
return hash_equals($passwordHash, $storedValue);
}
}