Lesson 3 of 4
How CSRF works
Listen to an AI summary of How CSRF works
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.Sam signs in to the sports shop
The browser keeps the session cookie
2.Sam opens the attacker's page
From a link in a chat message
3.The page sends a form to the sports shop
The browser adds Sam's cookie
4.The sports shop changes the address
The request looks like Sam's own
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.
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.
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
GETrequest, such as cancelling an order. - A line that switches off a framework's check for forged requests, such as
@csrf_exemptin Django (Python). Spring Security (Java) usescsrf(csrf -> csrf.disable()), orcsrf().disable()in older code, and Rails (Ruby) usesskip_forgery_protection.
Check your understanding
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.
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.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.
- Payday remindersBrightledger, IntermediateOpen
- Follow and unfollowChirp, IntermediateOpen
- Delete a postChirp, IntermediateOpen