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

Align authorization behavior to regular ASP.NET Core #7408

Merged
merged 9 commits into from
Sep 2, 2024

Conversation

tobias-tengler
Copy link
Collaborator

@tobias-tengler tobias-tengler commented Aug 27, 2024

We noticed some discrepancies between the ASP.NET Core [Authorize] attribute and Hot Chocolate's one.

This PR

  • updates the [Authorize] attribute to properly generate the @authorize directive, if both roles and policy are specified (Diff: cb798d3)
  • updates the DefaultAuthorizationHandler to construct an AuthorizationPolicy out of the roles and specified policy (similar to what the AuthorizationMiddleware of ASP.NET Core is doing), which is then evaluated through the registered IAuthorizationService. Previously the authorization code was custom and there wouldn't be a RoleRequirement passed to the IAuthorizationService. In our code we use a custom IAuthorizationService to apply fallbacks, so there was a mismatch between what was happening for our REST and GraphQL APIs. (Diff: bfde46f)
  • removes the tests for the case where the default policy is set to null, as the property isn't nullable (setting it to null would also have unexpected implications in regular ASP.NET Core) (Diff: c64dd91)

I have also run this against our internal test suite of authorization tests and they're all passing now with these changes.

Copy link

codecov bot commented Aug 27, 2024

Codecov Report

Attention: Patch coverage is 97.22222% with 2 lines in your changes missing coverage. Please review.

Project coverage is 77.83%. Comparing base (26e6c73) to head (16328fd).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...tCore.Authorization/DefaultAuthorizationHandler.cs 88.88% 1 Missing ⚠️
...ons/HotChocolateAuthorizeRequestExecutorBuilder.cs 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7408      +/-   ##
==========================================
- Coverage   78.81%   77.83%   -0.98%     
==========================================
  Files        2434     2815     +381     
  Lines      119868   141032   +21164     
==========================================
+ Hits        94469   109770   +15301     
- Misses      25399    31262    +5863     
Flag Coverage Δ
unittests 77.83% <97.22%> (-0.98%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tobias-tengler
Copy link
Collaborator Author

I don't get this part in the test setup:
https://github.com/ChilliCream/graphql-platform/blob/main/src/HotChocolate/AspNetCore/test/AspNetCore.Authorization.Tests/AuthorizationTestData.cs#L29-L45

The iterator returns the same exact setup twice. Is this an oversight or intended like this?

@tobias-tengler tobias-tengler force-pushed the tte/fix-authorization-requirements branch 2 times, most recently from dbe05f6 to 8f61b4e Compare August 27, 2024 10:55
@tobias-tengler tobias-tengler marked this pull request as ready for review August 27, 2024 11:30
@tobias-tengler
Copy link
Collaborator Author

Why do we output the name of the missing policy in the GraphQL error message?

The name could leak details about how the authorization works. Can we make this error generic?

@tobias-tengler tobias-tengler force-pushed the tte/fix-authorization-requirements branch 2 times, most recently from 4bceb0d to c64dd91 Compare August 27, 2024 12:08
@tobias-tengler tobias-tengler force-pushed the tte/fix-authorization-requirements branch from 6780041 to 8ca0dd3 Compare September 2, 2024 18:42
@tobias-tengler tobias-tengler added the 👓 ready-for-review The PR is ready for review. label Sep 2, 2024
@tobias-tengler tobias-tengler changed the title Fix AuthorizeAttribute Align authorization behavior to regular ASP.NET Core Sep 2, 2024
@michaelstaib michaelstaib merged commit 4003717 into main Sep 2, 2024
3 checks passed
@michaelstaib michaelstaib deleted the tte/fix-authorization-requirements branch September 2, 2024 19:00
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.

2 participants