@@ -50,80 +50,80 @@ |
||
50 | 50 | * @package OC\AppFramework\Middleware\Security |
51 | 51 | */ |
52 | 52 | class RateLimitingMiddleware extends Middleware { |
53 | - /** @var IRequest $request */ |
|
54 | - private $request; |
|
55 | - /** @var IUserSession */ |
|
56 | - private $userSession; |
|
57 | - /** @var ControllerMethodReflector */ |
|
58 | - private $reflector; |
|
59 | - /** @var Limiter */ |
|
60 | - private $limiter; |
|
53 | + /** @var IRequest $request */ |
|
54 | + private $request; |
|
55 | + /** @var IUserSession */ |
|
56 | + private $userSession; |
|
57 | + /** @var ControllerMethodReflector */ |
|
58 | + private $reflector; |
|
59 | + /** @var Limiter */ |
|
60 | + private $limiter; |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param IRequest $request |
|
64 | - * @param IUserSession $userSession |
|
65 | - * @param ControllerMethodReflector $reflector |
|
66 | - * @param Limiter $limiter |
|
67 | - */ |
|
68 | - public function __construct(IRequest $request, |
|
69 | - IUserSession $userSession, |
|
70 | - ControllerMethodReflector $reflector, |
|
71 | - Limiter $limiter) { |
|
72 | - $this->request = $request; |
|
73 | - $this->userSession = $userSession; |
|
74 | - $this->reflector = $reflector; |
|
75 | - $this->limiter = $limiter; |
|
76 | - } |
|
62 | + /** |
|
63 | + * @param IRequest $request |
|
64 | + * @param IUserSession $userSession |
|
65 | + * @param ControllerMethodReflector $reflector |
|
66 | + * @param Limiter $limiter |
|
67 | + */ |
|
68 | + public function __construct(IRequest $request, |
|
69 | + IUserSession $userSession, |
|
70 | + ControllerMethodReflector $reflector, |
|
71 | + Limiter $limiter) { |
|
72 | + $this->request = $request; |
|
73 | + $this->userSession = $userSession; |
|
74 | + $this->reflector = $reflector; |
|
75 | + $this->limiter = $limiter; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * {@inheritDoc} |
|
80 | - * @throws RateLimitExceededException |
|
81 | - */ |
|
82 | - public function beforeController($controller, $methodName) { |
|
83 | - parent::beforeController($controller, $methodName); |
|
78 | + /** |
|
79 | + * {@inheritDoc} |
|
80 | + * @throws RateLimitExceededException |
|
81 | + */ |
|
82 | + public function beforeController($controller, $methodName) { |
|
83 | + parent::beforeController($controller, $methodName); |
|
84 | 84 | |
85 | - $anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit'); |
|
86 | - $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period'); |
|
87 | - $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit'); |
|
88 | - $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period'); |
|
89 | - $rateLimitIdentifier = get_class($controller) . '::' . $methodName; |
|
90 | - if ($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
|
91 | - $this->limiter->registerUserRequest( |
|
92 | - $rateLimitIdentifier, |
|
93 | - $userLimit, |
|
94 | - $userPeriod, |
|
95 | - $this->userSession->getUser() |
|
96 | - ); |
|
97 | - } elseif ($anonLimit !== '' && $anonPeriod !== '') { |
|
98 | - $this->limiter->registerAnonRequest( |
|
99 | - $rateLimitIdentifier, |
|
100 | - $anonLimit, |
|
101 | - $anonPeriod, |
|
102 | - $this->request->getRemoteAddress() |
|
103 | - ); |
|
104 | - } |
|
105 | - } |
|
85 | + $anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit'); |
|
86 | + $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period'); |
|
87 | + $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit'); |
|
88 | + $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period'); |
|
89 | + $rateLimitIdentifier = get_class($controller) . '::' . $methodName; |
|
90 | + if ($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
|
91 | + $this->limiter->registerUserRequest( |
|
92 | + $rateLimitIdentifier, |
|
93 | + $userLimit, |
|
94 | + $userPeriod, |
|
95 | + $this->userSession->getUser() |
|
96 | + ); |
|
97 | + } elseif ($anonLimit !== '' && $anonPeriod !== '') { |
|
98 | + $this->limiter->registerAnonRequest( |
|
99 | + $rateLimitIdentifier, |
|
100 | + $anonLimit, |
|
101 | + $anonPeriod, |
|
102 | + $this->request->getRemoteAddress() |
|
103 | + ); |
|
104 | + } |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * {@inheritDoc} |
|
109 | - */ |
|
110 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
111 | - if ($exception instanceof RateLimitExceededException) { |
|
112 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
113 | - $response = new DataResponse([], $exception->getCode()); |
|
114 | - } else { |
|
115 | - $response = new TemplateResponse( |
|
116 | - 'core', |
|
117 | - '429', |
|
118 | - [], |
|
119 | - TemplateResponse::RENDER_AS_GUEST |
|
120 | - ); |
|
121 | - $response->setStatus($exception->getCode()); |
|
122 | - } |
|
107 | + /** |
|
108 | + * {@inheritDoc} |
|
109 | + */ |
|
110 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
111 | + if ($exception instanceof RateLimitExceededException) { |
|
112 | + if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
113 | + $response = new DataResponse([], $exception->getCode()); |
|
114 | + } else { |
|
115 | + $response = new TemplateResponse( |
|
116 | + 'core', |
|
117 | + '429', |
|
118 | + [], |
|
119 | + TemplateResponse::RENDER_AS_GUEST |
|
120 | + ); |
|
121 | + $response->setStatus($exception->getCode()); |
|
122 | + } |
|
123 | 123 | |
124 | - return $response; |
|
125 | - } |
|
124 | + return $response; |
|
125 | + } |
|
126 | 126 | |
127 | - throw $exception; |
|
128 | - } |
|
127 | + throw $exception; |
|
128 | + } |
|
129 | 129 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period'); |
87 | 87 | $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit'); |
88 | 88 | $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period'); |
89 | - $rateLimitIdentifier = get_class($controller) . '::' . $methodName; |
|
89 | + $rateLimitIdentifier = get_class($controller).'::'.$methodName; |
|
90 | 90 | if ($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
91 | 91 | $this->limiter->registerUserRequest( |
92 | 92 | $rateLimitIdentifier, |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | */ |
110 | 110 | public function afterException($controller, $methodName, \Exception $exception) { |
111 | 111 | if ($exception instanceof RateLimitExceededException) { |
112 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
112 | + if (stripos($this->request->getHeader('Accept'), 'html') === false) { |
|
113 | 113 | $response = new DataResponse([], $exception->getCode()); |
114 | 114 | } else { |
115 | 115 | $response = new TemplateResponse( |
@@ -33,159 +33,159 @@ |
||
33 | 33 | use Throwable; |
34 | 34 | |
35 | 35 | class Base { |
36 | - private $template; // The template |
|
37 | - private $vars; // Vars |
|
38 | - |
|
39 | - /** @var \OCP\IL10N */ |
|
40 | - private $l10n; |
|
41 | - |
|
42 | - /** @var Defaults */ |
|
43 | - private $theme; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param string $template |
|
47 | - * @param string $requestToken |
|
48 | - * @param \OCP\IL10N $l10n |
|
49 | - * @param Defaults $theme |
|
50 | - */ |
|
51 | - public function __construct($template, $requestToken, $l10n, $theme) { |
|
52 | - $this->vars = []; |
|
53 | - $this->vars['requesttoken'] = $requestToken; |
|
54 | - $this->l10n = $l10n; |
|
55 | - $this->template = $template; |
|
56 | - $this->theme = $theme; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $serverRoot |
|
61 | - * @param string|false $app_dir |
|
62 | - * @param string $theme |
|
63 | - * @param string $app |
|
64 | - * @return string[] |
|
65 | - */ |
|
66 | - protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
67 | - // Check if the app is in the app folder or in the root |
|
68 | - if (file_exists($app_dir.'/templates/')) { |
|
69 | - return [ |
|
70 | - $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
71 | - $app_dir.'/templates/', |
|
72 | - ]; |
|
73 | - } |
|
74 | - return [ |
|
75 | - $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
76 | - $serverRoot.'/'.$app.'/templates/', |
|
77 | - ]; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param string $serverRoot |
|
82 | - * @param string $theme |
|
83 | - * @return string[] |
|
84 | - */ |
|
85 | - protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
86 | - return [ |
|
87 | - $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
88 | - $serverRoot.'/core/templates/', |
|
89 | - ]; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Assign variables |
|
94 | - * @param string $key key |
|
95 | - * @param array|bool|integer|string|Throwable $value value |
|
96 | - * @return bool |
|
97 | - * |
|
98 | - * This function assigns a variable. It can be accessed via $_[$key] in |
|
99 | - * the template. |
|
100 | - * |
|
101 | - * If the key existed before, it will be overwritten |
|
102 | - */ |
|
103 | - public function assign($key, $value) { |
|
104 | - $this->vars[$key] = $value; |
|
105 | - return true; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Appends a variable |
|
110 | - * @param string $key key |
|
111 | - * @param mixed $value value |
|
112 | - * |
|
113 | - * This function assigns a variable in an array context. If the key already |
|
114 | - * exists, the value will be appended. It can be accessed via |
|
115 | - * $_[$key][$position] in the template. |
|
116 | - */ |
|
117 | - public function append($key, $value) { |
|
118 | - if (array_key_exists($key, $this->vars)) { |
|
119 | - $this->vars[$key][] = $value; |
|
120 | - } else { |
|
121 | - $this->vars[$key] = [ $value ]; |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Prints the proceeded template |
|
127 | - * @return bool |
|
128 | - * |
|
129 | - * This function proceeds the template and prints its output. |
|
130 | - */ |
|
131 | - public function printPage() { |
|
132 | - $data = $this->fetchPage(); |
|
133 | - if ($data === false) { |
|
134 | - return false; |
|
135 | - } else { |
|
136 | - print $data; |
|
137 | - return true; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Process the template |
|
143 | - * |
|
144 | - * @param array|null $additionalParams |
|
145 | - * @return string This function processes the template. |
|
146 | - * |
|
147 | - * This function processes the template. |
|
148 | - */ |
|
149 | - public function fetchPage($additionalParams = null) { |
|
150 | - return $this->load($this->template, $additionalParams); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * doing the actual work |
|
155 | - * |
|
156 | - * @param string $file |
|
157 | - * @param array|null $additionalParams |
|
158 | - * @return string content |
|
159 | - * |
|
160 | - * Includes the template file, fetches its output |
|
161 | - */ |
|
162 | - protected function load($file, $additionalParams = null) { |
|
163 | - // Register the variables |
|
164 | - $_ = $this->vars; |
|
165 | - $l = $this->l10n; |
|
166 | - $theme = $this->theme; |
|
167 | - |
|
168 | - if (!is_null($additionalParams)) { |
|
169 | - $_ = array_merge($additionalParams, $this->vars); |
|
170 | - foreach ($_ as $var => $value) { |
|
171 | - if (!isset(${$var})) { |
|
172 | - ${$var} = $value; |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - // Include |
|
178 | - ob_start(); |
|
179 | - try { |
|
180 | - include $file; |
|
181 | - $data = ob_get_contents(); |
|
182 | - } catch (\Exception $e) { |
|
183 | - @ob_end_clean(); |
|
184 | - throw $e; |
|
185 | - } |
|
186 | - @ob_end_clean(); |
|
187 | - |
|
188 | - // Return data |
|
189 | - return $data; |
|
190 | - } |
|
36 | + private $template; // The template |
|
37 | + private $vars; // Vars |
|
38 | + |
|
39 | + /** @var \OCP\IL10N */ |
|
40 | + private $l10n; |
|
41 | + |
|
42 | + /** @var Defaults */ |
|
43 | + private $theme; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param string $template |
|
47 | + * @param string $requestToken |
|
48 | + * @param \OCP\IL10N $l10n |
|
49 | + * @param Defaults $theme |
|
50 | + */ |
|
51 | + public function __construct($template, $requestToken, $l10n, $theme) { |
|
52 | + $this->vars = []; |
|
53 | + $this->vars['requesttoken'] = $requestToken; |
|
54 | + $this->l10n = $l10n; |
|
55 | + $this->template = $template; |
|
56 | + $this->theme = $theme; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $serverRoot |
|
61 | + * @param string|false $app_dir |
|
62 | + * @param string $theme |
|
63 | + * @param string $app |
|
64 | + * @return string[] |
|
65 | + */ |
|
66 | + protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
67 | + // Check if the app is in the app folder or in the root |
|
68 | + if (file_exists($app_dir.'/templates/')) { |
|
69 | + return [ |
|
70 | + $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
71 | + $app_dir.'/templates/', |
|
72 | + ]; |
|
73 | + } |
|
74 | + return [ |
|
75 | + $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
76 | + $serverRoot.'/'.$app.'/templates/', |
|
77 | + ]; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param string $serverRoot |
|
82 | + * @param string $theme |
|
83 | + * @return string[] |
|
84 | + */ |
|
85 | + protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
86 | + return [ |
|
87 | + $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
88 | + $serverRoot.'/core/templates/', |
|
89 | + ]; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Assign variables |
|
94 | + * @param string $key key |
|
95 | + * @param array|bool|integer|string|Throwable $value value |
|
96 | + * @return bool |
|
97 | + * |
|
98 | + * This function assigns a variable. It can be accessed via $_[$key] in |
|
99 | + * the template. |
|
100 | + * |
|
101 | + * If the key existed before, it will be overwritten |
|
102 | + */ |
|
103 | + public function assign($key, $value) { |
|
104 | + $this->vars[$key] = $value; |
|
105 | + return true; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Appends a variable |
|
110 | + * @param string $key key |
|
111 | + * @param mixed $value value |
|
112 | + * |
|
113 | + * This function assigns a variable in an array context. If the key already |
|
114 | + * exists, the value will be appended. It can be accessed via |
|
115 | + * $_[$key][$position] in the template. |
|
116 | + */ |
|
117 | + public function append($key, $value) { |
|
118 | + if (array_key_exists($key, $this->vars)) { |
|
119 | + $this->vars[$key][] = $value; |
|
120 | + } else { |
|
121 | + $this->vars[$key] = [ $value ]; |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Prints the proceeded template |
|
127 | + * @return bool |
|
128 | + * |
|
129 | + * This function proceeds the template and prints its output. |
|
130 | + */ |
|
131 | + public function printPage() { |
|
132 | + $data = $this->fetchPage(); |
|
133 | + if ($data === false) { |
|
134 | + return false; |
|
135 | + } else { |
|
136 | + print $data; |
|
137 | + return true; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Process the template |
|
143 | + * |
|
144 | + * @param array|null $additionalParams |
|
145 | + * @return string This function processes the template. |
|
146 | + * |
|
147 | + * This function processes the template. |
|
148 | + */ |
|
149 | + public function fetchPage($additionalParams = null) { |
|
150 | + return $this->load($this->template, $additionalParams); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * doing the actual work |
|
155 | + * |
|
156 | + * @param string $file |
|
157 | + * @param array|null $additionalParams |
|
158 | + * @return string content |
|
159 | + * |
|
160 | + * Includes the template file, fetches its output |
|
161 | + */ |
|
162 | + protected function load($file, $additionalParams = null) { |
|
163 | + // Register the variables |
|
164 | + $_ = $this->vars; |
|
165 | + $l = $this->l10n; |
|
166 | + $theme = $this->theme; |
|
167 | + |
|
168 | + if (!is_null($additionalParams)) { |
|
169 | + $_ = array_merge($additionalParams, $this->vars); |
|
170 | + foreach ($_ as $var => $value) { |
|
171 | + if (!isset(${$var})) { |
|
172 | + ${$var} = $value; |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + // Include |
|
178 | + ob_start(); |
|
179 | + try { |
|
180 | + include $file; |
|
181 | + $data = ob_get_contents(); |
|
182 | + } catch (\Exception $e) { |
|
183 | + @ob_end_clean(); |
|
184 | + throw $e; |
|
185 | + } |
|
186 | + @ob_end_clean(); |
|
187 | + |
|
188 | + // Return data |
|
189 | + return $data; |
|
190 | + } |
|
191 | 191 | } |