Skip to content

Commit

Permalink
check for validate csrf token
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 26, 2023
1 parent bbcb052 commit f5bae61
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/Http/Middleware/EnsureFrontendRequestsAreStateful.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@ public function handle($request, $next)
{
$this->configureSecureCookieSessions();

return (new Pipeline(app()))->send($request)->through(static::fromFrontend($request) ? array_values(array_unique([
function ($request, $next) {
$request->attributes->set('sanctum', true);

return $next($request);
},
config('sanctum.middleware.encrypt_cookies', \Illuminate\Cookie\Middleware\EncryptCookies::class),
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
config('sanctum.middleware.validate_csrf_token', \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class),
config('sanctum.middleware.verify_csrf_token', \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class),
])) : [])->then(function ($request) use ($next) {
return (new Pipeline(app()))->send($request)->through(
static::fromFrontend($request) ? $this->frontendMiddleware() : []
)->then(function ($request) use ($next) {
return $next($request);
});
}
Expand All @@ -48,6 +39,30 @@ protected function configureSecureCookieSessions()
]);
}

/**
* Get the middleware that should be applied to requests from the "frontend".
*
* @return array
*/
protected function frontendMiddleware()
{
$middleware = array_values(array_unique([
config('sanctum.middleware.encrypt_cookies', \Illuminate\Cookie\Middleware\EncryptCookies::class),
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
config('sanctum.middleware.validate_csrf_token', \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class),
config('sanctum.middleware.verify_csrf_token', \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class),
]));

array_unshift($middleware, function ($request, $next) {
$request->attributes->set('sanctum', true);

return $next($request);
});

return $middleware;
}

/**
* Determine if the given request is from the first-party application frontend.
*
Expand Down

4 comments on commit f5bae61

@andreladocruz
Copy link

Choose a reason for hiding this comment

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

Why do we need 2 different keys? "validate_csrf_token" and "verify_csrf_token"

@ankurk91
Copy link
Contributor

Choose a reason for hiding this comment

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

May be to keep backward compatibility.
array_unique will take care of duplicate

@andreladocruz
Copy link

@andreladocruz andreladocruz commented on f5bae61 Apr 28, 2023

Choose a reason for hiding this comment

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

array_unique will not remove... I had to add both keys to my sanctum.php config file as I have a different VerifyCsrfToken class.

Should be

config('sanctum.middleware.validate_csrf_token', config('sanctum.middleware.verify_csrf_token',\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class)),

Instead

@andreladocruz
Copy link

Choose a reason for hiding this comment

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

@taylorotwell my little feedback

Please sign in to comment.