RateLimitingPlugin.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. private IUserManager $userManager;
  23. private CalDavBackend $calDavBackend;
  24. private IAppConfig $config;
  25. private LoggerInterface $logger;
  26. private ?string $userId;
  27. public function __construct(Limiter $limiter,
  28. IUserManager $userManager,
  29. CalDavBackend $calDavBackend,
  30. LoggerInterface $logger,
  31. IAppConfig $config,
  32. ?string $userId) {
  33. $this->limiter = $limiter;
  34. $this->userManager = $userManager;
  35. $this->calDavBackend = $calDavBackend;
  36. $this->config = $config;
  37. $this->logger = $logger;
  38. $this->userId = $userId;
  39. }
  40. public function initialize(DAV\Server $server): void {
  41. $server->on('beforeBind', [$this, 'beforeBind'], 1);
  42. }
  43. public function beforeBind(string $path): void {
  44. if ($this->userId === null) {
  45. // We only care about authenticated users here
  46. return;
  47. }
  48. $user = $this->userManager->get($this->userId);
  49. if ($user === null) {
  50. // We only care about authenticated users here
  51. return;
  52. }
  53. $pathParts = explode('/', $path);
  54. if (count($pathParts) === 3 && $pathParts[0] === 'calendars') {
  55. // Path looks like calendars/username/calendarname so a new calendar or subscription is created
  56. try {
  57. $this->limiter->registerUserRequest(
  58. 'caldav-create-calendar',
  59. $this->config->getValueInt('dav', 'rateLimitCalendarCreation', 10),
  60. $this->config->getValueInt('dav', 'rateLimitPeriodCalendarCreation', 3600),
  61. $user
  62. );
  63. } catch (RateLimitExceededException $e) {
  64. throw new TooManyRequests('Too many calendars created', 0, $e);
  65. }
  66. $calendarLimit = $this->config->getValueInt('dav', 'maximumCalendarsSubscriptions', 30);
  67. if ($calendarLimit === -1) {
  68. return;
  69. }
  70. $numCalendars = $this->calDavBackend->getCalendarsForUserCount('principals/users/' . $user->getUID());
  71. $numSubscriptions = $this->calDavBackend->getSubscriptionsForUserCount('principals/users/' . $user->getUID());
  72. if (($numCalendars + $numSubscriptions) >= $calendarLimit) {
  73. $this->logger->warning('Maximum number of calendars/subscriptions reached', [
  74. 'calendars' => $numCalendars,
  75. 'subscription' => $numSubscriptions,
  76. 'limit' => $calendarLimit,
  77. ]);
  78. throw new Forbidden('Calendar limit reached', 0);
  79. }
  80. }
  81. }
  82. }