]> source.dussan.org Git - nextcloud-server.git/commitdiff
Verify that destination is not a directory. 20472/head
authorDaniel Kesselberg <mail@danielkesselberg.de>
Tue, 14 Apr 2020 09:48:43 +0000 (11:48 +0200)
committerDaniel Kesselberg <mail@danielkesselberg.de>
Tue, 14 Apr 2020 10:52:12 +0000 (12:52 +0200)
Otherwise file_put_contents will fail later.

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
apps/dav/lib/Upload/ChunkingPlugin.php
apps/dav/tests/unit/Upload/ChunkingPluginTest.php

index 35487f61429a2bcacec7518eae37de3c10c62214..5a8d762de807da32384c02088e17c2d98f455bea 100644 (file)
 
 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;
 
@@ -45,6 +48,9 @@ class ChunkingPlugin extends ServerPlugin {
        /**
         * @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);
@@ -53,6 +59,16 @@ class ChunkingPlugin extends ServerPlugin {
                        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);
        }
index abbded089dbc1baba22ed53b1e570074b86de51b..3a3fcec66d6ddbe6857faf87852a386db6de8589 100644 (file)
@@ -27,6 +27,7 @@ namespace OCA\DAV\Tests\unit\Upload;
 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;
@@ -87,16 +88,41 @@ class ChunkingPluginTest extends TestCase {
                $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() {
                $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')
@@ -117,10 +143,14 @@ class ChunkingPluginTest extends TestCase {
                        ->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')
@@ -143,7 +173,7 @@ class ChunkingPluginTest extends TestCase {
                $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');
@@ -153,10 +183,14 @@ class ChunkingPluginTest extends TestCase {
                        ->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')