-
Notifications
You must be signed in to change notification settings - Fork 138
fix(SecureView): hide disfunctional *download* files action #5171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+405
−38
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Richdocuments\DAV; | ||
|
|
||
| use OCA\DAV\Connector\Sabre\FilesPlugin; | ||
| use OCA\DAV\Connector\Sabre\Node; | ||
| use OCA\Richdocuments\Middleware\WOPIMiddleware; | ||
| use OCA\Richdocuments\Service\SecureViewService; | ||
| use OCA\Richdocuments\Storage\SecureViewWrapper; | ||
| use OCP\Files\ForbiddenException; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\StorageNotAvailableException; | ||
| use OCP\IAppConfig; | ||
| use Psr\Log\LoggerInterface; | ||
| use Sabre\DAV\INode; | ||
| use Sabre\DAV\PropFind; | ||
| use Sabre\DAV\Server; | ||
| use Sabre\DAV\ServerPlugin; | ||
|
|
||
| class SecureViewPlugin extends ServerPlugin { | ||
| public function __construct( | ||
| protected WOPIMiddleware $wopiMiddleware, | ||
| protected IAppConfig $appConfig, | ||
| protected SecureViewService $secureViewService, | ||
| protected LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| public function initialize(Server $server) { | ||
| if (!$this->secureViewService->isEnabled()) { | ||
| return; | ||
| } | ||
| $server->on('propFind', $this->handleGetProperties(...)); | ||
| } | ||
|
|
||
| private function handleGetProperties(PropFind $propFind, INode $node): void { | ||
| if (!$node instanceof Node) { | ||
| return; | ||
| } | ||
|
|
||
| $requestedProperties = $propFind->getRequestedProperties(); | ||
| if (!in_array(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, $requestedProperties, true)) { | ||
| return; | ||
| } | ||
| $currentValue = $propFind->get(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME); | ||
| if ($currentValue === 'true') { | ||
| // We won't unhide, hence can return early | ||
| return; | ||
| } | ||
|
|
||
| if (!$this->isDownloadable($node->getNode())) { | ||
| // 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? | ||
| $propFind->set(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, 'true'); | ||
| // avoid potential race condition with FilesPlugin that may set it to "false" | ||
| $propFind->handle(FilesPlugin::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, 'true'); | ||
| } | ||
| } | ||
|
|
||
| private function isDownloadable(\OCP\Files\Node $node): bool { | ||
| $storage = $node->getStorage(); | ||
| if ($this->wopiMiddleware->isWOPIRequest() | ||
| || $storage === null | ||
| || !$storage->instanceOfStorage(SecureViewWrapper::class) | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| try { | ||
| return !$this->secureViewService->shouldSecure($node->getInternalPath(), $storage); | ||
| } catch (StorageNotAvailableException|ForbiddenException|NotFoundException $e) { | ||
| // Exceptions cannot be nicely inferred. | ||
| return false; | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning('SecureViewPlugin caught an exception that likely is ignorable. Still preventing download.', | ||
| ['exception' => $e,] | ||
| ); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Richdocuments\Listener; | ||
|
|
||
| use OCA\DAV\Events\SabrePluginAddEvent; | ||
| use OCA\Richdocuments\DAV\SecureViewPlugin; | ||
| use OCP\BeforeSabrePubliclyLoadedEvent; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use Psr\Container\ContainerInterface; | ||
|
|
||
| /** @template-implements IEventListener<SabrePluginAddEvent|BeforeSabrePubliclyLoadedEvent> */ | ||
| class AddSabrePluginListener implements IEventListener { | ||
|
|
||
| public function __construct( | ||
| protected ContainerInterface $server, | ||
| ) { | ||
| } | ||
|
|
||
| public function handle(Event $event): void { | ||
| if ( | ||
| !$event instanceof SabrePluginAddEvent | ||
| && !$event instanceof BeforeSabrePubliclyLoadedEvent | ||
| ) { | ||
| return; | ||
| } | ||
| $davServer = $event->getServer(); | ||
| $davServer->addPlugin($this->server->get(SecureViewPlugin::class)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\Richdocuments\Service; | ||
|
|
||
| use OCA\Richdocuments\AppConfig; | ||
| use OCA\Richdocuments\PermissionManager; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Files\Storage\ISharedStorage; | ||
| use OCP\Files\Storage\IStorage; | ||
| use OCP\IAppConfig; | ||
| use OCP\IUserSession; | ||
|
|
||
| class SecureViewService { | ||
| public function __construct( | ||
| protected IUserSession $userSession, | ||
| protected PermissionManager $permissionManager, | ||
| protected IAppConfig $appConfig, | ||
| ) { | ||
| } | ||
|
|
||
| public function isEnabled(): bool { | ||
| return $this->appConfig->getValueString(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_enabled', 'no') !== 'no'; | ||
| } | ||
|
|
||
| /** | ||
| * @throws NotFoundException | ||
| */ | ||
| public function shouldSecure(string $path, IStorage $storage, bool $tryOpen = true): bool { | ||
| if ($tryOpen) { | ||
| // pity… fopen() does not document any possible Exceptions | ||
| $fp = $storage->fopen($path, 'r'); | ||
| fclose($fp); | ||
| } | ||
|
|
||
| $cacheEntry = $storage->getCache()->get($path); | ||
| if (!$cacheEntry) { | ||
| $parent = dirname($path); | ||
| if ($parent === '.') { | ||
| $parent = ''; | ||
| } | ||
| $cacheEntry = $storage->getCache()->get($parent); | ||
| if (!$cacheEntry) { | ||
| throw new NotFoundException(sprintf('Could not find cache entry for path and parent of %s within storage %s ', $path, $storage->getId())); | ||
| } | ||
| } | ||
|
|
||
| $isSharedStorage = $storage->instanceOfStorage(ISharedStorage::class); | ||
| /** @noinspection PhpPossiblePolymorphicInvocationInspection */ | ||
| /** @psalm-suppress UndefinedMethod **/ | ||
| $share = $isSharedStorage ? $storage->getShare() : null; | ||
| $userId = $this->userSession->getUser()?->getUID(); | ||
|
|
||
| return $this->permissionManager->shouldWatermark($cacheEntry, $userId, $share, $storage->getOwner($path) ?: null); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@susnux this works, but is uncomfortably hacky. Maybe sufficient for 32 and 31, but what do you think of making the
davapp emit an event here and stay the whole owner of that attribute? Listeners can then mark a node download-hidden only.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not that sure because for the DAV part probably @salmart-dev or @CarlSchwan would be good to also include in the process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blizzz wasn't the
setalone enough? The plugin in core is using handle, which should not override an already set value 🤔The event solution sounds like a good idea if we need to make sure that the property is not purposefully or inadvertently set back to false and can live with more complex flow/code.
If it's a common case, another idea would be to add some code in Sabre/dav to "declare" a property as "irreversible" once a certain value has been set. So that any subsequent
setorhandleare not executed any longer. The requirement would be that whichever plugin owns the property has to be the first one setting it.Another solution to make the above more generic would be to allow declaring a property with a callable parameter which checks if the value should be updatable/settable or not. This way one could implement more variants. I'm not sure which other use-cases we have though, so I'm not sure if this even makes sense, but would avoid the extra event, listener, handling complexity.