Fix ApplyPolicy.Validation not enforced on code-first fields#9834
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a regression where code-first field authorization configured with ApplyPolicy.Validation did not trigger request-level authorization enforcement (allowing unauthenticated requests to resolve protected fields), restoring parity with attribute-based and SDL-based authorization configuration.
Changes:
- Write the
AuthorizeAtRequestLevelmarker todescriptor.Extend().Context.Featuresfor code-first fields configured withApplyPolicy.Validation, matching the attribute/SDL paths soAuthorizationTypeInterceptorcan flag the schema correctly. - Add a targeted test covering the scenario where only a field (not the containing query type) carries a validation policy, ensuring request-level enforcement is activated.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/HotChocolate/Core/src/Authorization/Extensions/AuthorizeObjectFieldDescriptorExtensions.cs |
Fixes the feature write location so request-level validation auth is correctly enabled for code-first fields. |
src/HotChocolate/Core/test/Authorization.Tests/CodeFirstAuthorizationTests.cs |
Adds a regression test that fails pre-fix and passes with the corrected marker propagation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced Jun 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
.Authorize(ApplyPolicy.Validation)on a field never enforced the policy: unauthenticated requests resolved the protected field, while the annotation-based[Authorize(Apply = ApplyPolicy.Validation)]correctly rejected them.AuthorizeAtRequestLevelmarker to the field configuration's features (which nothing reads) instead of the descriptor context. The attribute and SDL paths write it to the descriptor context, where theAuthorizationTypeInterceptorreads it to flag the schema for request-level enforcement.AuthorizeAtRequestLevelwrites inAuthorizeObjectFieldDescriptorExtensionsnow targetdescriptor.Extend().Context, matchingAuthorizeAttribute. TheAllowAnonymouswrite is unchanged (it correctly lives on the field configuration).Test plan
Authorize_Field_Validation_NoAccess_When_Type_Not_Authorized, which isolates a field-level validation policy on an otherwise-unauthorized query type (the case existing tests masked via a type-level policy). Verified it fails on the old code and passes on the fix.dotnet testforHotChocolate.Authorization.Tests(33/33) andHotChocolate.AspNetCore.Authorization.Tests(110/110), green on net8.0/net9.0/net10.0.Closes #9830