Skip to content

Commit

Permalink
Verify that destination is not a directory.
Browse files Browse the repository at this point in the history
Otherwise file_put_contents will fail later.

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Apr 14, 2020
1 parent 09bb8ac commit 2d61d9e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
16 changes: 16 additions & 0 deletions apps/dav/lib/Upload/ChunkingPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

namespace OCA\DAV\Upload;

use OCA\DAV\Connector\Sabre\Directory;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\INode;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;

Expand All @@ -45,6 +48,9 @@ public function initialize(Server $server) {
/**
* @param string $sourcePath source path
* @param string $destination destination path
* @return bool|void
* @throws BadRequest
* @throws NotFound
*/
public function beforeMove($sourcePath, $destination) {
$this->sourceNode = $this->server->tree->getNodeForPath($sourcePath);
Expand All @@ -53,6 +59,16 @@ public function beforeMove($sourcePath, $destination) {
return;
}

try {
/** @var INode $destinationNode */
$destinationNode = $this->server->tree->getNodeForPath($destination);
if ($destinationNode instanceof Directory) {
throw new BadRequest("The given destination $destination is a directory.");
}
} catch (NotFound $e) {
// If the destination does not exist yet it's not a directory either ;)
}

$this->verifySize();
return $this->performMove($sourcePath, $destination);
}
Expand Down
42 changes: 37 additions & 5 deletions apps/dav/tests/unit/Upload/ChunkingPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Upload\ChunkingPlugin;
use OCA\DAV\Upload\FutureFile;
use Sabre\DAV\Exception\NotFound;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Test\TestCase;
Expand Down Expand Up @@ -77,7 +78,7 @@ protected function setUp(): void {
public function testBeforeMoveFutureFileSkip() {
$node = $this->createMock(Directory::class);

$this->tree->expects($this->any())
$this->tree->expects($this->at(0))
->method('getNodeForPath')
->with('source')
->willReturn($node);
Expand All @@ -87,16 +88,39 @@ public function testBeforeMoveFutureFileSkip() {
$this->assertNull($this->plugin->beforeMove('source', 'target'));
}

public function testBeforeMoveDestinationIsDirectory() {
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
$this->expectExceptionMessage('The given destination target is a directory.');

$sourceNode = $this->createMock(FutureFile::class);
$targetNode = $this->createMock(Directory::class);

$this->tree->expects($this->at(0))
->method('getNodeForPath')
->with('source')
->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willReturn($targetNode);

$this->assertNull($this->plugin->beforeMove('source', 'target'));
}

public function testBeforeMoveFutureFileSkipNonExisting() {
$sourceNode = $this->createMock(FutureFile::class);
$sourceNode->expects($this->once())
->method('getSize')
->willReturn(4);

$this->tree->expects($this->any())
$this->tree->expects($this->at(0))
->method('getNodeForPath')
->with('source')
->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willThrowException(new NotFound());
$this->tree->expects($this->any())
->method('nodeExists')
->with('target')
Expand All @@ -117,10 +141,14 @@ public function testBeforeMoveFutureFileMoveIt() {
->method('getSize')
->willReturn(4);

$this->tree->expects($this->any())
$this->tree->expects($this->at(0))
->method('getNodeForPath')
->with('source')
->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willThrowException(new NotFound());
$this->tree->expects($this->any())
->method('nodeExists')
->with('target')
Expand All @@ -143,7 +171,7 @@ public function testBeforeMoveFutureFileMoveIt() {
$this->assertFalse($this->plugin->beforeMove('source', 'target'));
}


public function testBeforeMoveSizeIsWrong() {
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
$this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
Expand All @@ -153,10 +181,14 @@ public function testBeforeMoveSizeIsWrong() {
->method('getSize')
->willReturn(3);

$this->tree->expects($this->any())
$this->tree->expects($this->at(0))
->method('getNodeForPath')
->with('source')
->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willThrowException(new NotFound());
$this->request->expects($this->once())
->method('getHeader')
->with('OC-Total-Length')
Expand Down

0 comments on commit 2d61d9e

Please sign in to comment.