security: add global AutoValidateAntiforgeryToken filter#1059
Open
BenjaminMichaelis wants to merge 2 commits intomainfrom
Open
security: add global AutoValidateAntiforgeryToken filter#1059BenjaminMichaelis wants to merge 2 commits intomainfrom
BenjaminMichaelis wants to merge 2 commits intomainfrom
Conversation
Replaces TODO comment with explicit AddControllersWithViews registration that applies AutoValidateAntiforgeryTokenAttribute globally. Ensures all MVC controller POST/PUT/DELETE actions require a valid antiforgery token by default, providing defense-in-depth against CSRF even if individual actions are not annotated.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a global AutoValidateAntiforgeryTokenAttribute filter to MVC by registering AddControllersWithViews(...) in Program.cs, aiming to enforce CSRF protection by default and remove the prior TODO.
Changes:
- Register MVC controllers with views via
AddControllersWithViews(...). - Add a global
AutoValidateAntiforgeryTokenAttributefilter to automatically validate antiforgery tokens on unsafe HTTP methods.
Comment on lines
+235
to
+238
| builder.Services.AddControllersWithViews(options => | ||
| { | ||
| options.Filters.Add(new Microsoft.AspNetCore.Mvc.AutoValidateAntiforgeryTokenAttribute()); | ||
| }); |
Member
Author
There was a problem hiding this comment.
Good catch! Fixed in ffa555f — added [IgnoreAntiforgeryToken] to ChatController, McpTokenController, and ListingSourceCodeController. All three are [ApiController] / ControllerBase classes called from JS or bearer token clients where CSRF doesn't apply.
API controllers (Chat, McpToken, ListingSourceCode) are called from JS clients and bearer token auth - they don't use cookie-based auth so CSRF doesn't apply. Adding [IgnoreAntiforgeryToken] to each prevents the global AutoValidateAntiforgeryToken filter from rejecting their requests.
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 global
AutoValidateAntiforgeryTokenAttributefilter to all MVC controller actions, resolving the long-standing TODO comment inProgram.cs.What changed
Replaced the TODO comment with an explicit
AddControllersWithViewsregistration:Why
This was identified during an OWASP .NET Security Cheat Sheet alignment review. The Microsoft docs explicitly recommend applying
AutoValidateAntiforgeryTokenglobally for non-API scenarios:Context
[IgnoreAntiforgeryToken]if needed.AddControllersWithViewscoexists cleanly with the existingAddRazorPages()— this is the standard pattern for mixed MVC + Razor Pages apps.Risk
Low. The only impact would be if an MVC controller POST action is called without a valid antiforgery token — which would only affect requests that are already missing CSRF protection (i.e., exactly the scenario this fixes).