Skip to content
Open
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
4 changes: 4 additions & 0 deletions apps/dav/lib/Connector/Sabre/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ public function moveInto($targetName, $fullSourcePath, INode $sourceNode) {
throw new ServiceUnavailable($e->getMessage(), $e->getCode(), $e);
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry(), $ex);
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage(), false, $ex);
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -499,6 +501,8 @@ public function copyInto($targetName, $sourcePath, INode $sourceNode) {
throw new ServiceUnavailable($e->getMessage(), $e->getCode(), $e);
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry(), $ex);
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage(), false, $ex);
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/lib/Connector/Sabre/ObjectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ public function copy($sourcePath, $destinationPath) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (ForbiddenException $ex) {
throw new Forbidden($ex->getMessage(), $ex->getRetry());
} catch (InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage(), false, $ex);
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
Expand Down
39 changes: 39 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,43 @@ public function testFailingMove(): void {

$targetNode->moveInto(basename($destination), $source, $sourceNode);
}

public function testMoveIntoInvalidPathFromStorage(): void {
$this->expectException(InvalidPath::class);
$this->expectExceptionMessage('Invalid path "b/foo:bar"');

$view = $this->getMockBuilder(TestViewDirectory::class)
->onlyMethods(['rename'])
->setConstructorArgs([['a' => true, 'a/b' => true, 'c' => true], ['a/b' => true]])
->getMock();
$view->method('rename')
->willThrowException(new InvalidPathException('Invalid path "b/foo:bar"'));

$sourceNode = new Directory($view, new FileInfo('a/b', null, null, ['type' => FileInfo::TYPE_FOLDER], null));
$targetNode = $this->getMockBuilder(Directory::class)
->onlyMethods(['childExists'])
->setConstructorArgs([$view, new FileInfo('c', null, null, ['type' => FileInfo::TYPE_FOLDER], null)])
->getMock();
$targetNode->method('childExists')
->willReturn(false);

$targetNode->moveInto('b', 'a/b', $sourceNode);
}

public function testCopyIntoInvalidPathFromStorage(): void {
$this->expectException(InvalidPath::class);
$this->expectExceptionMessage('Invalid path "b/foo:bar"');

$view = $this->getMockBuilder(TestViewDirectory::class)
->onlyMethods(['copy'])
->setConstructorArgs([['c' => true], []])
->getMock();
$view->method('copy')
->willThrowException(new InvalidPathException('Invalid path "b/foo:bar"'));

$sourceNode = new Directory($view, new FileInfo('a/b', null, null, ['type' => FileInfo::TYPE_FOLDER], null));
$targetNode = new Directory($view, new FileInfo('c', null, null, ['type' => FileInfo::TYPE_FOLDER], null));

$targetNode->copyInto('b', 'a/b', $sourceNode);
}
}
29 changes: 29 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\ObjectTree;
use OCP\Files\InvalidPathException;
use OCP\Files\Mount\IMountManager;

