DavAclPlugin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Richard Steinmetz <richard@steinmetz.cloud>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Connector\Sabre;
  29. use OCA\DAV\CalDAV\Calendar;
  30. use OCA\DAV\CardDAV\AddressBook;
  31. use Sabre\CalDAV\Principal\User;
  32. use Sabre\DAV\Exception\NotFound;
  33. use Sabre\DAV\INode;
  34. use Sabre\DAV\PropFind;
  35. use Sabre\HTTP\RequestInterface;
  36. use Sabre\HTTP\ResponseInterface;
  37. /**
  38. * Class DavAclPlugin is a wrapper around \Sabre\DAVACL\Plugin that returns 404
  39. * responses in case the resource to a response has been forbidden instead of
  40. * a 403. This is used to prevent enumeration of valid resources.
  41. *
  42. * @see https://github.com/owncloud/core/issues/22578
  43. * @package OCA\DAV\Connector\Sabre
  44. */
  45. class DavAclPlugin extends \Sabre\DAVACL\Plugin {
  46. public function __construct() {
  47. $this->hideNodesFromListings = true;
  48. $this->allowUnauthenticatedAccess = false;
  49. }
  50. public function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
  51. $access = parent::checkPrivileges($uri, $privileges, $recursion, false);
  52. if ($access === false && $throwExceptions) {
  53. /** @var INode $node */
  54. $node = $this->server->tree->getNodeForPath($uri);
  55. switch (get_class($node)) {
  56. case AddressBook::class:
  57. $type = 'Addressbook';
  58. break;
  59. case Calendar::class:
  60. $type = 'Calendar';
  61. break;
  62. default:
  63. $type = 'Node';
  64. break;
  65. }
  66. throw new NotFound(
  67. sprintf(
  68. "%s with name '%s' could not be found",
  69. $type,
  70. $node->getName()
  71. )
  72. );
  73. }
  74. return $access;
  75. }
  76. public function propFind(PropFind $propFind, INode $node) {
  77. if ($node instanceof Node) {
  78. // files don't use dav acls
  79. return;
  80. }
  81. // If the node is neither readable nor writable then fail unless its of
  82. // the standard user-principal
  83. if (!($node instanceof User)) {
  84. $path = $propFind->getPath();
  85. $readPermissions = $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, false);
  86. $writePermissions = $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, false);
  87. if ($readPermissions === false && $writePermissions === false) {
  88. $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, true);
  89. $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, true);
  90. }
  91. }
  92. return parent::propFind($propFind, $node);
  93. }
  94. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  95. $path = $request->getPath();
  96. // prevent the plugin from causing an unneeded overhead for file requests
  97. if (str_starts_with($path, 'files/')) {
  98. return;
  99. }
  100. parent::beforeMethod($request, $response);
  101. if (!str_starts_with($path, 'addressbooks/') && !str_starts_with($path, 'calendars/')) {
  102. return;
  103. }
  104. [$parentName] = \Sabre\Uri\split($path);
  105. if ($request->getMethod() === 'REPORT') {
  106. // is calendars/users/bob or addressbooks/users/bob readable?
  107. $this->checkPrivileges($parentName, '{DAV:}read');
  108. } elseif ($request->getMethod() === 'MKCALENDAR' || $request->getMethod() === 'MKCOL') {
  109. // is calendars/users/bob or addressbooks/users/bob writeable?
  110. $this->checkPrivileges($parentName, '{DAV:}write');
  111. }
  112. }
  113. }