Skip to content

Commit 6673ea0

Browse files
authored
Merge pull request #53645 from nextcloud/backport/53564/stable30
[stable30] Fix CSRF token not being refreshed while a two-factor challenge is ongoing
2 parents af53917 + 8aecc9d commit 6673ea0

14 files changed

+80
-7
lines changed

core/Controller/CSRFTokenController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function __construct(
3333
*
3434
* 200: CSRF token returned
3535
* 403: Strict cookie check failed
36+
*
37+
* @NoTwoFactorRequired
3638
*/
3739
#[PublicPage]
3840
#[NoCSRFRequired]

core/Controller/TwoFactorChallengeController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OCP\ISession;
2626
use OCP\IURLGenerator;
2727
use OCP\IUserSession;
28+
use OCP\Util;
2829
use Psr\Log\LoggerInterface;
2930

3031
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
@@ -89,6 +90,7 @@ public function selectChallenge($redirect_url) {
8990
'logout_url' => $this->getLogoutUrl(),
9091
'hasSetupProviders' => !empty($setupProviders),
9192
];
93+
Util::addScript('core', 'twofactor-request-token');
9294
return new StandaloneTemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest');
9395
}
9496

@@ -141,6 +143,7 @@ public function showChallenge($challengeProviderId, $redirect_url) {
141143
if ($provider instanceof IProvidesCustomCSP) {
142144
$response->setContentSecurityPolicy($provider->getCSP());
143145
}
146+
Util::addScript('core', 'twofactor-request-token');
144147
return $response;
145148
}
146149

@@ -204,6 +207,7 @@ public function setupProviders(?string $redirect_url = null): StandaloneTemplate
204207
'redirect_url' => $redirect_url,
205208
];
206209

210+
Util::addScript('core', 'twofactor-request-token');
207211
return new StandaloneTemplateResponse($this->appName, 'twofactorsetupselection', $data, 'guest');
208212
}
209213

@@ -235,6 +239,7 @@ public function setupProvider(string $providerId, ?string $redirect_url = null)
235239
'template' => $tmpl->fetchPage(),
236240
];
237241
$response = new StandaloneTemplateResponse($this->appName, 'twofactorsetupchallenge', $data, 'guest');
242+
Util::addScript('core', 'twofactor-request-token');
238243
return $response;
239244
}
240245

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { onRequestTokenUpdate } from '@nextcloud/auth'
7+
import { getBaseUrl } from '@nextcloud/router'
8+
9+
document.addEventListener('DOMContentLoaded', () => {
10+
onRequestTokenUpdate((token) => {
11+
const cancelLink = window.document.getElementById('cancel-login')
12+
if (!cancelLink) {
13+
return
14+
}
15+
16+
const href = cancelLink.getAttribute('href')
17+
if (!href) {
18+
return
19+
}
20+
21+
const parsedHref = new URL(href, getBaseUrl())
22+
parsedHref.searchParams.set('requesttoken', token)
23+
cancelLink.setAttribute('href', parsedHref.pathname + parsedHref.search)
24+
})
25+
})

core/templates/twofactorselectchallenge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
</a>
7777
</p>
7878
<?php endif; ?>
79-
<p><a class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
79+
<p><a id="cancel-login" class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
8080
<?php p($l->t('Cancel login')) ?>
8181
</a></p>
8282
</div>

core/templates/twofactorsetupchallenge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="body-login-container update">
1515
<h2 class="two-factor-header"><?php p($provider->getDisplayName()); ?></h2>
1616
<?php print_unescaped($template); ?>
17-
<p><a class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
17+
<p><a id="cancel-login" class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
1818
<?php p($l->t('Cancel login')) ?>
1919
</a></p>
2020
</div>

core/templates/twofactorsetupselection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</li>
3636
<?php endforeach; ?>
3737
</ul>
38-
<p><a class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
38+
<p><a id="cancel-login" class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
3939
<?php p($l->t('Cancel login')) ?>
4040
</a></p>
4141
</div>

core/templates/twofactorshowchallenge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</a>
3939
</p>
4040
<?php endif; ?>
41-
<p><a class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
41+
<p><a id="cancel-login" class="two-factor-secondary" href="<?php print_unescaped($_['logout_url']); ?>">
4242
<?php p($l->t('Cancel login')) ?>
4343
</a></p>
4444
</div>

dist/core-common.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-common.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-twofactor-request-token.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)