RateLimitingPlugin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OCA\DAV\CalDAV\Security;
  24. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  25. use OC\Security\RateLimiting\Limiter;
  26. use OCA\DAV\CalDAV\CalDavBackend;
  27. use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
  28. use OCP\IAppConfig;
  29. use OCP\IUserManager;
  30. use Psr\Log\LoggerInterface;
  31. use Sabre\DAV;
  32. use Sabre\DAV\Exception\Forbidden;
  33. use Sabre\DAV\ServerPlugin;
  34. use function count;
  35. use function explode;
  36. class RateLimitingPlugin extends ServerPlugin {
  37. private Limiter $limiter;
  38. private IUserManager $userManager;
  39. private CalDavBackend $calDavBackend;
  40. private IAppConfig $config;
  41. private LoggerInterface $logger;
  42. private ?string $userId;
  43. public function __construct(Limiter $limiter,
  44. IUserManager $userManager,
  45. CalDavBackend $calDavBackend,
  46. LoggerInterface $logger,
  47. IAppConfig $config,
  48. ?string $userId) {
  49. $this->limiter = $limiter;
  50. $this->userManager = $userManager;
  51. $this->calDavBackend = $calDavBackend;
  52. $this->config = $config;
  53. $this->logger = $logger;
  54. $this->userId = $userId;
  55. }
  56. public function initialize(DAV\Server $server): void {
  57. $server->on('beforeBind', [$this, 'beforeBind'], 1);
  58. }
  59. public function beforeBind(string $path): void {
  60. if ($this->userId === null) {
  61. // We only care about authenticated users here
  62. return;
  63. }
  64. $user = $this->userManager->get($this->userId);
  65. if ($user === null) {
  66. // We only care about authenticated users here
  67. return;
  68. }
  69. $pathParts = explode('/', $path);
  70. if (count($pathParts) === 3 && $pathParts[0] === 'calendars') {
  71. // Path looks like calendars/username/calendarname so a new calendar or subscription is created
  72. try {
  73. $this->limiter->registerUserRequest(
  74. 'caldav-create-calendar',
  75. $this->config->getValueInt('dav', 'rateLimitCalendarCreation', 10),
  76. $this->config->getValueInt('dav', 'rateLimitPeriodCalendarCreation', 3600),
  77. $user
  78. );
  79. } catch (RateLimitExceededException $e) {
  80. throw new TooManyRequests('Too many calendars created', 0, $e);
  81. }
  82. $calendarLimit = $this->config->getValueInt('dav', 'maximumCalendarsSubscriptions', 30);
  83. if ($calendarLimit === -1) {
  84. return;
  85. }
  86. $numCalendars = $this->calDavBackend->getCalendarsForUserCount('principals/users/' . $user->getUID());
  87. $numSubscriptions = $this->calDavBackend->getSubscriptionsForUserCount('principals/users/' . $user->getUID());
  88. if (($numCalendars + $numSubscriptions) >= $calendarLimit) {
  89. $this->logger->warning('Maximum number of calendars/subscriptions reached', [
  90. 'calendars' => $numCalendars,
  91. 'subscription' => $numSubscriptions,
  92. 'limit' => $calendarLimit,
  93. ]);
  94. throw new Forbidden('Calendar limit reached', 0);
  95. }
  96. }
  97. }
  98. }