Skip to content
Closed
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
27 changes: 26 additions & 1 deletion apps/files/lib/Command/TransferOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class TransferOwnership extends Command {

Expand All @@ -61,6 +63,9 @@ class TransferOwnership extends Command {
/** @var ProviderFactory */
private $shareProviderFactory;

/** @var EventDispatcherInterface */
private $eventDispatcher;

/** @var bool */
private $filesExist = false;

Expand All @@ -85,13 +90,20 @@ class TransferOwnership extends Command {
/** @var string */
private $finalTarget;

public function __construct(IUserManager $userManager, IManager $shareManager, IMountManager $mountManager, Manager $encryptionManager, ILogger $logger, ProviderFactory $shareProviderFactory) {
/** @var array , Files affected with signature mismatch */
private $affectedFiles = [];

public function __construct(IUserManager $userManager, IManager $shareManager,
IMountManager $mountManager, Manager $encryptionManager,
ILogger $logger, ProviderFactory $shareProviderFactory,
EventDispatcherInterface $eventDispatcher) {
$this->userManager = $userManager;
$this->shareManager = $shareManager;
$this->mountManager = $mountManager;
$this->encryptionManager = $encryptionManager;
$this->logger = $logger;
$this->shareProviderFactory = $shareProviderFactory;
$this->eventDispatcher = $eventDispatcher;
parent::__construct();
}

Expand Down Expand Up @@ -287,7 +299,20 @@ protected function transfer(OutputInterface $output) {
}
}

$this->eventDispatcher->addListener('files.aftersignaturemismatch', function (GenericEvent $event) {
if ($event->hasArgument('signatureMismatch') && $event->hasArgument('fileName')) {
$fileName = $event->getArgument('fileName');
if (!isset($this->affectedFiles[$fileName])) {
$this->affectedFiles[$fileName] = 1;
}
}
}, 10);

$view->rename($sourcePath, $this->finalTarget);
if (\count($this->affectedFiles) >= 1) {
$glueString = (\count($this->affectedFiles) > 1) ? ", " : "";
$output->writeln("<error>The affected files are : " . \implode($glueString, \array_keys($this->affectedFiles)) . "</error>");
}

if (!\is_dir("$this->sourceUser/files")) {
// because the files folder is moved away we need to recreate it
Expand Down
19 changes: 18 additions & 1 deletion lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
use OCP\Files\Storage;
use OCP\ILogger;
use OCP\Files\Cache\ICacheEntry;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Encryption extends Wrapper {
use LocalTempFileTrait;
Expand Down Expand Up @@ -85,6 +87,9 @@ class Encryption extends Wrapper {
/** @var ArrayCache */
private $arrayCache;

/** @var EventDispatcherInterface */
private $eventDispatcher;

/** @var array which has information of sourcePath during rename operation */
private $sourcePath;

Expand Down Expand Up @@ -112,7 +117,8 @@ public function __construct(
IStorage $keyStorage = null,
Update $update = null,
Manager $mountManager = null,
ArrayCache $arrayCache = null
ArrayCache $arrayCache = null,
EventDispatcherInterface $eventDispatcher = null
) {
$this->mountPoint = $parameters['mountPoint'];
$this->mount = $parameters['mount'];
Expand All @@ -126,6 +132,7 @@ public function __construct(
$this->update = $update;
$this->mountManager = $mountManager;
$this->arrayCache = $arrayCache;
$this->eventDispatcher = ($eventDispatcher === null) ? \OC::$server->getEventDispatcher() : $eventDispatcher;
parent::__construct($parameters);
}

Expand Down Expand Up @@ -774,6 +781,13 @@ private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInte
}
} else {
try {
$signatureMismatch = false;
$this->eventDispatcher->addListener('files.aftersignaturemismatch', function (GenericEvent $event) use ($sourceInternalPath, &$signatureMismatch) {
if ($event->hasArgument('signatureMismatch') && ($event->getArgument('signatureMismatch') === true)) {
$event->setArgument('fileName', $sourceInternalPath);
$signatureMismatch = true;
}
}, 20);
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
if ($isRename && (\count($mount) === 1)) {
$sourceStorageMountPoint = $mount[0]->getMountPoint();
Expand All @@ -785,6 +799,9 @@ private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInte
list(, $result) = \OC_Helper::streamCopy($source, $target);
\fclose($source);
\fclose($target);
if ($signatureMismatch === true) {
$this->logger->warning("The file $sourceInternalPath has signature mismatch");
}
} catch (\Exception $e) {
Encryption::setDisableWriteEncryption(false);
\fclose($source);
Expand Down
3 changes: 3 additions & 0 deletions lib/private/Files/Stream/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use Icewind\Streams\Wrapper;
use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException;
use Symfony\Component\EventDispatcher\GenericEvent;

class Encryption extends Wrapper {

Expand Down Expand Up @@ -281,6 +282,7 @@ public function stream_eof() {

public function stream_read($count) {
$result = '';
$genericEvent = new GenericEvent('SignatureMismatch', []);

$count = \min($count, $this->unencryptedSize - $this->position);
while ($count > 0) {
Expand All @@ -302,6 +304,7 @@ public function stream_read($count) {
$count -= ($this->unencryptedBlockSize - $blockPosition);
}
}
\OC::$server->getEventDispatcher()->dispatch('files.aftersignaturemismatch', $genericEvent);
return $result;
}

Expand Down
11 changes: 10 additions & 1 deletion tests/lib/Files/Storage/Wrapper/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use OC\Files\View;
use OC\User\Manager;
use OCP\Files\Storage\IStorage;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\Files\Storage\Storage;
use OC\Files\Storage\Wrapper\Encryption;
use OC\Files\Cache\Cache;
Expand Down Expand Up @@ -101,6 +103,9 @@ class EncryptionTest extends Storage {
/** @var \OC\Memcache\ArrayCache | \PHPUnit_Framework_MockObject_MockObject */
private $arrayCache;

/** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */
private $eventDisaptcher;

/** @var integer dummy unencrypted size */
private $dummySize = -1;

Expand Down Expand Up @@ -178,6 +183,8 @@ protected function setUp() {
->disableOriginalConstructor()->getMock();
$this->mountManager->expects($this->any())->method('findByStorageId')->willReturn([]);

$this->eventDisaptcher = $this->createMock(EventDispatcherInterface::class);

$this->instance = $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
Expand All @@ -187,7 +194,9 @@ protected function setUp() {
'mountPoint' => '/',
'mount' => $this->mount
],
$this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
$this->encryptionManager, $this->util, $this->logger,
$this->file, null, $this->keyStore, $this->update,
$this->mountManager, $this->arrayCache, $this->eventDisaptcher
]
)
->setMethods(['getMetaData', 'getCache', 'getEncryptionModule'])
Expand Down