2424namespace OC \AppFramework \Middleware \PublicShare ;
2525
2626use OC \AppFramework \Middleware \PublicShare \Exceptions \NeedAuthenticationException ;
27+ use OC \Security \Bruteforce \Throttler ;
2728use OCP \AppFramework \AuthPublicShareController ;
2829use OCP \AppFramework \Http \NotFoundResponse ;
2930use 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}
0 commit comments