-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix ComponentStatePersistenceManager iteration to prevent AntiforgeryValidationException in Blazor WASM #63674
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
Conversation
…derModes Co-authored-by: javiercn <[email protected]>
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.
Pull Request Overview
This PR fixes a critical iteration bug in the ComponentStatePersistenceManager.InferRenderModes method that was causing AntiforgeryValidationException in Blazor WASM applications. The issue occurred when callbacks modified the collection during forward iteration, causing subsequent callbacks to be skipped.
- Changed iteration direction from forward to backward in
InferRenderModesmethod - Added explanatory comment matching the existing pattern in
TryPauseAsync - Ensures all registered callbacks are executed even when some remove themselves during iteration
ilonatommy
left a comment
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.
Looks good but could use a unit test.
|
@copilot can you add a unit test for this? The test should verify that all callbacks get invoked even when a callback unregisters itself during a |
…uring iteration Co-authored-by: javiercn <[email protected]>
Added the requested unit test |
Added a comprehensive unit test as requested. The test verifies that all callbacks are invoked even when the first callback unregisters itself during execution, which validates the backward iteration pattern fix. (590aaa5) |
|
/backport to release/10.0 |
|
Started backporting to release/10.0: https://github.com/dotnet/aspnetcore/actions/runs/17766651506 |
The
ComponentStatePersistenceManager.InferRenderModesmethod was iterating forward through the_registeredCallbackscollection, which caused callbacks to be skipped when other callbacks modified the collection during iteration.Problem
In Blazor Web Applications with global WASM interactivity, the
ResourceCollectionProviderdisposes its subscription during the persistence process, which removes its entry from the_registeredCallbackscollection while the collection is being iterated. This causes subsequent callbacks (particularly theDefaultAntiforgeryStateProvider) to be skipped, leading to missing antiforgery tokens and resulting inAntiforgeryValidationExceptionduring logout operations.Solution
Changed the
InferRenderModesmethod to iterate backwards through the_registeredCallbackscollection, matching the pattern already implemented in theTryPauseAsyncmethod. This ensures that all registered callbacks are executed even when some callbacks remove themselves from the collection during iteration.Before:
After:
This change includes the same explanatory comment used in
TryPauseAsyncto maintain code consistency and document the reasoning behind the backward iteration pattern.Testing
PersistStateAsync_InvokesAllCallbacksWhenFirstCallbackUnregistersItselfthat validates all callbacks are invoked even when the first callback unregisters itself during executionFixes #58822.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.