Skip to content

Commit 4162b68

Browse files
Merge pull request #51908 from nextcloud/backport/51905/stable29
[stable29] fix(session): Only mark sessions of permanent tokens as app passwords
2 parents a0d642e + 91fda24 commit 4162b68

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/private/User/Session.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -864,9 +864,8 @@ public function tryTokenLogin(IRequest $request) {
864864
return true;
865865
}
866866

867-
// Remember me tokens are not app_passwords
868-
if ($dbToken->getRemember() === IToken::DO_NOT_REMEMBER) {
869-
// Set the session variable so we know this is an app password
867+
// Set the session variable so we know this is an app password
868+
if ($dbToken instanceof PublicKeyToken && $dbToken->getType() === IToken::PERMANENT_TOKEN) {
870869
$this->session->set('app_password', $token);
871870
}
872871

tests/lib/User/SessionTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\Security\Bruteforce\IThrottler;
3636
use OCP\Security\ISecureRandom;
3737
use OCP\User\Events\PostLoginEvent;
38+
use PHPUnit\Framework\ExpectationFailedException;
3839
use PHPUnit\Framework\MockObject\MockObject;
3940
use Psr\Log\LoggerInterface;
4041
use function array_diff;
@@ -612,6 +613,45 @@ public function testTryTokenLoginSessionIdTokenNotFound(): void {
612613
self::assertFalse($loginResult);
613614
}
614615

616+
public function testTryTokenLoginNotAnAppPassword(): void {
617+
$request = $this->createMock(IRequest::class);
618+
$this->config->expects(self::once())
619+
->method('getSystemValueString')
620+
->with('instanceid')
621+
->willReturn('abc123');
622+
$request->method('getHeader')->with('Authorization')->willReturn('');
623+
$request->method('getCookie')->with('abc123')->willReturn('abcde12345');
624+
$this->session->expects(self::once())
625+
->method('getId')
626+
->willReturn('abcde12345');
627+
$dbToken = new PublicKeyToken();
628+
$dbToken->setId(42);
629+
$dbToken->setUid('johnny');
630+
$dbToken->setLoginName('johnny');
631+
$dbToken->setLastCheck(0);
632+
$dbToken->setType(IToken::TEMPORARY_TOKEN);
633+
$dbToken->setRemember(IToken::REMEMBER);
634+
$this->tokenProvider->expects(self::any())
635+
->method('getToken')
636+
->with('abcde12345')
637+
->willReturn($dbToken);
638+
$this->session->method('set')
639+
->willReturnCallback(function ($key, $value) {
640+
if ($key === 'app_password') {
641+
throw new ExpectationFailedException('app_password should not be set in session');
642+
}
643+
});
644+
$user = $this->createMock(IUser::class);
645+
$user->method('isEnabled')->willReturn(true);
646+
$this->manager->method('get')
647+
->with('johnny')
648+
->willReturn($user);
649+
650+
$loginResult = $this->userSession->tryTokenLogin($request);
651+
652+
self::assertTrue($loginResult);
653+
}
654+
615655
public function testRememberLoginValidToken() {
616656
$session = $this->createMock(Memory::class);
617657
$managerMethods = get_class_methods(Manager::class);

0 commit comments

Comments
 (0)