|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | +namespace OCA\Richdocuments\DAV; |
| 9 | + |
| 10 | +use OCA\DAV\Connector\Sabre\FilesPlugin; |
| 11 | +use OCA\DAV\Connector\Sabre\Node; |
| 12 | +use OCA\Richdocuments\AppConfig; |
| 13 | +use OCA\Richdocuments\Middleware\WOPIMiddleware; |
| 14 | +use OCA\Richdocuments\PermissionManager; |
| 15 | +use OCA\Richdocuments\Storage\SecureViewWrapper; |
| 16 | +use OCP\Files\ForbiddenException; |
| 17 | +use OCP\Files\NotFoundException; |
| 18 | +use OCP\Files\Storage\ISharedStorage; |
| 19 | +use OCP\Files\Storage\IStorage; |
| 20 | +use OCP\IAppConfig; |
| 21 | +use OCP\IUserSession; |
| 22 | +use Sabre\DAV\INode; |
| 23 | +use Sabre\DAV\PropFind; |
| 24 | +use Sabre\DAV\Server; |
| 25 | +use Sabre\DAV\ServerPlugin; |
| 26 | + |
| 27 | +class SecureViewPlugin extends ServerPlugin { |
| 28 | + public function __construct( |
| 29 | + protected WOPIMiddleware $wopiMiddleware, |
| 30 | + protected IUserSession $userSession, |
| 31 | + protected PermissionManager $permissionManager, |
| 32 | + protected IAppConfig $appConfig, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function initialize(Server $server) { |
| 37 | + if ($this->appConfig->getValueString(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_enabled', 'no') === 'no') { |
| 38 | + return; |
| 39 | + } |
| 40 | + $server->on('propFind', $this->handleGetProperties(...)); |
| 41 | + } |
| 42 | + |
| 43 | + private function handleGetProperties(PropFind $propFind, INode $node): void { |
| 44 | + if (!$node instanceof Node) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $requestedProperties = $propFind->getRequestedProperties(); |
| 49 | + if (!in_array(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, $requestedProperties, true)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + $currentValue = $propFind->get(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME); |
| 53 | + if ($currentValue === 'true') { |
| 54 | + // We won't unhide, hence can return early |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!$this->isDownloadable($node->getNode())) { |
| 59 | + // FIXME: coordinate with Files how a better solution looks like. Maybe by setting it only to 'true' by any provider? To avoid overwriting. Or throwing a dedicated event in just this case? |
| 60 | + $propFind->set(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, 'true'); |
| 61 | + // avoid potential race condition with FilesPlugin that may set it to "false" |
| 62 | + $propFind->handle(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, 'true'); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private function isDownloadable(\OCP\Files\Node $node): bool { |
| 67 | + try { |
| 68 | + $this->checkFileAccess($node->getInternalPath(), $node->getStorage()); |
| 69 | + return true; |
| 70 | + } catch (ForbiddenException) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private function checkFileAccess(string $path, IStorage $storage): void { |
| 76 | + if ($this->wopiMiddleware->isWOPIRequest() || !$storage->instanceOfStorage(SecureViewWrapper::class)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if ($this->shouldSecure($path, $storage)) { |
| 81 | + throw new ForbiddenException('Download blocked due the secure view policy', false); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // FIXME: remove duplication by moving into a SecureViewService |
| 86 | + private function shouldSecure(string $path, ?IStorage $sourceStorage = null): bool { |
| 87 | + if ($sourceStorage !== $this && $sourceStorage !== null) { |
| 88 | + $fp = $sourceStorage->fopen($path, 'r'); |
| 89 | + fclose($fp); |
| 90 | + } |
| 91 | + |
| 92 | + $storage = $sourceStorage ?? $this; |
| 93 | + $cacheEntry = $storage->getCache()->get($path); |
| 94 | + if (!$cacheEntry) { |
| 95 | + $parent = dirname($path); |
| 96 | + if ($parent === '.') { |
| 97 | + $parent = ''; |
| 98 | + } |
| 99 | + $cacheEntry = $storage->getCache()->get($parent); |
| 100 | + if (!$cacheEntry) { |
| 101 | + throw new NotFoundException(sprintf('Could not find cache entry for path and parent of %s within storage %s ', $path, $storage->getId())); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + $isSharedStorage = $storage->instanceOfStorage(ISharedStorage::class); |
| 106 | + |
| 107 | + $share = $isSharedStorage ? $storage->getShare() : null; |
| 108 | + $userId = $this->userSession->getUser()?->getUID(); |
| 109 | + |
| 110 | + return $this->permissionManager->shouldWatermark($cacheEntry, $userId, $share, $storage->getOwner($path) ?: null); |
| 111 | + } |
| 112 | +} |
0 commit comments