Cybersecurity / 9 min read
Cross Site Request Forgery
Cross Site Request Forgery, commonly called CSRF, is an attack that causes a person’s browser to perform an unwanted action on a website where they are already signed in.

Overview
Cross Site Request Forgery, commonly called CSRF, is an attack that causes a person’s browser to perform an unwanted action on a website where they are already signed in.
An attacker might send a misleading link through email or chat, or place malicious content on a page the victim visits while using the targeted application.
If the application accepts the resulting request without checking whether it came from a legitimate interaction, it may carry out the action using the victim’s account.
For an ordinary user, this could mean transferring money, changing an email address or making an unwanted purchase.
For an administrator, the consequences can extend to changes that affect the entire application.
How CSRF Works
A browser can automatically attach credentials to requests sent to a website, including session cookies and certain forms of browser managed authentication.
Whether those credentials accompany a particular request depends on browser rules, cookie settings and the type of request being made.
A vulnerable application treats the presence of valid credentials as enough reason to perform an action, without separately checking whether the user intended to request it.
The attacker takes advantage of this behaviour by arranging for the victim’s browser to send a request containing parameters chosen by the attacker.
The browser supplies any applicable credentials, and the application processes the request with the victim’s permissions.
The attacker usually does not need to know the victim’s password or read their session cookie.
Why State Changes Matter
CSRF usually targets operations that change something on the server, such as updating account details, submitting an order or transferring funds.
Simply making the browser retrieve a private page generally provides less benefit because the response goes to the victim’s browser, and the attacker normally cannot read it.
The main objective is therefore to make the application perform an action rather than directly obtain the response.
Login CSRF
Login CSRF works differently from the usual case because it can target someone who is not yet authenticated.
The attacker causes the victim’s browser to sign in to an account controlled by the attacker.
If the victim does not notice which account they are using, they may enter personal information, save payment details or carry out activities that become associated with that account.
The attacker can later access the account and inspect information or activity the application has retained.
Stored CSRF
An attacker may be able to place the content that triggers a CSRF request inside the vulnerable application itself.
This is sometimes called stored CSRF.
For example, a field that accepts HTML might allow an image or iframe whose URL triggers an unwanted operation.
Cross site scripting can also provide a way to place or execute malicious content within the application.
Hosting the trigger inside the application can increase the chance of a victim encountering it during an authenticated session.
The victim may also be more willing to trust content displayed on a website they already use.
Other Names
CSRF is also known as XSRF, Sea Surf, Session Riding, Cross Site Reference Forgery and Hostile Linking.
Microsoft has also used the term One Click attack in threat modelling material and documentation.
Approaches That Do Not Provide Sufficient Protection
Relying on a Secret Cookie
A cookie does not prove that the user intended to perform an action merely because its value is secret.
If the browser automatically includes that cookie with a forged request, the application receives the same credential it would receive during normal use.
A session identifier establishes which session a request belongs to, but it does not establish why the request was sent.
This differs from a properly implemented CSRF token mechanism that requires and validates additional information beyond an automatically attached cookie.
Accepting Only POST Requests
Restricting operations to POST requests does not eliminate CSRF.
An attacker can prepare an HTML form containing hidden fields and persuade the victim to submit it.
JavaScript on the attacker’s page may also submit the form automatically.
Using suitable HTTP methods remains important, but the method alone does not establish user intent.
Splitting an Operation Into Several Steps
A transaction does not become resistant to CSRF simply because it requires several requests.
If the attacker can predict the required sequence and supply the necessary values, they may be able to reproduce the steps through the victim’s browser.
Protection must come from checks that the attacker cannot satisfy, rather than from the number of screens involved.
Putting Session Identifiers in URLs
Including a session identifier in a URL may appear useful because an attacker might not know its value.
However, this exposes the identifier through places such as browser history, copied links, logs and potentially referral information.
A defence should not depend on introducing another way to disclose session credentials.
Relying on HTTPS
HTTPS protects traffic while it travels between the browser and the server.
It does not prevent the browser from sending an unwanted request over that protected connection.
Use HTTPS alongside CSRF defences so that attackers cannot undermine them by modifying unencrypted traffic.
Using Incomplete Origin or Referer Checks
Checking the Origin or Referer header can provide meaningful CSRF protection when implemented correctly.
An attacker’s ordinary browser script cannot freely set these headers to arbitrary trusted values, so describing them as easily spoofed in a browser based CSRF attack is misleading.
However, headers may be absent or contain an opaque origin, and incomplete hostname comparisons can allow untrusted origins to pass validation.
Compare complete origins against trusted values and define how protected endpoints handle missing information rather than silently accepting every request without it. OWASP Cheat Sheet Series
Examples
The Banking Scenario
Alice uses a banking application to send money to Bob.
Maria wants to make Alice’s browser submit a different transfer while Alice is signed in.
To do this, Maria needs to construct a request that the vulnerable application will accept and find a way to make Alice’s browser send it.
The following examples use an illustrative banking address.
Example 1: A Transfer Through GET
Suppose the application allows transfers through a GET request.
Alice’s intended transfer of 100 dollars to Bob might use this request.
GET /transfer.do?acct=BOB&amount=100 HTTP/1.1
Host: bank.example
Maria changes the recipient and amount to create a request for a transfer of 100,000 dollars to her own account.
https://bank.example/transfer.do?acct=MARIA&amount=100000
She then places the URL behind a misleading link.
<a href="https://bank.example/transfer.do?acct=MARIA&amount=100000">
View my pictures
</a>
If Alice follows the link while authenticated, and the browser supplies the required credentials, the vulnerable application may process the transfer.
Maria could also place the URL in an image element.
<img
src="https://bank.example/transfer.do?acct=MARIA&amount=100000"
width="0"
height="0"
alt=""
>
If the browser loads the image URL and includes the required authentication, the request may reach the transfer endpoint without Alice deliberately clicking a banking link.
Whether this succeeds depends on protections such as cookie restrictions, browser behaviour and the application’s CSRF checks.
Embedding an image in an email also depends on whether the email client loads remote content and whether the request carries usable authentication.
The original article cites a 2008 attack involving uTorrent as a historical example of CSRF being used to trigger unwanted downloads.
Example 2: A Transfer Through POST
Suppose the bank changes its transfer operation to accept POST requests.
Alice’s legitimate request might look like this.
POST /transfer.do HTTP/1.1
Host: bank.example
Content-Type: application/x-www-form-urlencoded
acct=BOB&amount=100
An ordinary link or image cannot directly generate this POST request, but an HTML form can.
Maria could prepare a form containing her chosen recipient and amount.
<form action="https://bank.example/transfer.do" method="POST">
<input type="hidden" name="acct" value="MARIA">
<input type="hidden" name="amount" value="100000">
<input type="submit" value="View my pictures">
</form>
The button suggests that it opens pictures, while submitting it actually sends the transfer parameters to the bank.
The attacker could also submit the form automatically when the page loads.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pictures</title>
</head>
<body>
<form
id="transfer"
action="https://bank.example/transfer.do"
method="POST"
>
<input type="hidden" name="acct" value="MARIA">
<input type="hidden" name="amount" value="100000">
</form>
<script>
document.getElementById("transfer").submit();
</script>
</body>
</html>
Success still depends on the browser sending the necessary credentials and the server accepting the request without adequate verification.
Changing the operation from GET to POST therefore removes one delivery method while leaving the underlying vulnerability unresolved.
Example 3: PUT Requests and JSON
APIs may use methods such as PUT or DELETE rather than relying exclusively on GET and POST.
For example, a transfer endpoint might expect a PUT request containing JSON.
PUT /transfer.do HTTP/1.1
Host: bank.example
Content-Type: application/json
{"acct":"BOB","amount":100}
JavaScript can attempt to send a similar request from another website.
<script>
async function submitTransfer() {
await fetch("https://bank.example/transfer.do", {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
acct: "MARIA",
amount: 100000
})
});
}
</script>
A browser normally sends a CORS preflight before allowing this combination of method and content type across origins.
The server must permit the requesting origin, method and headers before the browser sends the actual request.
For a request using cookies across origins, the server must also permit credentials and specify an allowed origin explicitly.
The wildcard value in the following header does not authorise credentialed CORS access by itself.
Access-Control-Allow-Origin: *
An unsafe CORS configuration can expose an application, but the wildcard header alone does not make the authenticated PUT example work. OWASP Cheat Sheet Series
Reviewing and Testing Applications
Code reviews should examine operations that change application state and identify what prevents another website from triggering them through an authenticated browser.
Testing should cover sensitive actions such as account updates, password changes, purchases and administrative operations.
The OWASP Code Review Guide and Web Security Testing Guide provide material for reviewing and testing these weaknesses.
The OWASP CSRF Prevention Cheat Sheet describes defensive approaches and implementation considerations.
Framework Support and Protection Libraries
Many application frameworks provide CSRF protection, including frameworks and platforms such as Spring, Ruby on Rails, Struts, Joomla and .NET.
Developers should check the protection available in their chosen framework, enable it where required and confirm that sensitive endpoints are covered.
For Java applications, OWASP CSRFGuard provides a dedicated option for adding CSRF protection.
OWASP ESAPI also includes related functionality, with implementation material discussed in the resources accompanying the original article.
The original material additionally points readers to the OWASP Top Ten CSRF podcast for further explanation.
Related Attacks
Cross Site Scripting, commonly called XSS, can interact with CSRF by allowing malicious code to run within a trusted application.
Cross Site History Manipulation, commonly called XSHM, is another related topic identified in the original article.
References
OWASP CSRF Prevention Cheat Sheet
OWASP CORS Security Cheat Sheet
← Back to Cybersecurity