Skip to content

Commit 9681be3

Browse files
authored
Merge pull request #35652 from nextcloud/backport/35057/stable23
[stable23] Add brute force protection on all methods wrapped by PublicShareMiddleware
2 parents d78baf4 + b384a96 commit 9681be3

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/private/AppFramework/DependencyInjection/DIContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
299299
new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
300300
$c->get(IRequest::class),
301301
$c->get(ISession::class),
302-
$c->get(\OCP\IConfig::class)
302+
$c->get(\OCP\IConfig::class),
303+
$c->get(OC\Security\Bruteforce\Throttler::class)
303304
)
304305
);
305306
$dispatcher->registerMiddleware(

lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace OC\AppFramework\Middleware\PublicShare;
2525

2626
use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
27+
use OC\Security\Bruteforce\Throttler;
2728
use OCP\AppFramework\AuthPublicShareController;
2829
use OCP\AppFramework\Http\NotFoundResponse;
2930
use OCP\AppFramework\Middleware;
@@ -42,18 +43,26 @@ class PublicShareMiddleware extends Middleware {
4243

4344
/** @var IConfig */
4445
private $config;
46+
/** @var Throttler */
47+
private $throttler;
4548

46-
public function __construct(IRequest $request, ISession $session, IConfig $config) {
49+
public function __construct(IRequest $request, ISession $session, IConfig $config, Throttler $throttler) {
4750
$this->request = $request;
4851
$this->session = $session;
4952
$this->config = $config;
53+
$this->throttler = $throttler;
5054
}
5155

5256
public function beforeController($controller, $methodName) {
5357
if (!($controller instanceof PublicShareController)) {
5458
return;
5559
}
5660

61+
$controllerClassPath = explode('\\', get_class($controller));
62+
$controllerShortClass = end($controllerClassPath);
63+
$bruteforceProtectionAction = $controllerShortClass . '::' . $methodName;
64+
$this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $bruteforceProtectionAction);
65+
5766
if (!$this->isLinkSharingEnabled()) {
5867
throw new NotFoundException('Link sharing is disabled');
5968
}
@@ -79,6 +88,7 @@ public function beforeController($controller, $methodName) {
7988

8089
// If authentication succeeds just continue
8190
if ($controller->isAuthenticated()) {
91+
$this->throttle($bruteforceProtectionAction, $token);
8292
return;
8393
}
8494

@@ -88,6 +98,7 @@ public function beforeController($controller, $methodName) {
8898
throw new NeedAuthenticationException();
8999
}
90100

101+
$this->throttle($bruteforceProtectionAction, $token);
91102
throw new NotFoundException();
92103
}
93104

@@ -128,4 +139,10 @@ private function isLinkSharingEnabled(): bool {
128139

129140
return true;
130141
}
142+
143+
private function throttle($bruteforceProtectionAction, $token): void {
144+
$ip = $this->request->getRemoteAddress();
145+
$this->throttler->sleepDelay($ip, $bruteforceProtectionAction);
146+
$this->throttler->registerAttempt($bruteforceProtectionAction, $ip, ['token' => $token]);
147+
}
131148
}

tests/lib/AppFramework/Middleware/PublicShare/PublicShareMiddlewareTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
2727
use OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware;
28+
use OC\Security\Bruteforce\Throttler;
2829
use OCP\AppFramework\AuthPublicShareController;
2930
use OCP\AppFramework\Controller;
3031
use OCP\AppFramework\Http\NotFoundResponse;
@@ -44,6 +45,8 @@ class PublicShareMiddlewareTest extends \Test\TestCase {
4445
private $session;
4546
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
4647
private $config;
48+
/** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
49+
private $throttler;
4750

4851
/** @var PublicShareMiddleware */
4952
private $middleware;
@@ -55,11 +58,13 @@ protected function setUp(): void {
5558
$this->request = $this->createMock(IRequest::class);
5659
$this->session = $this->createMock(ISession::class);
5760
$this->config = $this->createMock(IConfig::class);
61+
$this->throttler = $this->createMock(Throttler::class);
5862

5963
$this->middleware = new PublicShareMiddleware(
6064
$this->request,
6165
$this->session,
62-
$this->config
66+
$this->config,
67+
$this->throttler
6368
);
6469
}
6570

0 commit comments

Comments
 (0)