|
| 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 | + |
| 9 | +namespace OCA\Richdocuments\Storage; |
| 10 | + |
| 11 | +use OC\Files\Storage\Wrapper\Wrapper; |
| 12 | +use OCA\Richdocuments\Middleware\WOPIMiddleware; |
| 13 | +use OCA\Richdocuments\PermissionManager; |
| 14 | +use OCP\Files\Folder; |
| 15 | +use OCP\Files\ForbiddenException; |
| 16 | +use OCP\Files\IRootFolder; |
| 17 | +use OCP\Files\Storage\ISharedStorage; |
| 18 | +use OCP\Files\Storage\IStorage; |
| 19 | +use OCP\IUserSession; |
| 20 | +use OCP\Server; |
| 21 | + |
| 22 | +class SecureViewWrapper extends Wrapper { |
| 23 | + private PermissionManager $permissionManager; |
| 24 | + private WOPIMiddleware $wopiMiddleware; |
| 25 | + private IRootFolder $rootFolder; |
| 26 | + private IUserSession $userSession; |
| 27 | + |
| 28 | + private string $mountPoint; |
| 29 | + |
| 30 | + public function __construct(array $parameters) { |
| 31 | + parent::__construct($parameters); |
| 32 | + |
| 33 | + $this->permissionManager = Server::get(PermissionManager::class); |
| 34 | + $this->wopiMiddleware = Server::get(WOPIMiddleware::class); |
| 35 | + $this->rootFolder = Server::get(IRootFolder::class); |
| 36 | + $this->userSession = Server::get(IUserSession::class); |
| 37 | + |
| 38 | + $this->mountPoint = $parameters['mountPoint']; |
| 39 | + } |
| 40 | + |
| 41 | + public function fopen($path, $mode) { |
| 42 | + $this->checkFileAccess($path); |
| 43 | + |
| 44 | + return $this->storage->fopen($path, $mode); |
| 45 | + } |
| 46 | + |
| 47 | + public function file_get_contents(string $path): false|string { |
| 48 | + $this->checkFileAccess($path); |
| 49 | + |
| 50 | + return $this->storage->file_get_contents($path); |
| 51 | + } |
| 52 | + |
| 53 | + public function copy(string $source, string $target): bool { |
| 54 | + $this->checkSourceAndTarget($source, $target); |
| 55 | + |
| 56 | + return parent::copy($source, $target); |
| 57 | + } |
| 58 | + |
| 59 | + public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { |
| 60 | + $this->checkSourceAndTarget($sourceInternalPath, $targetInternalPath, $sourceStorage); |
| 61 | + |
| 62 | + return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
| 63 | + } |
| 64 | + |
| 65 | + public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool { |
| 66 | + $this->checkSourceAndTarget($sourceInternalPath, $targetInternalPath, $sourceStorage); |
| 67 | + |
| 68 | + return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
| 69 | + } |
| 70 | + |
| 71 | + public function rename(string $source, string $target): bool { |
| 72 | + $this->checkSourceAndTarget($source, $target); |
| 73 | + |
| 74 | + return parent::rename($source, $target); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @throws ForbiddenException |
| 79 | + */ |
| 80 | + private function checkFileAccess(string $path): void { |
| 81 | + if ($this->shouldSecure($path) && !$this->wopiMiddleware->isWOPIRequest()) { |
| 82 | + throw new ForbiddenException('Download blocked due the secure view policy', false); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 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 | + |
| 94 | + $isSharedStorage = $storage->instanceOfStorage(ISharedStorage::class); |
| 95 | + $mountNode = $this->rootFolder->get($storage->getMountPoint()); |
| 96 | + $node = $mountNode instanceof Folder ? $mountNode->get($path) : $mountNode; |
| 97 | + $share = $isSharedStorage ? $node->getStorage()->getShare() : null; |
| 98 | + $userId = $this->userSession->getUser()?->getUID(); |
| 99 | + |
| 100 | + return $this->permissionManager->shouldWatermark($node, $userId, $share); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + private function checkSourceAndTarget(string $source, string $target, ?IStorage $sourceStorage = null): void { |
| 105 | + if ($this->shouldSecure($source, $sourceStorage) && !$this->shouldSecure($target)) { |
| 106 | + throw new ForbiddenException('Download blocked due the secure view policy. The source requires secure view that the target cannot offer.', false); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments