1
0

BruteForceMiddleware.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
  5. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\AppFramework\Middleware\Security;
  28. use OC\AppFramework\Utility\ControllerMethodReflector;
  29. use OC\Security\Bruteforce\Throttler;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\Attribute\BruteForceProtection;
  33. use OCP\AppFramework\Http\Response;
  34. use OCP\AppFramework\Http\TooManyRequestsResponse;
  35. use OCP\AppFramework\Middleware;
  36. use OCP\AppFramework\OCS\OCSException;
  37. use OCP\AppFramework\OCSController;
  38. use OCP\IRequest;
  39. use OCP\Security\Bruteforce\MaxDelayReached;
  40. use Psr\Log\LoggerInterface;
  41. use ReflectionMethod;
  42. /**
  43. * Class BruteForceMiddleware performs the bruteforce protection for controllers
  44. * that are annotated with @BruteForceProtection(action=$action) whereas $action
  45. * is the action that should be logged within the database.
  46. *
  47. * @package OC\AppFramework\Middleware\Security
  48. */
  49. class BruteForceMiddleware extends Middleware {
  50. public function __construct(
  51. protected ControllerMethodReflector $reflector,
  52. protected Throttler $throttler,
  53. protected IRequest $request,
  54. protected LoggerInterface $logger,
  55. ) {
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function beforeController($controller, $methodName) {
  61. parent::beforeController($controller, $methodName);
  62. if ($this->reflector->hasAnnotation('BruteForceProtection')) {
  63. $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
  64. $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $action);
  65. } else {
  66. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  67. $attributes = $reflectionMethod->getAttributes(BruteForceProtection::class);
  68. if (!empty($attributes)) {
  69. $remoteAddress = $this->request->getRemoteAddress();
  70. foreach ($attributes as $attribute) {
  71. /** @var BruteForceProtection $protection */
  72. $protection = $attribute->newInstance();
  73. $action = $protection->getAction();
  74. $this->throttler->sleepDelayOrThrowOnMax($remoteAddress, $action);
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function afterController($controller, $methodName, Response $response) {
  83. if ($response->isThrottled()) {
  84. try {
  85. if ($this->reflector->hasAnnotation('BruteForceProtection')) {
  86. $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
  87. $ip = $this->request->getRemoteAddress();
  88. $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
  89. $this->throttler->sleepDelayOrThrowOnMax($ip, $action);
  90. } else {
  91. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  92. $attributes = $reflectionMethod->getAttributes(BruteForceProtection::class);
  93. if (!empty($attributes)) {
  94. $ip = $this->request->getRemoteAddress();
  95. $metaData = $response->getThrottleMetadata();
  96. foreach ($attributes as $attribute) {
  97. /** @var BruteForceProtection $protection */
  98. $protection = $attribute->newInstance();
  99. $action = $protection->getAction();
  100. if (!isset($metaData['action']) || $metaData['action'] === $action) {
  101. $this->throttler->registerAttempt($action, $ip, $metaData);
  102. $this->throttler->sleepDelayOrThrowOnMax($ip, $action);
  103. }
  104. }
  105. } else {
  106. $this->logger->debug('Response for ' . get_class($controller) . '::' . $methodName . ' got bruteforce throttled but has no annotation nor attribute defined.');
  107. }
  108. }
  109. } catch (MaxDelayReached $e) {
  110. if ($controller instanceof OCSController) {
  111. throw new OCSException($e->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
  112. }
  113. return new TooManyRequestsResponse();
  114. }
  115. }
  116. return parent::afterController($controller, $methodName, $response);
  117. }
  118. /**
  119. * @param Controller $controller
  120. * @param string $methodName
  121. * @param \Exception $exception
  122. * @throws \Exception
  123. * @return Response
  124. */
  125. public function afterException($controller, $methodName, \Exception $exception): Response {
  126. if ($exception instanceof MaxDelayReached) {
  127. if ($controller instanceof OCSController) {
  128. throw new OCSException($exception->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
  129. }
  130. return new TooManyRequestsResponse();
  131. }
  132. throw $exception;
  133. }
  134. }