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 ;
@@ -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}
0 commit comments