Skip to content

Commit

Permalink
Merge pull request #35649 from nextcloud/bugfix/object-storage-master…
Browse files Browse the repository at this point in the history
…-key-enc

Update the unencrypted file size when closing streams
  • Loading branch information
juliusknorr authored Dec 7, 2022
2 parents 0012787 + 59ae6e9 commit 87e638b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/private/Files/Stream/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function stream_close() {
$cacheEntry = $cache->get($this->internalPath);
if ($cacheEntry) {
$version = $cacheEntry['encryptedVersion'] + 1;
$cache->update($cacheEntry->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
$cache->update($cacheEntry->getId(), ['encrypted' => $version, 'encryptedVersion' => $version, 'unencrypted_size' => $this->unencryptedSize]);
}
}

Expand Down Expand Up @@ -528,6 +528,7 @@ protected function readCache() {
*/
protected function writeHeader() {
$header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
$this->fileUpdated = true;
return parent::stream_write($header);
}

Expand Down
20 changes: 11 additions & 9 deletions tests/lib/Files/Stream/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EncryptionTest extends \Test\TestCase {
public const DEFAULT_WRAPPER = '\OC\Files\Stream\Encryption';

/** @var \OCP\Encryption\IEncryptionModule | \PHPUnit\Framework\MockObject\MockObject */
private $encryptionModule;
Expand All @@ -22,7 +23,7 @@ class EncryptionTest extends \Test\TestCase {
* @param integer $unencryptedSize
* @return resource
*/
protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC\Files\Stream\Encryption') {
protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = self::DEFAULT_WRAPPER, $unencryptedSizeOnClose = 0) {
clearstatcache();
$size = filesize($fileName);
$source = fopen($fileName, $mode);
Expand Down Expand Up @@ -64,9 +65,10 @@ protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC
$entry = new CacheEntry([
'fileid' => 5,
'encryptedVersion' => 2,
'unencrypted_size' => $unencryptedSizeOnClose,
]);
$cache->expects($this->any())->method('get')->willReturn($entry);
$cache->expects($this->any())->method('update')->with(5, ['encrypted' => 3, 'encryptedVersion' => 3]);
$cache->expects($this->any())->method('update')->with(5, ['encrypted' => 3, 'encryptedVersion' => 3, 'unencrypted_size' => $unencryptedSizeOnClose]);


return $wrapper::wrap($source, $internalPath,
Expand Down Expand Up @@ -188,15 +190,15 @@ public function dataProviderStreamOpen() {

public function testWriteRead() {
$fileName = tempnam("/tmp", "FOO");
$stream = $this->getStream($fileName, 'w+', 0);
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6);
$this->assertEquals(6, fwrite($stream, 'foobar'));
fclose($stream);

$stream = $this->getStream($fileName, 'r', 6);
$this->assertEquals('foobar', fread($stream, 100));
fclose($stream);

$stream = $this->getStream($fileName, 'r+', 6);
$stream = $this->getStream($fileName, 'r+', 6, self::DEFAULT_WRAPPER, 6);
$this->assertEquals(3, fwrite($stream, 'bar'));
fclose($stream);

Expand All @@ -209,7 +211,7 @@ public function testWriteRead() {

public function testRewind() {
$fileName = tempnam("/tmp", "FOO");
$stream = $this->getStream($fileName, 'w+', 0);
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 6);
$this->assertEquals(6, fwrite($stream, 'foobar'));
$this->assertEquals(true, rewind($stream));
$this->assertEquals('foobar', fread($stream, 100));
Expand All @@ -227,7 +229,7 @@ public function testRewind() {
public function testSeek() {
$fileName = tempnam("/tmp", "FOO");

$stream = $this->getStream($fileName, 'w+', 0);
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, 9);
$this->assertEquals(6, fwrite($stream, 'foobar'));
$this->assertEquals(0, fseek($stream, 3));
$this->assertEquals(6, fwrite($stream, 'foobar'));
Expand Down Expand Up @@ -261,7 +263,7 @@ public function testWriteReadBigFile($testFile) {
$expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
// write it
$fileName = tempnam("/tmp", "FOO");
$stream = $this->getStream($fileName, 'w+', 0);
$stream = $this->getStream($fileName, 'w+', 0, self::DEFAULT_WRAPPER, strlen($expectedData));
// while writing the file from the beginning to the end we should never try
// to read parts of the file. This should only happen for write operations
// in the middle of a file
Expand Down Expand Up @@ -302,7 +304,7 @@ public function testWriteToNonSeekableStorage($testFile) {
$expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
// write it
$fileName = tempnam("/tmp", "FOO");
$stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper');
$stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper', strlen($expectedData));
// while writing the file from the beginning to the end we should never try
// to read parts of the file. This should only happen for write operations
// in the middle of a file
Expand All @@ -311,7 +313,7 @@ public function testWriteToNonSeekableStorage($testFile) {
fclose($stream);

// read it all
$stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper');
$stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper', strlen($expectedData));
$data = stream_get_contents($stream);
fclose($stream);

Expand Down

0 comments on commit 87e638b

Please sign in to comment.