Skip to content

Commit 91ab811

Browse files
committed
Verify that destination is not a directory.
Otherwise file_put_contents will fail later. Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 09bb8ac commit 91ab811

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

apps/dav/lib/Upload/ChunkingPlugin.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323

2424
namespace OCA\DAV\Upload;
2525

26+
use OCA\DAV\Connector\Sabre\Directory;
2627
use Sabre\DAV\Exception\BadRequest;
28+
use Sabre\DAV\Exception\NotFound;
29+
use Sabre\DAV\INode;
2730
use Sabre\DAV\Server;
2831
use Sabre\DAV\ServerPlugin;
2932

@@ -45,6 +48,9 @@ public function initialize(Server $server) {
4548
/**
4649
* @param string $sourcePath source path
4750
* @param string $destination destination path
51+
* @return bool|void
52+
* @throws BadRequest
53+
* @throws NotFound
4854
*/
4955
public function beforeMove($sourcePath, $destination) {
5056
$this->sourceNode = $this->server->tree->getNodeForPath($sourcePath);
@@ -53,6 +59,16 @@ public function beforeMove($sourcePath, $destination) {
5359
return;
5460
}
5561

62+
try {
63+
/** @var INode $destinationNode */
64+
$destinationNode = $this->server->tree->getNodeForPath($destination);
65+
if ($destinationNode instanceof Directory) {
66+
throw new BadRequest("The given destination $destination is a directory.");
67+
}
68+
} catch (NotFound $e) {
69+
// If the destination does not exist yet it's not a directory either ;)
70+
}
71+
5672
$this->verifySize();
5773
return $this->performMove($sourcePath, $destination);
5874
}

apps/dav/tests/unit/Upload/ChunkingPluginTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\DAV\Connector\Sabre\Directory;
2828
use OCA\DAV\Upload\ChunkingPlugin;
2929
use OCA\DAV\Upload\FutureFile;
30+
use Sabre\DAV\Exception\NotFound;
3031
use Sabre\HTTP\RequestInterface;
3132
use Sabre\HTTP\ResponseInterface;
3233
use Test\TestCase;
@@ -87,16 +88,41 @@ public function testBeforeMoveFutureFileSkip() {
8788
$this->assertNull($this->plugin->beforeMove('source', 'target'));
8889
}
8990

91+
public function testBeforeMoveDestinationIsDirectory() {
92+
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
93+
$this->expectExceptionMessage('The given destination target is a directory.');
94+
95+
$sourceNode = $this->createMock(FutureFile::class);
96+
$targetNode = $this->createMock(Directory::class);
97+
98+
$this->tree->expects($this->at(0))
99+
->method('getNodeForPath')
100+
->with('source')
101+
->willReturn($sourceNode);
102+
$this->tree->expects($this->at(1))
103+
->method('getNodeForPath')
104+
->with('target')
105+
->willReturn($targetNode);
106+
$this->response->expects($this->never())
107+
->method('setStatus');
108+
109+
$this->assertNull($this->plugin->beforeMove('source', 'target'));
110+
}
111+
90112
public function testBeforeMoveFutureFileSkipNonExisting() {
91113
$sourceNode = $this->createMock(FutureFile::class);
92114
$sourceNode->expects($this->once())
93115
->method('getSize')
94116
->willReturn(4);
95117

96-
$this->tree->expects($this->any())
118+
$this->tree->expects($this->at(0))
97119
->method('getNodeForPath')
98120
->with('source')
99121
->willReturn($sourceNode);
122+
$this->tree->expects($this->at(1))
123+
->method('getNodeForPath')
124+
->with('target')
125+
->willThrowException(new NotFound());
100126
$this->tree->expects($this->any())
101127
->method('nodeExists')
102128
->with('target')
@@ -117,10 +143,14 @@ public function testBeforeMoveFutureFileMoveIt() {
117143
->method('getSize')
118144
->willReturn(4);
119145

120-
$this->tree->expects($this->any())
146+
$this->tree->expects($this->at(0))
121147
->method('getNodeForPath')
122148
->with('source')
123149
->willReturn($sourceNode);
150+
$this->tree->expects($this->at(1))
151+
->method('getNodeForPath')
152+
->with('target')
153+
->willThrowException(new NotFound());
124154
$this->tree->expects($this->any())
125155
->method('nodeExists')
126156
->with('target')
@@ -143,7 +173,7 @@ public function testBeforeMoveFutureFileMoveIt() {
143173
$this->assertFalse($this->plugin->beforeMove('source', 'target'));
144174
}
145175

146-
176+
147177
public function testBeforeMoveSizeIsWrong() {
148178
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
149179
$this->expectExceptionMessage('Chunks on server do not sum up to 4 but to 3 bytes');
@@ -153,10 +183,14 @@ public function testBeforeMoveSizeIsWrong() {
153183
->method('getSize')
154184
->willReturn(3);
155185

156-
$this->tree->expects($this->any())
186+
$this->tree->expects($this->at(0))
157187
->method('getNodeForPath')
158188
->with('source')
159189
->willReturn($sourceNode);
190+
$this->tree->expects($this->at(1))
191+
->method('getNodeForPath')
192+
->with('target')
193+
->willThrowException(new NotFound());
160194
$this->request->expects($this->once())
161195
->method('getHeader')
162196
->with('OC-Total-Length')

0 commit comments

Comments
 (0)