diff options
author | Louis Chemineau <louis@chmn.me> | 2024-02-13 14:46:04 +0100 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2024-02-21 21:47:50 +0100 |
commit | 5606cadae7cae53c0a5397818a78854233e80bc0 (patch) | |
tree | a1ae950738838d0505a357f387d1ff596ee9d8b6 /apps | |
parent | 6734dea78089af8c206b4dcc5bed43e130a0c5b6 (diff) | |
download | nextcloud-server-5606cadae7cae53c0a5397818a78854233e80bc0.tar.gz nextcloud-server-5606cadae7cae53c0a5397818a78854233e80bc0.zip |
Check share attributes when downloading versions
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/dav/lib/Connector/Sabre/ServerFactory.php | 2 | ||||
-rw-r--r-- | apps/dav/lib/DAV/ViewOnlyPlugin.php | 20 | ||||
-rw-r--r-- | apps/dav/lib/Server.php | 2 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php | 28 | ||||
-rw-r--r-- | apps/dav/tests/unit/ServerTest.php | 1 |
5 files changed, 45 insertions, 8 deletions
diff --git a/apps/dav/lib/Connector/Sabre/ServerFactory.php b/apps/dav/lib/Connector/Sabre/ServerFactory.php index 4c57f3412e3..755d13f8371 100644 --- a/apps/dav/lib/Connector/Sabre/ServerFactory.php +++ b/apps/dav/lib/Connector/Sabre/ServerFactory.php @@ -161,7 +161,7 @@ class ServerFactory { // Allow view-only plugin for webdav requests $server->addPlugin(new ViewOnlyPlugin( - $this->logger + $userFolder, )); if ($this->userSession->isLoggedIn()) { diff --git a/apps/dav/lib/DAV/ViewOnlyPlugin.php b/apps/dav/lib/DAV/ViewOnlyPlugin.php index 51e3622142d..0ae472460be 100644 --- a/apps/dav/lib/DAV/ViewOnlyPlugin.php +++ b/apps/dav/lib/DAV/ViewOnlyPlugin.php @@ -24,8 +24,8 @@ namespace OCA\DAV\DAV; use OCA\DAV\Connector\Sabre\Exception\Forbidden; use OCA\DAV\Connector\Sabre\File as DavFile; use OCA\Files_Versions\Sabre\VersionFile; +use OCP\Files\Folder; use OCP\Files\NotFoundException; -use Psr\Log\LoggerInterface; use Sabre\DAV\Server; use Sabre\DAV\ServerPlugin; use Sabre\HTTP\RequestInterface; @@ -36,10 +36,12 @@ use Sabre\DAV\Exception\NotFound; */ class ViewOnlyPlugin extends ServerPlugin { private ?Server $server = null; - private LoggerInterface $logger; + private ?Folder $userFolder; - public function __construct(LoggerInterface $logger) { - $this->logger = $logger; + public function __construct( + ?Folder $userFolder, + ) { + $this->userFolder = $userFolder; } /** @@ -76,6 +78,16 @@ class ViewOnlyPlugin extends ServerPlugin { $node = $davNode->getNode(); } else if ($davNode instanceof VersionFile) { $node = $davNode->getVersion()->getSourceFile(); + $currentUserId = $this->userFolder?->getOwner()?->getUID(); + // The version source file is relative to the owner storage. + // But we need the node from the current user perspective. + if ($node->getOwner()->getUID() !== $currentUserId) { + $nodes = $this->userFolder->getById($node->getId()); + $node = array_pop($nodes); + if (!$node) { + throw new NotFoundException("Version file not accessible by current user"); + } + } } else { return true; } diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 37b5eb3b70b..809b804796d 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -236,7 +236,7 @@ class Server { // Allow view-only plugin for webdav requests $this->server->addPlugin(new ViewOnlyPlugin( - $logger + \OC::$server->getUserFolder(), )); if (BrowserErrorPagePlugin::isBrowserRequest($request)) { diff --git a/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php b/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php index e0f6f1302a5..a00a04d147f 100644 --- a/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php +++ b/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php @@ -26,10 +26,11 @@ use OCA\DAV\Connector\Sabre\File as DavFile; use OCA\Files_Versions\Versions\IVersion; use OCA\Files_Versions\Sabre\VersionFile; use OCP\Files\File; +use OCP\Files\Folder; use OCP\Files\Storage\IStorage; +use OCP\IUser; use OCP\Share\IAttributes; use OCP\Share\IShare; -use Psr\Log\LoggerInterface; use Sabre\DAV\Server; use Sabre\DAV\Tree; use Test\TestCase; @@ -43,10 +44,13 @@ class ViewOnlyPluginTest extends TestCase { private $tree; /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */ private $request; + /** @var Folder | \PHPUnit\Framework\MockObject\MockObject */ + private $userFolder; public function setUp(): void { + $this->userFolder = $this->createMock(Folder::class); $this->plugin = new ViewOnlyPlugin( - $this->createMock(LoggerInterface::class) + $this->userFolder, ); $this->request = $this->createMock(RequestInterface::class); $this->tree = $this->createMock(Tree::class); @@ -111,6 +115,26 @@ class ViewOnlyPluginTest extends TestCase { $davNode->expects($this->once()) ->method('getVersion') ->willReturn($version); + + $currentUser = $this->createMock(IUser::class); + $currentUser->expects($this->once()) + ->method('getUID') + ->willReturn('alice'); + $nodeInfo->expects($this->once()) + ->method('getOwner') + ->willReturn($currentUser); + + $nodeInfo = $this->createMock(File::class); + $owner = $this->createMock(IUser::class); + $owner->expects($this->once()) + ->method('getUID') + ->willReturn('bob'); + $this->userFolder->expects($this->once()) + ->method('getById') + ->willReturn([$nodeInfo]); + $this->userFolder->expects($this->once()) + ->method('getOwner') + ->willReturn($owner); } else { $davPath = 'files/path/to/file.odt'; $davNode = $this->createMock(DavFile::class); diff --git a/apps/dav/tests/unit/ServerTest.php b/apps/dav/tests/unit/ServerTest.php index 62e2accd697..26309d5fcd4 100644 --- a/apps/dav/tests/unit/ServerTest.php +++ b/apps/dav/tests/unit/ServerTest.php @@ -45,6 +45,7 @@ class ServerTest extends \Test\TestCase { /** @var IRequest | \PHPUnit\Framework\MockObject\MockObject $r */ $r = $this->createMock(IRequest::class); $r->expects($this->any())->method('getRequestUri')->willReturn($uri); + $this->loginAsUser('admin'); $s = new Server($r, '/'); $this->assertNotNull($s->server); foreach ($plugins as $plugin) { |