Repository navigation
Improve login flow v2 approval pages and warn about IP mismatch #66223
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
base: master
Are you sure you want to change the base?
Changes from all commits
4956dfd
0128fb0
1bfe0e8
c41ab65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| use OC\Core\Exception\LoginFlowV2NotFoundException; | ||
| use OC\Core\ResponseDefinitions; | ||
| use OC\Core\Service\LoginFlowV2Service; | ||
| use OC\Security\Ip\Address; | ||
| use OCP\AppFramework\Controller; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\Attribute\FrontpageRoute; | ||
|
|
@@ -30,7 +31,6 @@ | |
| use OCP\AppFramework\Http\StandaloneTemplateResponse; | ||
| use OCP\AppFramework\Services\IInitialState; | ||
| use OCP\Authentication\Exceptions\InvalidTokenException; | ||
| use OCP\Defaults; | ||
| use OCP\IL10N; | ||
| use OCP\IRequest; | ||
| use OCP\ISession; | ||
|
|
@@ -56,7 +56,6 @@ public function __construct( | |
| private ISession $session, | ||
| private IUserSession $userSession, | ||
| private ISecureRandom $random, | ||
| private Defaults $defaults, | ||
| private ?string $userId, | ||
| private IL10N $l10n, | ||
| private IInitialState $initialState, | ||
|
|
@@ -109,7 +108,7 @@ public function landing(string $token, $user = '', int $direct = 0): Response { | |
| #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)] | ||
| #[UseSession] | ||
| #[FrontpageRoute(verb: 'GET', url: '/login/v2/flow')] | ||
| public function showAuthPickerPage(string $user = '', int $direct = 0): StandaloneTemplateResponse { | ||
| public function showAuthPickerPage(string $user = '', int $direct = 0): Response { | ||
| try { | ||
| $flow = $this->getFlowByLoginToken(); | ||
| } catch (LoginFlowV2NotFoundException $e) { | ||
|
|
@@ -123,13 +122,17 @@ public function showAuthPickerPage(string $user = '', int $direct = 0): Standalo | |
| ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS | ||
| ); | ||
| $this->session->set(self::STATE_NAME, $stateToken); | ||
| $loginRedirectUrl = $this->urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.grantPage', ['stateToken' => $stateToken, 'user' => $user, 'direct' => $direct]); | ||
| if ($this->userSession->getUser() !== null) { | ||
| return new RedirectResponse($loginRedirectUrl); | ||
| } | ||
|
|
||
| $this->initialState->provideInitialState('loginFlowState', 'auth'); | ||
| $this->provideFlowContext($flow); | ||
| $this->initialState->provideInitialState('loginFlowState', 'auth-v2'); | ||
| $this->initialState->provideInitialState('loginFlowAuth', [ | ||
| 'client' => $flow->getClientName(), | ||
| 'instanceName' => $this->defaults->getName(), | ||
| 'stateToken' => $stateToken, | ||
| 'loginRedirectUrl' => $this->urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.grantPage', ['stateToken' => $stateToken, 'user' => $user, 'direct' => $direct]), | ||
| 'direct' => $direct === 1, | ||
| 'loginRedirectUrl' => $loginRedirectUrl, | ||
| 'appTokenUrl' => $this->urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.apptokenRedirect'), | ||
| ]); | ||
|
|
||
|
|
@@ -164,16 +167,10 @@ public function grantPage(?string $stateToken, int $direct = 0): StandaloneTempl | |
| return $this->loginTokenForbiddenClientResponse(); | ||
| } | ||
|
|
||
| $user = $this->userSession->getUser(); | ||
| \assert($user !== null); | ||
|
|
||
| $this->initialState->provideInitialState('loginFlowState', 'grant'); | ||
| $this->provideFlowContext($flow); | ||
| $this->initialState->provideInitialState('loginFlowState', 'grant-v2'); | ||
| $this->initialState->provideInitialState('loginFlowGrant', [ | ||
| 'actionUrl' => $this->urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.generateAppPassword'), | ||
| 'userId' => $user->getUID(), | ||
| 'userDisplayName' => $user->getDisplayName(), | ||
| 'client' => $flow->getClientName(), | ||
| 'instanceName' => $this->defaults->getName(), | ||
| 'stateToken' => $stateToken, | ||
| 'direct' => $direct === 1, | ||
| ]); | ||
|
|
@@ -188,6 +185,33 @@ public function grantPage(?string $stateToken, int $direct = 0): StandaloneTempl | |
| } | ||
|
|
||
| #[PublicPage] | ||
| #[UseSession] | ||
| #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)] | ||
| #[FrontpageRoute(verb: 'POST', url: '/login/v2/decline')] | ||
| public function decline(?string $stateToken): JSONResponse { | ||
| if ($stateToken === null || !$this->isValidStateToken($stateToken)) { | ||
| return new JSONResponse([], Http::STATUS_FORBIDDEN); | ||
| } | ||
|
|
||
| try { | ||
| $this->getFlowByLoginToken(); | ||
| } catch (LoginFlowV2NotFoundException $e) { | ||
| return new JSONResponse([], Http::STATUS_GONE); | ||
| } catch (LoginFlowV2ClientForbiddenException $e) { | ||
| return new JSONResponse([], Http::STATUS_FORBIDDEN); | ||
| } | ||
|
|
||
| if (!$this->loginFlowV2Service->decline($this->session->get(self::TOKEN_NAME))) { | ||
| return new JSONResponse([], Http::STATUS_CONFLICT); | ||
| } | ||
|
|
||
| $this->session->remove(self::TOKEN_NAME); | ||
| $this->session->remove(self::STATE_NAME); | ||
| return new JSONResponse([]); | ||
| } | ||
|
|
||
| #[PublicPage] | ||
| #[UseSession] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this one need
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There might be a race condition when not locking the session, as state token lookup/validation could be separated from the token cleanup. Would you recommend removing $this->session->remove(self::TOKEN_NAME);
$this->session->remove(self::STATE_NAME);
$this->session->close(); |
||
| #[FrontpageRoute(verb: 'POST', url: '/login/v2/apptoken')] | ||
| public function apptokenRedirect(?string $stateToken, string $user, string $password) { | ||
| if ($stateToken === null) { | ||
|
|
@@ -270,6 +294,9 @@ private function handleFlowDone(bool $result): StandaloneTemplateResponse { | |
| Util::addScript('core', 'login_flow'); | ||
| Util::addStyle('core', 'login_flow'); | ||
| $this->initialState->provideInitialState('loginFlowState', 'done'); | ||
| $this->initialState->provideInitialState('loginFlowDone', [ | ||
| 'securitySettingsUrl' => $this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security']), | ||
| ]); | ||
| return new StandaloneTemplateResponse( | ||
| $this->appName, | ||
| 'loginflow', | ||
|
|
@@ -304,7 +331,7 @@ public function init(): JSONResponse { | |
| // Get client user agent | ||
| $userAgent = $this->request->getHeader('user-agent'); | ||
|
|
||
| $tokens = $this->loginFlowV2Service->createTokens($userAgent); | ||
| $tokens = $this->loginFlowV2Service->createTokens($userAgent, $this->request->getRemoteAddress()); | ||
|
|
||
| $data = [ | ||
| 'poll' => [ | ||
|
|
@@ -317,6 +344,29 @@ public function init(): JSONResponse { | |
| return new JSONResponse($data); | ||
| } | ||
|
|
||
| private function provideFlowContext(LoginFlowV2 $flow): void { | ||
| $clientIp = $flow->getClientIp(); | ||
| $browserIp = $this->request->getRemoteAddress(); | ||
| $sameIp = null; | ||
| if ($clientIp !== null && Address::isValid($clientIp) && Address::isValid($browserIp)) { | ||
| $sameIp = (string)new Address($clientIp) === (string)new Address($browserIp); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit worried about IPv6 here. With privacy addresses the same machine can show up with a different IP per connection, and with dual stack the app can come in over v6 while the browser uses v4. Both would show the warning on a normal login. Maybe compare the /64 for v6, and leave
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good points! Comparing the IPv6 /64 is a great idea. The downside is that an attacker on the same shared subnet would not trigger a warning. But that's already the case anyways for IPv4 behind NAT, so we don't loose much. For the case when one IP is v4 and the other is v6 I'm less sure, hmmm... An attacker could easily disable the warning then by using whatever IP version the user is not using on the phishing website. Even without that info, there's a substantial chance the attacker can disable the warning by guessing the correct IP version. There is a trade-off here: Do we want fewer false alarms for dual-stack, but then also less warnings for actual attacks? @susnux @skjnldsv |
||
| } | ||
|
|
||
| $user = $this->userSession->getUser(); | ||
| $this->initialState->provideInitialState('loginFlowContext', [ | ||
| 'userAgent' => $flow->getClientName(), | ||
| 'serverUrl' => $this->getServerPath(), | ||
| 'userId' => $user?->getUID(), | ||
| 'userDisplayName' => $user?->getDisplayName(), | ||
| 'declineUrl' => $this->urlGenerator->linkToRouteAbsolute('core.ClientFlowLoginV2.decline'), | ||
| 'securitySettingsUrl' => $this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security']), | ||
| 'createdAt' => $flow->getTimestamp(), | ||
| 'clientIp' => $clientIp, | ||
| 'browserIp' => $browserIp, | ||
| 'sameIp' => $sameIp, | ||
| ]); | ||
| } | ||
|
|
||
| private function isValidStateToken(string $stateToken): bool { | ||
| $currentToken = $this->session->get(self::STATE_NAME); | ||
| if (!is_string($stateToken) || !is_string($currentToken)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Core\Migrations; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\Attributes\AddColumn; | ||
| use OCP\Migration\Attributes\ColumnType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
|
|
||
| #[AddColumn(table: 'login_flow_v2', name: 'client_ip', type: ColumnType::STRING)] | ||
| class Version36000Date20261103000000 extends SimpleMigrationStep { | ||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
| $table = $schema->getTable('login_flow_v2'); | ||
| if ($table->hasColumn('client_ip')) { | ||
| return null; | ||
| } | ||
|
|
||
| $table->addColumn('client_ip', Types::STRING, [ | ||
| 'notnull' => false, | ||
| 'length' => 45, | ||
| ]); | ||
|
|
||
| return $schema; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.