BruteForceMiddleware.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\AppFramework\Middleware\Security;
  27. use OC\AppFramework\Utility\ControllerMethodReflector;
  28. use OC\Security\Bruteforce\Throttler;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\Response;
  32. use OCP\AppFramework\Http\TooManyRequestsResponse;
  33. use OCP\AppFramework\Middleware;
  34. use OCP\AppFramework\OCS\OCSException;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\IRequest;
  37. use OCP\Security\Bruteforce\MaxDelayReached;
  38. /**
  39. * Class BruteForceMiddleware performs the bruteforce protection for controllers
  40. * that are annotated with @BruteForceProtection(action=$action) whereas $action
  41. * is the action that should be logged within the database.
  42. *
  43. * @package OC\AppFramework\Middleware\Security
  44. */
  45. class BruteForceMiddleware extends Middleware {
  46. private ControllerMethodReflector $reflector;
  47. private Throttler $throttler;
  48. private IRequest $request;
  49. public function __construct(ControllerMethodReflector $controllerMethodReflector,
  50. Throttler $throttler,
  51. IRequest $request) {
  52. $this->reflector = $controllerMethodReflector;
  53. $this->throttler = $throttler;
  54. $this->request = $request;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function beforeController($controller, $methodName) {
  60. parent::beforeController($controller, $methodName);
  61. if ($this->reflector->hasAnnotation('BruteForceProtection')) {
  62. $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
  63. $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $action);
  64. }
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function afterController($controller, $methodName, Response $response) {
  70. if ($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) {
  71. $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
  72. $ip = $this->request->getRemoteAddress();
  73. $this->throttler->sleepDelay($ip, $action);
  74. $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
  75. }
  76. return parent::afterController($controller, $methodName, $response);
  77. }
  78. /**
  79. * @param Controller $controller
  80. * @param string $methodName
  81. * @param \Exception $exception
  82. * @throws \Exception
  83. * @return Response
  84. */
  85. public function afterException($controller, $methodName, \Exception $exception): Response {
  86. if ($exception instanceof MaxDelayReached) {
  87. if ($controller instanceof OCSController) {
  88. throw new OCSException($exception->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
  89. }
  90. return new TooManyRequestsResponse();
  91. }
  92. throw $exception;
  93. }
  94. }