-
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
InferRenderModes
method - Added explanatory comment matching the existing pattern in
TryPauseAsync
- Ensures all registered callbacks are executed even when some remove themselves during iteration
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.InferRenderModes
method was iterating forward through the_registeredCallbacks
collection, which caused callbacks to be skipped when other callbacks modified the collection during iteration.Problem
In Blazor Web Applications with global WASM interactivity, the
ResourceCollectionProvider
disposes its subscription during the persistence process, which removes its entry from the_registeredCallbacks
collection while the collection is being iterated. This causes subsequent callbacks (particularly theDefaultAntiforgeryStateProvider
) to be skipped, leading to missing antiforgery tokens and resulting inAntiforgeryValidationException
during logout operations.Solution
Changed the
InferRenderModes
method to iterate backwards through the_registeredCallbacks
collection, matching the pattern already implemented in theTryPauseAsync
method. 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
TryPauseAsync
to maintain code consistency and document the reasoning behind the backward iteration pattern.Testing
PersistStateAsync_InvokesAllCallbacksWhenFirstCallbackUnregistersItself
that 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.