DavAclPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use OCA\DAV\CardDAV\AddressBook;
  29. use Sabre\CalDAV\Principal\User;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAV\INode;
  32. use Sabre\DAV\PropFind;
  33. use Sabre\HTTP\RequestInterface;
  34. use Sabre\HTTP\ResponseInterface;
  35. /**
  36. * Class DavAclPlugin is a wrapper around \Sabre\DAVACL\Plugin that returns 404
  37. * responses in case the resource to a response has been forbidden instead of
  38. * a 403. This is used to prevent enumeration of valid resources.
  39. *
  40. * @see https://github.com/owncloud/core/issues/22578
  41. * @package OCA\DAV\Connector\Sabre
  42. */
  43. class DavAclPlugin extends \Sabre\DAVACL\Plugin {
  44. public function __construct() {
  45. $this->hideNodesFromListings = true;
  46. $this->allowUnauthenticatedAccess = false;
  47. }
  48. public function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
  49. $access = parent::checkPrivileges($uri, $privileges, $recursion, false);
  50. if ($access === false && $throwExceptions) {
  51. /** @var INode $node */
  52. $node = $this->server->tree->getNodeForPath($uri);
  53. switch (get_class($node)) {
  54. case AddressBook::class:
  55. $type = 'Addressbook';
  56. break;
  57. default:
  58. $type = 'Node';
  59. break;
  60. }
  61. throw new NotFound(
  62. sprintf(
  63. "%s with name '%s' could not be found",
  64. $type,
  65. $node->getName()
  66. )
  67. );
  68. }
  69. return $access;
  70. }
  71. public function propFind(PropFind $propFind, INode $node) {
  72. // If the node is neither readable nor writable then fail unless its of
  73. // the standard user-principal
  74. if (!($node instanceof User)) {
  75. $path = $propFind->getPath();
  76. $readPermissions = $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, false);
  77. $writePermissions = $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, false);
  78. if ($readPermissions === false && $writePermissions === false) {
  79. $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, true);
  80. $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, true);
  81. }
  82. }
  83. return parent::propFind($propFind, $node);
  84. }
  85. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  86. $path = $request->getPath();
  87. // prevent the plugin from causing an unneeded overhead for file requests
  88. if (str_starts_with($path, 'files/')) {
  89. return;
  90. }
  91. parent::beforeMethod($request, $response);
  92. $createAddressbookOrCalendarRequest = ($request->getMethod() === 'MKCALENDAR' || $request->getMethod() === 'MKCOL')
  93. && (str_starts_with($path, 'addressbooks/') || str_starts_with($path, 'calendars/'));
  94. if ($createAddressbookOrCalendarRequest) {
  95. [$parentName] = \Sabre\Uri\split($path);
  96. // is calendars/users/bob or addressbooks/users/bob writeable?
  97. $this->checkPrivileges($parentName, '{DAV:}write');
  98. }
  99. }
  100. }