1
0

CardDavRateLimitingPlugin.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CardDAV\Security;
  8. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  9. use OC\Security\RateLimiting\Limiter;
  10. use OCA\DAV\CardDAV\CardDavBackend;
  11. use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
  12. use OCP\IAppConfig;
  13. use OCP\IUserManager;
  14. use Psr\Log\LoggerInterface;
  15. use Sabre\DAV;
  16. use Sabre\DAV\Exception\Forbidden;
  17. use Sabre\DAV\ServerPlugin;
  18. use function count;
  19. use function explode;
  20. class CardDavRateLimitingPlugin extends ServerPlugin {
  21. private ?string $userId;
  22. public function __construct(
  23. private Limiter $limiter,
  24. private IUserManager $userManager,
  25. private CardDavBackend $cardDavBackend,
  26. private LoggerInterface $logger,
  27. private IAppConfig $config,
  28. ?string $userId,
  29. ) {
  30. $this->limiter = $limiter;
  31. $this->userManager = $userManager;
  32. $this->cardDavBackend = $cardDavBackend;
  33. $this->config = $config;
  34. $this->logger = $logger;
  35. $this->userId = $userId;
  36. }
  37. public function initialize(DAV\Server $server): void {
  38. $server->on('beforeBind', [$this, 'beforeBind'], 1);
  39. }
  40. public function beforeBind(string $path): void {
  41. if ($this->userId === null) {
  42. // We only care about authenticated users here
  43. return;
  44. }
  45. $user = $this->userManager->get($this->userId);
  46. if ($user === null) {
  47. // We only care about authenticated users here
  48. return;
  49. }
  50. $pathParts = explode('/', $path);
  51. if (count($pathParts) === 4 && $pathParts[0] === 'addressbooks') {
  52. // Path looks like addressbooks/users/username/addressbooksname so a new addressbook is created
  53. try {
  54. $this->limiter->registerUserRequest(
  55. 'carddav-create-address-book',
  56. $this->config->getValueInt('dav', 'rateLimitAddressBookCreation', 10),
  57. $this->config->getValueInt('dav', 'rateLimitPeriodAddressBookCreation', 3600),
  58. $user
  59. );
  60. } catch (RateLimitExceededException $e) {
  61. throw new TooManyRequests('Too many addressbooks created', 0, $e);
  62. }
  63. $addressBookLimit = $this->config->getValueInt('dav', 'maximumAdressbooks', 10);
  64. if ($addressBookLimit === -1) {
  65. return;
  66. }
  67. $numAddressbooks = $this->cardDavBackend->getAddressBooksForUserCount('principals/users/' . $user->getUID());
  68. if ($numAddressbooks >= $addressBookLimit) {
  69. $this->logger->warning('Maximum number of address books reached', [
  70. 'addressbooks' => $numAddressbooks,
  71. 'addressBookLimit' => $addressBookLimit,
  72. ]);
  73. throw new Forbidden('AddressBook limit reached', 0);
  74. }
  75. }
  76. }
  77. }