Skip to content

Commit 159676f

Browse files
Merge pull request #38280 from nextcloud/backport/38274/stable21
[stable21] fix(middleware): Also abort the request when reaching max delay in af…
2 parents 9de7429 + 6ae4876 commit 159676f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/private/AppFramework/Middleware/Security/BruteForceMiddleware.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ public function afterController($controller, $methodName, Response $response) {
8787
if ($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) {
8888
$action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
8989
$ip = $this->request->getRemoteAddress();
90-
$this->throttler->sleepDelay($ip, $action);
9190
$this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
91+
try {
92+
$this->throttler->sleepDelayOrThrowOnMax($ip, $action);
93+
} catch (MaxDelayReached $e) {
94+
if ($controller instanceof OCSController) {
95+
throw new OCSException($e->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
96+
}
97+
98+
return new TooManyRequestsResponse();
99+
}
92100
}
93101

94102
return parent::afterController($controller, $methodName, $response);

tests/lib/AppFramework/Middleware/Security/BruteForceMiddlewareTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testAfterControllerWithAnnotationAndThrottledRequest() {
126126
->willReturn('127.0.0.1');
127127
$this->throttler
128128
->expects($this->once())
129-
->method('sleepDelay')
129+
->method('sleepDelayOrThrowOnMax')
130130
->with('127.0.0.1', 'login');
131131
$this->throttler
132132
->expects($this->once())
@@ -158,7 +158,7 @@ public function testAfterControllerWithAnnotationAndNotThrottledRequest() {
158158
->method('getRemoteAddress');
159159
$this->throttler
160160
->expects($this->never())
161-
->method('sleepDelay');
161+
->method('sleepDelayOrThrowOnMax');
162162
$this->throttler
163163
->expects($this->never())
164164
->method('registerAttempt');
@@ -182,7 +182,7 @@ public function testAfterControllerWithoutAnnotation() {
182182
->method('getRemoteAddress');
183183
$this->throttler
184184
->expects($this->never())
185-
->method('sleepDelay');
185+
->method('sleepDelayOrThrowOnMax');
186186

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

0 commit comments

Comments
 (0)