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

[stable28] fix(Session): avoid password confirmation on SSO #45704

Merged
merged 2 commits into from
Jun 12, 2024
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
5 changes: 4 additions & 1 deletion core/Controller/OCJSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace OC\Core\Controller;

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Authentication\Token\IProvider;
use OC\CapabilitiesManager;
use OC\Template\JSConfigHelper;
use OCP\App\IAppManager;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function __construct(
IURLGenerator $urlGenerator,
CapabilitiesManager $capabilitiesManager,
IInitialStateService $initialStateService,
IProvider $tokenProvider,
) {
parent::__construct($appName, $request);

Expand All @@ -78,7 +80,8 @@ public function __construct(
$iniWrapper,
$urlGenerator,
$capabilitiesManager,
$initialStateService
$initialStateService,
$tokenProvider
);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$c->get(IControllerMethodReflector::class),
$c->get(ISession::class),
$c->get(IUserSession::class),
$c->get(ITimeFactory::class)
$c->get(ITimeFactory::class),
$c->get(\OC\Authentication\Token\IProvider::class),
)
);
$dispatcher->registerMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@

use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Authentication\Token\IProvider;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\ISession;
use OCP\IUserSession;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\User\Backend\IPasswordConfirmationBackend;
use ReflectionMethod;

Expand All @@ -45,6 +50,7 @@ class PasswordConfirmationMiddleware extends Middleware {
private $timeFactory;
/** @var array */
private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
private IProvider $tokenProvider;

/**
* PasswordConfirmationMiddleware constructor.
Expand All @@ -57,11 +63,14 @@ class PasswordConfirmationMiddleware extends Middleware {
public function __construct(ControllerMethodReflector $reflector,
ISession $session,
IUserSession $userSession,
ITimeFactory $timeFactory) {
ITimeFactory $timeFactory,
IProvider $tokenProvider,
) {
$this->reflector = $reflector;
$this->session = $session;
$this->userSession = $userSession;
$this->timeFactory = $timeFactory;
$this->tokenProvider = $tokenProvider;
}

/**
Expand All @@ -86,8 +95,21 @@ public function beforeController($controller, $methodName) {
$backendClassName = $user->getBackendClassName();
}

try {
$sessionId = $this->session->getId();
$token = $this->tokenProvider->getToken($sessionId);
} catch (SessionNotAvailableException|InvalidTokenException|WipeTokenException|ExpiredTokenException) {
// States we do not deal with here.
return;
}
$scope = $token->getScopeAsArray();
if (isset($scope['password-unconfirmable']) && $scope['password-unconfirmable'] === true) {
// Users logging in from SSO backends cannot confirm their password by design
return;
}

$lastConfirm = (int) $this->session->get('last-password-confirm');
// we can't check the password against a SAML backend, so skip password confirmation in this case
// TODO: confirm excludedUserBackEnds can go away and remove it
if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay
throw new NotConfirmedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function renewSessionToken(string $oldSessionId, string $sessionId): OCPI
OCPIToken::TEMPORARY_TOKEN,
$token->getRemember()
);
$newToken->setScope($token->getScopeAsArray());
$this->cacheToken($newToken);

$this->cacheInvalidHash($token->getToken());
Expand Down
73 changes: 37 additions & 36 deletions lib/private/Template/JSConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
namespace OC\Template;

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Authentication\Token\IProvider;
use OC\CapabilitiesManager;
use OC\Share\Share;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Authentication\Exceptions\ExpiredTokenException;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\Exceptions\WipeTokenException;
use OCP\Constants;
use OCP\Defaults;
use OCP\Files\FileInfo;
Expand All @@ -49,47 +53,29 @@
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\User\Backend\IPasswordConfirmationBackend;
use OCP\Util;

class JSConfigHelper {
protected IL10N $l;
protected Defaults $defaults;
protected IAppManager $appManager;
protected ISession $session;
protected ?IUser $currentUser;
protected IConfig $config;
protected IGroupManager $groupManager;
protected IniGetWrapper $iniWrapper;
protected IURLGenerator $urlGenerator;
protected CapabilitiesManager $capabilitiesManager;
protected IInitialStateService $initialStateService;

/** @var array user back-ends excluded from password verification */
private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];

