
CSRF Explained: Your Session Cookie Doesn't Prove the Request Came From You
- كاتب موثّق
- OWASP Top 10 2025
محتويات المقال
GET /settings/2fa/disable from her own browser, with her valid session cookie, so the first reply from the team is that she must have clicked it herself. She did click something: a link in a forum post, a few seconds earlier. The page behind it navigated her browser to that URL, and her browser added the cookie, as browsers do.
The common misconception
HttpOnly, and sent only over HTTPS. Chromium-based browsers treat cookies without a SameSite attribute as Lax, which many developers remember as "cross-site requests don't get cookies". But reading a cookie and sending one are different things. The attacker never needs to read it. The browser attaches it for them.
What actually happens
The browser attaches cookies, not your page
Another site can send even when it can't read
<form> element submission, can be made cross-origin without being blocked." The other site never sees your response, and it doesn't need to: the email is changed, the money is sent, 2FA is off. MDN lists when an app is exposed: it changes state with HTTP requests, it "Uses only cookies to validate that the request came from an authenticated user", and every parameter is predictable. The OWASP Top 10 2025 lists CSRF, CWE-352, among the notable weaknesses of A01 Broken Access Control.
SameSite narrows the gap, but it isn't the wall
Lax. We tested the three values in headless Chromium 156, with a page on one local site sending requests to an app on another:
Cookie Request started by the other site Cookie sentSameSite=None auto-submitted POST form yesSameSite=Lax auto-submitted POST form noSameSite=Lax auto-submitted GET form (navigates) yesSameSite=Lax <img> pointing at the GET URL noSameSite=Strict any of the three noWhat proves intent: something the other site can't produce
Sec-Fetch-Site: cross-site, a header page scripts can't set, so a server can refuse cross-site writes. APIs called with fetch can require a custom header or a JSON body, which turns the call into a request the browser won't send cross-origin without permission.
Moving the action behind a token
var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication().AddCookie(); // SameSite=Laxbuilder.Services.AddAuthorization();builder.Services.AddSingleton<UserStore>();var app = builder.Build();app.UseAuthentication();app.UseAuthorization();// "Only our settings page links here, and it carries the cookie"app.MapGet("/settings/2fa/disable", async ( ClaimsPrincipal user, UserStore users) =>{ await users.DisableTwoFactorAsync(user.Identity!.Name!); return Results.Redirect("/settings");}).RequireAuthorization();app.Run();var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication().AddCookie(o => o.Cookie.SameSite = SameSiteMode.Strict); // extra layer, not the fixbuilder.Services.AddAuthorization();builder.Services.AddAntiforgery();builder.Services.AddSingleton<UserStore>();var app = builder.Build();app.UseAuthentication();app.UseAuthorization();app.UseAntiforgery(); // after authentication and authorizationapp.MapGet("/settings", (HttpContext ctx, IAntiforgery antiforgery) =>{ var tokens = antiforgery.GetAndStoreTokens(ctx); // a hidden field return Results.Content(SettingsPage.Render(tokens), "text/html");}).RequireAuthorization();// POST only, and the form must carry the token and the passwordapp.MapPost("/settings/2fa/disable", async ([FromForm] string password, ClaimsPrincipal user, UserStore users) => await users.DisableTwoFactorAsync(user.Identity!.Name!, password) ? Results.Redirect("/settings") : Results.BadRequest()) .RequireAuthorization();app.Run();SameSite=Lax | ||
SameSite=Strict | ||
Sec-Fetch-Site check | Origin fallback | |
fetch needs your permission |
How to apply it in your project
List the state-changing endpoints that use cookies Every POST, PUT, PATCH and DELETE, plus any GET that writes, where the user is identified by a cookie. Make GET read-only Move every action that changes state off GET. That alone closes the gap SameSite=Laxleaves open.Turn on the framework's protection In ASP.NET Core, call AddAntiforgeryandUseAntiforgeryfor Minimal APIs; MVC and Razor Pages validate tokens through built-in filters. Never answer a failing token withDisableAntiforgeryon a cookie-authenticated endpoint.Set cookie attributes on purpose Use Strictfor session cookies when your login flow allows it, otherwiseLax, always withSecureandHttpOnly. Treat it as defense in depth.Re-confirm the dangerous actions Changing the email or password, disabling 2FA and sending money should ask for the password or a second factor again.
Key takeaways
A session cookie proves which browser sent a request, not which site started it. Other sites can't read your cookies, but they can make the browser send them. SameSite=Laxblocks cross-site POSTs in Chromium, yet it lets top-level GET navigations through.Use your framework's CSRF tokens on every state-changing request, and keep GET read-only. XSS defeats CSRF defenses, so CSRF protection only works on top of solid output encoding.
📚 المصادر
Cross-Site Request Forgery Prevention Cheat Sheet — OWASP Cheat Sheet Series Cross-site request forgery (CSRF) — MDN Web Docs A01:2025 Broken Access Control — OWASP Top 10:2025 Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core — Microsoft Learn Work with SameSite cookies in ASP.NET Core — Microsoft Learn
التعليقات (0)