Add reference doc for automatic CSRF protection in .NET 11#37230
Open
DeagleGross wants to merge 5 commits into
Open
Add reference doc for automatic CSRF protection in .NET 11#37230DeagleGross wants to merge 5 commits into
DeagleGross wants to merge 5 commits into
Conversation
Introduces aspnetcore/security/csrf-protection.md covering the new auto-injected CsrfProtectionMiddleware that ships in .NET 11. Documents: * Decision flow (Sec-Fetch-Site / Origin / CORS / non-browser fallback) * Why the headers can be trusted (forbidden request headers) * Default behavior and integration with .DisableAntiforgery() / [IgnoreAntiforgeryToken] * Allowing cross-origin clients through resolved CORS policy * Per-endpoint opt-out and global DisableCsrfProtection escape hatch * ICsrfProtection customization, browser support, troubleshooting * Adopting CSRF-only protection in apps currently on the token-based system Follow-up commits in this PR will add a migration article (xref:migration/antiforgery-to-csrf), a callout in the existing anti-request-forgery doc, the .NET 11 release-notes include, and toc.yml entries. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adds aspnetcore/migration/antiforgery-to-csrf.md as a standalone article that leads with the positive recommendation (modern apps can drop the token-based system in favor of the automatic CSRF middleware) and includes an upgrade-troubleshooting section for the cross-origin 400 case. Also trims the duplicate 'Adopting CSRF-only protection' section in csrf-protection.md to a one-line pointer, and links the new article from Additional resources. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Blazor SSR's antiforgery dependency is likely to change, so don't mention it as a reason to keep the token-based system. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- antiforgery-to-csrf.md: replace fictional .RequireAntiforgeryToken() extension with the real RequireAntiforgeryTokenAttribute. Drop the reference to scripts from the IAntiforgery sentence (it's server-side). Soften "most apps that are upgrading have UseAntiforgery()" to "most apps that use the token-based system have UseAntiforgery()". - csrf-protection.md: add "Aspect" header to the first column of the Interaction-with-token-based-antiforgery comparison table. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Security: "Automatic CSRF protection" after the existing antiforgery entry, pointing to security/csrf-protection. - Migration and updates: "Adopt automatic CSRF protection in .NET 11" right after Overview, pointing to migration/antiforgery-to-csrf. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new reference doc at
aspnetcore/security/csrf-protection.mddocumenting the new automatic Cross-Site Request Forgery (CSRF) protection middleware shipping in .NET 11 (implementation PR: dotnet/aspnetcore#66585, issue: dotnet/aspnetcore#65127).The new middleware is auto-injected by
WebApplication.CreateBuilderand validatesSec-Fetch-SiteandOriginheaders rather than synchronized tokens. It's additive to the existing token-based antiforgery system documented insecurity/anti-request-forgery.md; the two coexist.What's in the doc
Sec-Fetch-Site→ resolved CORS policy →Sec-Fetch-Sitedeny → Origin-vs-Host fallback → non-browser allow), with a short subsection on whySec-Fetch-SiteandOriginare trustworthy (forbidden request headers — JS can't override them)..DisableAntiforgery()and[IgnoreAntiforgeryToken].AllowAnyOriginis intentionally not honored as a CSRF trust signal.[DisableCors]is not a CSRF opt-out..DisableAntiforgery()and MVC[IgnoreAntiforgeryToken], both bridged to the sameIAntiforgeryMetadatain .NET 11.DisableCsrfProtectionconfig key.ICsrfProtection— interface, registration pattern, sample allowlist implementation.UseAntiforgery()/ token form fields when the app targets modern browsers and doesn't rely onIAntiforgeryAdditionalDataProvider.curlreproducer.Follow-up commits planned in this PR
This is the first of several files in the plan. Subsequent commits to this branch will add:
aspnetcore/migration/antiforgery-to-csrf.md— standalone migration / "I just upgraded and my SPA gets 400s" guide.aspnetcore/migration/100-to-110/includes/csrf.mdplus## Securitysection in100-to-110.mdreferencing it.aspnetcore/release-notes/aspnetcore-11/includes/csrf-protection.mdplus a## Securitysection inaspnetcore-11.md.aspnetcore/security/anti-request-forgery.md(inside the>= aspnetcore-8.0moniker block) linking out to this new doc and the migration article.aspnetcore/toc.ymlentry under Security and a migration entry.Marked as draft until the follow-ups land.
Verification
All
<xref:...>targets resolve against existing uids in the repo:security/anti-request-forgery✓security/cors✓security/data-protection/introduction✓Code claims (decision flow, MVC bridge, CORS resolution order, config key, log category) were cross-checked against the implementation on
dotnet/aspnetcorebranchdmkorolev/csrf-pr:src/DefaultBuilder/src/Internal/DefaultCsrfProtection.cssrc/DefaultBuilder/src/Internal/CsrfProtectionMiddleware.cssrc/DefaultBuilder/src/WebApplicationBuilder.cssrc/DefaultBuilder/src/WebHost.cssrc/Http/Http.Abstractions/src/Antiforgery/ICsrfProtection.csandCsrfProtectionResult.cssrc/Mvc/Mvc.ViewFeatures/src/Filters/AntiforgeryApplicationModelProvider.cssrc/Middleware/CORS/src/Infrastructure/CorsEndpointConventionBuilderExtensions.csInternal previews