Skip to content

Changed getTokenFromRequest method to handle null value and prevent type error #1819

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

Open
wants to merge 3 commits into
base: 13.x
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions src/Guards/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Cookie\CookieValuePrefix;
use Illuminate\Cookie\Middleware\EncryptCookies;
Expand Down Expand Up @@ -256,20 +257,26 @@ protected function decodeJwtTokenCookie(): array
*/
protected function validCsrf(array $token): bool
{
return isset($token['csrf']) && hash_equals(
$token['csrf'], $this->getTokenFromRequest()
);
$requestToken = $this->getTokenFromRequest();

return isset($token['csrf']) &&
is_string($requestToken) &&
hash_equals($token['csrf'], $requestToken);
}

/**
* Get the CSRF token from the request.
*/
protected function getTokenFromRequest(): string
protected function getTokenFromRequest(): ?string
{
$token = $this->request->header('X-CSRF-TOKEN');

if (! $token && $header = $this->request->header('X-XSRF-TOKEN')) {
$token = CookieValuePrefix::remove($this->encrypter->decrypt($header, static::serialized()));
try {
$token = CookieValuePrefix::remove($this->encrypter->decrypt($header, static::serialized()));
} catch (DecryptException) {
$token = null;
}
}

return $token;
Expand Down
Loading