From 56f15c15a13f7acffb3617b594bb127d7adafa86 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sun, 22 May 2022 16:54:07 -0400 Subject: [PATCH] We can make this middleware significantly simpler --- .../Middleware/EnsureStatefulRequests.php | 41 +++---------------- 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/app/Http/Middleware/EnsureStatefulRequests.php b/app/Http/Middleware/EnsureStatefulRequests.php index 2671763c9..db6e19ae9 100644 --- a/app/Http/Middleware/EnsureStatefulRequests.php +++ b/app/Http/Middleware/EnsureStatefulRequests.php @@ -2,27 +2,10 @@ namespace Pterodactyl\Http\Middleware; -use Illuminate\Http\Request; -use Illuminate\Routing\Pipeline; -use Illuminate\Session\Middleware\StartSession; -use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse; use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful; class EnsureStatefulRequests extends EnsureFrontendRequestsAreStateful { - /** - * {@inheritDoc} - */ - public function handle($request, $next) - { - $this->configureSecureCookieSessions(); - - return (new Pipeline(app())) - ->send($request) - ->through($this->isStateful($request) ? $this->statefulMiddleware() : []) - ->then(fn ($request) => $next($request)); - } - /** * Determines if a request is stateful or not. This is determined using the default * Sanctum "fromFrontend" helper method. However, we also check if the request includes @@ -32,26 +15,12 @@ class EnsureStatefulRequests extends EnsureFrontendRequestsAreStateful * We don't want to support API usage using the cookies, except for requests stemming * from the front-end we control. */ - protected function isStateful(Request $request): bool + public static function fromFrontend($request) { - return static::fromFrontend($request) || $request->hasCookie(config('session.cookie')); - } + if (parent::fromFrontend($request)) { + return true; + } - /** - * Returns the middleware to be applied to a stateful request to the API. - */ - protected function statefulMiddleware(): array - { - return [ - function ($request, $next) { - $request->attributes->set('sanctum', true); - - return $next($request); - }, - config('sanctum.middleware.encrypt_cookies', EncryptCookies::class), - AddQueuedCookiesToResponse::class, - StartSession::class, - config('sanctum.middleware.verify_csrf_token', VerifyCsrfToken::class), - ]; + return $request->hasCookie(config('session.cookie')); } }