/**
Expand Down Expand Up @@ -86,6 +87,34 @@ public function testCopy(string $sourcePath, string $targetPath, string $targetP
$objectTree->copy($sourcePath, $targetPath);
}

public function testCopyInvalidPathFromStorage(): void {
$this->expectException(InvalidPath::class);
$this->expectExceptionMessage('Invalid path "b/foo:bar"');

$view = $this->createMock(View::class);
$view->method('file_exists')
->willReturn(false);
$view->method('copy')
->willThrowException(new InvalidPathException('Invalid path "b/foo:bar"'));

$info = $this->createMock(FileInfo::class);
$info->method('isCreatable')
->willReturn(true);
$view->method('getFileInfo')
->willReturn($info);
$view->method('getRelativePath')
->willReturn('');

$rootDir = new Directory($view, $info);
$objectTree = $this->getMockBuilder(ObjectTree::class)
->onlyMethods(['nodeExists', 'getNodeForPath'])
->setConstructorArgs([$rootDir, $view])
->getMock();

$objectTree->init($rootDir, $view, Filesystem::getMountManager());
$objectTree->copy('a', 'b');
}

#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'copyDataProvider')]
public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent): void {
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
Expand Down
5 changes: 4 additions & 1 deletion apps/files_external/lib/Lib/Storage/SMB.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\ForbiddenException;
use Icewind\SMB\Exception\InvalidArgumentException;
use Icewind\SMB\Exception\InvalidPathException;
use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotFoundException;
use Icewind\SMB\Exception\OutOfSpaceException;
Expand Down Expand Up @@ -405,7 +406,7 @@ public function unlink(string $path): bool {
$this->share->del($path);
return true;
}
} catch (NotFoundException $e) {
} catch (NotFoundException|InvalidPathException $e) {
return false;
} catch (ForbiddenException $e) {
return false;
Expand Down Expand Up @@ -498,6 +499,8 @@ public function fopen(string $path, string $mode) {
} catch (OutOfSpaceException $e) {
$this->logger->warning('Failed to open ' . $path . ' on ' . $this->getId() . ', out of space.', ['exception' => $e]);
throw new EntityTooLargeException('not enough available space to create file', 0, $e);
} catch (InvalidPathException $e) {
throw new \OCP\Files\InvalidPathException($e->getMessage(), 0, $e);
} catch (ConnectException $e) {
$this->logger->error('Error while opening file ' . $path . ' on ' . $this->getId(), ['exception' => $e]);
throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
Expand Down
24 changes: 24 additions & 0 deletions apps/files_external/tests/Storage/SmbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

use OC\Files\Notify\Change;
use OC\Files\Notify\RenameChange;
use OC\Files\Storage\Temporary;
use OCA\Files_External\Lib\Storage\SMB;
use OCP\Files\InvalidPathException;
use OCP\Files\Notify\IChange;
use PHPUnit\Framework\ExpectationFailedException;

Expand Down Expand Up @@ -172,4 +174,26 @@ public function testRmdirRoot(): void {
// root can't be deleted
$this->assertFalse($this->instance->rmdir(''));
}

public function testUnlinkInvalidPath(): void {
$this->assertFalse($this->instance->unlink('foo:bar.txt'));
}

public function testFopenInvalidPath(): void {
$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('foo:bar.txt');

$this->instance->fopen('foo:bar.txt', 'w');
}

public function testCopyFromStorageInvalidName(): void {
$source = new Temporary([]);
$source->mkdir('folder');
$source->file_put_contents('folder/foo:bar.txt', 'foo');

$this->expectException(InvalidPathException::class);
$this->expectExceptionMessage('foo:bar.txt');

$this->instance->copyFromStorage($source, 'folder', 'folder');
}
}
12 changes: 11 additions & 1 deletion lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
} else {
$source = $sourceStorage->fopen($sourceInternalPath, 'r');
$result = false;
$writeException = null;
if ($source) {
try {
$this->writeStream($targetInternalPath, $source);
Expand All @@ -552,6 +553,7 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP
throw new ForbiddenException($e->getMessage(), false, $e);
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->warning('Failed to copy stream to storage', ['exception' => $e]);
$writeException = $e;
}
}

Expand All @@ -562,9 +564,17 @@ public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalP

if (!$result) {
// delete partially written target file
$this->unlink($targetInternalPath);
try {
$this->unlink($targetInternalPath);
} catch (\Exception $e) {
Server::get(LoggerInterface::class)->warning('Failed to delete partially written target file', ['exception' => $e]);
}
// delete cache entry that was created by fopen
$this->getCache()->remove($targetInternalPath);

if ($writeException instanceof InvalidPathException) {
throw $writeException;
}
}
}
return (bool)$result;
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/Files/Storage/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,45 @@ public function testMoveFromStorageNestedJail(): void {
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
$this->assertTrue($instance->file_exists('bar.txt'));
}

public function testCopyFromStorageCleanupFailure(): void {
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
->onlyMethods(['writeStream', 'unlink'])
->setConstructorArgs([['datadir' => $this->tmpDir]])
->getMock();
$instance->method('writeStream')
->willThrowException(new \Exception('write'));
$instance->method('unlink')
->willThrowException(new \Exception('unlink'));

$source = new \OC\Files\Storage\Temporary([]);
$source->file_put_contents('foo.txt', 'bar');

$this->assertFalse($instance->copyFromStorage($source, 'foo.txt', 'foo.txt'));
}

public function testCopyFromStorageInvalidPath(): void {
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
->onlyMethods(['writeStream'])
->setConstructorArgs([['datadir' => $this->tmpDir]])
->getMock();
$instance->method('writeStream')
->willReturnCallback(function (string $path) use ($instance): int {
$instance->file_put_contents($path, 'partial');
throw new InvalidPathException('invalid');
});

$source = new \OC\Files\Storage\Temporary([]);
$source->file_put_contents('foo.txt', 'bar');

try {
$instance->copyFromStorage($source, 'foo.txt', 'foo.txt');
$this->fail('Expected InvalidPathException');
} catch (InvalidPathException $e) {
$this->assertSame('invalid', $e->getMessage());
}
$this->assertFalse($instance->file_exists('foo.txt'));
}
}