BruteForceMiddleware.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if ($this->reflector->hasAnnotation('BruteForceProtection')) {
  85. $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
  86. $ip = $this->request->getRemoteAddress();
  87. $this->throttler->sleepDelay($ip, $action);
  88. $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
  89. } else {
  90. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  91. $attributes = $reflectionMethod->getAttributes(BruteForceProtection::class);
  92. if (!empty($attributes)) {
  93. $ip = $this->request->getRemoteAddress();
  94. $metaData = $response->getThrottleMetadata();
  95. foreach ($attributes as $attribute) {
  96. /** @var BruteForceProtection $protection */
  97. $protection = $attribute->newInstance();
  98. $action = $protection->getAction();
  99. if (!isset($metaData['action']) || $metaData['action'] === $action) {
  100. $this->throttler->sleepDelay($ip, $action);
  101. $this->throttler->registerAttempt($action, $ip, $metaData);
  102. }
  103. }
  104. } else {
  105. $this->logger->debug('Response for ' . get_class($controller) . '::' . $methodName . ' got bruteforce throttled but has no annotation nor attribute defined.');
  106. }
  107. }
  108. }
  109. return parent::afterController($controller, $methodName, $response);
  110. }
  111. /**
  112. * @param Controller $controller
  113. * @param string $methodName
  114. * @param \Exception $exception
  115. * @throws \Exception
  116. * @return Response
  117. */
  118. public function afterException($controller, $methodName, \Exception $exception): Response {
  119. if ($exception instanceof MaxDelayReached) {
  120. if ($controller instanceof OCSController) {
  121. throw new OCSException($exception->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
  122. }
  123. return new TooManyRequestsResponse();
  124. }
  125. throw $exception;
  126. }
  127. }