Skip to content

Commit 6577143

Browse files
authored
Merge pull request #36585 from nextcloud/backport/36552/stable25
[stable25] fix(client-login-flow): Handle missing stateToken gracefully
2 parents 37a90a3 + adb4507 commit 6577143

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

core/Controller/ClientFlowLoginV2Controller.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public function showAuthPickerPage($user = ''): StandaloneTemplateResponse {
149149
* @NoCSRFRequired
150150
* @NoSameSiteCookieRequired
151151
*/
152-
public function grantPage(string $stateToken): StandaloneTemplateResponse {
152+
public function grantPage(?string $stateToken): StandaloneTemplateResponse {
153+
if ($stateToken === null) {
154+
return $this->stateTokenMissingResponse();
155+
}
153156
if (!$this->isValidStateToken($stateToken)) {
154157
return $this->stateTokenForbiddenResponse();
155158
}
@@ -181,7 +184,11 @@ public function grantPage(string $stateToken): StandaloneTemplateResponse {
181184
/**
182185
* @PublicPage
183186
*/
184-
public function apptokenRedirect(string $stateToken, string $user, string $password) {
187+
public function apptokenRedirect(?string $stateToken, string $user, string $password) {
188+
if ($stateToken === null) {
189+
return $this->stateTokenMissingResponse();
190+
}
191+
185192
if (!$this->isValidStateToken($stateToken)) {
186193
return $this->stateTokenForbiddenResponse();
187194
}
@@ -224,7 +231,10 @@ public function apptokenRedirect(string $stateToken, string $user, string $passw
224231
* @NoAdminRequired
225232
* @UseSession
226233
*/
227-
public function generateAppPassword(string $stateToken): Response {
234+
public function generateAppPassword(?string $stateToken): Response {
235+
if ($stateToken === null) {
236+
return $this->stateTokenMissingResponse();
237+
}
228238
if (!$this->isValidStateToken($stateToken)) {
229239
return $this->stateTokenForbiddenResponse();
230240
}
@@ -297,6 +307,19 @@ private function isValidStateToken(string $stateToken): bool {
297307
return hash_equals($currentToken, $stateToken);
298308
}
299309

310+
private function stateTokenMissingResponse(): StandaloneTemplateResponse {
311+
$response = new StandaloneTemplateResponse(
312+
$this->appName,
313+
'403',
314+
[
315+
'message' => $this->l10n->t('State token missing'),
316+
],
317+
'guest'
318+
);
319+
$response->setStatus(Http::STATUS_FORBIDDEN);
320+
return $response;
321+
}
322+
300323
private function stateTokenForbiddenResponse(): StandaloneTemplateResponse {
301324
$response = new StandaloneTemplateResponse(
302325
$this->appName,

tests/Core/Controller/ClientFlowLoginV2ControllerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ public function testShowAuthPickerValidLoginToken() {
188188
$this->controller->showAuthPickerPage();
189189
}
190190

191+
public function testGrantPageNoStateToken(): void {
192+
$result = $this->controller->grantPage(null);
193+
194+
$this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus());
195+
}
196+
191197
public function testGrantPageInvalidStateToken() {
192198
$this->session->method('get')
193199
->willReturnCallback(function ($name) {

0 commit comments

Comments
 (0)