forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mark a request as being stateful if a cookie for the session is provi…
…ded at all This accounts for poorly configured API clients that try to use cookies for authentication purposes. Treat everything with a session cookie as being a stateful request from the front-end.
- Loading branch information
1 parent
33bafe9
commit 0fa33e0
Showing
3 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
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 | ||
* a cookie value for the Pterodactyl session. If so, we assume this is a stateful | ||
* request. | ||
* | ||
* 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 | ||
{ | ||
return static::fromFrontend($request) || $request->hasCookie(config('session.cookie')); | ||
} | ||
|
||
/** | ||
* 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), | ||
]; | ||
} | ||
} |