Skip to content

Commit d6098f9

Browse files
committed
fix(encryption): Inject IAppConfig for encryptHomeStorage value
Signed-off-by: Stephen Cuppett <steve@cuppett.com>
1 parent 5345235 commit d6098f9

4 files changed

Lines changed: 15 additions & 26 deletions

File tree

lib/private/Encryption/EncryptionWrapper.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use OC\Memcache\ArrayCache;
1515
use OCP\Encryption\IFile;
1616
use OCP\Encryption\Keys\IStorage as EncryptionKeysStorage;
17-
use OCP\Exceptions\AppConfigTypeConflictException;
1817
use OCP\Files\Mount\IMountPoint;
1918
use OCP\Files\Storage\IDisableEncryptionStorage;
2019
use OCP\Files\Storage\IStorage;
@@ -40,6 +39,7 @@ class EncryptionWrapper {
4039
public function __construct(
4140
private ArrayCache $arrayCache,
4241
private Manager $manager,
42+
private IAppConfig $appConfig,
4343
private LoggerInterface $logger,
4444
) {
4545
}
@@ -73,7 +73,8 @@ public function wrapStorage(string $mountPoint, IStorage $storage, IMountPoint $
7373
}
7474

7575
// Skip encryption for home mounts if encryptHomeStorage is disabled
76-
if ($mount instanceof HomeMountPoint && !$this->shouldEncryptHomeStorage()) {
76+
if ($mount instanceof HomeMountPoint &&
77+
!$this->appConfig->getValueBool('encryption', 'encryptHomeStorage', true)) {
7778
return $storage;
7879
}
7980
}
@@ -103,24 +104,4 @@ public function wrapStorage(string $mountPoint, IStorage $storage, IMountPoint $
103104
$this->arrayCache
104105
);
105106
}
106-
107-
private function shouldEncryptHomeStorage(): bool {
108-
$appConfig = Server::get(IAppConfig::class);
109-
try {
110-
return $appConfig->getValueBool('encryption', 'encryptHomeStorage', true);
111-
} catch (AppConfigTypeConflictException) {
112-
// Stored as VALUE_STRING from a pre-upgrade installation.
113-
// RetypeEncryptionConfigKeys repair step will fix the type on occ upgrade.
114-
return $this->parseLegacyBoolString(
115-
$appConfig->getValueString('encryption', 'encryptHomeStorage', '1')
116-
);
117-
} catch (\Throwable) {
118-
// DB not ready (e.g. oc_appconfig does not yet exist during install).
119-
return true;
120-
}
121-
}
122-
123-
private function parseLegacyBoolString(string $value): bool {
124-
return in_array(strtolower(trim($value)), ['1', 'true', 'yes', 'on'], true);
125-
}
126107
}

lib/private/Encryption/Manager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\Encryption\IManager;
1919
use OCP\Files\Mount\IMountPoint;
2020
use OCP\Files\Storage\IStorage;
21+
use OCP\IAppConfig;
2122
use OCP\IConfig;
2223
use OCP\IL10N;
2324
use Psr\Log\LoggerInterface;
@@ -32,6 +33,7 @@ public function __construct(
3233
protected View $rootView,
3334
protected Util $util,
3435
protected ArrayCache $arrayCache,
36+
protected IAppConfig $appConfig,
3537
) {
3638
$this->encryptionModules = [];
3739
}
@@ -205,13 +207,13 @@ public function getDefaultEncryptionModuleId() {
205207
public function setupStorage() {
206208
// If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper
207209
if (!empty($this->encryptionModules) || $this->isEnabled()) {
208-
$encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
210+
$encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->appConfig, $this->logger);
209211
Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2);
210212
}
211213
}
212214

213215
public function forceWrapStorage(IMountPoint $mountPoint, IStorage $storage) {
214-
$encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
216+
$encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->appConfig, $this->logger);
215217
return $encryptionWrapper->wrapStorage($mountPoint->getMountPoint(), $storage, $mountPoint, true);
216218
}
217219

lib/private/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ public function __construct(
384384
$c->getL10N('core'),
385385
new View(),
386386
$util,
387-
new ArrayCache()
387+
new ArrayCache(),
388+
$c->get(IAppConfig::class),
388389
);
389390
});
390391
$this->registerAlias(\OCP\Encryption\IManager::class, Encryption\Manager::class);

tests/lib/Encryption/EncryptionWrapperTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Files_Trashbin\Storage;
1616
use OCP\Files\Mount\IMountPoint;
1717
use OCP\Files\Storage\IDisableEncryptionStorage;
18+
use OCP\IAppConfig;
1819
use Psr\Log\LoggerInterface;
1920
use Test\TestCase;
2021

@@ -31,15 +32,19 @@ class EncryptionWrapperTest extends TestCase {
3132
/** @var \PHPUnit\Framework\MockObject\MockObject|ArrayCache */
3233
private $arrayCache;
3334

35+
/** @var \PHPUnit\Framework\MockObject\MockObject|IAppConfig */
36+
private $appConfig;
37+
3438
#[\Override]
3539
protected function setUp(): void {
3640
parent::setUp();
3741

3842
$this->arrayCache = $this->createMock(ArrayCache::class);
3943
$this->manager = $this->createMock(Manager::class);
4044
$this->logger = $this->createMock(LoggerInterface::class);
45+
$this->appConfig = $this->createMock(IAppConfig::class);
4146

42-
$this->instance = new EncryptionWrapper($this->arrayCache, $this->manager, $this->logger);
47+
$this->instance = new EncryptionWrapper($this->arrayCache, $this->manager, $this->appConfig, $this->logger);
4348
}
4449

4550

0 commit comments

Comments
 (0)