Skip to content
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

feat(security): Add PHP \Attribute for remaining security annotations #37905

Conversation

nickvergessen
Copy link
Member

@nickvergessen nickvergessen commented Apr 24, 2023

Summary

Allowing the remaining annotations of the security middlewares as Attributes

TODO

Checklist

@nickvergessen nickvergessen added this to the Nextcloud 27 milestone Apr 24, 2023
@nickvergessen nickvergessen self-assigned this Apr 24, 2023
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Psalm found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@nickvergessen nickvergessen force-pushed the techdebt/noid/add-attributes-for-remaining-security-annotations branch from 4f53919 to a64daa3 Compare April 25, 2023 11:13
@nickvergessen nickvergessen added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Apr 25, 2023
@nickvergessen nickvergessen marked this pull request as ready for review April 25, 2023 11:13
@nickvergessen nickvergessen force-pushed the techdebt/noid/add-attributes-for-remaining-security-annotations branch from a64daa3 to 203f182 Compare April 25, 2023 11:53
@nickvergessen nickvergessen added the pending documentation This pull request needs an associated documentation update label Apr 25, 2023
Signed-off-by: Joas Schilling <coding@schilljs.com>
@nickvergessen nickvergessen force-pushed the techdebt/noid/add-attributes-for-remaining-security-annotations branch from 203f182 to ecb8b55 Compare April 25, 2023 12:50
@nickvergessen
Copy link
Member Author

Comment for #37039 on merge:

Added:

> # Backend
> 
> * `AuthorizedAdminSetting` controller access restriction can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/basics/setting.html
> * `CORS` controller restriction can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/digging_deeper/rest_apis.html
> * `NoAdminRequired` controller access restriction can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/basics/controllers.html#authentication
> * `NoCSRFRequired` controller functionality can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/prologue/security.html#cross-site-request-forgery
> * `PasswordConfirmationRequired` controller restriction can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/digging_deeper/javascript-apis.html#nextcloud-password-confirmation
> * `PublicPage` controller access restriction can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/basics/controllers.html#authentication
> * `SubAdminRequired` controller access restriction can now be enabled using native PHP attributes https://github.com/nextcloud/server/pull/37905  https://docs.nextcloud.com//server/stable/developer_manual/basics/controllers.html#authentication
> 
> ## Deprecations
> 
> * `@AuthorizedAdminSetting` annotation for controllers is now deprecated in favor of the `#[AuthorizedAdminSetting]` attribute https://docs.nextcloud.com//server/stable/developer_manual/basics/setting.html
> * `@CORS` annotation for controllers is now deprecated in favor of the `#[CORS]` attribute https://docs.nextcloud.com//server/stable/developer_manual/digging_deeper/rest_apis.html
> * `@NoAdminRequired` annotation for controllers is now deprecated in favor of the `#[NoAdminRequired]` attribute https://docs.nextcloud.com//server/stable/developer_manual/basics/controllers.html#authentication
> * `@NoCSRFRequired` annotation for controllers is now deprecated in favor of the `#[NoCSRFRequired]` attribute https://docs.nextcloud.com//server/stable/developer_manual/prologue/security.html#cross-site-request-forgery
> * `@PasswordConfirmationRequired` annotation for controllers is now deprecated in favor of the `#[PasswordConfirmationRequired]` attribute https://docs.nextcloud.com//server/stable/developer_manual/digging_deeper/javascript-apis.html#nextcloud-password-confirmation
> * `@PublicPage` annotation for controllers is now deprecated in favor of the `#[PublicPage]` attribute https://docs.nextcloud.com//server/stable/developer_manual/basics/controllers.html#authentication
> * `@SubAdminRequired` annotation for controllers is now deprecated in favor of the `#[SubAdminRequired]` attribute https://docs.nextcloud.com//server/stable/developer_manual/basics/controllers.html#authentication

Copy link
Contributor

@vitormattos vitormattos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only suggestions, not blocking points.

if ($this->reflector->hasAnnotation($annotationName)) {
return true;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -114,23 +141,25 @@ public function beforeController($controller, $methodName) {
* @throws SecurityException
*/
public function afterController($controller, $methodName, Response $response) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Middleware abstract class:

Suggested change
public function afterController($controller, $methodName, Response $response) {
public function afterController($controller, $methodName, Response $response): void {

* @param class-string<T> $attributeClass
* @return boolean
*/
protected function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, string $annotationName, string $attributeClass): bool {
Copy link
Contributor

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.

Copy link
Member Author

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

* @return boolean
*/
protected function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, string $annotationName, string $attributeClass): bool {
if ($this->reflector->hasAnnotation($annotationName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
if ($this->reflector->hasAnnotation($annotationName)) {
if (!empty($reflectionMethod->getAttributes($attributeClass))) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For AuthorizedAdminSetting we need the values, so i changed it there but then went for an independent approach, so that's why after that attempt some might diverge, but as mentioned above they are just 2 ifs so it doesn't really matter

@nickvergessen nickvergessen merged commit 75f17b6 into master Apr 25, 2023
@nickvergessen nickvergessen deleted the techdebt/noid/add-attributes-for-remaining-security-annotations branch April 25, 2023 15:13
@nickvergessen nickvergessen removed the pending documentation This pull request needs an associated documentation update label May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants