Skip to content

Commit 71688e3

Browse files
authored
Merge pull request #56167 from nextcloud/fix/fix-encryption-with-user-keys
fix(encryption): Fix user key support with basic auth
2 parents 70446b8 + 928a45c commit 71688e3

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

apps/encryption/lib/KeyManager.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class KeyManager {
2323
private string $recoveryKeyId;
2424
private string $publicShareKeyId;
2525
private string $masterKeyId;
26-
private ?string $keyUid;
2726
private string $publicKeyId = 'publicKey';
2827
private string $privateKeyId = 'privateKey';
2928
private string $shareKeyId = 'shareKey';
@@ -33,7 +32,7 @@ public function __construct(
3332
private IStorage $keyStorage,
3433
private Crypt $crypt,
3534
private IConfig $config,
36-
IUserSession $userSession,
35+
private IUserSession $userSession,
3736
private Session $session,
3837
private LoggerInterface $logger,
3938
private Util $util,
@@ -61,8 +60,6 @@ public function __construct(
6160
$this->masterKeyId = 'master_' . substr(md5((string)time()), 0, 8);
6261
$this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
6362
}
64-
65-
$this->keyUid = $userSession->isLoggedIn() ? $userSession->getUser()?->getUID() : null;
6663
}
6764

6865
/**
@@ -352,7 +349,7 @@ public function getPrivateKey($userId) {
352349
* @param ?bool $useLegacyFileKey null means try both
353350
*/
354351
public function getFileKey(string $path, ?bool $useLegacyFileKey, bool $useDecryptAll = false): string {
355-
$publicAccess = ($this->keyUid === null);
352+
$publicAccess = !$this->userSession->isLoggedIn();
356353
$encryptedFileKey = '';
357354
if ($useLegacyFileKey ?? true) {
358355
$encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
@@ -381,7 +378,7 @@ public function getFileKey(string $path, ?bool $useLegacyFileKey, bool $useDecry
381378
$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.' . $this->privateKeyId, Encryption::ID);
382379
$privateKey = $this->crypt->decryptPrivateKey($privateKey);
383380
} else {
384-
$uid = $this->keyUid;
381+
$uid = $this->userSession->getUser()?->getUID();
385382
$shareKey = $this->getShareKey($path, $uid);
386383
$privateKey = $this->session->getPrivateKey();
387384
}

apps/encryption/tests/KeyManagerTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OCP\Files\Cache\ICache;
2323
use OCP\Files\Storage\IStorage as FilesIStorage;
2424
use OCP\IConfig;
25+
use OCP\IUser;
2526
use OCP\IUserSession;
2627
use OCP\Lock\ILockingProvider;
2728
use OCP\Lock\LockedException;
@@ -356,6 +357,9 @@ public static function dataTestGetFileKey(): array {
356357
public function testGetFileKey(?string $uid, bool $isMasterKeyEnabled, string $privateKey, string $encryptedFileKey, string $expected): void {
357358
$path = '/foo.txt';
358359

360+
$this->userMock->expects(self::once())
361+
->method('isLoggedIn')
362+
->willReturn($uid !== null);
359363
if ($isMasterKeyEnabled) {
360364
$expectedUid = 'masterKeyId';
361365
$this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
@@ -364,10 +368,16 @@ public function testGetFileKey(?string $uid, bool $isMasterKeyEnabled, string $p
364368
$expectedUid = 'systemKeyId';
365369
} else {
366370
$expectedUid = $uid;
371+
$userObjectMock = $this->createMock(IUser::class);
372+
$userObjectMock->expects(self::once())
373+
->method('getUID')
374+
->willReturn($uid);
375+
$this->userMock->expects(self::once())
376+
->method('getUser')
377+
->willReturn($userObjectMock);
367378
}
368379

369380
$this->invokePrivate($this->instance, 'masterKeyId', ['masterKeyId']);
370-
$this->invokePrivate($this->instance, 'keyUid', [$uid]);
371381

372382
$this->keyStorageMock->expects($this->exactly(2))
373383
->method('getFileKey')

0 commit comments

Comments
 (0)