Merge pull request #20472 from nextcloud/fix/19848/make-sure-destination-is-not-a-directory

Verify that destination is not a directory.
This commit is contained in:
Roeland Jago Douma 2020-04-14 15:20:46 +02:00 committed by GitHub
commit d7a74d0e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 4 deletions

View File

@ -23,7 +23,10 @@
namespace OCA\DAV\Upload; namespace OCA\DAV\Upload;
use OCA\DAV\Connector\Sabre\Directory;
use Sabre\DAV\Exception\BadRequest; use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\INode;
use Sabre\DAV\Server; use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin; use Sabre\DAV\ServerPlugin;
@ -45,6 +48,9 @@ class ChunkingPlugin extends ServerPlugin {
/** /**
* @param string $sourcePath source path * @param string $sourcePath source path
* @param string $destination destination path * @param string $destination destination path
* @return bool|void
* @throws BadRequest
* @throws NotFound
*/ */
public function beforeMove($sourcePath, $destination) { public function beforeMove($sourcePath, $destination) {
$this->sourceNode = $this->server->tree->getNodeForPath($sourcePath); $this->sourceNode = $this->server->tree->getNodeForPath($sourcePath);
@ -53,6 +59,16 @@ class ChunkingPlugin extends ServerPlugin {
return; 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(); $this->verifySize();
return $this->performMove($sourcePath, $destination); return $this->performMove($sourcePath, $destination);
} }

View File

@ -27,6 +27,7 @@ namespace OCA\DAV\Tests\unit\Upload;
use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Upload\ChunkingPlugin; use OCA\DAV\Upload\ChunkingPlugin;
use OCA\DAV\Upload\FutureFile; use OCA\DAV\Upload\FutureFile;
use Sabre\DAV\Exception\NotFound;
use Sabre\HTTP\RequestInterface; use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface; use Sabre\HTTP\ResponseInterface;
use Test\TestCase; use Test\TestCase;
@ -87,16 +88,41 @@ class ChunkingPluginTest extends TestCase {
$this->assertNull($this->plugin->beforeMove('source', 'target')); $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->response->expects($this->never())
->method('setStatus');
$this->assertNull($this->plugin->beforeMove('source', 'target'));
}
public function testBeforeMoveFutureFileSkipNonExisting() { public function testBeforeMoveFutureFileSkipNonExisting() {
$sourceNode = $this->createMock(FutureFile::class); $sourceNode = $this->createMock(FutureFile::class);
$sourceNode->expects($this->once()) $sourceNode->expects($this->once())
->method('getSize') ->method('getSize')
->willReturn(4); ->willReturn(4);
$this->tree->expects($this->any()) $this->tree->expects($this->at(0))
->method('getNodeForPath') ->method('getNodeForPath')
->with('source') ->with('source')
->willReturn($sourceNode); ->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willThrowException(new NotFound());
$this->tree->expects($this->any()) $this->tree->expects($this->any())
->method('nodeExists') ->method('nodeExists')
->with('target') ->with('target')
@ -117,10 +143,14 @@ class ChunkingPluginTest extends TestCase {
->method('getSize') ->method('getSize')
->willReturn(4); ->willReturn(4);
$this->tree->expects($this->any()) $this->tree->expects($this->at(0))
->method('getNodeForPath') ->method('getNodeForPath')
->with('source') ->with('source')
->willReturn($sourceNode); ->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willThrowException(new NotFound());
$this->tree->expects($this->any()) $this->tree->expects($this->any())
->method('nodeExists') ->method('nodeExists')
->with('target') ->with('target')
@ -143,7 +173,7 @@ class ChunkingPluginTest extends TestCase {
$this->assertFalse($this->plugin->beforeMove('source', 'target')); $this->assertFalse($this->plugin->beforeMove('source', 'target'));
} }
public function testBeforeMoveSizeIsWrong() { public function testBeforeMoveSizeIsWrong() {
$this->expectException(\Sabre\DAV\Exception\BadRequest::class); $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
$this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes'); $this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
@ -153,10 +183,14 @@ class ChunkingPluginTest extends TestCase {
->method('getSize') ->method('getSize')
->willReturn(3); ->willReturn(3);
$this->tree->expects($this->any()) $this->tree->expects($this->at(0))
->method('getNodeForPath') ->method('getNodeForPath')
->with('source') ->with('source')
->willReturn($sourceNode); ->willReturn($sourceNode);
$this->tree->expects($this->at(1))
->method('getNodeForPath')
->with('target')
->willThrowException(new NotFound());
$this->request->expects($this->once()) $this->request->expects($this->once())
->method('getHeader') ->method('getHeader')
->with('OC-Total-Length') ->with('OC-Total-Length')