@@ -44,69 +44,69 @@ |
||
| 44 | 44 | abstract class Middleware { |
| 45 | 45 | |
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * This is being run in normal order before the controller is being |
|
| 49 | - * called which allows several modifications and checks |
|
| 50 | - * |
|
| 51 | - * @param Controller $controller the controller that is being called |
|
| 52 | - * @param string $methodName the name of the method that will be called on |
|
| 53 | - * the controller |
|
| 54 | - * @since 6.0.0 |
|
| 55 | - */ |
|
| 56 | - public function beforeController($controller, $methodName){ |
|
| 47 | + /** |
|
| 48 | + * This is being run in normal order before the controller is being |
|
| 49 | + * called which allows several modifications and checks |
|
| 50 | + * |
|
| 51 | + * @param Controller $controller the controller that is being called |
|
| 52 | + * @param string $methodName the name of the method that will be called on |
|
| 53 | + * the controller |
|
| 54 | + * @since 6.0.0 |
|
| 55 | + */ |
|
| 56 | + public function beforeController($controller, $methodName){ |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * This is being run when either the beforeController method or the |
|
| 63 | - * controller method itself is throwing an exception. The middleware is |
|
| 64 | - * asked in reverse order to handle the exception and to return a response. |
|
| 65 | - * If the response is null, it is assumed that the exception could not be |
|
| 66 | - * handled and the error will be thrown again |
|
| 67 | - * |
|
| 68 | - * @param Controller $controller the controller that is being called |
|
| 69 | - * @param string $methodName the name of the method that will be called on |
|
| 70 | - * the controller |
|
| 71 | - * @param \Exception $exception the thrown exception |
|
| 72 | - * @throws \Exception the passed in exception if it can't handle it |
|
| 73 | - * @return Response a Response object in case that the exception was handled |
|
| 74 | - * @since 6.0.0 |
|
| 75 | - */ |
|
| 76 | - public function afterException($controller, $methodName, \Exception $exception){ |
|
| 77 | - throw $exception; |
|
| 78 | - } |
|
| 61 | + /** |
|
| 62 | + * This is being run when either the beforeController method or the |
|
| 63 | + * controller method itself is throwing an exception. The middleware is |
|
| 64 | + * asked in reverse order to handle the exception and to return a response. |
|
| 65 | + * If the response is null, it is assumed that the exception could not be |
|
| 66 | + * handled and the error will be thrown again |
|
| 67 | + * |
|
| 68 | + * @param Controller $controller the controller that is being called |
|
| 69 | + * @param string $methodName the name of the method that will be called on |
|
| 70 | + * the controller |
|
| 71 | + * @param \Exception $exception the thrown exception |
|
| 72 | + * @throws \Exception the passed in exception if it can't handle it |
|
| 73 | + * @return Response a Response object in case that the exception was handled |
|
| 74 | + * @since 6.0.0 |
|
| 75 | + */ |
|
| 76 | + public function afterException($controller, $methodName, \Exception $exception){ |
|
| 77 | + throw $exception; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * This is being run after a successful controllermethod call and allows |
|
| 83 | - * the manipulation of a Response object. The middleware is run in reverse order |
|
| 84 | - * |
|
| 85 | - * @param Controller $controller the controller that is being called |
|
| 86 | - * @param string $methodName the name of the method that will be called on |
|
| 87 | - * the controller |
|
| 88 | - * @param Response $response the generated response from the controller |
|
| 89 | - * @return Response a Response object |
|
| 90 | - * @since 6.0.0 |
|
| 91 | - */ |
|
| 92 | - public function afterController($controller, $methodName, Response $response){ |
|
| 93 | - return $response; |
|
| 94 | - } |
|
| 81 | + /** |
|
| 82 | + * This is being run after a successful controllermethod call and allows |
|
| 83 | + * the manipulation of a Response object. The middleware is run in reverse order |
|
| 84 | + * |
|
| 85 | + * @param Controller $controller the controller that is being called |
|
| 86 | + * @param string $methodName the name of the method that will be called on |
|
| 87 | + * the controller |
|
| 88 | + * @param Response $response the generated response from the controller |
|
| 89 | + * @return Response a Response object |
|
| 90 | + * @since 6.0.0 |
|
| 91 | + */ |
|
| 92 | + public function afterController($controller, $methodName, Response $response){ |
|
| 93 | + return $response; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * This is being run after the response object has been rendered and |
|
| 99 | - * allows the manipulation of the output. The middleware is run in reverse order |
|
| 100 | - * |
|
| 101 | - * @param Controller $controller the controller that is being called |
|
| 102 | - * @param string $methodName the name of the method that will be called on |
|
| 103 | - * the controller |
|
| 104 | - * @param string $output the generated output from a response |
|
| 105 | - * @return string the output that should be printed |
|
| 106 | - * @since 6.0.0 |
|
| 107 | - */ |
|
| 108 | - public function beforeOutput($controller, $methodName, $output){ |
|
| 109 | - return $output; |
|
| 110 | - } |
|
| 97 | + /** |
|
| 98 | + * This is being run after the response object has been rendered and |
|
| 99 | + * allows the manipulation of the output. The middleware is run in reverse order |
|
| 100 | + * |
|
| 101 | + * @param Controller $controller the controller that is being called |
|
| 102 | + * @param string $methodName the name of the method that will be called on |
|
| 103 | + * the controller |
|
| 104 | + * @param string $output the generated output from a response |
|
| 105 | + * @return string the output that should be printed |
|
| 106 | + * @since 6.0.0 |
|
| 107 | + */ |
|
| 108 | + public function beforeOutput($controller, $methodName, $output){ |
|
| 109 | + return $output; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | 112 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * the controller |
| 54 | 54 | * @since 6.0.0 |
| 55 | 55 | */ |
| 56 | - public function beforeController($controller, $methodName){ |
|
| 56 | + public function beforeController($controller, $methodName) { |
|
| 57 | 57 | |
| 58 | 58 | } |
| 59 | 59 | |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | * @return Response a Response object in case that the exception was handled |
| 74 | 74 | * @since 6.0.0 |
| 75 | 75 | */ |
| 76 | - public function afterException($controller, $methodName, \Exception $exception){ |
|
| 76 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 77 | 77 | throw $exception; |
| 78 | 78 | } |
| 79 | 79 | |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * @return Response a Response object |
| 90 | 90 | * @since 6.0.0 |
| 91 | 91 | */ |
| 92 | - public function afterController($controller, $methodName, Response $response){ |
|
| 92 | + public function afterController($controller, $methodName, Response $response) { |
|
| 93 | 93 | return $response; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * @return string the output that should be printed |
| 106 | 106 | * @since 6.0.0 |
| 107 | 107 | */ |
| 108 | - public function beforeOutput($controller, $methodName, $output){ |
|
| 108 | + public function beforeOutput($controller, $methodName, $output) { |
|
| 109 | 109 | return $output; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -45,116 +45,116 @@ |
||
| 45 | 45 | * https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS |
| 46 | 46 | */ |
| 47 | 47 | class CORSMiddleware extends Middleware { |
| 48 | - /** @var IRequest */ |
|
| 49 | - private $request; |
|
| 50 | - /** @var ControllerMethodReflector */ |
|
| 51 | - private $reflector; |
|
| 52 | - /** @var Session */ |
|
| 53 | - private $session; |
|
| 54 | - /** @var Throttler */ |
|
| 55 | - private $throttler; |
|
| 48 | + /** @var IRequest */ |
|
| 49 | + private $request; |
|
| 50 | + /** @var ControllerMethodReflector */ |
|
| 51 | + private $reflector; |
|
| 52 | + /** @var Session */ |
|
| 53 | + private $session; |
|
| 54 | + /** @var Throttler */ |
|
| 55 | + private $throttler; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param IRequest $request |
|
| 59 | - * @param ControllerMethodReflector $reflector |
|
| 60 | - * @param Session $session |
|
| 61 | - * @param Throttler $throttler |
|
| 62 | - */ |
|
| 63 | - public function __construct(IRequest $request, |
|
| 64 | - ControllerMethodReflector $reflector, |
|
| 65 | - Session $session, |
|
| 66 | - Throttler $throttler) { |
|
| 67 | - $this->request = $request; |
|
| 68 | - $this->reflector = $reflector; |
|
| 69 | - $this->session = $session; |
|
| 70 | - $this->throttler = $throttler; |
|
| 71 | - } |
|
| 57 | + /** |
|
| 58 | + * @param IRequest $request |
|
| 59 | + * @param ControllerMethodReflector $reflector |
|
| 60 | + * @param Session $session |
|
| 61 | + * @param Throttler $throttler |
|
| 62 | + */ |
|
| 63 | + public function __construct(IRequest $request, |
|
| 64 | + ControllerMethodReflector $reflector, |
|
| 65 | + Session $session, |
|
| 66 | + Throttler $throttler) { |
|
| 67 | + $this->request = $request; |
|
| 68 | + $this->reflector = $reflector; |
|
| 69 | + $this->session = $session; |
|
| 70 | + $this->throttler = $throttler; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * This is being run in normal order before the controller is being |
|
| 75 | - * called which allows several modifications and checks |
|
| 76 | - * |
|
| 77 | - * @param Controller $controller the controller that is being called |
|
| 78 | - * @param string $methodName the name of the method that will be called on |
|
| 79 | - * the controller |
|
| 80 | - * @throws SecurityException |
|
| 81 | - * @since 6.0.0 |
|
| 82 | - */ |
|
| 83 | - public function beforeController($controller, $methodName){ |
|
| 84 | - // ensure that @CORS annotated API routes are not used in conjunction |
|
| 85 | - // with session authentication since this enables CSRF attack vectors |
|
| 86 | - if ($this->reflector->hasAnnotation('CORS') && |
|
| 87 | - !$this->reflector->hasAnnotation('PublicPage')) { |
|
| 88 | - $user = $this->request->server['PHP_AUTH_USER']; |
|
| 89 | - $pass = $this->request->server['PHP_AUTH_PW']; |
|
| 73 | + /** |
|
| 74 | + * This is being run in normal order before the controller is being |
|
| 75 | + * called which allows several modifications and checks |
|
| 76 | + * |
|
| 77 | + * @param Controller $controller the controller that is being called |
|
| 78 | + * @param string $methodName the name of the method that will be called on |
|
| 79 | + * the controller |
|
| 80 | + * @throws SecurityException |
|
| 81 | + * @since 6.0.0 |
|
| 82 | + */ |
|
| 83 | + public function beforeController($controller, $methodName){ |
|
| 84 | + // ensure that @CORS annotated API routes are not used in conjunction |
|
| 85 | + // with session authentication since this enables CSRF attack vectors |
|
| 86 | + if ($this->reflector->hasAnnotation('CORS') && |
|
| 87 | + !$this->reflector->hasAnnotation('PublicPage')) { |
|
| 88 | + $user = $this->request->server['PHP_AUTH_USER']; |
|
| 89 | + $pass = $this->request->server['PHP_AUTH_PW']; |
|
| 90 | 90 | |
| 91 | - $this->session->logout(); |
|
| 92 | - try { |
|
| 93 | - if (!$this->session->logClientIn($user, $pass, $this->request, $this->throttler)) { |
|
| 94 | - throw new SecurityException('CORS requires basic auth', Http::STATUS_UNAUTHORIZED); |
|
| 95 | - } |
|
| 96 | - } catch (PasswordLoginForbiddenException $ex) { |
|
| 97 | - throw new SecurityException('Password login forbidden, use token instead', Http::STATUS_UNAUTHORIZED); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - } |
|
| 91 | + $this->session->logout(); |
|
| 92 | + try { |
|
| 93 | + if (!$this->session->logClientIn($user, $pass, $this->request, $this->throttler)) { |
|
| 94 | + throw new SecurityException('CORS requires basic auth', Http::STATUS_UNAUTHORIZED); |
|
| 95 | + } |
|
| 96 | + } catch (PasswordLoginForbiddenException $ex) { |
|
| 97 | + throw new SecurityException('Password login forbidden, use token instead', Http::STATUS_UNAUTHORIZED); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * This is being run after a successful controllermethod call and allows |
|
| 104 | - * the manipulation of a Response object. The middleware is run in reverse order |
|
| 105 | - * |
|
| 106 | - * @param Controller $controller the controller that is being called |
|
| 107 | - * @param string $methodName the name of the method that will be called on |
|
| 108 | - * the controller |
|
| 109 | - * @param Response $response the generated response from the controller |
|
| 110 | - * @return Response a Response object |
|
| 111 | - * @throws SecurityException |
|
| 112 | - */ |
|
| 113 | - public function afterController($controller, $methodName, Response $response){ |
|
| 114 | - // only react if its a CORS request and if the request sends origin and |
|
| 102 | + /** |
|
| 103 | + * This is being run after a successful controllermethod call and allows |
|
| 104 | + * the manipulation of a Response object. The middleware is run in reverse order |
|
| 105 | + * |
|
| 106 | + * @param Controller $controller the controller that is being called |
|
| 107 | + * @param string $methodName the name of the method that will be called on |
|
| 108 | + * the controller |
|
| 109 | + * @param Response $response the generated response from the controller |
|
| 110 | + * @return Response a Response object |
|
| 111 | + * @throws SecurityException |
|
| 112 | + */ |
|
| 113 | + public function afterController($controller, $methodName, Response $response){ |
|
| 114 | + // only react if its a CORS request and if the request sends origin and |
|
| 115 | 115 | |
| 116 | - if(isset($this->request->server['HTTP_ORIGIN']) && |
|
| 117 | - $this->reflector->hasAnnotation('CORS')) { |
|
| 116 | + if(isset($this->request->server['HTTP_ORIGIN']) && |
|
| 117 | + $this->reflector->hasAnnotation('CORS')) { |
|
| 118 | 118 | |
| 119 | - // allow credentials headers must not be true or CSRF is possible |
|
| 120 | - // otherwise |
|
| 121 | - foreach($response->getHeaders() as $header => $value) { |
|
| 122 | - if(strtolower($header) === 'access-control-allow-credentials' && |
|
| 123 | - strtolower(trim($value)) === 'true') { |
|
| 124 | - $msg = 'Access-Control-Allow-Credentials must not be '. |
|
| 125 | - 'set to true in order to prevent CSRF'; |
|
| 126 | - throw new SecurityException($msg); |
|
| 127 | - } |
|
| 128 | - } |
|
| 119 | + // allow credentials headers must not be true or CSRF is possible |
|
| 120 | + // otherwise |
|
| 121 | + foreach($response->getHeaders() as $header => $value) { |
|
| 122 | + if(strtolower($header) === 'access-control-allow-credentials' && |
|
| 123 | + strtolower(trim($value)) === 'true') { |
|
| 124 | + $msg = 'Access-Control-Allow-Credentials must not be '. |
|
| 125 | + 'set to true in order to prevent CSRF'; |
|
| 126 | + throw new SecurityException($msg); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - $origin = $this->request->server['HTTP_ORIGIN']; |
|
| 131 | - $response->addHeader('Access-Control-Allow-Origin', $origin); |
|
| 132 | - } |
|
| 133 | - return $response; |
|
| 134 | - } |
|
| 130 | + $origin = $this->request->server['HTTP_ORIGIN']; |
|
| 131 | + $response->addHeader('Access-Control-Allow-Origin', $origin); |
|
| 132 | + } |
|
| 133 | + return $response; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * If an SecurityException is being caught return a JSON error response |
|
| 138 | - * |
|
| 139 | - * @param Controller $controller the controller that is being called |
|
| 140 | - * @param string $methodName the name of the method that will be called on |
|
| 141 | - * the controller |
|
| 142 | - * @param \Exception $exception the thrown exception |
|
| 143 | - * @throws \Exception the passed in exception if it can't handle it |
|
| 144 | - * @return Response a Response object or null in case that the exception could not be handled |
|
| 145 | - */ |
|
| 146 | - public function afterException($controller, $methodName, \Exception $exception){ |
|
| 147 | - if($exception instanceof SecurityException){ |
|
| 148 | - $response = new JSONResponse(['message' => $exception->getMessage()]); |
|
| 149 | - if($exception->getCode() !== 0) { |
|
| 150 | - $response->setStatus($exception->getCode()); |
|
| 151 | - } else { |
|
| 152 | - $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 153 | - } |
|
| 154 | - return $response; |
|
| 155 | - } |
|
| 136 | + /** |
|
| 137 | + * If an SecurityException is being caught return a JSON error response |
|
| 138 | + * |
|
| 139 | + * @param Controller $controller the controller that is being called |
|
| 140 | + * @param string $methodName the name of the method that will be called on |
|
| 141 | + * the controller |
|
| 142 | + * @param \Exception $exception the thrown exception |
|
| 143 | + * @throws \Exception the passed in exception if it can't handle it |
|
| 144 | + * @return Response a Response object or null in case that the exception could not be handled |
|
| 145 | + */ |
|
| 146 | + public function afterException($controller, $methodName, \Exception $exception){ |
|
| 147 | + if($exception instanceof SecurityException){ |
|
| 148 | + $response = new JSONResponse(['message' => $exception->getMessage()]); |
|
| 149 | + if($exception->getCode() !== 0) { |
|
| 150 | + $response->setStatus($exception->getCode()); |
|
| 151 | + } else { |
|
| 152 | + $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 153 | + } |
|
| 154 | + return $response; |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - throw $exception; |
|
| 158 | - } |
|
| 157 | + throw $exception; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | 160 | } |
@@ -80,11 +80,11 @@ discard block |
||
| 80 | 80 | * @throws SecurityException |
| 81 | 81 | * @since 6.0.0 |
| 82 | 82 | */ |
| 83 | - public function beforeController($controller, $methodName){ |
|
| 83 | + public function beforeController($controller, $methodName) { |
|
| 84 | 84 | // ensure that @CORS annotated API routes are not used in conjunction |
| 85 | 85 | // with session authentication since this enables CSRF attack vectors |
| 86 | 86 | if ($this->reflector->hasAnnotation('CORS') && |
| 87 | - !$this->reflector->hasAnnotation('PublicPage')) { |
|
| 87 | + !$this->reflector->hasAnnotation('PublicPage')) { |
|
| 88 | 88 | $user = $this->request->server['PHP_AUTH_USER']; |
| 89 | 89 | $pass = $this->request->server['PHP_AUTH_PW']; |
| 90 | 90 | |
@@ -110,16 +110,16 @@ discard block |
||
| 110 | 110 | * @return Response a Response object |
| 111 | 111 | * @throws SecurityException |
| 112 | 112 | */ |
| 113 | - public function afterController($controller, $methodName, Response $response){ |
|
| 113 | + public function afterController($controller, $methodName, Response $response) { |
|
| 114 | 114 | // only react if its a CORS request and if the request sends origin and |
| 115 | 115 | |
| 116 | - if(isset($this->request->server['HTTP_ORIGIN']) && |
|
| 116 | + if (isset($this->request->server['HTTP_ORIGIN']) && |
|
| 117 | 117 | $this->reflector->hasAnnotation('CORS')) { |
| 118 | 118 | |
| 119 | 119 | // allow credentials headers must not be true or CSRF is possible |
| 120 | 120 | // otherwise |
| 121 | - foreach($response->getHeaders() as $header => $value) { |
|
| 122 | - if(strtolower($header) === 'access-control-allow-credentials' && |
|
| 121 | + foreach ($response->getHeaders() as $header => $value) { |
|
| 122 | + if (strtolower($header) === 'access-control-allow-credentials' && |
|
| 123 | 123 | strtolower(trim($value)) === 'true') { |
| 124 | 124 | $msg = 'Access-Control-Allow-Credentials must not be '. |
| 125 | 125 | 'set to true in order to prevent CSRF'; |
@@ -143,10 +143,10 @@ discard block |
||
| 143 | 143 | * @throws \Exception the passed in exception if it can't handle it |
| 144 | 144 | * @return Response a Response object or null in case that the exception could not be handled |
| 145 | 145 | */ |
| 146 | - public function afterException($controller, $methodName, \Exception $exception){ |
|
| 147 | - if($exception instanceof SecurityException){ |
|
| 148 | - $response = new JSONResponse(['message' => $exception->getMessage()]); |
|
| 149 | - if($exception->getCode() !== 0) { |
|
| 146 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 147 | + if ($exception instanceof SecurityException) { |
|
| 148 | + $response = new JSONResponse(['message' => $exception->getMessage()]); |
|
| 149 | + if ($exception->getCode() !== 0) { |
|
| 150 | 150 | $response->setStatus($exception->getCode()); |
| 151 | 151 | } else { |
| 152 | 152 | $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); |
@@ -71,7 +71,7 @@ |
||
| 71 | 71 | * @param Response $response |
| 72 | 72 | * @return Response |
| 73 | 73 | */ |
| 74 | - public function afterController($controller, $methodName, Response $response){ |
|
| 74 | + public function afterController($controller, $methodName, Response $response) { |
|
| 75 | 75 | $useSession = $this->reflector->hasAnnotation('UseSession'); |
| 76 | 76 | if ($useSession) { |
| 77 | 77 | $this->session->close(); |
@@ -32,52 +32,52 @@ |
||
| 32 | 32 | |
| 33 | 33 | class SessionMiddleware extends Middleware { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var IRequest |
|
| 37 | - */ |
|
| 38 | - private $request; |
|
| 35 | + /** |
|
| 36 | + * @var IRequest |
|
| 37 | + */ |
|
| 38 | + private $request; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @var ControllerMethodReflector |
|
| 42 | - */ |
|
| 43 | - private $reflector; |
|
| 40 | + /** |
|
| 41 | + * @var ControllerMethodReflector |
|
| 42 | + */ |
|
| 43 | + private $reflector; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param IRequest $request |
|
| 47 | - * @param ControllerMethodReflector $reflector |
|
| 48 | - */ |
|
| 49 | - public function __construct(IRequest $request, |
|
| 50 | - ControllerMethodReflector $reflector, |
|
| 51 | - ISession $session |
|
| 45 | + /** |
|
| 46 | + * @param IRequest $request |
|
| 47 | + * @param ControllerMethodReflector $reflector |
|
| 48 | + */ |
|
| 49 | + public function __construct(IRequest $request, |
|
| 50 | + ControllerMethodReflector $reflector, |
|
| 51 | + ISession $session |
|
| 52 | 52 | ) { |
| 53 | - $this->request = $request; |
|
| 54 | - $this->reflector = $reflector; |
|
| 55 | - $this->session = $session; |
|
| 56 | - } |
|
| 53 | + $this->request = $request; |
|
| 54 | + $this->reflector = $reflector; |
|
| 55 | + $this->session = $session; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param Controller $controller |
|
| 60 | - * @param string $methodName |
|
| 61 | - */ |
|
| 62 | - public function beforeController($controller, $methodName) { |
|
| 63 | - $useSession = $this->reflector->hasAnnotation('UseSession'); |
|
| 64 | - if (!$useSession) { |
|
| 65 | - $this->session->close(); |
|
| 66 | - } |
|
| 67 | - } |
|
| 58 | + /** |
|
| 59 | + * @param Controller $controller |
|
| 60 | + * @param string $methodName |
|
| 61 | + */ |
|
| 62 | + public function beforeController($controller, $methodName) { |
|
| 63 | + $useSession = $this->reflector->hasAnnotation('UseSession'); |
|
| 64 | + if (!$useSession) { |
|
| 65 | + $this->session->close(); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @param Controller $controller |
|
| 71 | - * @param string $methodName |
|
| 72 | - * @param Response $response |
|
| 73 | - * @return Response |
|
| 74 | - */ |
|
| 75 | - public function afterController($controller, $methodName, Response $response){ |
|
| 76 | - $useSession = $this->reflector->hasAnnotation('UseSession'); |
|
| 77 | - if ($useSession) { |
|
| 78 | - $this->session->close(); |
|
| 79 | - } |
|
| 80 | - return $response; |
|
| 81 | - } |
|
| 69 | + /** |
|
| 70 | + * @param Controller $controller |
|
| 71 | + * @param string $methodName |
|
| 72 | + * @param Response $response |
|
| 73 | + * @return Response |
|
| 74 | + */ |
|
| 75 | + public function afterController($controller, $methodName, Response $response){ |
|
| 76 | + $useSession = $this->reflector->hasAnnotation('UseSession'); |
|
| 77 | + if ($useSession) { |
|
| 78 | + $this->session->close(); |
|
| 79 | + } |
|
| 80 | + return $response; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | 83 | } |
@@ -59,8 +59,8 @@ discard block |
||
| 59 | 59 | * @throws \Exception |
| 60 | 60 | */ |
| 61 | 61 | public function beforeController($controller, $methodName) { |
| 62 | - if(!$this->reflector->hasAnnotation('NoSubadminRequired')) { |
|
| 63 | - if(!$this->isSubAdmin) { |
|
| 62 | + if (!$this->reflector->hasAnnotation('NoSubadminRequired')) { |
|
| 63 | + if (!$this->isSubAdmin) { |
|
| 64 | 64 | throw new NotAdminException('Logged in user must be a subadmin'); |
| 65 | 65 | } |
| 66 | 66 | } |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | * @throws \Exception |
| 76 | 76 | */ |
| 77 | 77 | public function afterException($controller, $methodName, \Exception $exception) { |
| 78 | - if($exception instanceof NotAdminException) { |
|
| 78 | + if ($exception instanceof NotAdminException) { |
|
| 79 | 79 | $response = new TemplateResponse('core', '403', array(), 'guest'); |
| 80 | 80 | $response->setStatus(Http::STATUS_FORBIDDEN); |
| 81 | 81 | return $response; |
@@ -38,51 +38,51 @@ |
||
| 38 | 38 | * @package OC\Settings\Middleware |
| 39 | 39 | */ |
| 40 | 40 | class SubadminMiddleware extends Middleware { |
| 41 | - /** @var bool */ |
|
| 42 | - protected $isSubAdmin; |
|
| 43 | - /** @var ControllerMethodReflector */ |
|
| 44 | - protected $reflector; |
|
| 41 | + /** @var bool */ |
|
| 42 | + protected $isSubAdmin; |
|
| 43 | + /** @var ControllerMethodReflector */ |
|
| 44 | + protected $reflector; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param ControllerMethodReflector $reflector |
|
| 48 | - * @param bool $isSubAdmin |
|
| 49 | - */ |
|
| 50 | - public function __construct(ControllerMethodReflector $reflector, |
|
| 51 | - $isSubAdmin) { |
|
| 52 | - $this->reflector = $reflector; |
|
| 53 | - $this->isSubAdmin = $isSubAdmin; |
|
| 54 | - } |
|
| 46 | + /** |
|
| 47 | + * @param ControllerMethodReflector $reflector |
|
| 48 | + * @param bool $isSubAdmin |
|
| 49 | + */ |
|
| 50 | + public function __construct(ControllerMethodReflector $reflector, |
|
| 51 | + $isSubAdmin) { |
|
| 52 | + $this->reflector = $reflector; |
|
| 53 | + $this->isSubAdmin = $isSubAdmin; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Check if sharing is enabled before the controllers is executed |
|
| 58 | - * @param Controller $controller |
|
| 59 | - * @param string $methodName |
|
| 60 | - * @throws \Exception |
|
| 61 | - */ |
|
| 62 | - public function beforeController($controller, $methodName) { |
|
| 63 | - if(!$this->reflector->hasAnnotation('NoSubadminRequired')) { |
|
| 64 | - if(!$this->isSubAdmin) { |
|
| 65 | - throw new NotAdminException('Logged in user must be a subadmin'); |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - } |
|
| 56 | + /** |
|
| 57 | + * Check if sharing is enabled before the controllers is executed |
|
| 58 | + * @param Controller $controller |
|
| 59 | + * @param string $methodName |
|
| 60 | + * @throws \Exception |
|
| 61 | + */ |
|
| 62 | + public function beforeController($controller, $methodName) { |
|
| 63 | + if(!$this->reflector->hasAnnotation('NoSubadminRequired')) { |
|
| 64 | + if(!$this->isSubAdmin) { |
|
| 65 | + throw new NotAdminException('Logged in user must be a subadmin'); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Return 403 page in case of an exception |
|
| 72 | - * @param Controller $controller |
|
| 73 | - * @param string $methodName |
|
| 74 | - * @param \Exception $exception |
|
| 75 | - * @return TemplateResponse |
|
| 76 | - * @throws \Exception |
|
| 77 | - */ |
|
| 78 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 79 | - if($exception instanceof NotAdminException) { |
|
| 80 | - $response = new TemplateResponse('core', '403', array(), 'guest'); |
|
| 81 | - $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 82 | - return $response; |
|
| 83 | - } |
|
| 70 | + /** |
|
| 71 | + * Return 403 page in case of an exception |
|
| 72 | + * @param Controller $controller |
|
| 73 | + * @param string $methodName |
|
| 74 | + * @param \Exception $exception |
|
| 75 | + * @return TemplateResponse |
|
| 76 | + * @throws \Exception |
|
| 77 | + */ |
|
| 78 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 79 | + if($exception instanceof NotAdminException) { |
|
| 80 | + $response = new TemplateResponse('core', '403', array(), 'guest'); |
|
| 81 | + $response->setStatus(Http::STATUS_FORBIDDEN); |
|
| 82 | + return $response; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - throw $exception; |
|
| 86 | - } |
|
| 85 | + throw $exception; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | 88 | } |
@@ -48,87 +48,87 @@ |
||
| 48 | 48 | * @package OC\AppFramework\Middleware\Security |
| 49 | 49 | */ |
| 50 | 50 | class RateLimitingMiddleware extends Middleware { |
| 51 | - /** @var IRequest $request */ |
|
| 52 | - private $request; |
|
| 53 | - /** @var IUserSession */ |
|
| 54 | - private $userSession; |
|
| 55 | - /** @var ControllerMethodReflector */ |
|
| 56 | - private $reflector; |
|
| 57 | - /** @var Limiter */ |
|
| 58 | - private $limiter; |
|
| 51 | + /** @var IRequest $request */ |
|
| 52 | + private $request; |
|
| 53 | + /** @var IUserSession */ |
|
| 54 | + private $userSession; |
|
| 55 | + /** @var ControllerMethodReflector */ |
|
| 56 | + private $reflector; |
|
| 57 | + /** @var Limiter */ |
|
| 58 | + private $limiter; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @param IRequest $request |
|
| 62 | - * @param IUserSession $userSession |
|
| 63 | - * @param ControllerMethodReflector $reflector |
|
| 64 | - * @param Limiter $limiter |
|
| 65 | - */ |
|
| 66 | - public function __construct(IRequest $request, |
|
| 67 | - IUserSession $userSession, |
|
| 68 | - ControllerMethodReflector $reflector, |
|
| 69 | - Limiter $limiter) { |
|
| 70 | - $this->request = $request; |
|
| 71 | - $this->userSession = $userSession; |
|
| 72 | - $this->reflector = $reflector; |
|
| 73 | - $this->limiter = $limiter; |
|
| 74 | - } |
|
| 60 | + /** |
|
| 61 | + * @param IRequest $request |
|
| 62 | + * @param IUserSession $userSession |
|
| 63 | + * @param ControllerMethodReflector $reflector |
|
| 64 | + * @param Limiter $limiter |
|
| 65 | + */ |
|
| 66 | + public function __construct(IRequest $request, |
|
| 67 | + IUserSession $userSession, |
|
| 68 | + ControllerMethodReflector $reflector, |
|
| 69 | + Limiter $limiter) { |
|
| 70 | + $this->request = $request; |
|
| 71 | + $this->userSession = $userSession; |
|
| 72 | + $this->reflector = $reflector; |
|
| 73 | + $this->limiter = $limiter; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * {@inheritDoc} |
|
| 78 | - * @throws RateLimitExceededException |
|
| 79 | - */ |
|
| 80 | - public function beforeController($controller, $methodName) { |
|
| 81 | - parent::beforeController($controller, $methodName); |
|
| 76 | + /** |
|
| 77 | + * {@inheritDoc} |
|
| 78 | + * @throws RateLimitExceededException |
|
| 79 | + */ |
|
| 80 | + public function beforeController($controller, $methodName) { |
|
| 81 | + parent::beforeController($controller, $methodName); |
|
| 82 | 82 | |
| 83 | - $anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit'); |
|
| 84 | - $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period'); |
|
| 85 | - $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit'); |
|
| 86 | - $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period'); |
|
| 87 | - $rateLimitIdentifier = get_class($controller) . '::' . $methodName; |
|
| 88 | - if($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
|
| 89 | - $this->limiter->registerUserRequest( |
|
| 90 | - $rateLimitIdentifier, |
|
| 91 | - $userLimit, |
|
| 92 | - $userPeriod, |
|
| 93 | - $this->userSession->getUser() |
|
| 94 | - ); |
|
| 95 | - } elseif ($anonLimit !== '' && $anonPeriod !== '') { |
|
| 96 | - $this->limiter->registerAnonRequest( |
|
| 97 | - $rateLimitIdentifier, |
|
| 98 | - $anonLimit, |
|
| 99 | - $anonPeriod, |
|
| 100 | - $this->request->getRemoteAddress() |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - } |
|
| 83 | + $anonLimit = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'limit'); |
|
| 84 | + $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period'); |
|
| 85 | + $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit'); |
|
| 86 | + $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period'); |
|
| 87 | + $rateLimitIdentifier = get_class($controller) . '::' . $methodName; |
|
| 88 | + if($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
|
| 89 | + $this->limiter->registerUserRequest( |
|
| 90 | + $rateLimitIdentifier, |
|
| 91 | + $userLimit, |
|
| 92 | + $userPeriod, |
|
| 93 | + $this->userSession->getUser() |
|
| 94 | + ); |
|
| 95 | + } elseif ($anonLimit !== '' && $anonPeriod !== '') { |
|
| 96 | + $this->limiter->registerAnonRequest( |
|
| 97 | + $rateLimitIdentifier, |
|
| 98 | + $anonLimit, |
|
| 99 | + $anonPeriod, |
|
| 100 | + $this->request->getRemoteAddress() |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * {@inheritDoc} |
|
| 107 | - */ |
|
| 108 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 109 | - if($exception instanceof RateLimitExceededException) { |
|
| 110 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
| 111 | - $response = new JSONResponse( |
|
| 112 | - [ |
|
| 113 | - 'message' => $exception->getMessage(), |
|
| 114 | - ], |
|
| 115 | - $exception->getCode() |
|
| 116 | - ); |
|
| 117 | - } else { |
|
| 118 | - $response = new TemplateResponse( |
|
| 119 | - 'core', |
|
| 120 | - '403', |
|
| 121 | - [ |
|
| 122 | - 'file' => $exception->getMessage() |
|
| 123 | - ], |
|
| 124 | - 'guest' |
|
| 125 | - ); |
|
| 126 | - $response->setStatus($exception->getCode()); |
|
| 127 | - } |
|
| 105 | + /** |
|
| 106 | + * {@inheritDoc} |
|
| 107 | + */ |
|
| 108 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 109 | + if($exception instanceof RateLimitExceededException) { |
|
| 110 | + if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
| 111 | + $response = new JSONResponse( |
|
| 112 | + [ |
|
| 113 | + 'message' => $exception->getMessage(), |
|
| 114 | + ], |
|
| 115 | + $exception->getCode() |
|
| 116 | + ); |
|
| 117 | + } else { |
|
| 118 | + $response = new TemplateResponse( |
|
| 119 | + 'core', |
|
| 120 | + '403', |
|
| 121 | + [ |
|
| 122 | + 'file' => $exception->getMessage() |
|
| 123 | + ], |
|
| 124 | + 'guest' |
|
| 125 | + ); |
|
| 126 | + $response->setStatus($exception->getCode()); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - return $response; |
|
| 130 | - } |
|
| 129 | + return $response; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - throw $exception; |
|
| 133 | - } |
|
| 132 | + throw $exception; |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -84,8 +84,8 @@ discard block |
||
| 84 | 84 | $anonPeriod = $this->reflector->getAnnotationParameter('AnonRateThrottle', 'period'); |
| 85 | 85 | $userLimit = $this->reflector->getAnnotationParameter('UserRateThrottle', 'limit'); |
| 86 | 86 | $userPeriod = $this->reflector->getAnnotationParameter('UserRateThrottle', 'period'); |
| 87 | - $rateLimitIdentifier = get_class($controller) . '::' . $methodName; |
|
| 88 | - if($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
|
| 87 | + $rateLimitIdentifier = get_class($controller).'::'.$methodName; |
|
| 88 | + if ($userLimit !== '' && $userPeriod !== '' && $this->userSession->isLoggedIn()) { |
|
| 89 | 89 | $this->limiter->registerUserRequest( |
| 90 | 90 | $rateLimitIdentifier, |
| 91 | 91 | $userLimit, |
@@ -106,8 +106,8 @@ discard block |
||
| 106 | 106 | * {@inheritDoc} |
| 107 | 107 | */ |
| 108 | 108 | public function afterException($controller, $methodName, \Exception $exception) { |
| 109 | - if($exception instanceof RateLimitExceededException) { |
|
| 110 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
| 109 | + if ($exception instanceof RateLimitExceededException) { |
|
| 110 | + if (stripos($this->request->getHeader('Accept'), 'html') === false) { |
|
| 111 | 111 | $response = new JSONResponse( |
| 112 | 112 | [ |
| 113 | 113 | 'message' => $exception->getMessage(), |
@@ -24,7 +24,6 @@ |
||
| 24 | 24 | use OC\AppFramework\Utility\ControllerMethodReflector; |
| 25 | 25 | use OC\Security\RateLimiting\Exception\RateLimitExceededException; |
| 26 | 26 | use OC\Security\RateLimiting\Limiter; |
| 27 | -use OCP\AppFramework\Controller; |
|
| 28 | 27 | use OCP\AppFramework\Http\JSONResponse; |
| 29 | 28 | use OCP\AppFramework\Http\TemplateResponse; |
| 30 | 29 | use OCP\AppFramework\Middleware; |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | public function beforeController($controller, $methodName) { |
| 62 | 62 | parent::beforeController($controller, $methodName); |
| 63 | 63 | |
| 64 | - if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
| 64 | + if ($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
| 65 | 65 | $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
| 66 | 66 | $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
| 67 | 67 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * {@inheritDoc} |
| 72 | 72 | */ |
| 73 | 73 | public function afterController($controller, $methodName, Response $response) { |
| 74 | - if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
| 74 | + if ($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
| 75 | 75 | $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
| 76 | 76 | $ip = $this->request->getRemoteAddress(); |
| 77 | 77 | $this->throttler->sleepDelay($ip, $action); |
@@ -35,49 +35,49 @@ |
||
| 35 | 35 | * @package OC\AppFramework\Middleware\Security |
| 36 | 36 | */ |
| 37 | 37 | class BruteForceMiddleware extends Middleware { |
| 38 | - /** @var ControllerMethodReflector */ |
|
| 39 | - private $reflector; |
|
| 40 | - /** @var Throttler */ |
|
| 41 | - private $throttler; |
|
| 42 | - /** @var IRequest */ |
|
| 43 | - private $request; |
|
| 38 | + /** @var ControllerMethodReflector */ |
|
| 39 | + private $reflector; |
|
| 40 | + /** @var Throttler */ |
|
| 41 | + private $throttler; |
|
| 42 | + /** @var IRequest */ |
|
| 43 | + private $request; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param ControllerMethodReflector $controllerMethodReflector |
|
| 47 | - * @param Throttler $throttler |
|
| 48 | - * @param IRequest $request |
|
| 49 | - */ |
|
| 50 | - public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
| 51 | - Throttler $throttler, |
|
| 52 | - IRequest $request) { |
|
| 53 | - $this->reflector = $controllerMethodReflector; |
|
| 54 | - $this->throttler = $throttler; |
|
| 55 | - $this->request = $request; |
|
| 56 | - } |
|
| 45 | + /** |
|
| 46 | + * @param ControllerMethodReflector $controllerMethodReflector |
|
| 47 | + * @param Throttler $throttler |
|
| 48 | + * @param IRequest $request |
|
| 49 | + */ |
|
| 50 | + public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
| 51 | + Throttler $throttler, |
|
| 52 | + IRequest $request) { |
|
| 53 | + $this->reflector = $controllerMethodReflector; |
|
| 54 | + $this->throttler = $throttler; |
|
| 55 | + $this->request = $request; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * {@inheritDoc} |
|
| 60 | - */ |
|
| 61 | - public function beforeController($controller, $methodName) { |
|
| 62 | - parent::beforeController($controller, $methodName); |
|
| 58 | + /** |
|
| 59 | + * {@inheritDoc} |
|
| 60 | + */ |
|
| 61 | + public function beforeController($controller, $methodName) { |
|
| 62 | + parent::beforeController($controller, $methodName); |
|
| 63 | 63 | |
| 64 | - if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
| 65 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 66 | - $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
| 67 | - } |
|
| 68 | - } |
|
| 64 | + if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
| 65 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 66 | + $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * {@inheritDoc} |
|
| 72 | - */ |
|
| 73 | - public function afterController($controller, $methodName, Response $response) { |
|
| 74 | - if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
| 75 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 76 | - $ip = $this->request->getRemoteAddress(); |
|
| 77 | - $this->throttler->sleepDelay($ip, $action); |
|
| 78 | - $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata()); |
|
| 79 | - } |
|
| 70 | + /** |
|
| 71 | + * {@inheritDoc} |
|
| 72 | + */ |
|
| 73 | + public function afterController($controller, $methodName, Response $response) { |
|
| 74 | + if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
| 75 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 76 | + $ip = $this->request->getRemoteAddress(); |
|
| 77 | + $this->throttler->sleepDelay($ip, $action); |
|
| 78 | + $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata()); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - return parent::afterController($controller, $methodName, $response); |
|
| 82 | - } |
|
| 81 | + return parent::afterController($controller, $methodName, $response); |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -63,206 +63,206 @@ |
||
| 63 | 63 | * check fails |
| 64 | 64 | */ |
| 65 | 65 | class SecurityMiddleware extends Middleware { |
| 66 | - /** @var INavigationManager */ |
|
| 67 | - private $navigationManager; |
|
| 68 | - /** @var IRequest */ |
|
| 69 | - private $request; |
|
| 70 | - /** @var ControllerMethodReflector */ |
|
| 71 | - private $reflector; |
|
| 72 | - /** @var string */ |
|
| 73 | - private $appName; |
|
| 74 | - /** @var IURLGenerator */ |
|
| 75 | - private $urlGenerator; |
|
| 76 | - /** @var ILogger */ |
|
| 77 | - private $logger; |
|
| 78 | - /** @var ISession */ |
|
| 79 | - private $session; |
|
| 80 | - /** @var bool */ |
|
| 81 | - private $isLoggedIn; |
|
| 82 | - /** @var bool */ |
|
| 83 | - private $isAdminUser; |
|
| 84 | - /** @var ContentSecurityPolicyManager */ |
|
| 85 | - private $contentSecurityPolicyManager; |
|
| 86 | - /** @var CsrfTokenManager */ |
|
| 87 | - private $csrfTokenManager; |
|
| 88 | - /** @var ContentSecurityPolicyNonceManager */ |
|
| 89 | - private $cspNonceManager; |
|
| 66 | + /** @var INavigationManager */ |
|
| 67 | + private $navigationManager; |
|
| 68 | + /** @var IRequest */ |
|
| 69 | + private $request; |
|
| 70 | + /** @var ControllerMethodReflector */ |
|
| 71 | + private $reflector; |
|
| 72 | + /** @var string */ |
|
| 73 | + private $appName; |
|
| 74 | + /** @var IURLGenerator */ |
|
| 75 | + private $urlGenerator; |
|
| 76 | + /** @var ILogger */ |
|
| 77 | + private $logger; |
|
| 78 | + /** @var ISession */ |
|
| 79 | + private $session; |
|
| 80 | + /** @var bool */ |
|
| 81 | + private $isLoggedIn; |
|
| 82 | + /** @var bool */ |
|
| 83 | + private $isAdminUser; |
|
| 84 | + /** @var ContentSecurityPolicyManager */ |
|
| 85 | + private $contentSecurityPolicyManager; |
|
| 86 | + /** @var CsrfTokenManager */ |
|
| 87 | + private $csrfTokenManager; |
|
| 88 | + /** @var ContentSecurityPolicyNonceManager */ |
|
| 89 | + private $cspNonceManager; |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * @param IRequest $request |
|
| 93 | - * @param ControllerMethodReflector $reflector |
|
| 94 | - * @param INavigationManager $navigationManager |
|
| 95 | - * @param IURLGenerator $urlGenerator |
|
| 96 | - * @param ILogger $logger |
|
| 97 | - * @param ISession $session |
|
| 98 | - * @param string $appName |
|
| 99 | - * @param bool $isLoggedIn |
|
| 100 | - * @param bool $isAdminUser |
|
| 101 | - * @param ContentSecurityPolicyManager $contentSecurityPolicyManager |
|
| 102 | - * @param CSRFTokenManager $csrfTokenManager |
|
| 103 | - * @param ContentSecurityPolicyNonceManager $cspNonceManager |
|
| 104 | - */ |
|
| 105 | - public function __construct(IRequest $request, |
|
| 106 | - ControllerMethodReflector $reflector, |
|
| 107 | - INavigationManager $navigationManager, |
|
| 108 | - IURLGenerator $urlGenerator, |
|
| 109 | - ILogger $logger, |
|
| 110 | - ISession $session, |
|
| 111 | - $appName, |
|
| 112 | - $isLoggedIn, |
|
| 113 | - $isAdminUser, |
|
| 114 | - ContentSecurityPolicyManager $contentSecurityPolicyManager, |
|
| 115 | - CsrfTokenManager $csrfTokenManager, |
|
| 116 | - ContentSecurityPolicyNonceManager $cspNonceManager) { |
|
| 117 | - $this->navigationManager = $navigationManager; |
|
| 118 | - $this->request = $request; |
|
| 119 | - $this->reflector = $reflector; |
|
| 120 | - $this->appName = $appName; |
|
| 121 | - $this->urlGenerator = $urlGenerator; |
|
| 122 | - $this->logger = $logger; |
|
| 123 | - $this->session = $session; |
|
| 124 | - $this->isLoggedIn = $isLoggedIn; |
|
| 125 | - $this->isAdminUser = $isAdminUser; |
|
| 126 | - $this->contentSecurityPolicyManager = $contentSecurityPolicyManager; |
|
| 127 | - $this->csrfTokenManager = $csrfTokenManager; |
|
| 128 | - $this->cspNonceManager = $cspNonceManager; |
|
| 129 | - } |
|
| 91 | + /** |
|
| 92 | + * @param IRequest $request |
|
| 93 | + * @param ControllerMethodReflector $reflector |
|
| 94 | + * @param INavigationManager $navigationManager |
|
| 95 | + * @param IURLGenerator $urlGenerator |
|
| 96 | + * @param ILogger $logger |
|
| 97 | + * @param ISession $session |
|
| 98 | + * @param string $appName |
|
| 99 | + * @param bool $isLoggedIn |
|
| 100 | + * @param bool $isAdminUser |
|
| 101 | + * @param ContentSecurityPolicyManager $contentSecurityPolicyManager |
|
| 102 | + * @param CSRFTokenManager $csrfTokenManager |
|
| 103 | + * @param ContentSecurityPolicyNonceManager $cspNonceManager |
|
| 104 | + */ |
|
| 105 | + public function __construct(IRequest $request, |
|
| 106 | + ControllerMethodReflector $reflector, |
|
| 107 | + INavigationManager $navigationManager, |
|
| 108 | + IURLGenerator $urlGenerator, |
|
| 109 | + ILogger $logger, |
|
| 110 | + ISession $session, |
|
| 111 | + $appName, |
|
| 112 | + $isLoggedIn, |
|
| 113 | + $isAdminUser, |
|
| 114 | + ContentSecurityPolicyManager $contentSecurityPolicyManager, |
|
| 115 | + CsrfTokenManager $csrfTokenManager, |
|
| 116 | + ContentSecurityPolicyNonceManager $cspNonceManager) { |
|
| 117 | + $this->navigationManager = $navigationManager; |
|
| 118 | + $this->request = $request; |
|
| 119 | + $this->reflector = $reflector; |
|
| 120 | + $this->appName = $appName; |
|
| 121 | + $this->urlGenerator = $urlGenerator; |
|
| 122 | + $this->logger = $logger; |
|
| 123 | + $this->session = $session; |
|
| 124 | + $this->isLoggedIn = $isLoggedIn; |
|
| 125 | + $this->isAdminUser = $isAdminUser; |
|
| 126 | + $this->contentSecurityPolicyManager = $contentSecurityPolicyManager; |
|
| 127 | + $this->csrfTokenManager = $csrfTokenManager; |
|
| 128 | + $this->cspNonceManager = $cspNonceManager; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * This runs all the security checks before a method call. The |
|
| 133 | - * security checks are determined by inspecting the controller method |
|
| 134 | - * annotations |
|
| 135 | - * @param Controller $controller the controller |
|
| 136 | - * @param string $methodName the name of the method |
|
| 137 | - * @throws SecurityException when a security check fails |
|
| 138 | - */ |
|
| 139 | - public function beforeController($controller, $methodName) { |
|
| 131 | + /** |
|
| 132 | + * This runs all the security checks before a method call. The |
|
| 133 | + * security checks are determined by inspecting the controller method |
|
| 134 | + * annotations |
|
| 135 | + * @param Controller $controller the controller |
|
| 136 | + * @param string $methodName the name of the method |
|
| 137 | + * @throws SecurityException when a security check fails |
|
| 138 | + */ |
|
| 139 | + public function beforeController($controller, $methodName) { |
|
| 140 | 140 | |
| 141 | - // this will set the current navigation entry of the app, use this only |
|
| 142 | - // for normal HTML requests and not for AJAX requests |
|
| 143 | - $this->navigationManager->setActiveEntry($this->appName); |
|
| 141 | + // this will set the current navigation entry of the app, use this only |
|
| 142 | + // for normal HTML requests and not for AJAX requests |
|
| 143 | + $this->navigationManager->setActiveEntry($this->appName); |
|
| 144 | 144 | |
| 145 | - // security checks |
|
| 146 | - $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
|
| 147 | - if(!$isPublicPage) { |
|
| 148 | - if(!$this->isLoggedIn) { |
|
| 149 | - throw new NotLoggedInException(); |
|
| 150 | - } |
|
| 145 | + // security checks |
|
| 146 | + $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
|
| 147 | + if(!$isPublicPage) { |
|
| 148 | + if(!$this->isLoggedIn) { |
|
| 149 | + throw new NotLoggedInException(); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - if(!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
| 153 | - if(!$this->isAdminUser) { |
|
| 154 | - throw new NotAdminException(); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 152 | + if(!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
| 153 | + if(!$this->isAdminUser) { |
|
| 154 | + throw new NotAdminException(); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
| 160 | - $lastConfirm = (int) $this->session->get('last-password-confirm'); |
|
| 161 | - if ($lastConfirm < (time() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
| 162 | - throw new NotConfirmedException(); |
|
| 163 | - } |
|
| 164 | - } |
|
| 159 | + if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
| 160 | + $lastConfirm = (int) $this->session->get('last-password-confirm'); |
|
| 161 | + if ($lastConfirm < (time() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
| 162 | + throw new NotConfirmedException(); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - // Check for strict cookie requirement |
|
| 167 | - if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 168 | - if(!$this->request->passesStrictCookieCheck()) { |
|
| 169 | - throw new StrictCookieMissingException(); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - // CSRF check - also registers the CSRF token since the session may be closed later |
|
| 173 | - Util::callRegister(); |
|
| 174 | - if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 175 | - /* |
|
| 166 | + // Check for strict cookie requirement |
|
| 167 | + if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 168 | + if(!$this->request->passesStrictCookieCheck()) { |
|
| 169 | + throw new StrictCookieMissingException(); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + // CSRF check - also registers the CSRF token since the session may be closed later |
|
| 173 | + Util::callRegister(); |
|
| 174 | + if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 175 | + /* |
|
| 176 | 176 | * Only allow the CSRF check to fail on OCS Requests. This kind of |
| 177 | 177 | * hacks around that we have no full token auth in place yet and we |
| 178 | 178 | * do want to offer CSRF checks for web requests. |
| 179 | 179 | */ |
| 180 | - if(!$this->request->passesCSRFCheck() && !( |
|
| 181 | - $controller instanceof OCSController && |
|
| 182 | - $this->request->getHeader('OCS-APIREQUEST') === 'true')) { |
|
| 183 | - throw new CrossSiteRequestForgeryException(); |
|
| 184 | - } |
|
| 185 | - } |
|
| 180 | + if(!$this->request->passesCSRFCheck() && !( |
|
| 181 | + $controller instanceof OCSController && |
|
| 182 | + $this->request->getHeader('OCS-APIREQUEST') === 'true')) { |
|
| 183 | + throw new CrossSiteRequestForgeryException(); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * FIXME: Use DI once available |
|
| 189 | - * Checks if app is enabled (also includes a check whether user is allowed to access the resource) |
|
| 190 | - * The getAppPath() check is here since components such as settings also use the AppFramework and |
|
| 191 | - * therefore won't pass this check. |
|
| 192 | - */ |
|
| 193 | - if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
| 194 | - throw new AppNotEnabledException(); |
|
| 195 | - } |
|
| 187 | + /** |
|
| 188 | + * FIXME: Use DI once available |
|
| 189 | + * Checks if app is enabled (also includes a check whether user is allowed to access the resource) |
|
| 190 | + * The getAppPath() check is here since components such as settings also use the AppFramework and |
|
| 191 | + * therefore won't pass this check. |
|
| 192 | + */ |
|
| 193 | + if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
| 194 | + throw new AppNotEnabledException(); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - } |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Performs the default CSP modifications that may be injected by other |
|
| 201 | - * applications |
|
| 202 | - * |
|
| 203 | - * @param Controller $controller |
|
| 204 | - * @param string $methodName |
|
| 205 | - * @param Response $response |
|
| 206 | - * @return Response |
|
| 207 | - */ |
|
| 208 | - public function afterController($controller, $methodName, Response $response) { |
|
| 209 | - $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy(); |
|
| 199 | + /** |
|
| 200 | + * Performs the default CSP modifications that may be injected by other |
|
| 201 | + * applications |
|
| 202 | + * |
|
| 203 | + * @param Controller $controller |
|
| 204 | + * @param string $methodName |
|
| 205 | + * @param Response $response |
|
| 206 | + * @return Response |
|
| 207 | + */ |
|
| 208 | + public function afterController($controller, $methodName, Response $response) { |
|
| 209 | + $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy(); |
|
| 210 | 210 | |
| 211 | - if (get_class($policy) === EmptyContentSecurityPolicy::class) { |
|
| 212 | - return $response; |
|
| 213 | - } |
|
| 211 | + if (get_class($policy) === EmptyContentSecurityPolicy::class) { |
|
| 212 | + return $response; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
|
| 216 | - $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
|
| 215 | + $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
|
| 216 | + $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
|
| 217 | 217 | |
| 218 | - if($this->cspNonceManager->browserSupportsCspV3()) { |
|
| 219 | - $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
| 220 | - } |
|
| 218 | + if($this->cspNonceManager->browserSupportsCspV3()) { |
|
| 219 | + $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - $response->setContentSecurityPolicy($defaultPolicy); |
|
| 222 | + $response->setContentSecurityPolicy($defaultPolicy); |
|
| 223 | 223 | |
| 224 | - return $response; |
|
| 225 | - } |
|
| 224 | + return $response; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - /** |
|
| 228 | - * If an SecurityException is being caught, ajax requests return a JSON error |
|
| 229 | - * response and non ajax requests redirect to the index |
|
| 230 | - * @param Controller $controller the controller that is being called |
|
| 231 | - * @param string $methodName the name of the method that will be called on |
|
| 232 | - * the controller |
|
| 233 | - * @param \Exception $exception the thrown exception |
|
| 234 | - * @throws \Exception the passed in exception if it can't handle it |
|
| 235 | - * @return Response a Response object or null in case that the exception could not be handled |
|
| 236 | - */ |
|
| 237 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 238 | - if($exception instanceof SecurityException) { |
|
| 239 | - if($exception instanceof StrictCookieMissingException) { |
|
| 240 | - return new RedirectResponse(\OC::$WEBROOT); |
|
| 241 | - } |
|
| 242 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
| 243 | - $response = new JSONResponse( |
|
| 244 | - array('message' => $exception->getMessage()), |
|
| 245 | - $exception->getCode() |
|
| 246 | - ); |
|
| 247 | - } else { |
|
| 248 | - if($exception instanceof NotLoggedInException) { |
|
| 249 | - $params = []; |
|
| 250 | - if (isset($this->request->server['REQUEST_URI'])) { |
|
| 251 | - $params['redirect_url'] = $this->request->server['REQUEST_URI']; |
|
| 252 | - } |
|
| 253 | - $url = $this->urlGenerator->linkToRoute('core.login.showLoginForm', $params); |
|
| 254 | - $response = new RedirectResponse($url); |
|
| 255 | - } else { |
|
| 256 | - $response = new TemplateResponse('core', '403', ['file' => $exception->getMessage()], 'guest'); |
|
| 257 | - $response->setStatus($exception->getCode()); |
|
| 258 | - } |
|
| 259 | - } |
|
| 227 | + /** |
|
| 228 | + * If an SecurityException is being caught, ajax requests return a JSON error |
|
| 229 | + * response and non ajax requests redirect to the index |
|
| 230 | + * @param Controller $controller the controller that is being called |
|
| 231 | + * @param string $methodName the name of the method that will be called on |
|
| 232 | + * the controller |
|
| 233 | + * @param \Exception $exception the thrown exception |
|
| 234 | + * @throws \Exception the passed in exception if it can't handle it |
|
| 235 | + * @return Response a Response object or null in case that the exception could not be handled |
|
| 236 | + */ |
|
| 237 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 238 | + if($exception instanceof SecurityException) { |
|
| 239 | + if($exception instanceof StrictCookieMissingException) { |
|
| 240 | + return new RedirectResponse(\OC::$WEBROOT); |
|
| 241 | + } |
|
| 242 | + if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
| 243 | + $response = new JSONResponse( |
|
| 244 | + array('message' => $exception->getMessage()), |
|
| 245 | + $exception->getCode() |
|
| 246 | + ); |
|
| 247 | + } else { |
|
| 248 | + if($exception instanceof NotLoggedInException) { |
|
| 249 | + $params = []; |
|
| 250 | + if (isset($this->request->server['REQUEST_URI'])) { |
|
| 251 | + $params['redirect_url'] = $this->request->server['REQUEST_URI']; |
|
| 252 | + } |
|
| 253 | + $url = $this->urlGenerator->linkToRoute('core.login.showLoginForm', $params); |
|
| 254 | + $response = new RedirectResponse($url); |
|
| 255 | + } else { |
|
| 256 | + $response = new TemplateResponse('core', '403', ['file' => $exception->getMessage()], 'guest'); |
|
| 257 | + $response->setStatus($exception->getCode()); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - $this->logger->debug($exception->getMessage()); |
|
| 262 | - return $response; |
|
| 263 | - } |
|
| 261 | + $this->logger->debug($exception->getMessage()); |
|
| 262 | + return $response; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - throw $exception; |
|
| 266 | - } |
|
| 265 | + throw $exception; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | 268 | } |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | // security checks |
| 146 | 146 | $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
| 147 | - if(!$isPublicPage) { |
|
| 148 | - if(!$this->isLoggedIn) { |
|
| 147 | + if (!$isPublicPage) { |
|
| 148 | + if (!$this->isLoggedIn) { |
|
| 149 | 149 | throw new NotLoggedInException(); |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | - if(!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
| 153 | - if(!$this->isAdminUser) { |
|
| 152 | + if (!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
| 153 | + if (!$this->isAdminUser) { |
|
| 154 | 154 | throw new NotAdminException(); |
| 155 | 155 | } |
| 156 | 156 | } |
@@ -164,20 +164,20 @@ discard block |
||
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | // Check for strict cookie requirement |
| 167 | - if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 168 | - if(!$this->request->passesStrictCookieCheck()) { |
|
| 167 | + if ($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 168 | + if (!$this->request->passesStrictCookieCheck()) { |
|
| 169 | 169 | throw new StrictCookieMissingException(); |
| 170 | 170 | } |
| 171 | 171 | } |
| 172 | 172 | // CSRF check - also registers the CSRF token since the session may be closed later |
| 173 | 173 | Util::callRegister(); |
| 174 | - if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 174 | + if (!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
| 175 | 175 | /* |
| 176 | 176 | * Only allow the CSRF check to fail on OCS Requests. This kind of |
| 177 | 177 | * hacks around that we have no full token auth in place yet and we |
| 178 | 178 | * do want to offer CSRF checks for web requests. |
| 179 | 179 | */ |
| 180 | - if(!$this->request->passesCSRFCheck() && !( |
|
| 180 | + if (!$this->request->passesCSRFCheck() && !( |
|
| 181 | 181 | $controller instanceof OCSController && |
| 182 | 182 | $this->request->getHeader('OCS-APIREQUEST') === 'true')) { |
| 183 | 183 | throw new CrossSiteRequestForgeryException(); |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * The getAppPath() check is here since components such as settings also use the AppFramework and |
| 191 | 191 | * therefore won't pass this check. |
| 192 | 192 | */ |
| 193 | - if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
| 193 | + if (\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
| 194 | 194 | throw new AppNotEnabledException(); |
| 195 | 195 | } |
| 196 | 196 | |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
| 216 | 216 | $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
| 217 | 217 | |
| 218 | - if($this->cspNonceManager->browserSupportsCspV3()) { |
|
| 218 | + if ($this->cspNonceManager->browserSupportsCspV3()) { |
|
| 219 | 219 | $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
| 220 | 220 | } |
| 221 | 221 | |
@@ -235,17 +235,17 @@ discard block |
||
| 235 | 235 | * @return Response a Response object or null in case that the exception could not be handled |
| 236 | 236 | */ |
| 237 | 237 | public function afterException($controller, $methodName, \Exception $exception) { |
| 238 | - if($exception instanceof SecurityException) { |
|
| 239 | - if($exception instanceof StrictCookieMissingException) { |
|
| 238 | + if ($exception instanceof SecurityException) { |
|
| 239 | + if ($exception instanceof StrictCookieMissingException) { |
|
| 240 | 240 | return new RedirectResponse(\OC::$WEBROOT); |
| 241 | 241 | } |
| 242 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
| 242 | + if (stripos($this->request->getHeader('Accept'), 'html') === false) { |
|
| 243 | 243 | $response = new JSONResponse( |
| 244 | 244 | array('message' => $exception->getMessage()), |
| 245 | 245 | $exception->getCode() |
| 246 | 246 | ); |
| 247 | 247 | } else { |
| 248 | - if($exception instanceof NotLoggedInException) { |
|
| 248 | + if ($exception instanceof NotLoggedInException) { |
|
| 249 | 249 | $params = []; |
| 250 | 250 | if (isset($this->request->server['REQUEST_URI'])) { |
| 251 | 251 | $params['redirect_url'] = $this->request->server['REQUEST_URI']; |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * @throws ShareNotFound |
| 93 | 93 | */ |
| 94 | 94 | public function beforeController($controller, $methodName) { |
| 95 | - if(!$this->isSharingEnabled()) { |
|
| 95 | + if (!$this->isSharingEnabled()) { |
|
| 96 | 96 | throw new NotFoundException('Sharing is disabled.'); |
| 97 | 97 | } |
| 98 | 98 | |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @throws \Exception |
| 120 | 120 | */ |
| 121 | 121 | public function afterException($controller, $methodName, \Exception $exception) { |
| 122 | - if(is_a($exception, '\OCP\Files\NotFoundException')) { |
|
| 122 | + if (is_a($exception, '\OCP\Files\NotFoundException')) { |
|
| 123 | 123 | return new NotFoundResponse(); |
| 124 | 124 | } |
| 125 | 125 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | private function isSharingEnabled() { |
| 157 | 157 | // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app |
| 158 | 158 | // Check whether the sharing application is enabled |
| 159 | - if(!$this->appManager->isEnabledForUser($this->appName)) { |
|
| 159 | + if (!$this->appManager->isEnabledForUser($this->appName)) { |
|
| 160 | 160 | return false; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Check whether public sharing is enabled |
| 177 | - if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 177 | + if ($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 178 | 178 | return false; |
| 179 | 179 | } |
| 180 | 180 | |
@@ -47,20 +47,20 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | class SharingCheckMiddleware extends Middleware { |
| 49 | 49 | |
| 50 | - /** @var string */ |
|
| 51 | - protected $appName; |
|
| 52 | - /** @var IConfig */ |
|
| 53 | - protected $config; |
|
| 54 | - /** @var IAppManager */ |
|
| 55 | - protected $appManager; |
|
| 56 | - /** @var IControllerMethodReflector */ |
|
| 57 | - protected $reflector; |
|
| 58 | - /** @var IManager */ |
|
| 59 | - protected $shareManager; |
|
| 60 | - /** @var IRequest */ |
|
| 61 | - protected $request; |
|
| 62 | - |
|
| 63 | - /*** |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $appName; |
|
| 52 | + /** @var IConfig */ |
|
| 53 | + protected $config; |
|
| 54 | + /** @var IAppManager */ |
|
| 55 | + protected $appManager; |
|
| 56 | + /** @var IControllerMethodReflector */ |
|
| 57 | + protected $reflector; |
|
| 58 | + /** @var IManager */ |
|
| 59 | + protected $shareManager; |
|
| 60 | + /** @var IRequest */ |
|
| 61 | + protected $request; |
|
| 62 | + |
|
| 63 | + /*** |
|
| 64 | 64 | * @param string $appName |
| 65 | 65 | * @param IConfig $config |
| 66 | 66 | * @param IAppManager $appManager |
@@ -68,118 +68,118 @@ discard block |
||
| 68 | 68 | * @param IManager $shareManager |
| 69 | 69 | * @param IRequest $request |
| 70 | 70 | */ |
| 71 | - public function __construct($appName, |
|
| 72 | - IConfig $config, |
|
| 73 | - IAppManager $appManager, |
|
| 74 | - IControllerMethodReflector $reflector, |
|
| 75 | - IManager $shareManager, |
|
| 76 | - IRequest $request |
|
| 77 | - ) { |
|
| 78 | - $this->appName = $appName; |
|
| 79 | - $this->config = $config; |
|
| 80 | - $this->appManager = $appManager; |
|
| 81 | - $this->reflector = $reflector; |
|
| 82 | - $this->shareManager = $shareManager; |
|
| 83 | - $this->request = $request; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Check if sharing is enabled before the controllers is executed |
|
| 88 | - * |
|
| 89 | - * @param Controller $controller |
|
| 90 | - * @param string $methodName |
|
| 91 | - * @throws NotFoundException |
|
| 92 | - * @throws S2SException |
|
| 93 | - * @throws ShareNotFound |
|
| 94 | - */ |
|
| 95 | - public function beforeController($controller, $methodName) { |
|
| 96 | - if(!$this->isSharingEnabled()) { |
|
| 97 | - throw new NotFoundException('Sharing is disabled.'); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - if ($controller instanceof ExternalSharesController && |
|
| 101 | - !$this->externalSharesChecks()) { |
|
| 102 | - throw new S2SException('Federated sharing not allowed'); |
|
| 103 | - } else if ($controller instanceof ShareController) { |
|
| 104 | - $token = $this->request->getParam('token'); |
|
| 105 | - $share = $this->shareManager->getShareByToken($token); |
|
| 106 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK |
|
| 107 | - && !$this->isLinkSharingEnabled()) { |
|
| 108 | - throw new NotFoundException('Link sharing is disabled'); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Return 404 page in case of a not found exception |
|
| 115 | - * |
|
| 116 | - * @param Controller $controller |
|
| 117 | - * @param string $methodName |
|
| 118 | - * @param \Exception $exception |
|
| 119 | - * @return NotFoundResponse |
|
| 120 | - * @throws \Exception |
|
| 121 | - */ |
|
| 122 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 123 | - if(is_a($exception, '\OCP\Files\NotFoundException')) { |
|
| 124 | - return new NotFoundResponse(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (is_a($exception, '\OCA\Files_Sharing\Exceptions\S2SException')) { |
|
| 128 | - return new JSONResponse($exception->getMessage(), 405); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - throw $exception; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Checks for externalshares controller |
|
| 136 | - * @return bool |
|
| 137 | - */ |
|
| 138 | - private function externalSharesChecks() { |
|
| 139 | - |
|
| 140 | - if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') && |
|
| 141 | - $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') { |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') && |
|
| 146 | - $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') { |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return true; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Check whether sharing is enabled |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - private function isSharingEnabled() { |
|
| 158 | - // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app |
|
| 159 | - // Check whether the sharing application is enabled |
|
| 160 | - if(!$this->appManager->isEnabledForUser($this->appName)) { |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return true; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Check if link sharing is allowed |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - private function isLinkSharingEnabled() { |
|
| 172 | - // Check if the shareAPI is enabled |
|
| 173 | - if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // Check whether public sharing is enabled |
|
| 178 | - if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 179 | - return false; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return true; |
|
| 183 | - } |
|
| 71 | + public function __construct($appName, |
|
| 72 | + IConfig $config, |
|
| 73 | + IAppManager $appManager, |
|
| 74 | + IControllerMethodReflector $reflector, |
|
| 75 | + IManager $shareManager, |
|
| 76 | + IRequest $request |
|
| 77 | + ) { |
|
| 78 | + $this->appName = $appName; |
|
| 79 | + $this->config = $config; |
|
| 80 | + $this->appManager = $appManager; |
|
| 81 | + $this->reflector = $reflector; |
|
| 82 | + $this->shareManager = $shareManager; |
|
| 83 | + $this->request = $request; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Check if sharing is enabled before the controllers is executed |
|
| 88 | + * |
|
| 89 | + * @param Controller $controller |
|
| 90 | + * @param string $methodName |
|
| 91 | + * @throws NotFoundException |
|
| 92 | + * @throws S2SException |
|
| 93 | + * @throws ShareNotFound |
|
| 94 | + */ |
|
| 95 | + public function beforeController($controller, $methodName) { |
|
| 96 | + if(!$this->isSharingEnabled()) { |
|
| 97 | + throw new NotFoundException('Sharing is disabled.'); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + if ($controller instanceof ExternalSharesController && |
|
| 101 | + !$this->externalSharesChecks()) { |
|
| 102 | + throw new S2SException('Federated sharing not allowed'); |
|
| 103 | + } else if ($controller instanceof ShareController) { |
|
| 104 | + $token = $this->request->getParam('token'); |
|
| 105 | + $share = $this->shareManager->getShareByToken($token); |
|
| 106 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK |
|
| 107 | + && !$this->isLinkSharingEnabled()) { |
|
| 108 | + throw new NotFoundException('Link sharing is disabled'); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Return 404 page in case of a not found exception |
|
| 115 | + * |
|
| 116 | + * @param Controller $controller |
|
| 117 | + * @param string $methodName |
|
| 118 | + * @param \Exception $exception |
|
| 119 | + * @return NotFoundResponse |
|
| 120 | + * @throws \Exception |
|
| 121 | + */ |
|
| 122 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 123 | + if(is_a($exception, '\OCP\Files\NotFoundException')) { |
|
| 124 | + return new NotFoundResponse(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (is_a($exception, '\OCA\Files_Sharing\Exceptions\S2SException')) { |
|
| 128 | + return new JSONResponse($exception->getMessage(), 405); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + throw $exception; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Checks for externalshares controller |
|
| 136 | + * @return bool |
|
| 137 | + */ |
|
| 138 | + private function externalSharesChecks() { |
|
| 139 | + |
|
| 140 | + if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') && |
|
| 141 | + $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') { |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') && |
|
| 146 | + $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') { |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return true; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Check whether sharing is enabled |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + private function isSharingEnabled() { |
|
| 158 | + // FIXME: This check is done here since the route is globally defined and not inside the files_sharing app |
|
| 159 | + // Check whether the sharing application is enabled |
|
| 160 | + if(!$this->appManager->isEnabledForUser($this->appName)) { |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return true; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Check if link sharing is allowed |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + private function isLinkSharingEnabled() { |
|
| 172 | + // Check if the shareAPI is enabled |
|
| 173 | + if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // Check whether public sharing is enabled |
|
| 178 | + if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return true; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | 185 | } |
@@ -35,46 +35,46 @@ |
||
| 35 | 35 | |
| 36 | 36 | class AddServerMiddleware extends Middleware { |
| 37 | 37 | |
| 38 | - /** @var string */ |
|
| 39 | - protected $appName; |
|
| 38 | + /** @var string */ |
|
| 39 | + protected $appName; |
|
| 40 | 40 | |
| 41 | - /** @var IL10N */ |
|
| 42 | - protected $l; |
|
| 41 | + /** @var IL10N */ |
|
| 42 | + protected $l; |
|
| 43 | 43 | |
| 44 | - /** @var ILogger */ |
|
| 45 | - protected $logger; |
|
| 44 | + /** @var ILogger */ |
|
| 45 | + protected $logger; |
|
| 46 | 46 | |
| 47 | - public function __construct($appName, IL10N $l, ILogger $logger) { |
|
| 48 | - $this->appName = $appName; |
|
| 49 | - $this->l = $l; |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 47 | + public function __construct($appName, IL10N $l, ILogger $logger) { |
|
| 48 | + $this->appName = $appName; |
|
| 49 | + $this->l = $l; |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Log error message and return a response which can be displayed to the user |
|
| 55 | - * |
|
| 56 | - * @param Controller $controller |
|
| 57 | - * @param string $methodName |
|
| 58 | - * @param \Exception $exception |
|
| 59 | - * @return JSONResponse |
|
| 60 | - * @throws \Exception |
|
| 61 | - */ |
|
| 62 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 63 | - if (($controller instanceof SettingsController) === false) { |
|
| 64 | - throw $exception; |
|
| 65 | - } |
|
| 66 | - $this->logger->error($exception->getMessage(), ['app' => $this->appName]); |
|
| 67 | - if ($exception instanceof HintException) { |
|
| 68 | - $message = $exception->getHint(); |
|
| 69 | - } else { |
|
| 70 | - $message = $exception->getMessage(); |
|
| 71 | - } |
|
| 53 | + /** |
|
| 54 | + * Log error message and return a response which can be displayed to the user |
|
| 55 | + * |
|
| 56 | + * @param Controller $controller |
|
| 57 | + * @param string $methodName |
|
| 58 | + * @param \Exception $exception |
|
| 59 | + * @return JSONResponse |
|
| 60 | + * @throws \Exception |
|
| 61 | + */ |
|
| 62 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 63 | + if (($controller instanceof SettingsController) === false) { |
|
| 64 | + throw $exception; |
|
| 65 | + } |
|
| 66 | + $this->logger->error($exception->getMessage(), ['app' => $this->appName]); |
|
| 67 | + if ($exception instanceof HintException) { |
|
| 68 | + $message = $exception->getHint(); |
|
| 69 | + } else { |
|
| 70 | + $message = $exception->getMessage(); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return new JSONResponse( |
|
| 74 | - ['message' => $message], |
|
| 75 | - Http::STATUS_BAD_REQUEST |
|
| 76 | - ); |
|
| 73 | + return new JSONResponse( |
|
| 74 | + ['message' => $message], |
|
| 75 | + Http::STATUS_BAD_REQUEST |
|
| 76 | + ); |
|
| 77 | 77 | |
| 78 | - } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | } |