-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(security): Add PHP \Attribute for remaining security annotations #37905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,10 +33,13 @@ | |||||
| use OC\User\Session; | ||||||
| use OCP\AppFramework\Controller; | ||||||
| use OCP\AppFramework\Http; | ||||||
| use OCP\AppFramework\Http\Attribute\CORS; | ||||||
| use OCP\AppFramework\Http\Attribute\PublicPage; | ||||||
| use OCP\AppFramework\Http\JSONResponse; | ||||||
| use OCP\AppFramework\Http\Response; | ||||||
| use OCP\AppFramework\Middleware; | ||||||
| use OCP\IRequest; | ||||||
| use ReflectionMethod; | ||||||
|
|
||||||
| /** | ||||||
| * This middleware sets the correct CORS headers on a response if the | ||||||
|
|
@@ -81,9 +84,12 @@ public function __construct(IRequest $request, | |||||
| * @since 6.0.0 | ||||||
| */ | ||||||
| public function beforeController($controller, $methodName) { | ||||||
| $reflectionMethod = new ReflectionMethod($controller, $methodName); | ||||||
|
|
||||||
| // ensure that @CORS annotated API routes are not used in conjunction | ||||||
| // with session authentication since this enables CSRF attack vectors | ||||||
| if ($this->reflector->hasAnnotation('CORS') && (!$this->reflector->hasAnnotation('PublicPage') || $this->session->isLoggedIn())) { | ||||||
| if ($this->hasAnnotationOrAttribute($reflectionMethod, 'CORS', CORS::class) && | ||||||
| (!$this->hasAnnotationOrAttribute($reflectionMethod, 'PublicPage', PublicPage::class) || $this->session->isLoggedIn())) { | ||||||
| $user = array_key_exists('PHP_AUTH_USER', $this->request->server) ? $this->request->server['PHP_AUTH_USER'] : null; | ||||||
| $pass = array_key_exists('PHP_AUTH_PW', $this->request->server) ? $this->request->server['PHP_AUTH_PW'] : null; | ||||||
|
|
||||||
|
|
@@ -103,7 +109,28 @@ public function beforeController($controller, $methodName) { | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * This is being run after a successful controllermethod call and allows | ||||||
| * @template T | ||||||
| * | ||||||
| * @param ReflectionMethod $reflectionMethod | ||||||
| * @param string $annotationName | ||||||
| * @param class-string<T> $attributeClass | ||||||
| * @return boolean | ||||||
| */ | ||||||
| protected function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, string $annotationName, string $attributeClass): bool { | ||||||
| if ($this->reflector->hasAnnotation($annotationName)) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At other implements of this method on this PR you used the follow code. Make any difference or only was about wrote more times the same method without doing a copy paste?
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| if (!empty($reflectionMethod->getAttributes($attributeClass))) { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * This is being run after a successful controller method call and allows | ||||||
| * the manipulation of a Response object. The middleware is run in reverse order | ||||||
| * | ||||||
| * @param Controller $controller the controller that is being called | ||||||
|
|
@@ -114,23 +141,25 @@ public function beforeController($controller, $methodName) { | |||||
| * @throws SecurityException | ||||||
| */ | ||||||
| public function afterController($controller, $methodName, Response $response) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will impact a lot of classes but would be good create a followup issue to do this fix return type and others from
Suggested change
|
||||||
| // only react if its a CORS request and if the request sends origin and | ||||||
| // only react if it's a CORS request and if the request sends origin and | ||||||
|
|
||||||
| if (isset($this->request->server['HTTP_ORIGIN']) && | ||||||
| $this->reflector->hasAnnotation('CORS')) { | ||||||
| // allow credentials headers must not be true or CSRF is possible | ||||||
| // otherwise | ||||||
| foreach ($response->getHeaders() as $header => $value) { | ||||||
| if (strtolower($header) === 'access-control-allow-credentials' && | ||||||
| strtolower(trim($value)) === 'true') { | ||||||
| $msg = 'Access-Control-Allow-Credentials must not be '. | ||||||
| 'set to true in order to prevent CSRF'; | ||||||
| throw new SecurityException($msg); | ||||||
| if (isset($this->request->server['HTTP_ORIGIN'])) { | ||||||
| $reflectionMethod = new ReflectionMethod($controller, $methodName); | ||||||
| if ($this->hasAnnotationOrAttribute($reflectionMethod, 'CORS', CORS::class)) { | ||||||
| // allow credentials headers must not be true or CSRF is possible | ||||||
| // otherwise | ||||||
| foreach ($response->getHeaders() as $header => $value) { | ||||||
| if (strtolower($header) === 'access-control-allow-credentials' && | ||||||
| strtolower(trim($value)) === 'true') { | ||||||
| $msg = 'Access-Control-Allow-Credentials must not be '. | ||||||
| 'set to true in order to prevent CSRF'; | ||||||
| throw new SecurityException($msg); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| $origin = $this->request->server['HTTP_ORIGIN']; | ||||||
| $response->addHeader('Access-Control-Allow-Origin', $origin); | ||||||
| $origin = $this->request->server['HTTP_ORIGIN']; | ||||||
| $response->addHeader('Access-Control-Allow-Origin', $origin); | ||||||
| } | ||||||
| } | ||||||
| return $response; | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method occur 3 times at this PR with the same signature.
Maybe will be good to move to parent class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ia only temporary until we drop the annotations and its 2 if statements. I don't really feel like creating an abstract parent class for 4 operational lines