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
30 changes: 26 additions & 4 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 @@ -82,7 +83,8 @@
IL10N $l,
IFactory $l10nFactory,
QuestionHelper $questionHelper,
ISecureRandom $secureRandom
ISecureRandom $secureRandom,
protected LoggerInterface $logger,
) {
$this->userSetup = $userSetup;
$this->userManager = $userManager;
Expand Down Expand Up @@ -251,9 +253,22 @@
} 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 @@ -278,7 +293,14 @@
$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
35 changes: 23 additions & 12 deletions apps/encryption/tests/Crypto/EncryptAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use OCP\Mail\IMailer;
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 Down Expand Up @@ -71,6 +73,8 @@ class EncryptAllTest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Security\ISecureRandom */
protected $secureRandom;

protected LoggerInterface&MockObject $logger;

/** @var EncryptAll */
protected $encryptAll;

Expand Down Expand Up @@ -101,6 +105,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 @@ -131,12 +136,13 @@ protected function setUp(): void {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
);
}

public function testEncryptAll() {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
public function testEncryptAll(): void {
/** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
Expand All @@ -150,7 +156,8 @@ public function testEncryptAll() {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
Expand All @@ -164,8 +171,8 @@ public function testEncryptAll() {
$encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
}

public function testEncryptAllWithMasterKey() {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
public function testEncryptAllWithMasterKey(): void {
/** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
Expand All @@ -179,7 +186,8 @@ public function testEncryptAllWithMasterKey() {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
Expand All @@ -194,8 +202,8 @@ public function testEncryptAllWithMasterKey() {
$encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
}

public function testCreateKeyPairs() {
/** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
public function testCreateKeyPairs(): void {
/** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
Expand All @@ -209,7 +217,8 @@ public function testCreateKeyPairs() {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->setMethods(['setupUserFS', 'generateOneTimePassword'])
Expand Down Expand Up @@ -259,7 +268,8 @@ public function testEncryptAllUsersFiles() {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->setMethods(['encryptUsersFiles'])
Expand Down Expand Up @@ -295,7 +305,8 @@ public function testEncryptUsersFiles() {
$this->l,
$this->l10nFactory,
$this->questionHelper,
$this->secureRandom
$this->secureRandom,
$this->logger,
]
)
->setMethods(['encryptFile', 'setupUserFS'])
Expand Down
48 changes: 31 additions & 17 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OCP\Encryption\IManager;
use OCP\Encryption\Keys\IStorage;
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 @@ -265,11 +266,11 @@ public function unlink($path) {
public function rename($source, $target) {
$result = $this->storage->rename($source, $target);

if ($result &&
if ($result
// versions always use the keys from the original file, so we can skip
// this step for versions
$this->isVersion($target) === false &&
$this->encryptionManager->isEnabled()) {
&& $this->isVersion($target) === false
&& $this->encryptionManager->isEnabled()) {
$sourcePath = $this->getFullPath($source);
if (!$this->util->isExcluded($sourcePath)) {
$targetPath = $this->getFullPath($target);
Expand All @@ -296,9 +297,9 @@ public function rename($source, $target) {
public function rmdir($path) {
$result = $this->storage->rmdir($path);
$fullPath = $this->getFullPath($path);
if ($result &&
$this->util->isExcluded($fullPath) === false &&
$this->encryptionManager->isEnabled()
if ($result
&& $this->util->isExcluded($fullPath) === false
&& $this->encryptionManager->isEnabled()
) {
$this->keyStorage->deleteAllFileKeys($fullPath);
}
Expand All @@ -317,9 +318,9 @@ public function isReadable($path) {

$metaData = $this->getMetaData($path);
if (
!$this->is_dir($path) &&
isset($metaData['encrypted']) &&
$metaData['encrypted'] === true
!$this->is_dir($path)
&& isset($metaData['encrypted'])
&& $metaData['encrypted'] === true
) {
$fullPath = $this->getFullPath($path);
$module = $this->getEncryptionModule($path);
Expand Down Expand Up @@ -493,9 +494,9 @@ protected function verifyUnencryptedSize(string $path, int $unencryptedSize): in
$size = $this->storage->filesize($path);
$result = $unencryptedSize;

if ($unencryptedSize < 0 ||
($size > 0 && $unencryptedSize === $size) ||
$unencryptedSize > $size
if ($unencryptedSize < 0
|| ($size > 0 && $unencryptedSize === $size)
|| $unencryptedSize > $size
) {
// check if we already calculate the unencrypted size for the
// given path to avoid recursions
Expand Down Expand Up @@ -764,8 +765,8 @@ private function copyBetweenStorage(
) {
// for versions we have nothing to do, because versions should always use the
// key from the original file. Just create a 1:1 copy and done
if ($this->isVersion($targetInternalPath) ||
$this->isVersion($sourceInternalPath)) {
if ($this->isVersion($targetInternalPath)
|| $this->isVersion($sourceInternalPath)) {
// remember that we try to create a version so that we can detect it during
// fopen($sourceInternalPath) and by-pass the encryption in order to
// create a 1:1 copy of the file
Expand Down Expand Up @@ -815,12 +816,16 @@ private function copyBetweenStorage(
try {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
$target = $this->fopen($targetInternalPath, 'w');
[, $result] = \OC_Helper::streamCopy($source, $target);
if ($source === false || $target === false) {
$result = false;
} else {
[, $result] = \OC_Helper::streamCopy($source, $target);
}
} 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 @@ -870,6 +875,9 @@ public function stat($path) {

public function hash($type, $path, $raw = false) {
$fh = $this->fopen($path, 'rb');
if ($fh === false) {
return false;
}
$ctx = hash_init($type);
hash_update_stream($ctx, $fh);
fclose($fh);
Expand Down Expand Up @@ -897,6 +905,9 @@ protected function readFirstBlock($path) {
$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 @@ -1046,6 +1057,9 @@ protected function shouldEncrypt($path) {
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] = \OC_Helper::streamCopy($stream, $target);
fclose($stream);
fclose($target);
Expand Down
Loading