Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(middleware): Also abort the request when reaching max delay in af…
…terController

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed May 15, 2023
commit 56596678e004cb6115a8bbe73d05ff9d6b5252b4
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ public function afterController($controller, $methodName, Response $response) {
if ($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) {
$action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
$ip = $this->request->getRemoteAddress();
$this->throttler->sleepDelay($ip, $action);
$this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
try {
$this->throttler->sleepDelayOrThrowOnMax($ip, $action);
} catch (MaxDelayReached $e) {
if ($controller instanceof OCSController) {
throw new OCSException($e->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
}

return new TooManyRequestsResponse();
}
}

return parent::afterController($controller, $methodName, $response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testAfterControllerWithAnnotationAndThrottledRequest() {
->willReturn('127.0.0.1');
$this->throttler
->expects($this->once())
->method('sleepDelay')
->method('sleepDelayOrThrowOnMax')
->with('127.0.0.1', 'login');
$this->throttler
->expects($this->once())
Expand Down Expand Up @@ -158,7 +158,7 @@ public function testAfterControllerWithAnnotationAndNotThrottledRequest() {
->method('getRemoteAddress');
$this->throttler
->expects($this->never())
->method('sleepDelay');
->method('sleepDelayOrThrowOnMax');
$this->throttler
->expects($this->never())
->method('registerAttempt');
Expand All @@ -182,7 +182,7 @@ public function testAfterControllerWithoutAnnotation() {
->method('getRemoteAddress');
$this->throttler
->expects($this->never())
->method('sleepDelay');
->method('sleepDelayOrThrowOnMax');

/** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
$controller = $this->createMock(Controller::class);
Expand Down