Skip to content

Antiforgery state doesn't always reach interactive components #54533

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

Closed
SteveSandersonMS opened this issue Mar 13, 2024 · 1 comment · Fixed by #57237
Closed

Antiforgery state doesn't always reach interactive components #54533

SteveSandersonMS opened this issue Mar 13, 2024 · 1 comment · Fixed by #57237
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-antiforgery feature-blazor-state-preservation
Milestone

Comments

@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Mar 13, 2024

Repro:

  • Create a Blazor Web App with global WebAssembly interactivity, and "include sample pages"
  • Edit Counter.razor to include a form
<form method="post">
    <AntiforgeryToken />
</form>
  • Launch the app
  • Verify that if you first land on the homepage, then do a client-side navigation to Counter, that the form does not contain any __RequestVerificationToken hidden field (see this using the DOM inspector)
  • Now reload the page while you're on Counter, so that's where you first land. Verify that the form does contain the __RequestVerificationToken hidden field. Even if you navigate away and back interactively, it will still be there

This inconsistency can't be desirable. Whether or not there's an antiforgery token shouldn't depend on which page you first arrived at.

Also note this is not a matter of prerendering. The exact same problem still occurs even if you disable prerendering.

You can work around it by adding <AntiforgeryToken /> into your layout. The fact that this workaround works suggests that the problem is:

  • We only use the persisted component state during the first WebAssembly render cycle
  • If there isn't an <AntiforgeryToken /> on the first WebAssembly render cycle, the persisted antiforgery state just gets discarded

I guess we could special-case things for antiforgery by ensuring the WebAssembly renderer always reads that state on the first render. Not sure if an equivalent problem could occur for other persistent component state (perhaps not, because app-specific persistent state would only exist if a component is prerendered to emit it, and then the same component should occur during the WebAssembly startup to collect that state - so maybe this bug is just unique to antiforgery data).

@SteveSandersonMS SteveSandersonMS added the area-blazor Includes: Blazor, Razor Components label Mar 13, 2024
SteveSandersonMS added a commit to dotnet-smartcomponents/smartcomponents that referenced this issue Mar 14, 2024
@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Mar 14, 2024
@mkArtakMSFT mkArtakMSFT added this to the 9.0-preview7 milestone Mar 14, 2024
@SteveSandersonMS SteveSandersonMS changed the title Antiforgery state doesn't always reach WebAssembly components Antiforgery state doesn't always reach interactive components Mar 20, 2024
@SteveSandersonMS
Copy link
Member Author

Note that this can also affect interactive Server components too, as shown by dotnet-smartcomponents/smartcomponents#14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-antiforgery feature-blazor-state-preservation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants