Skip to content

Lesson 3 of 4

How CSRF works

Listen to an AI summary of How CSRF works

0:00 / 0:32

What the browser sends with every request

When Sam signs in, the sports shop gives Sam's browser a cookie. Sam's cookie holds a session id, and the browser sends the cookie with every request to the shop.

The browser sends the shop's cookie with a request to the shop, whichever page started the request, unless a cookie setting stops the browser. A page from one website can make the browser send a request to any other website.

What is cross-site request forgery

A forgery is a fake made to look real. Cross-site means from one website to another. Cross-site request forgery (CWE-352), also called CSRF, is another website making a signed-in person's browser send a request the person did not intend.

A state-changing request is a request that changes something on the server, such as an address, a password or an order. An attacker forges state-changing requests.

  1. 1.Sam signs in to the sports shop

    The browser keeps the session cookie

  2. 2.Sam opens the attacker's page

    From a link in a chat message

  3. 3.The page sends a form to the sports shop

    The browser adds Sam's cookie

  4. 4.The sports shop changes the address

    The request looks like Sam's own

The four steps of a forged request.

The change of address on the sports shop

The sports shop has a form for a new delivery address. The Python code on the server handles the request the form sends. Look at what line 2 reads.

def change_address():
    user = find_user(request.cookies["session"])   # finds who is signed in
    user.line1 = request.form["line1"]             # the first line of the address
    user.city = request.form["city"]               # the city
    save(user)                                     # saves the new address

Line 2 reads the session cookie and finds Sam's record. Lines 3 and 4 copy two form fields, the boxes a person fills in, into Sam's record. No line checks which page started the request.

The page of the attacker

The attacker puts a page on the website evil.example and sends Sam a link in a chat message. The page is written in HTML. Look at the address on line 3, and at line 2.

  1. <html><!-- starts the page -->
  2. <body onload="document.forms[0].submit()"><!-- sends the form when the page has loaded -->
  3. <form method="POST" action="https://shop.example/account/address"><!-- starts a form that sends a request with the method POST to the sports shop -->
  4. <input type="hidden" name="line1" value="PO Box 1198"><!-- is a hidden field named line1 -->
  5. <input type="hidden" name="city" value="Dublin"><!-- is a hidden field named city -->
  6. </form><!-- ends the form -->
  7. </body>
  8. </html><!-- ends the page -->
The attacker's page at evil.example.

On line 3, the word action gives the address the form sends the request to. The address belongs to the sports shop. Lines 4 and 5 are hidden fields, form fields that the page does not show. The hidden fields hold a delivery address the attacker chose.

Line 2 holds a piece of JavaScript. onload tells the browser to run the JavaScript when the page has loaded, and the JavaScript sends the form. Sam presses nothing and sees an empty page.

The request the browser sends

Sam's browser sends the request in the picture to the sports shop. Look at the line that holds the cookie.

  1. POST /account/address HTTP/1.1the method POST, which sends data, and the path, which names the page
  2. Host: shop.examplethe website the request goes to
  3. Origin: https://evil.examplethe website of the page that started the request
  4. Cookie: session=7f3a9cSam's cookie, added by the browser
  5. an empty line ends the headers
  6. line1=PO+Box+1198&city=Dublinthe attacker's address, where + stands for a space
The forged request. The lines Host, Origin and Cookie are headers, named lines that hold extra information.

The request goes to shop.example, so the browser adds Sam's cookie. The attacker never learns the session id. The server saves the attacker's address, and Sam's next order goes to the attacker.

The forged request has the same cookie as a request from the shop's own form. The shop's code checks the cookie and nothing else, so the code cannot tell the two requests apart.

The next lesson covers the checks that refuse a forged request, such as a check of the Origin header. Some frameworks check every request unless a line switches the check off.

When the browser leaves the cookie off

The values Lax and Strict of the cookie setting SameSite (Sessions) keep the cookie off the forged request in the example. The sports shop set the cookie with no SameSite setting. Some browsers treat such a cookie as Lax, and others send the cookie, so safer code sets the value.

What a forged request can do

A forged request can do only what the signed-in person can do. A forged request from the browser of an administrator can change any account.

A GET request that changes something

A request whose method, the first word of the request, is GET asks to read a page. Some code changes something on a GET request, such as cancelling an order. The attacker then needs no form. An img tag, the HTML for a picture, tells the browser to fetch the picture from an address.

<img src="https://shop.example/order/cancel?id=1042">

The address after src cancels order 1042 on the sports shop. The browser sends a GET request to that address. Where the cookie has no SameSite setting, some browsers add Sam's cookie. A link that Sam follows to the same address carries the cookie in every browser, even with SameSite=Lax.

How CSRF differs from cross-site scripting

In cross-site scripting, also called XSS, the attacker's code runs inside a page of the sports shop (the lesson on cross-site scripting). In cross-site request forgery no code of the attacker runs on the sports shop. The attacker's page stays on the attacker's own website and only sends a request.

Examples of what to look for

  • Code that handles a state-changing request and checks the session cookie and nothing else.
  • Code that changes something on a GET request, such as cancelling an order.
  • A line that switches off a framework's check for forged requests, such as @csrf_exempt in Django (Python). Spring Security (Java) uses csrf(csrf -> csrf.disable()), or csrf().disable() in older code, and Rails (Ruby) uses skip_forgery_protection.

Check your understanding

  1. State what the browser adds to a request to the sports shop, whichever page started the request, when no cookie setting stops the browser.

    Show the answer

    The browser adds the shop's cookie, which holds Sam's session id.

  2. Which line of the attacker's page holds the address the form sends the request to? Name the website the address belongs to.

    Show the answer

    Line 3, after the word action. The address belongs to the sports shop.

  3. Give one reason why the sports shop cannot tell the forged request from a request Sam chose to send.

    Show the answer

    The forged request has Sam's own session cookie, and the shop's code checks the cookie and nothing else.

Practise it

Challenges with a weakness this lesson teaches.