DavAclPlugin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\CardDAV\AddressBook;
  30. use Sabre\CalDAV\Principal\User;
  31. use Sabre\DAV\Exception\NotFound;
  32. use Sabre\DAV\INode;
  33. use Sabre\DAV\PropFind;
  34. use Sabre\HTTP\RequestInterface;
  35. use Sabre\HTTP\ResponseInterface;
  36. /**
  37. * Class DavAclPlugin is a wrapper around \Sabre\DAVACL\Plugin that returns 404
  38. * responses in case the resource to a response has been forbidden instead of
  39. * a 403. This is used to prevent enumeration of valid resources.
  40. *
  41. * @see https://github.com/owncloud/core/issues/22578
  42. * @package OCA\DAV\Connector\Sabre
  43. */
  44. class DavAclPlugin extends \Sabre\DAVACL\Plugin {
  45. public function __construct() {
  46. $this->hideNodesFromListings = true;
  47. $this->allowUnauthenticatedAccess = false;
  48. }
  49. public function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
  50. $access = parent::checkPrivileges($uri, $privileges, $recursion, false);
  51. if ($access === false && $throwExceptions) {
  52. /** @var INode $node */
  53. $node = $this->server->tree->getNodeForPath($uri);
  54. switch (get_class($node)) {
  55. case AddressBook::class:
  56. $type = 'Addressbook';
  57. break;
  58. default:
  59. $type = 'Node';
  60. break;
  61. }
  62. throw new NotFound(
  63. sprintf(
  64. "%s with name '%s' could not be found",
  65. $type,
  66. $node->getName()
  67. )
  68. );
  69. }
  70. return $access;
  71. }
  72. public function propFind(PropFind $propFind, INode $node) {
  73. // If the node is neither readable nor writable then fail unless its of
  74. // the standard user-principal
  75. if (!($node instanceof User)) {
  76. $path = $propFind->getPath();
  77. $readPermissions = $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, false);
  78. $writePermissions = $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, false);
  79. if ($readPermissions === false && $writePermissions === false) {
  80. $this->checkPrivileges($path, '{DAV:}read', self::R_PARENT, true);
  81. $this->checkPrivileges($path, '{DAV:}write', self::R_PARENT, true);
  82. }
  83. }
  84. return parent::propFind($propFind, $node);
  85. }
  86. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  87. $path = $request->getPath();
  88. // prevent the plugin from causing an unneeded overhead for file requests
  89. if (str_starts_with($path, 'files/')) {
  90. return;
  91. }
  92. parent::beforeMethod($request, $response);
  93. if (!str_starts_with($path, 'addressbooks/') && !str_starts_with($path, 'calendars/')) {
  94. return;
  95. }
  96. [$parentName] = \Sabre\Uri\split($path);
  97. if ($request->getMethod() === 'REPORT') {
  98. // is calendars/users/bob or addressbooks/users/bob readable?
  99. $this->checkPrivileges($parentName, '{DAV:}read');
  100. } elseif ($request->getMethod() === 'MKCALENDAR' || $request->getMethod() === 'MKCOL') {
  101. // is calendars/users/bob or addressbooks/users/bob writeable?
  102. $this->checkPrivileges($parentName, '{DAV:}write');
  103. }
  104. }
  105. }