Skip to content

Commit 2d108e6

Browse files
Merge pull request #38279 from nextcloud/backport/38274/stable22
[stable22] fix(middleware): Also abort the request when reaching max delay in af…
2 parents 439a324 + 5659667 commit 2d108e6

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
@@ -86,8 +86,16 @@ public function afterController($controller, $methodName, Response $response) {
8686
if ($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) {
8787
$action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action');
8888
$ip = $this->request->getRemoteAddress();
89-
$this->throttler->sleepDelay($ip, $action);
9089
$this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata());
90+
try {
91+
$this->throttler->sleepDelayOrThrowOnMax($ip, $action);
92+
} catch (MaxDelayReached $e) {
93+
if ($controller instanceof OCSController) {
94+
throw new OCSException($e->getMessage(), Http::STATUS_TOO_MANY_REQUESTS);
95+
}
96+
97+
return new TooManyRequestsResponse();
98+
}
9199
}
92100

93101
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)