You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RateLimitingMiddleware.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\AppFramework\Middleware\Security;
  26. use OC\AppFramework\Utility\ControllerMethodReflector;
  27. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  28. use OC\Security\RateLimiting\Limiter;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\AppFramework\Middleware;
  32. use OCP\IRequest;
  33. use OCP\IUserSession;
  34. /**
  35. * Class RateLimitingMiddleware is the middleware responsible for implementing the
  36. * ratelimiting in Nextcloud.
  37. *
  38. * It parses annotations such as:
  39. *
  40. * @UserRateThrottle(limit=5, period=100)
  41. * @AnonRateThrottle(limit=1, period=100)
  42. *
  43. * Those annotations above would mean that logged-in users can access the page 5
  44. * times within 100 seconds, and anonymous users 1 time within 100 seconds. If
  45. * only an AnonRateThrottle is specified that one will also be applied to logged-in
  46. * users.
  47. *
  48. * @package OC\AppFramework\Middleware\Security
  49. */
  50. class RateLimitingMiddleware extends Middleware {
  51. /** @var IRequest $request */
  52. private $request;
  53. /** @var IUserSession */
  54. private $userSession;
  55. /** @var ControllerMethodReflector */
  56. private $reflector;
  57. /** @var Limiter */
  58. private $limiter;
  59. /**
  60. * @param IRequest $request
  61. * @param IUserSession $userSession
  62. * @param ControllerMethodReflector $reflector
  63. * @param Limiter $limiter
  64. */
  65. public function __construct(IRequest $request,
  66. IUserSession $userSession,
  67. ControllerMethodReflector $reflector,
  68. Limiter $limiter) {
  69. $this->request = $request;
  70. $this->userSession = $userSession;
  71. $this->reflector = $reflector;
  72. $this->limiter = $limiter;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. * @throws RateLimitExceededException
  77. */
  78. public function beforeController($controller, $methodName) {
  79. parent::beforeController($controller, $methodName);
  80. $anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit');
  81. $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period');
  82. $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit');
  83. $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period');
  84. $rateLimitIdentifier = get_class($controller) . '::' . $methodName;
  85. if ($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) {
  86. $this->limiter->registerUserRequest(
  87. $rateLimitIdentifier,
  88. $userLimit,
  89. $userPeriod,
  90. $this->userSession->getUser()
  91. );
  92. } elseif ($anonLimit !== '' && $anonPeriod !== '') {
  93. $this->limiter->registerAnonRequest(
  94. $rateLimitIdentifier,
  95. $anonLimit,
  96. $anonPeriod,
  97. $this->request->getRemoteAddress()
  98. );
  99. }
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function afterException($controller, $methodName, \Exception $exception) {
  105. if ($exception instanceof RateLimitExceededException) {
  106. if (stripos($this->request->getHeader('Accept'), 'html') === false) {
  107. $response = new DataResponse([], $exception->getCode());
  108. } else {
  109. $response = new TemplateResponse(
  110. 'core',
  111. '429',
  112. [],
  113. TemplateResponse::RENDER_AS_GUEST
  114. );
  115. $response->setStatus($exception->getCode());
  116. }
  117. return $response;
  118. }
  119. throw $exception;
  120. }
  121. }