Skip to content
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

fix(client-login-flow): Handle missing stateToken gracefully #36552

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
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
29 changes: 26 additions & 3 deletions core/Controller/ClientFlowLoginV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ public function showAuthPickerPage($user = ''): StandaloneTemplateResponse {
* @NoSameSiteCookieRequired
*/
#[UseSession]
public function grantPage(string $stateToken): StandaloneTemplateResponse {
public function grantPage(?string $stateToken): StandaloneTemplateResponse {
if ($stateToken === null) {
return $this->stateTokenMissingResponse();
}
if (!$this->isValidStateToken($stateToken)) {
return $this->stateTokenForbiddenResponse();
}
Expand Down Expand Up @@ -182,7 +185,11 @@ public function grantPage(string $stateToken): StandaloneTemplateResponse {
/**
* @PublicPage
*/
public function apptokenRedirect(string $stateToken, string $user, string $password) {
public function apptokenRedirect(?string $stateToken, string $user, string $password) {
if ($stateToken === null) {
return $this->loginTokenForbiddenResponse();
}

if (!$this->isValidStateToken($stateToken)) {
return $this->stateTokenForbiddenResponse();
}
Expand Down Expand Up @@ -225,7 +232,10 @@ public function apptokenRedirect(string $stateToken, string $user, string $passw
* @NoAdminRequired
*/
#[UseSession]
public function generateAppPassword(string $stateToken): Response {
public function generateAppPassword(?string $stateToken): Response {
if ($stateToken === null) {
return $this->stateTokenMissingResponse();
}
if (!$this->isValidStateToken($stateToken)) {
return $this->stateTokenForbiddenResponse();
}
Expand Down Expand Up @@ -298,6 +308,19 @@ private function isValidStateToken(string $stateToken): bool {
return hash_equals($currentToken, $stateToken);
}

private function stateTokenMissingResponse(): StandaloneTemplateResponse {
$response = new StandaloneTemplateResponse(
$this->appName,
'403',
[
'message' => $this->l10n->t('State token missing'),
],
'guest'
);
$response->setStatus(Http::STATUS_FORBIDDEN);
return $response;
}

private function stateTokenForbiddenResponse(): StandaloneTemplateResponse {
$response = new StandaloneTemplateResponse(
$this->appName,
Expand Down
6 changes: 6 additions & 0 deletions tests/Core/Controller/ClientFlowLoginV2ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public function testShowAuthPickerValidLoginToken() {
$this->controller->showAuthPickerPage();
}

public function testGrantPageNoStateToken(): void {
$result = $this->controller->grantPage(null);

$this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
}

public function testGrantPageInvalidStateToken() {
$this->session->method('get')
->willReturnCallback(function ($name) {
Expand Down