Skip to content

Commit

Permalink
Prevent running FixEncryptedVersion without master key
Browse files Browse the repository at this point in the history
Return an error when running occ encryption:fix-encrypted-version
when master key encryption is not enabled.

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
  • Loading branch information
PVince81 committed Jun 29, 2021
1 parent 101c65a commit d3eeecb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
19 changes: 18 additions & 1 deletion apps/encryption/lib/Command/FixEncryptedVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use OC\Files\View;
use OC\HintException;
use OCA\Encryption\Util;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\ILogger;
Expand All @@ -46,14 +47,25 @@ class FixEncryptedVersion extends Command {
/** @var IUserManager */
private $userManager;

/** @var Util */
private $util;

/** @var View */
private $view;

public function __construct(IConfig $config, ILogger $logger, IRootFolder $rootFolder, IUserManager $userManager, View $view) {
public function __construct(
IConfig $config,
ILogger $logger,
IRootFolder $rootFolder,
IUserManager $userManager,
Util $util,
View $view
) {
$this->config = $config;
$this->logger = $logger;
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
$this->util = $util;
$this->view = $view;
parent::__construct();
}
Expand Down Expand Up @@ -89,6 +101,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

if (!$this->util->isMasterKeyEnabled()) {
$output->writeln("<error>Repairing only works with master key encryption.</error>\n");
return 1;
}

$user = (string)$input->getArgument('user');
$pathToWalk = "/$user/files";

Expand Down
46 changes: 46 additions & 0 deletions apps/encryption/tests/Command/FixEncryptedVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OC\Files\View;
use OCA\Encryption\Command\FixEncryptedVersion;
use OCA\Encryption\Util;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
use Test\Traits\EncryptionTrait;
Expand All @@ -48,11 +49,17 @@ class FixEncryptedVersionTest extends TestCase {
/** @var CommandTester */
private $commandTester;

/** @var Util|\PHPUnit\Framework\MockObject\MockObject */
protected $util;

public function setUp(): void {
parent::setUp();

\OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '1');

$this->util = $this->getMockBuilder(Util::class)
->disableOriginalConstructor()->getMock();

$this->userId = $this->getUniqueId('user_');

$this->createUser($this->userId, 'foo12345678');
Expand All @@ -66,6 +73,7 @@ public function setUp(): void {
\OC::$server->getLogger(),
\OC::$server->getRootFolder(),
\OC::$server->getUserManager(),
$this->util,
new View('/')
);
$this->commandTester = new CommandTester($this->fixEncryptedVersion);
Expand All @@ -80,6 +88,9 @@ public function setUp(): void {
* but greater than zero
*/
public function testEncryptedVersionLessThanOriginalValue() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$view = new View("/" . $this->userId . "/files");

$view->touch('hello.txt');
Expand Down Expand Up @@ -145,6 +156,9 @@ public function testEncryptedVersionLessThanOriginalValue() {
* but greater than zero
*/
public function testEncryptedVersionGreaterThanOriginalValue() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$view = new View("/" . $this->userId . "/files");

$view->touch('hello.txt');
Expand Down Expand Up @@ -201,6 +215,9 @@ public function testEncryptedVersionGreaterThanOriginalValue() {
}

public function testVersionIsRestoredToOriginalIfNoFixIsFound() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$view = new View("/" . $this->userId . "/files");

$view->touch('bar.txt');
Expand Down Expand Up @@ -231,6 +248,9 @@ public function testVersionIsRestoredToOriginalIfNoFixIsFound() {
* Test commands with a file path
*/
public function testExecuteWithFilePathOption() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$view = new View("/" . $this->userId . "/files");

$view->touch('hello.txt');
Expand All @@ -252,6 +272,9 @@ public function testExecuteWithFilePathOption() {
* Test commands with a directory path
*/
public function testExecuteWithDirectoryPathOption() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$view = new View("/" . $this->userId . "/files");

$view->mkdir('sub');
Expand All @@ -274,6 +297,9 @@ public function testExecuteWithDirectoryPathOption() {
* Test commands with a directory path
*/
public function testExecuteWithNoUser() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$this->commandTester->execute([
'user' => null,
'--path' => "/"
Expand All @@ -288,6 +314,9 @@ public function testExecuteWithNoUser() {
* Test commands with a directory path
*/
public function testExecuteWithNonExistentPath() {
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);

$this->commandTester->execute([
'user' => $this->userId,
'--path' => '/non-exist'
Expand All @@ -297,4 +326,21 @@ public function testExecuteWithNonExistentPath() {

$this->assertStringContainsString('Please provide a valid path.', $output);
}

/**
* Test commands without master key
*/
public function testExecuteWithNoMasterKey() {
\OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '0');
$this->util->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(false);

$this->commandTester->execute([
'user' => $this->userId,
]);

$output = $this->commandTester->getDisplay();

$this->assertStringContainsString('only works with master key', $output);
}
}

0 comments on commit d3eeecb

Please sign in to comment.