mirror of
https://github.com/nextcloud/server.git
synced 2026-10-03 16:58:44 +02:00
123 lines
3.9 KiB
PHP
123 lines
3.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-FileCopyrightText: 2019 ownCloud GmbH
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
namespace OCA\DAV\DAV;
|
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
|
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 OCP\Files\Storage\ISharedStorage;
|
|
use Sabre\DAV\Exception\NotFound;
|
|
use Sabre\DAV\Server;
|
|
use Sabre\DAV\ServerPlugin;
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
/**
|
|
* Sabre plugin for restricting file share receiver download:
|
|
*/
|
|
class ViewOnlyPlugin extends ServerPlugin {
|
|
private ?Server $server = null;
|
|
|
|
public function __construct(
|
|
private ?Folder $userFolder,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* This initializes the plugin.
|
|
*
|
|
* This function is called by Sabre\DAV\Server, after
|
|
* addPlugin is called.
|
|
*
|
|
* This method should set up the required event subscriptions.
|
|
*/
|
|
#[\Override]
|
|
public function initialize(Server $server): void {
|
|
$this->server = $server;
|
|
//priority 90 to make sure the plugin is called before
|
|
//Sabre\DAV\CorePlugin::httpGet
|
|
$this->server->on('method:GET', [$this, 'checkViewOnly'], 90);
|
|
$this->server->on('method:COPY', [$this, 'checkViewOnly'], 90);
|
|
$this->server->on('method:MOVE', [$this, 'checkViewOnly'], 90);
|
|
}
|
|
|
|
/**
|
|
* Disallow download via DAV Api in case file being received share
|
|
* and having special permission
|
|
*
|
|
* @throws Forbidden
|
|
* @throws NotFoundException
|
|
*/
|
|
public function checkViewOnly(RequestInterface $request): bool {
|
|
$path = $request->getPath();
|
|
|
|
try {
|
|
assert($this->server !== null);
|
|
$davNode = $this->server->tree->getNodeForPath($path);
|
|
if ($davNode instanceof DavFile) {
|
|
// Restrict view-only to nodes which are shared
|
|
$node = $davNode->getNode();
|
|
} elseif ($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) {
|
|
$node = $this->userFolder->getFirstNodeById($node->getId());
|
|
if ($node === null) {
|
|
throw new NotFoundException('Version file not accessible by current user');
|
|
}
|
|
}
|
|
} else {
|
|
return true;
|
|
}
|
|
|
|
$storage = $node->getStorage();
|
|
if (!$storage->instanceOfStorage(ISharedStorage::class)) {
|
|
return true;
|
|
}
|
|
|
|
/** @var ISharedStorage $storage */
|
|
$share = $storage->getShare();
|
|
switch ($request->getMethod()) {
|
|
case 'GET':
|
|
// If download is disabled, but viewing is allowed, we still allow the GET method to return the file content.
|
|
if (!$share->canSeeContent()) {
|
|
throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
|
|
}
|
|
break;
|
|
case 'COPY':
|
|
case 'MOVE':
|
|
$destinationPath = $this->server->getCopyAndMoveInfo($request)['destination'];
|
|
$destinationParentPath = dirname($destinationPath);
|
|
if ($destinationParentPath === '.') {
|
|
$destinationParentPath = '';
|
|
}
|
|
$destinationParent = $this->server->tree->getNodeForPath($destinationParentPath);
|
|
// Copy and move operations within the same storage are allowed, because the destination has the same restrictions.
|
|
if (($destinationParent instanceof Directory) && $destinationParent->getNode()->getStorage()->getId() === $storage->getId()) {
|
|
break;
|
|
}
|
|
|
|
// If download is disabled, we disable the COPY and MOVE methods even if the shareapi_allow_view_without_download is set to true.
|
|
if (!$share->canDownload()) {
|
|
throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
|
|
}
|
|
break;
|
|
}
|
|
} catch (NotFound $e) {
|
|
// File not found
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|