Skip to content

Commit 0ecc8ed

Browse files
authored
Merge pull request #17278 from nextcloud/backport/17264/stable16
[stable16] handle moveFromStorage within the same storage even when storage wrap…
2 parents c1018bb + cbc4134 commit 0ecc8ed

File tree

3 files changed

+134
-21
lines changed

3 files changed

+134
-21
lines changed

lib/private/Files/Storage/Common.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
use OC\Files\Cache\Updater;
4747
use OC\Files\Filesystem;
4848
use OC\Files\Cache\Watcher;
49+
use OC\Files\Storage\Wrapper\Jail;
50+
use OC\Files\Storage\Wrapper\Wrapper;
4951
use OCP\Files\EmptyFileNameException;
5052
use OCP\Files\FileNameTooLongException;
5153
use OCP\Files\InvalidCharacterInPathException;
@@ -635,14 +637,40 @@ public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t
635637
return (bool)$result;
636638
}
637639

640+
/**
641+
* Check if a storage is the same as the current one, including wrapped storages
642+
*
643+
* @param IStorage $storage
644+
* @return bool
645+
*/
646+
private function isSameStorage(IStorage $storage): bool {
647+
while ($storage->instanceOfStorage(Wrapper::class)) {
648+
/**
649+
* @var Wrapper $sourceStorage
650+
*/
651+
$storage = $storage->getWrapperStorage();
652+
}
653+
654+
return $storage === $this;
655+
}
656+
638657
/**
639658
* @param IStorage $sourceStorage
640659
* @param string $sourceInternalPath
641660
* @param string $targetInternalPath
642661
* @return bool
643662
*/
644663
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
645-
if ($sourceStorage === $this) {
664+
if ($this->isSameStorage($sourceStorage)) {
665+
// resolve any jailed paths
666+
while ($sourceStorage->instanceOfStorage(Jail::class)) {
667+
/**
668+
* @var Jail $sourceStorage
669+
*/
670+
$sourceInternalPath = $sourceStorage->getUnjailedPath($sourceInternalPath);
671+
$sourceStorage = $sourceStorage->getUnjailedStorage();
672+
}
673+
646674
return $this->rename($sourceInternalPath, $targetInternalPath);
647675
}
648676

lib/private/Files/Storage/Wrapper/Jail.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public function getUnjailedPath($path) {
6262
}
6363
}
6464

65+
/**
66+
* This is separate from Wrapper::getWrapperStorage so we can get the jailed storage consistently even if the jail is inside another wrapper
67+
*/
68+
public function getUnjailedStorage() {
69+
return $this->storage;
70+
}
71+
72+
6573
public function getJailedPath($path) {
6674
$root = rtrim($this->rootPath, '/') . '/';
6775

Lines changed: 97 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
<?php
22
/**
3-
* ownCloud
4-
*
5-
* @author Robin Appelman
6-
* @copyright 2012 Robin Appelman icewind@owncloud.com
7-
*
8-
* This library is free software; you can redistribute it and/or
9-
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10-
* License as published by the Free Software Foundation; either
11-
* version 3 of the License, or any later version.
12-
*
13-
* This library is distributed in the hope that it will be useful,
14-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16-
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17-
*
18-
* You should have received a copy of the GNU Affero General Public
19-
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
20-
*
21-
*/
3+
* ownCloud
4+
*
5+
* @author Robin Appelman
6+
* @copyright 2012 Robin Appelman icewind@owncloud.com
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10+
* License as published by the Free Software Foundation; either
11+
* version 3 of the License, or any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public
19+
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
2222

2323
namespace Test\Files\Storage;
2424

25+
use OC\Files\Storage\Wrapper\Jail;
26+
use OC\Files\Storage\Wrapper\Wrapper;
27+
use PHPUnit\Framework\MockObject\MockObject;
28+
2529
/**
2630
* Class CommonTest
2731
*
@@ -34,15 +38,88 @@ class CommonTest extends Storage {
3438
* @var string tmpDir
3539
*/
3640
private $tmpDir;
41+
3742
protected function setUp() {
3843
parent::setUp();
3944

4045
$this->tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
41-
$this->instance=new \OC\Files\Storage\CommonTest(array('datadir'=>$this->tmpDir));
46+
$this->instance = new \OC\Files\Storage\CommonTest(['datadir' => $this->tmpDir]);
4247
}
4348

4449
protected function tearDown() {
4550
\OC_Helper::rmdirr($this->tmpDir);
4651
parent::tearDown();
4752
}
53+
54+
public function testMoveFromStorageWrapped() {
55+
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
56+
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
57+
->setMethods(['copyFromStorage', 'rmdir', 'unlink'])
58+
->setConstructorArgs([['datadir' => $this->tmpDir]])
59+
->getMock();
60+
$instance->method('copyFromStorage')
61+
->willThrowException(new \Exception('copy'));
62+
63+
$source = new Wrapper([
64+
'storage' => $instance,
65+
]);
66+
67+
$instance->file_put_contents('foo.txt', 'bar');
68+
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
69+
$this->assertTrue($instance->file_exists('bar.txt'));
70+
}
71+
72+
public function testMoveFromStorageJailed() {
73+
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
74+
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
75+
->setMethods(['copyFromStorage', 'rmdir', 'unlink'])
76+
->setConstructorArgs([['datadir' => $this->tmpDir]])
77+
->getMock();
78+
$instance->method('copyFromStorage')
79+
->willThrowException(new \Exception('copy'));
80+
81+
$source = new Jail([
82+
'storage' => $instance,
83+
'root' => 'foo'
84+
]);
85+
$source = new Wrapper([
86+
'storage' => $source
87+
]);
88+
89+
$instance->mkdir('foo');
90+
$instance->file_put_contents('foo/foo.txt', 'bar');
91+
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
92+
$this->assertTrue($instance->file_exists('bar.txt'));
93+
}
94+
95+
public function testMoveFromStorageNestedJail() {
96+
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
97+
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
98+
->setMethods(['copyFromStorage', 'rmdir', 'unlink'])
99+
->setConstructorArgs([['datadir' => $this->tmpDir]])
100+
->getMock();
101+
$instance->method('copyFromStorage')
102+
->willThrowException(new \Exception('copy'));
103+
104+
$source = new Jail([
105+
'storage' => $instance,
106+
'root' => 'foo'
107+
]);
108+
$source = new Wrapper([
109+
'storage' => $source
110+
]);
111+
$source = new Jail([
112+
'storage' => $source,
113+
'root' => 'bar'
114+
]);
115+
$source = new Wrapper([
116+
'storage' => $source
117+
]);
118+
119+
$instance->mkdir('foo');
120+
$instance->mkdir('foo/bar');
121+
$instance->file_put_contents('foo/bar/foo.txt', 'bar');
122+
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
123+
$this->assertTrue($instance->file_exists('bar.txt'));
124+
}
48125
}

0 commit comments

Comments
 (0)