RateLimitingPlugin.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Security;
  8. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  9. use OC\Security\RateLimiting\Limiter;
  10. use OCA\DAV\CalDAV\CalDavBackend;
  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 RateLimitingPlugin extends ServerPlugin {
  21. private Limiter $limiter;
  22. public function __construct(
  23. Limiter $limiter,
  24. private IUserManager $userManager,
  25. private CalDavBackend $calDavBackend,
  26. private LoggerInterface $logger,
  27. private IAppConfig $config,
  28. private ?string $userId,
  29. ) {
  30. $this->limiter = $limiter;
  31. }
  32. public function initialize(DAV\Server $server): void {
  33. $server->on('beforeBind', [$this, 'beforeBind'], 1);
  34. }
  35. public function beforeBind(string $path): void {
  36. if ($this->userId === null) {
  37. // We only care about authenticated users here
  38. return;
  39. }
  40. $user = $this->userManager->get($this->userId);
  41. if ($user === null) {
  42. // We only care about authenticated users here
  43. return;
  44. }
  45. $pathParts = explode('/', $path);
  46. if (count($pathParts) === 3 && $pathParts[0] === 'calendars') {
  47. // Path looks like calendars/username/calendarname so a new calendar or subscription is created
  48. try {
  49. $this->limiter->registerUserRequest(
  50. 'caldav-create-calendar',
  51. $this->config->getValueInt('dav', 'rateLimitCalendarCreation', 10),
  52. $this->config->getValueInt('dav', 'rateLimitPeriodCalendarCreation', 3600),
  53. $user
  54. );
  55. } catch (RateLimitExceededException $e) {
  56. throw new TooManyRequests('Too many calendars created', 0, $e);
  57. }
  58. $calendarLimit = $this->config->getValueInt('dav', 'maximumCalendarsSubscriptions', 30);
  59. if ($calendarLimit === -1) {
  60. return;
  61. }
  62. $numCalendars = $this->calDavBackend->getCalendarsForUserCount('principals/users/' . $user->getUID());
  63. $numSubscriptions = $this->calDavBackend->getSubscriptionsForUserCount('principals/users/' . $user->getUID());
  64. if (($numCalendars + $numSubscriptions) >= $calendarLimit) {
  65. $this->logger->warning('Maximum number of calendars/subscriptions reached', [
  66. 'calendars' => $numCalendars,
  67. 'subscription' => $numSubscriptions,
  68. 'limit' => $calendarLimit,
  69. ]);
  70. throw new Forbidden('Calendar limit reached', 0);
  71. }
  72. }
  73. }
  74. }