-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
BlazorWebAssemblyStandaloneWithIdentity uses cookie auth but no antiforgerytoken #31205
Comments
🍂🎁 Happy Holidays! ❄️⛄A green dinosaur 🦖 will be along shortly to assist. Stand-by ........ |
Thanks for opening the issue this way. We use the metadata to track work on these. It should be automatic and transparent. If you look at a request in browser tools, you should see the antiforgery cookie named something like I agree that it would be nice if the article addressed this subject to head off concerns about it. @JeremyLikness ... Plz confirm for us that devs don't need to sweat XSRF with basic use of this solution, and are there any gotchas 😈 that are worth calling out? |
I have run the There is no cookie in the dev tools called Am I missing something? |
Let me see. I have that test app here ... I'll run it and see what happens. I thought I recalled seeing it, but I might be mistaken. Stand-by .......................... |
Let's clarify something ... Are you running at secure endpoints (https)? How are you running these apps ... command-line |
Yes to running https. |
Ok ... I just wanted to make sure. You probably know this already, but Stand-by for a bit. I'm going to email Jeremy, too, in case he doesn't see the ping here. He has the security chops 👨🎓 to explain further any considerations for antiforgery with this particular sample app and probably why I'm seeing an antiforgery cookie but you aren't. UPDATE: Message sent. He'll be along shortly to assist. |
Hi, I will need to make one tweak to secure the logout endpoint: app.MapPost("/logout", async (
SignInManager<MyUser> signInManager,
[FromBody]object empty) =>
{
if (empty is not null)
{
await signInManager.SignOutAsync();
return Results.Ok();
}
return Results.NotFound();
}).RequireAuthorization(); This prevents the endpoint from accepting any forms posts. I spoke with our security team, and they indicated JSON-only endpoints that don't accept form auth or form posts don't need XSRF protections (which is why the MapIdentityEndpoints don't use anti-XSRF). The only way the In ASP.NET Hosted, the anti-XSRF token was rendered in the same page as the client and could be securely grabbed to send back. In standalone Blazor, the client is independent. There is no secure way to send the anti-XSRF token to the client (imagine an endpoint that exposes the token ... but then how do you protect that endpoint?). There are additional mitigations like SameSite cookies and custom headers and I'll see if those make sense to include in the sample (we definitely should mention them in the documentation). |
Thanks for that understanding, @JeremyLikness. Let's work it from @nathan-parkinson's issue here. When you're ready, paste content into an issue comment, and I'll place it into the article, edit, and ping you on the PR for review+updates. WRT to the logout POST endpoint, do you want me to make that update? I can knock that out right now. |
Go for it! |
Ok, that bit is done. I'll add text to the article covering it when I work this issue. |
Thank you both for your help with this. I had wondered how an antiforgery token could work across sites. Thanks again. |
Leaving a note here in case anyone stumbles on this. The code above ☝️ throws the logout into a ☠️ death loop because an error is triggered and the |
The missing piece is posting the empty content from the client. I'll work on that part (with SPA you just |
Before closing this out, I'll add the text to the article, mostly for localization purposes. Here are the code remarks ...
|
NOTE TO SELF ... I should be able to close this out with some small updates to the article. We merged the update to the Blazor sample app. |
The logout endpoint was the only endpoint of concern ... and AFAICT analyzing the work that we've done, this is resolved. Closing now. |
Description
The BlazorWebAssemblyStandaloneWithIdentity sample uses a cookie to store the authentication token but I believe that this opens the site up to csrf attacks.
This is usually mitigated by using antiforgerytokens but I've found it difficult to apply this to the sample as the api and client run as different sites (although I may have just dome something wrong).
Is is right that antiforgery tokens should be used here or is there a reason it is not required?
If it is required will it be added to this sample or can someone point me in the right direction to get this working?
Page URL
https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-identity?view=aspnetcore-8.0
Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/security/webassembly/standalone-with-identity.md
Document ID
c4e6ec41-7bea-e600-6473-c5c870aab082
Article author
@guardrex
The text was updated successfully, but these errors were encountered: