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
28 changes: 25 additions & 3 deletions apps/encryption/lib/Crypto/EncryptAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function __construct(
protected IFactory $l10nFactory,
protected QuestionHelper $questionHelper,
protected ISecureRandom $secureRandom,
protected LoggerInterface $logger,
) {
// store one time passwords for the users
$this->userPasswords = [];
Expand Down Expand Up @@ -207,9 +209,22 @@ protected function encryptUsersFiles($uid, ProgressBar $progress, $userCount) {
} else {
$progress->setMessage("encrypt files for user $userCount: $path");
$progress->advance();
if ($this->encryptFile($path) === false) {
$progress->setMessage("encrypt files for user $userCount: $path (already encrypted)");
try {
if ($this->encryptFile($path) === false) {
$progress->setMessage("encrypt files for user $userCount: $path (already encrypted)");
$progress->advance();
}
} catch (\Exception $e) {
$progress->setMessage("Failed to encrypt path $path: " . $e->getMessage());
$progress->advance();
$this->logger->error(
'Failed to encrypt path {path}',
[
'user' => $uid,
'path' => $path,
'exception' => $e,
]
);
}
}
}
Expand All @@ -234,7 +249,14 @@ protected function encryptFile($path) {
$target = $path . '.encrypted.' . time();

try {
$this->rootView->copy($source, $target);
$copySuccess = $this->rootView->copy($source, $target);
if ($copySuccess === false) {
/* Copy failed, abort */
if ($this->rootView->file_exists($target)) {
$this->rootView->unlink($target);
}
throw new \Exception('Copy failed for ' . $source);
}
$this->rootView->rename($target, $source);
} catch (DecryptionFailedException $e) {
if ($this->rootView->file_exists($target)) {
Expand Down
27 changes: 18 additions & 9 deletions apps/encryption/tests/Crypto/EncryptAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\Security\ISecureRandom;
use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
Expand All @@ -46,6 +47,7 @@ class EncryptAllTest extends TestCase {
protected \Symfony\Component\Console\Output\OutputInterface&MockObject $outputInterface;
protected UserInterface&MockObject $userInterface;
protected ISecureRandom&MockObject $secureRandom;
protected LoggerInterface&MockObject $logger;

protected EncryptAll $encryptAll;

Expand Down Expand Up @@ -76,6 +78,7 @@ protected function setUp(): void {
->disableOriginalConstructor()->getMock();
$this->userInterface = $this->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()->getMock();
$this->logger = $this->createMock(LoggerInterface::class);

/**
* We need format method to return a string
Expand Down Expand Up @@ -106,12 +109,13 @@ protected function setUp(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
);
}

public function testEncryptAll(): void {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
/** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
Expand All @@ -125,7 +129,8 @@ public function testEncryptAll(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
Expand All @@ -140,7 +145,7 @@ public function testEncryptAll(): void {
}

public function testEncryptAllWithMasterKey(): void {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
/** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
Expand All @@ -154,7 +159,8 @@ public function testEncryptAllWithMasterKey(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
Expand All @@ -170,7 +176,7 @@ public function testEncryptAllWithMasterKey(): void {
}

public function testCreateKeyPairs(): void {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
/** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
Expand All @@ -184,7 +190,8 @@ public function testCreateKeyPairs(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->onlyMethods(['setupUserFS', 'generateOneTimePassword'])
Expand Down Expand Up @@ -234,7 +241,8 @@ public function testEncryptAllUsersFiles(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->onlyMethods(['encryptUsersFiles'])
Expand Down Expand Up @@ -275,7 +283,8 @@ public function testEncryptUsersFiles(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->onlyMethods(['encryptFile', 'setupUserFS'])
Expand Down
20 changes: 17 additions & 3 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\Encryption\Keys\IStorage;
use OCP\Files;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\GenericFileException;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -685,12 +686,16 @@ private function copyBetweenStorage(
try {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
$target = $this->fopen($targetInternalPath, 'w');
[, $result] = Files::streamCopy($source, $target, true);
if ($source === false || $target === false) {
$result = false;
} else {
[, $result] = Files::streamCopy($source, $target, true);
}
} finally {
if (is_resource($source)) {
if (isset($source) && $source !== false) {
fclose($source);
}
if (is_resource($target)) {
if (isset($target) && $target !== false) {
fclose($target);
}
}
Expand Down Expand Up @@ -740,6 +745,9 @@ public function stat(string $path): array|false {

public function hash(string $type, string $path, bool $raw = false): string|false {
$fh = $this->fopen($path, 'rb');
if ($fh === false) {
return false;
}
$ctx = hash_init($type);
hash_update_stream($ctx, $fh);
fclose($fh);
Expand All @@ -764,6 +772,9 @@ protected function readFirstBlock(string $path): string {
$firstBlock = '';
if ($this->storage->is_file($path)) {
$handle = $this->storage->fopen($path, 'r');
if ($handle === false) {
return '';
}
$firstBlock = fread($handle, $this->util->getHeaderSize());
fclose($handle);
}
Expand Down Expand Up @@ -894,6 +905,9 @@ protected function shouldEncrypt(string $path): bool {
public function writeStream(string $path, $stream, ?int $size = null): int {
// always fall back to fopen
$target = $this->fopen($path, 'w');
if ($target === false) {
throw new GenericFileException("Failed to open $path for writing");
}
[$count, $result] = Files::streamCopy($stream, $target, true);
fclose($stream);
fclose($target);
Expand Down
Loading