الانتقال إلى المحتوى
الأمان (Security)OWASP Top 10· 7 دقائق للقراءة

CSRF Explained: Your Session Cookie Doesn't Prove the Request Came From You

The browser attaches your cookie to requests that other sites start. Only a token they can't read, or a browser signal, proves the request came from your own page.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • OWASP Top 10 2025
محتويات المقال

A user reports that two-factor authentication on her account was switched off, and she didn't do it. The logs show 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

The belief: a request that carries the user's session cookie must have come from your own site. Only your site set the cookie, and other sites can't read it, so the cookie proves the user acted in your app.

The first half is true, which is why the belief holds. The does stop other sites from reading your cookies and your responses. Session cookies are usually scoped to your domain, marked 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

OWASP's CSRF Prevention Cheat Sheet starts from the mechanism: "browser requests automatically include all cookies including session cookies." Microsoft's docs say the same: these attacks "are possible because web browsers send some types of authentication tokens automatically with every request to a website." The cookie identifies the browser. It says nothing about which page, tab or site started the request. So, in OWASP's words, "unprotected target sites cannot distinguish between legitimate authorized requests and forged authenticated requests."

Another site can send even when it can't read

MDN explains why forms are the classic tool: "Simple requests, which are the sort of request that result from a <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

The attribute tells the browser when to withhold a cookie from cross-site requests. ASP.NET Core's cookie authentication defaults to Lax. We tested the three values in headless Chromium 156, with a page on one local site sending requests to an app on another:

text
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                     no

That third line is the story above. OWASP calls it "the single most common way SameSite-based defenses fail in practice": "If any state-changing operation in the application is reachable via a GET request, SameSite=Lax will not stop it." There are other gaps. SameSite works per site, not per origin, so MDN warns that "you have to trust all your site's subdomains." And defaults differ: "Chromium-based browsers treat cookies without a SameSite attribute as Lax, but Firefox and Safari do not."

What proves intent: something the other site can't produce

A token is a random value the server puts in its own page and checks on every state-changing request. As MDN puts it, "Because an attacker can't guess the token value, they can't issue a successful forgery." Browsers also help: in our test, every forged request arrived with 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

C# Program.cs
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();

The fix doesn't invent anything. OWASP's first rule is "check if your framework has built-in CSRF protection and use it", and Microsoft documents that "By default, Minimal APIs that accept form data require antiforgery token validation and fail before running application code if antiforgery validation isn't successful." A forged form has no valid token, so the handler never runs. Asking for the password again follows OWASP's advice to consider "user interaction based protection for highly sensitive operations."

DefenseWhat it relies onWhere it falls short
SameSite=LaxThe browser withholds cookies on cross-site POSTsGET navigations, sibling subdomains, older browsers
SameSite=StrictThe browser withholds cookies on all cross-site requestsUsers arriving from links look logged out
CSRF tokenA secret the other site can't readMust cover every state-changing request
Sec-Fetch-Site checkA header the browser sets itselfOlder browsers need an Origin fallback
Custom header or JSON bodyCross-origin fetch needs your permissionA loose CORS policy with credentials reopens it

How to apply it in your project

  1. List the state-changing endpoints that use cookiesEvery POST, PUT, PATCH and DELETE, plus any GET that writes, where the user is identified by a cookie.
  2. Make GET read-onlyMove every action that changes state off GET. That alone closes the gap SameSite=Lax leaves open.
  3. Turn on the framework's protectionIn ASP.NET Core, call AddAntiforgery and UseAntiforgery for Minimal APIs; MVC and Razor Pages validate tokens through built-in filters. Never answer a failing token with DisableAntiforgery on a cookie-authenticated endpoint.
  4. Set cookie attributes on purposeUse Strict for session cookies when your login flow allows it, otherwise Lax, always with Secure and HttpOnly. Treat it as defense in depth.
  5. Re-confirm the dangerous actionsChanging 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=Lax blocks 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.

📚 المصادر

  1. Cross-Site Request Forgery Prevention Cheat Sheet — OWASP Cheat Sheet Series
  2. Cross-site request forgery (CSRF) — MDN Web Docs
  3. A01:2025 Broken Access Control — OWASP Top 10:2025
  4. Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core — Microsoft Learn
  5. Work with SameSite cookies in ASP.NET Core — Microsoft Learn

التعليقات (0)