aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorJulien Veyssier <julien-nc@posteo.net>2022-12-07 16:26:26 +0100
committerJulien Veyssier <julien-nc@posteo.net>2023-03-15 10:18:38 +0100
commita7d1313c14011bbc5290a1e9baf9c1f3f9e03af5 (patch)
tree81d7de7365e176a2efee30b17871df4f7a84b3fe /lib/private
parent4ef565973c482d4192985ccc228b1610754437b6 (diff)
downloadnextcloud-server-a7d1313c14011bbc5290a1e9baf9c1f3f9e03af5.tar.gz
nextcloud-server-a7d1313c14011bbc5290a1e9baf9c1f3f9e03af5.zip
manual backport of #35057
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/AppFramework/DependencyInjection/DIContainer.php3
-rw-r--r--lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php21
2 files changed, 22 insertions, 2 deletions
diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php
index e06d5226a28..55278dabad5 100644
--- a/lib/private/AppFramework/DependencyInjection/DIContainer.php
+++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php
@@ -301,7 +301,8 @@ class DIContainer extends SimpleContainer implements IAppContainer {
new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
$c->get(IRequest::class),
$c->get(ISession::class),
- $c->get(\OCP\IConfig::class)
+ $c->get(\OCP\IConfig::class),
+ $c->get(OC\Security\Bruteforce\Throttler::class)
)
);
$dispatcher->registerMiddleware(
diff --git a/lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php b/lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php
index d3beb4fd3a8..f20bd333452 100644
--- a/lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php
+++ b/lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php
@@ -24,6 +24,7 @@
namespace OC\AppFramework\Middleware\PublicShare;
use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
+use OC\Security\Bruteforce\Throttler;
use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Middleware;
@@ -43,10 +44,14 @@ class PublicShareMiddleware extends Middleware {
/** @var IConfig */
private $config;
- public function __construct(IRequest $request, ISession $session, IConfig $config) {
+ /** @var Throttler */
+ private $throttler;
+
+ public function __construct(IRequest $request, ISession $session, IConfig $config, Throttler $throttler) {
$this->request = $request;
$this->session = $session;
$this->config = $config;
+ $this->throttler = $throttler;
}
public function beforeController($controller, $methodName) {
@@ -54,6 +59,11 @@ class PublicShareMiddleware extends Middleware {
return;
}
+ $controllerClassPath = explode('\\', get_class($controller));
+ $controllerShortClass = end($controllerClassPath);
+ $bruteforceProtectionAction = $controllerShortClass . '::' . $methodName;
+ $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $bruteforceProtectionAction);
+
if (!$this->isLinkSharingEnabled()) {
throw new NotFoundException('Link sharing is disabled');
}
@@ -68,6 +78,8 @@ class PublicShareMiddleware extends Middleware {
$controller->setToken($token);
if (!$controller->isValidToken()) {
+ $this->throttle($bruteforceProtectionAction, $token);
+
$controller->shareNotFound();
throw new NotFoundException();
}
@@ -88,6 +100,7 @@ class PublicShareMiddleware extends Middleware {
throw new NeedAuthenticationException();
}
+ $this->throttle($bruteforceProtectionAction, $token);
throw new NotFoundException();
}
@@ -128,4 +141,10 @@ class PublicShareMiddleware extends Middleware {
return true;
}
+
+ private function throttle($bruteforceProtectionAction, $token): void {
+ $ip = $this->request->getRemoteAddress();
+ $this->throttler->sleepDelay($ip, $bruteforceProtectionAction);
+ $this->throttler->registerAttempt($bruteforceProtectionAction, $ip, ['token' => $token]);
+ }
}