public function __construct(IL10N $l,
Defaults $defaults,
IAppManager $appManager,
ISession $session,
?IUser $currentUser,
IConfig $config,
IGroupManager $groupManager,
IniGetWrapper $iniWrapper,
IURLGenerator $urlGenerator,
CapabilitiesManager $capabilitiesManager,
IInitialStateService $initialStateService) {
$this->l = $l;
$this->defaults = $defaults;
$this->appManager = $appManager;
$this->session = $session;
$this->currentUser = $currentUser;
$this->config = $config;
$this->groupManager = $groupManager;
$this->iniWrapper = $iniWrapper;
$this->urlGenerator = $urlGenerator;
$this->capabilitiesManager = $capabilitiesManager;
$this->initialStateService = $initialStateService;
public function __construct(
protected IL10N $l,
protected Defaults $defaults,
protected IAppManager $appManager,
protected ISession $session,
protected ?IUser $currentUser,
protected IConfig $config,
protected IGroupManager $groupManager,
protected IniGetWrapper $iniWrapper,
protected IURLGenerator $urlGenerator,
protected CapabilitiesManager $capabilitiesManager,
protected IInitialStateService $initialStateService,
protected IProvider $tokenProvider,
) {
}

public function getConfig(): string {
Expand Down Expand Up @@ -155,9 +141,13 @@ public function getConfig(): string {
}

if ($this->currentUser instanceof IUser) {
$lastConfirmTimestamp = $this->session->get('last-password-confirm');
if (!is_int($lastConfirmTimestamp)) {
$lastConfirmTimestamp = 0;
if ($this->canUserValidatePassword()) {
$lastConfirmTimestamp = $this->session->get('last-password-confirm');
if (!is_int($lastConfirmTimestamp)) {
$lastConfirmTimestamp = 0;
}
} else {
$lastConfirmTimestamp = PHP_INT_MAX;
}
} else {
$lastConfirmTimestamp = 0;
Expand Down Expand Up @@ -311,4 +301,15 @@ public function getConfig(): string {

return $result;
}

protected function canUserValidatePassword(): bool {
try {
$token = $this->tokenProvider->getToken($this->session->getId());
} catch (ExpiredTokenException|WipeTokenException|InvalidTokenException|SessionNotAvailableException) {
// actually we do not know, so we fall back to this statement
return true;
}
$scope = $token->getScopeAsArray();
return !isset($scope['password-unconfirmable']) || $scope['password-unconfirmable'] === false;
}
}
4 changes: 3 additions & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
namespace OC;

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Authentication\Token\IProvider;
use OC\Search\SearchQuery;
use OC\Template\CSSResourceLocator;
use OC\Template\JSConfigHelper;
Expand Down Expand Up @@ -259,7 +260,8 @@ public function __construct($renderAs, $appId = '') {
\OC::$server->get(IniGetWrapper::class),
\OC::$server->getURLGenerator(),
\OC::$server->getCapabilitiesManager(),
\OCP\Server::get(IInitialStateService::class)
\OCP\Server::get(IInitialStateService::class),
\OCP\Server::get(IProvider::class),
);
$config = $jsConfigHelper->getConfig();
if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
Expand Down
10 changes: 9 additions & 1 deletion lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use OC\Authentication\Token\IProvider;
use OC\User\LoginException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroupManager;
Expand Down Expand Up @@ -196,6 +196,14 @@ public static function loginWithApache(\OCP\Authentication\IApacheBackend $backe

$userSession->createSessionToken($request, $uid, $uid, $password);
$userSession->createRememberMeToken($userSession->getUser());

if (empty($password)) {
$tokenProvider = \OC::$server->get(IProvider::class);
$token = $tokenProvider->getToken($userSession->getSession()->getId());
$token->setScope(['password-unconfirmable' => true]);
$tokenProvider->updateToken($token);
}

// setup the filesystem
OC_Util::setupFS($uid);
// first call the post_login hooks, the login-process needs to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ public function testAnnotation() {
#[PasswordConfirmationRequired]
public function testAttribute() {
}

#[PasswordConfirmationRequired]
public function testSSO() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
use OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\Authentication\Token\IProvider;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Token\IToken;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUser;
Expand All @@ -49,13 +51,15 @@ class PasswordConfirmationMiddlewareTest extends TestCase {
private $controller;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $timeFactory;
private IProvider|\PHPUnit\Framework\MockObject\MockObject $tokenProvider;

protected function setUp(): void {
$this->reflector = new ControllerMethodReflector();
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->user = $this->createMock(IUser::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->tokenProvider = $this->createMock(IProvider::class);
$this->controller = new PasswordConfirmationMiddlewareController(
'test',
$this->createMock(IRequest::class)
Expand All @@ -65,7 +69,8 @@ protected function setUp(): void {
$this->reflector,
$this->session,
$this->userSession,
$this->timeFactory
$this->timeFactory,
$this->tokenProvider,
);
}

Expand Down Expand Up @@ -107,6 +112,13 @@ public function testAnnotation($backend, $lastConfirm, $currentTime, $exception)
$this->timeFactory->method('getTime')
->willReturn($currentTime);

$token = $this->createMock(IToken::class);
$token->method('getScopeAsArray')
->willReturn([]);
$this->tokenProvider->expects($this->once())
->method('getToken')
->willReturn($token);

$thrown = false;
try {
$this->middleware->beforeController($this->controller, __FUNCTION__);
Expand Down Expand Up @@ -135,6 +147,13 @@ public function testAttribute($backend, $lastConfirm, $currentTime, $exception)
$this->timeFactory->method('getTime')
->willReturn($currentTime);

$token = $this->createMock(IToken::class);
$token->method('getScopeAsArray')
->willReturn([]);
$this->tokenProvider->expects($this->once())
->method('getToken')
->willReturn($token);

$thrown = false;
try {
$this->middleware->beforeController($this->controller, __FUNCTION__);
Expand All @@ -145,6 +164,8 @@ public function testAttribute($backend, $lastConfirm, $currentTime, $exception)
$this->assertSame($exception, $thrown);
}



public function dataProvider() {
return [
['foo', 2000, 4000, true],
Expand All @@ -155,4 +176,41 @@ public function dataProvider() {
['foo', 2000, 3816, true],
];
}

public function testSSO() {
static $sessionId = 'mySession1d';

$this->reflector->reflect($this->controller, __FUNCTION__);

$this->user->method('getBackendClassName')
->willReturn('fictional_backend');
$this->userSession->method('getUser')
->willReturn($this->user);

$this->session->method('get')
->with('last-password-confirm')
->willReturn(0);
$this->session->method('getId')
->willReturn($sessionId);

$this->timeFactory->method('getTime')
->willReturn(9876);

$token = $this->createMock(IToken::class);
$token->method('getScopeAsArray')
->willReturn(['password-unconfirmable' => true]);
$this->tokenProvider->expects($this->once())
->method('getToken')
->with($sessionId)
->willReturn($token);

$thrown = false;
try {
$this->middleware->beforeController($this->controller, __FUNCTION__);
} catch (NotConfirmedException) {
$thrown = true;
}

$this->assertSame(false, $thrown);
}
}
Loading