CIDADAXSEC
← All topics
Practitioner

Cross-site request forgery (CSRF)

CSRF forces a victim's browser to send an unauthorized request to a site they are logged into, so the app treats it as legitimate.

Material

Cross-site request forgery occurs when an application trusts a browser request simply because it carries a valid session cookie. The attacker hosts a page that forces the victim's browser to perform an action without the victim's knowledge or consent.

How it works

The victim's browser sends the session cookie automatically to the target domain, no matter which page is open. The only thing telling a legitimate request apart from a forged one is the page origin, and that origin never travels with the request.

html
<!DOCTYPE html>
<html lang="id">
<body onload="document.getElementById('csrf').submit()">
  <form action="https://target.website/account/email" method="POST" id="csrf">
    <input type="hidden" name="email" value="attacker@example.com">
  </form>
</body>
</html>

The form above is submitted automatically as soon as the page loads. Because SameSite for session cookies now leans toward the Lax mode, a cross-site post like this still gets through on a top-level navigation, so POST endpoints need separate protection.

http
POST /account/email HTTP/1.1
Host: target.website
Cookie: session=8f2c19b7e0
Content-Type: application/x-www-form-urlencoded
Origin: https://evil.example
Referer: https://evil.example/csrf.html

email=attacker@example.com

Attack forms

  • A hidden form on the attacker's page, submitted automatically without any interaction from the victim.
  • A state-changing GET triggered through an image tag or a link, running an action that should have required a POST.
  • A SameSite bypass through method override, since a cross-site POST navigation changes character once it is reduced to a GET.
  • A multi-step attack, such as changing an email and then confirming it, which needs two sequential requests from the victim's browser.
  • Fragile Referer-based rejection, because it only checks whether the Referer header is present rather than what it says.

Prevention

  • A random CSRF token bound to the session and verified by the server on every state-changing request.
  • Compare Origin or Sec-Fetch-Site instead of merely checking whether a Referer header exists.
  • Set SameSite=Lax or Strict on session cookies, and keep the token because Lax does not cover every navigation case.
  • Ask for an explicit confirmation or re-authentication for high-risk operations such as changing an email or a payout destination.
  • Add the CSP directive form-action 'self' so forms cannot be submitted to another site.
http
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Set-Cookie: session=8f2c19b7e0; HttpOnly; Secure; SameSite=Strict
Content-Security-Policy: form-action 'self'
X-Frame-Options: DENY

Remember that CSRF protection only matters for state-changing requests. Read-only endpoints generally do not need it, and adding a token there only adds overhead without benefit.

Note: a SameSite cookie alone is never enough, because a client-side redirect and a sibling domain can make a request look same-site. The token stays mandatory as long as sensitive endpoints remain reachable through a plain form.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.