Skip to content

security: add global AutoValidateAntiforgeryToken filter#1059

Open
BenjaminMichaelis wants to merge 2 commits intomainfrom
agents/dotnet-security-cheat-sheet-review
Open

security: add global AutoValidateAntiforgeryToken filter#1059
BenjaminMichaelis wants to merge 2 commits intomainfrom
agents/dotnet-security-cheat-sheet-review

Conversation

@BenjaminMichaelis
Copy link
Copy Markdown
Member

Summary

Adds a global AutoValidateAntiforgeryTokenAttribute filter to all MVC controller actions, resolving the long-standing TODO comment in Program.cs.

What changed

Replaced the TODO comment with an explicit AddControllersWithViews registration:

builder.Services.AddControllersWithViews(options =>
{
    options.Filters.Add(new Microsoft.AspNetCore.Mvc.AutoValidateAntiforgeryTokenAttribute());
});

Why

This was identified during an OWASP .NET Security Cheat Sheet alignment review. The Microsoft docs explicitly recommend applying AutoValidateAntiforgeryToken globally for non-API scenarios:

"It's more likely in this scenario for a POST action method to be left unprotected by mistake, leaving the app vulnerable to CSRF attacks."

Context

  • Razor Pages (Identity, login, register, etc.) are already auto-protected — no change needed there.
  • API controllers (Chat, MCP) use bearer token auth, so CSRF does not apply to them. They can use [IgnoreAntiforgeryToken] if needed.
  • AddControllersWithViews coexists cleanly with the existing AddRazorPages() — 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).

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.
@BenjaminMichaelis BenjaminMichaelis marked this pull request as ready for review May 6, 2026 21:47
Copilot AI review requested due to automatic review settings May 6, 2026 21:47
Copy link
Copy Markdown
Contributor

Copilot AI left a 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 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 AutoValidateAntiforgeryTokenAttribute filter 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());
});
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants