QA Interview Hub · Knowledge base · Quizzes · Українська

Penetration Testing › Client side: XSS, CSRF, CORS, clickjacking

CSRF and SameSite: a foreign request with your cookies

CSRF is when a foreign page makes the browser send a request with the user's cookie. The server performs a state change and the attacker does not see the response. Defences are an anti-CSRF token, an Origin check and SameSite, but SameSite is only one layer: Lax allows top-level GET navigations, and a subdomain of the same site is not cross-site. Changes go only through POST, PUT or DELETE.

Pentest · Client side

A user opened a foreign page and a transfer appeared in the bank. He clicked nothing in the bank.

A user is logged in to a bank and at the same time opens a foreign page. That page automatically submits a form to the bank’s address and the bank carries out the transfer: the browser added the session cookie itself. This is CSRF (cross-site request forgery). It is covered briefly in the OWASP overview, and here are the mechanism in more depth and the defence in detail, in particular the SameSite attribute.

Mechanism. The browser attaches cookies to every request to their site, even when another page initiated the request. The attack needs three conditions: the action changes state (a transfer, an email change), authentication relies on a cookie, and all request parameters are predictable. The attacking page:

<form action="https://bank.example/transfer" method="POST">
  <input name="to" value="attacker">
  <input name="amount" value="1000">
</form>
<script>document.forms[0].submit()</script>

The attacker does not see the response (this separates CSRF from XSS): he only forces an action. So CSRF is about changes, not reading.

SameSite. A cookie attribute decides whether to send it in "cross-site" requests (a site is the registrable domain, for example example.com, with all subdomains):

  • Strict — never sent from another site, even when following a link;
  • Lax — sent only on a top-level navigation with an ordinary GET (a link click), but not in a POST from a foreign form, an iframe or fetch;
  • None — always sent, requires Secure.

What happens to the session cookie

POST from foreign · Nonecookie sentPOST from foreign · Laxno cookieGET link · Laxcookie sent
Figure 1. Lax stops a cross-site POST but a GET navigation gets the cookie: changes must never be made through GET.

In Chrome a cookie without an explicit SameSite is treated as Lax (since 2020, version 80); for such cookies Chrome still sends them on a top-level cross-site POST for two minutes after they are set. Other browsers behave differently, so set it explicitly: Set-Cookie: sid=…; Secure; HttpOnly; SameSite=Lax (or Strict for sensitive sessions).

Other defences.

  • An anti-CSRF token: a random value tied to the session that the page passes in the body or a header and the server compares. A foreign page does not know it;
  • Checking Origin (and if needed Referer) and the Sec-Fetch-Site header: the browser itself marks that the request is cross-site;
  • Re-authentication for sensitive actions (a password, a confirmation code).

A caution: "JSON only" is not a defence by itself (a form with text/plain can carry a string that looks like JSON), while an Authorization: Bearer header that a script adds manually is protected from CSRF, because the browser does not add it on its own.

A real case. In September 2008 Princeton researchers William Zeller and Edward Felten showed CSRF flaws on well-known sites: ING Direct, YouTube, MetaFilter and The New York Times. In the ING Direct case an attacker could open extra accounts on behalf of a user and move money out. It is one of the first publicly described CSRF flaws on a bank site. The sites fixed the flaws, and the authors also released a browser extension that protected only against cross-site POSTs. SameSite did not exist then: protection rested on server-side tokens.

Limits and bypasses.

  • Site ≠ origin. A subdomain blog.example.com shares the site with app.example.com: XSS on the blog sends requests with the cookie, and SameSite changes nothing;
  • A state-changing GET. An address /delete?id=5 opened by navigation gets the cookie even with Lax;
  • XSS defeats all tokens because the script reads them from the page;
  • Login CSRF: a foreign login form signs the user in to the attacker’s account; a token is needed on the login too.

How QA checks it. Look at the cookie attributes in DevTools (Application → Cookies tab): SameSite, Secure, HttpOnly. On an authorized environment: repeat a change without a token or with another user’s token (403 is expected), with another Origin, with GET instead of POST. A separate pass over pages on subdomains. Check that the refusal really changed nothing (read the state after the attempt).

Check yourself

The session cookie has SameSite=Lax, and an email change is done through GET /account/email?new=…. Is it protected from CSRF?

Answer

No. Lax sends the cookie on a top-level navigation by link (GET), so a foreign page can redirect to this address. Changes are made only with POST/PUT/DELETE and a token or an Origin check.

Why is XSS on the subdomain blog.example.com dangerous for app.example.com even though the cookie has SameSite=Strict?

Answer

Both subdomains belong to the same site, so for SameSite requests from the blog are not "cross-site": the cookie is attached. So subdomains with risky content are isolated and tokens or an Origin check are kept in addition.

In an interview. Junior: CSRF is when a foreign page makes the user’s browser send a request with his cookie. The defence is a token, SameSite and an Origin check. Middle/Senior: I distinguish site and origin, remember that Lax lets GET navigations through, so changes only with POST and a token; I check subdomains, login CSRF and that a refusal really changed nothing. SameSite is a layer, not a replacement for tokens.

More articles in this chapter