ViewOnlyPlugin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2019 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\DAV;
  8. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  9. use OCA\DAV\Connector\Sabre\File as DavFile;
  10. use OCA\Files_Versions\Sabre\VersionFile;
  11. use OCP\Files\Folder;
  12. use OCP\Files\NotFoundException;
  13. use OCP\Files\Storage\ISharedStorage;
  14. use Sabre\DAV\Exception\NotFound;
  15. use Sabre\DAV\Server;
  16. use Sabre\DAV\ServerPlugin;
  17. use Sabre\HTTP\RequestInterface;
  18. /**
  19. * Sabre plugin for restricting file share receiver download:
  20. */
  21. class ViewOnlyPlugin extends ServerPlugin {
  22. private ?Server $server = null;
  23. public function __construct(
  24. private ?Folder $userFolder,
  25. ) {
  26. }
  27. /**
  28. * This initializes the plugin.
  29. *
  30. * This function is called by Sabre\DAV\Server, after
  31. * addPlugin is called.
  32. *
  33. * This method should set up the required event subscriptions.
  34. */
  35. public function initialize(Server $server): void {
  36. $this->server = $server;
  37. //priority 90 to make sure the plugin is called before
  38. //Sabre\DAV\CorePlugin::httpGet
  39. $this->server->on('method:GET', [$this, 'checkViewOnly'], 90);
  40. $this->server->on('method:COPY', [$this, 'checkViewOnly'], 90);
  41. $this->server->on('method:MOVE', [$this, 'checkViewOnly'], 90);
  42. }
  43. /**
  44. * Disallow download via DAV Api in case file being received share
  45. * and having special permission
  46. *
  47. * @throws Forbidden
  48. * @throws NotFoundException
  49. */
  50. public function checkViewOnly(RequestInterface $request): bool {
  51. $path = $request->getPath();
  52. try {
  53. assert($this->server !== null);
  54. $davNode = $this->server->tree->getNodeForPath($path);
  55. if ($davNode instanceof DavFile) {
  56. // Restrict view-only to nodes which are shared
  57. $node = $davNode->getNode();
  58. } elseif ($davNode instanceof VersionFile) {
  59. $node = $davNode->getVersion()->getSourceFile();
  60. $currentUserId = $this->userFolder?->getOwner()?->getUID();
  61. // The version source file is relative to the owner storage.
  62. // But we need the node from the current user perspective.
  63. if ($node->getOwner()->getUID() !== $currentUserId) {
  64. $nodes = $this->userFolder->getById($node->getId());
  65. $node = array_pop($nodes);
  66. if (!$node) {
  67. throw new NotFoundException('Version file not accessible by current user');
  68. }
  69. }
  70. } else {
  71. return true;
  72. }
  73. $storage = $node->getStorage();
  74. if (!$storage->instanceOfStorage(ISharedStorage::class)) {
  75. return true;
  76. }
  77. // Extract extra permissions
  78. /** @var ISharedStorage $storage */
  79. $share = $storage->getShare();
  80. $attributes = $share->getAttributes();
  81. if ($attributes === null) {
  82. return true;
  83. }
  84. // Check if read-only and on whether permission can download is both set and disabled.
  85. $canDownload = $attributes->getAttribute('permissions', 'download');
  86. if ($canDownload !== null && !$canDownload) {
  87. throw new Forbidden('Access to this shared resource has been denied because its download permission is disabled.');
  88. }
  89. } catch (NotFound $e) {
  90. // File not found
  91. }
  92. return true;
  93. }
  94. }