Commit 2eaf7db
security: add global AutoValidateAntiforgeryToken filter (#1059)
## 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:
```csharp
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](https://learn.microsoft.com/aspnet/core/security/anti-request-forgery#generate-antiforgery-tokens-with-iantiforgery)
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).
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>1 parent 6cb62fc commit 2eaf7db
3 files changed
Lines changed: 6 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
235 | | - | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
236 | 239 | | |
237 | 240 | | |
238 | 241 | | |
| |||
0 commit comments