Skip to content
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
Run session token renewals in a database transaction
The session token renewal does
1) Read the old token
2) Write a new token
3) Delete the old token

If two processes succeed to read the old token there can be two new tokens because
the queries were not run in a transaction. This is particularly problematic on
clustered DBs where 1) would go to a read node and 2) and 3) go to a write node.

Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst authored and backportbot-nextcloud[bot] committed Nov 3, 2022
commit b8ad0752fa52d81cdacc623bb1456ad72124aa94
55 changes: 32 additions & 23 deletions lib/private/Authentication/Token/PublicKeyTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@
use OC\Authentication\Exceptions\TokenPasswordExpiredException;
use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Exceptions\WipeTokenException;
use OCP\AppFramework\Db\TTransactional;
use OCP\Cache\CappedMemoryCache;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use Psr\Log\LoggerInterface;

class PublicKeyTokenProvider implements IProvider {
use TTransactional;

/** @var PublicKeyTokenMapper */
private $mapper;

Expand All @@ -51,6 +55,8 @@ class PublicKeyTokenProvider implements IProvider {
/** @var IConfig */
private $config;

private IDBConnection $db;

/** @var LoggerInterface */
private $logger;

Expand All @@ -63,11 +69,13 @@ class PublicKeyTokenProvider implements IProvider {
public function __construct(PublicKeyTokenMapper $mapper,
ICrypto $crypto,
IConfig $config,
IDBConnection $db,
LoggerInterface $logger,
ITimeFactory $time) {
$this->mapper = $mapper;
$this->crypto = $crypto;
$this->config = $config;
$this->db = $db;
$this->logger = $logger;
$this->time = $time;

Expand Down Expand Up @@ -158,31 +166,32 @@ public function getTokenById(int $tokenId): IToken {
public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
$this->cache->clear();

$token = $this->getToken($oldSessionId);

if (!($token instanceof PublicKeyToken)) {
throw new InvalidTokenException("Invalid token type");
}
return $this->atomic(function () use ($oldSessionId, $sessionId) {
$token = $this->getToken($oldSessionId);

$password = null;
if (!is_null($token->getPassword())) {
$privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
$password = $this->decryptPassword($token->getPassword(), $privateKey);
}

$newToken = $this->generateToken(
$sessionId,
$token->getUID(),
$token->getLoginName(),
$password,
$token->getName(),
IToken::TEMPORARY_TOKEN,
$token->getRemember()
);

$this->mapper->delete($token);
if (!($token instanceof PublicKeyToken)) {
throw new InvalidTokenException("Invalid token type");
}

return $newToken;
$password = null;
if (!is_null($token->getPassword())) {
$privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
$password = $this->decryptPassword($token->getPassword(), $privateKey);
}
$newToken = $this->generateToken(
$sessionId,
$token->getUID(),
$token->getLoginName(),
$password,
$token->getName(),
IToken::TEMPORARY_TOKEN,
$token->getRemember()
);

$this->mapper->delete($token);

return $newToken;
}, $this->db);
}

public function invalidateToken(string $token) {
Expand Down
20 changes: 18 additions & 2 deletions tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
*
Expand Down Expand Up @@ -34,6 +37,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

Expand All @@ -46,6 +50,8 @@ class PublicKeyTokenProviderTest extends TestCase {
private $crypto;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;
/** @var IDBConnection|IDBConnection|MockObject */
private IDBConnection $db;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $logger;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
Expand All @@ -66,14 +72,24 @@ protected function setUp(): void {
['secret', '', '1f4h9s'],
['openssl', [], []],
]);
$this->db = $this->createMock(IDBConnection::class);
$this->db->method('atomic')->willReturnCallback(function ($cb) {
return $cb();
});
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->time = 1313131;
$this->timeFactory->method('getTime')
->willReturn($this->time);

$this->tokenProvider = new PublicKeyTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger,
$this->timeFactory);
$this->tokenProvider = new PublicKeyTokenProvider(
$this->mapper,
$this->crypto,
$this->config,
$this->db,
$this->logger,
$this->timeFactory,
);
}

public function testGenerateToken() {
Expand Down