Skip to content

Fix authz metadata guard consistency - #67742

Merged
cincuranet merged 7 commits into
dotnet:mainfrom
GrantTotinov:fix-authz-metadata-guard-consistency
Jul 22, 2026
Merged

cincuranet merged 7 commits into
dotnet:mainfrom
GrantTotinov:fix-authz-metadata-guard-consistency

Conversation

@GrantTotinov

Copy link
Copy Markdown
Contributor

Fix unhandled-security-metadata guard to cover AuthorizationPolicy and IAuthorizationRequirementData

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Fix unhandled-security-metadata guard to cover AuthorizationPolicy and IAuthorizationRequirementData

Description

Routing has a defensive guard that throws InvalidOperationException when an endpoint carries authorization metadata that would otherwise not be processed — once in EndpointRoutingMiddleware.ExecuteShortCircuit (short-circuited endpoints) and once in EndpointMiddleware.Invoke (when the authorization middleware didn't run).

AuthorizationMiddleware builds its effective policy from three metadata shapes:

  • IAuthorizeData
  • AuthorizationPolicy
  • IAuthorizationRequirementData

Both guard checks only looked for IAuthorizeData. As a result, for endpoints configured with an AuthorizationPolicy or an IAuthorizationRequirementData-only attribute (without any IAuthorizeData), the intended fail-fast validation did not fire, and the guard silently under-reported. This was a consistency / defense-in-depth gap, not a change in documented behavior for [Authorize] / RequireAuthorization.

Changes

  • Added a shared internal helper, AuthorizationMetadataHelper.HasAuthorizationMetadata(Endpoint), that checks all three metadata shapes:
  internal static bool HasAuthorizationMetadata(Endpoint endpoint) =>
      endpoint.Metadata.GetMetadata<IAuthorizeData>() is not null
      || endpoint.Metadata.GetMetadata<AuthorizationPolicy>() is not null
      || endpoint.Metadata.GetMetadata<IAuthorizationRequirementData>() is not null;
  • Replaced the single-type IAuthorizeData checks at both guard sites with calls to this shared helper, keeping them in sync with AuthorizationMiddleware as the existing "kept in sync" comments intend.
  • Preserved the AuthorizationMiddlewareInvokedKey condition in EndpointMiddleware — only the metadata-detection part was extracted, not the overall guard logic.
  • No new dependency or public API was introduced; the routing project already references Microsoft.AspNetCore.Authorization.
  • Existing behavior for [Authorize] / RequireAuthorization is unchanged.

Testing

Added tests covering AuthorizationPolicy-only and IAuthorizationRequirementData-only endpoints at both guard sites:

  • EndpointMiddlewareTest.cs — verifies Invoke throws when the authorization middleware wasn't invoked and the endpoint carries only AuthorizationPolicy or only IAuthorizationRequirementData metadata.
  • EndpointRoutingMiddlewareTest.cs — verifies ExecuteShortCircuit throws when a short-circuited endpoint carries only AuthorizationPolicy or only IAuthorizationRequirementData metadata.

Fixes #67559

Copilot AI review requested due to automatic review settings July 11, 2026 14:18
@GrantTotinov
GrantTotinov requested a review from halter73 as a code owner July 11, 2026 14:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR tightens the routing “unhandled authorization metadata” guards so they detect all authorization metadata shapes that AuthorizationMiddleware can process, improving consistency and defense-in-depth for endpoints that would otherwise silently skip auth policy evaluation.

Changes:

  • Introduced AuthorizationMetadataHelper.HasAuthorizationMetadata(Endpoint) to detect IAuthorizeData, AuthorizationPolicy, and IAuthorizationRequirementData.
  • Updated both guard sites (EndpointRoutingMiddleware.ExecuteShortCircuit and EndpointMiddleware.Invoke) to use the shared helper.
  • Added unit tests to cover AuthorizationPolicy-only and IAuthorizationRequirementData-only metadata scenarios at both guard sites.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Http/Routing/src/AuthorizationMetadataHelper.cs Adds a shared internal helper to detect all authorization metadata shapes used by AuthorizationMiddleware.
src/Http/Routing/src/EndpointRoutingMiddleware.cs Uses the shared helper when validating short-circuit endpoints for unhandled authorization metadata.
src/Http/Routing/src/EndpointMiddleware.cs Uses the shared helper when validating endpoints for missing authorization middleware invocation.
src/Http/Routing/test/UnitTests/EndpointRoutingMiddlewareTest.cs Adds short-circuit guard tests for AuthorizationPolicy-only and IAuthorizationRequirementData-only endpoints (currently missing required using/qualification).
src/Http/Routing/test/UnitTests/EndpointMiddlewareTest.cs Adds missing-auth-middleware guard tests for AuthorizationPolicy-only and IAuthorizationRequirementData-only endpoints.

@GrantTotinov

Copy link
Copy Markdown
Contributor Author

@halter73 The failing checks (Helix Subset 1, components-e2e) don't seem related to this change based on what I can see, but let me know if I'm missing something. Happy to make any adjustments needed. Thanks!

@rokonec rokonec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@rokonec

rokonec commented Jul 20, 2026

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@cincuranet
cincuranet merged commit 51a14e4 into dotnet:main Jul 22, 2026
26 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 23, 2026
github-actions Bot added a commit that referenced this pull request Jul 25, 2026
…rMode_Top_AppendKeepsViewportStable

The test has shown intermittent failures in CI across multiple builds against
unrelated PRs (#67742, #67929).

Associated issue: #aw_qgridtop

Co-authored-by: Copilot <[email protected]>
ilonatommy pushed a commit that referenced this pull request Jul 27, 2026
…rMode_Top_AppendKeepsViewportStable (#68018)

The test has shown intermittent failures in CI across multiple builds against
unrelated PRs (#67742, #67929).

Associated issue: #68017

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled-security-metadata guard misses AuthorizationPolicy and IAuthorizationRequirementData

4 participants