Skip to content

Commit bc97ff7

Browse files
committed
manual backport of #35057
Signed-off-by: Julien Veyssier <[email protected]>
1 parent 5d9f78a commit bc97ff7

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/private/AppFramework/DependencyInjection/DIContainer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ public function __construct($appName, $urlParams = [], ServerContainer $server =
301301
new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
302302
$c->get(IRequest::class),
303303
$c->get(ISession::class),
304-
$c->get(\OCP\IConfig::class)
304+
$c->get(\OCP\IConfig::class),
305+
$c->get(OC\Security\Bruteforce\Throttler::class)
305306
)
306307
);
307308
$dispatcher->registerMiddleware(

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

Lines changed: 20 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;
@@ -43,17 +44,26 @@ class PublicShareMiddleware extends Middleware {
4344
/** @var IConfig */
4445
private $config;
4546

46-
public function __construct(IRequest $request, ISession $session, IConfig $config) {
47+
/** @var Throttler */
48+
private $throttler;
49+
50+
public function __construct(IRequest $request, ISession $session, IConfig $config, Throttler $throttler) {
4751
$this->request = $request;
4852
$this->session = $session;
4953
$this->config = $config;
54+
$this->throttler = $throttler;
5055
}
5156

5257
public function beforeController($controller, $methodName) {
5358
if (!($controller instanceof PublicShareController)) {
5459
return;
5560
}
5661

62+
$controllerClassPath = explode('\\', get_class($controller));
63+
$controllerShortClass = end($controllerClassPath);
64+
$bruteforceProtectionAction = $controllerShortClass . '::' . $methodName;
65+
$this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $bruteforceProtectionAction);
66+
5767
if (!$this->isLinkSharingEnabled()) {
5868
throw new NotFoundException('Link sharing is disabled');
5969
}
@@ -68,6 +78,8 @@ public function beforeController($controller, $methodName) {
6878
$controller->setToken($token);
6979

7080
if (!$controller->isValidToken()) {
81+
$this->throttle($bruteforceProtectionAction, $token);
82+
7183
$controller->shareNotFound();
7284
throw new NotFoundException();
7385
}
@@ -88,6 +100,7 @@ public function beforeController($controller, $methodName) {
88100
throw new NeedAuthenticationException();
89101
}
90102

103+
$this->throttle($bruteforceProtectionAction, $token);
91104
throw new NotFoundException();
92105
}
93106

@@ -128,4 +141,10 @@ private function isLinkSharingEnabled(): bool {
128141

129142
return true;
130143
}
144+
145+
private function throttle($bruteforceProtectionAction, $token): void {
146+
$ip = $this->request->getRemoteAddress();
147+
$this->throttler->sleepDelay($ip, $bruteforceProtectionAction);
148+
$this->throttler->registerAttempt($bruteforceProtectionAction, $ip, ['token' => $token]);
149+
}
131150
}

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 IConfig|\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)