---
url: "https://owasp.org/www-community/attacks/csrf"
titulek: "Cross Site Request Forgery (CSRF) | OWASP Foundation"
h1: "Cross Site Request Forgery (CSRF)"
popis: "Cross Site Request Forgery (CSRF) on the main website for The OWASP Foundation. OWASP is a nonprofit foundation that works to improve the security of software."
jazyk: "en"
kanonicka: "https://community.owasp.org/attacks/csrf"
drobecky: []
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-14 14:33:37"
znaku: 15665
slov: 2156
kodovani: "utf-8"
sha256: "0f0911cb525fd51f75ba6c7228031ae6745a78567604029d1a48a1a21c0e2194"
---
# Cross Site Request Forgery (CSRF)
**Author:** KirstenS
**Contributor(s):** Dave Wichers, Davisnw, Paul Petefish, Adar Weidman, Michael Brooks, Ahsan Mir, Dc, D0ubl3 h3lix, Jim Manico, Robert Gilbert, Tgondrom, Pawel Krawczyk, Brandt, A V Minhaz, Kevin Lorenzo, Andrew Smith, Christina Schelin, Ari Elias-Bachrach, Sarciszewski, kingthorin, Ben Spatafora, Krishna Madala
## Overview
Cross-Site Request Forgery (CSRF) is an attack that forces an end user
to execute unwanted actions on a web application in which they’re
currently authenticated. With a little help of social engineering
(such as sending a link via email or chat), an attacker may trick the
users of a web application into executing actions of the attacker’s
choosing. If the victim is a normal user, a successful CSRF attack can
force the user to perform state changing requests like transferring
funds, changing their email address, and so forth. If the victim is an
administrative account, CSRF can compromise the entire web application.
### How to Review Code for CSRF Vulnerabilities
See the [OWASP Code Review
Guide](/www-project-code-review-guide) article on how to
[review code for CSRF
vulnerabilities](/www-project-code-review-guide/reviewing-code-for-csrf-issues).
### How to Test for CSRF Vulnerabilities
See the [OWASP Testing Guide](https://owasp.org/www-project-web-security-testing-guide/) article on how to
test for CSRF vulnerabilities.
### How to Prevent CSRF Vulnerabilities
See the [CSRF Prevention Cheat
Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html) for prevention measures.
Listen to the [OWASP Top Ten CSRF
Podcast](http://wiki.owasp.org/download/jmanico/owasp_podcast_69.mp3).
Most frameworks have built-in CSRF support such as
[Joomla](http://docs.joomla.org/How_to_add_CSRF_anti-spoofing_to_forms),
[Spring](http://blog.eyallupu.com/2012/04/csrf-defense-in-spring-mvc-31.html),
[Struts](http://web.securityinnovation.com/appsec-weekly/blog/bid/84318/Cross-Site-Request-Forgery-CSRF-Prevention-Using-Struts-2),
[Ruby on
Rails](http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf),
[.NET](https://www.troyhunt.com/2010/11/owasp-top-10-for-net-developers-part-5.html)
and others.
Use [OWASP CSRF Guard](/www-project-csrfguard/) to
add CSRF protection to your Java applications.
John Melton also has an [excellent blog
post](http://www.jtmelton.com/2010/05/16/the-owasp-top-ten-and-esapi-part-6-cross-site-request-forgery-csrf/)
describing how to use the native anti-CSRF functionality of the [OWASP
ESAPI](http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API).
## Description
CSRF is an attack that tricks the victim into submitting a malicious
request. It inherits the identity and privileges of the victim to
perform an undesired function on the victim’s behalf (though note that
this is not true of login CSRF, a special form of the attack described
below). For most sites, browser requests automatically include any
credentials associated with the site, such as the user’s session
cookie, IP address, Windows domain credentials, and so forth.
Therefore, if the user is currently authenticated to the site, the site
will have no way to distinguish between the forged request sent by the
victim and a legitimate request sent by the victim.
CSRF attacks target functionality that causes a state change on the
server, such as changing the victim’s email address or password, or
purchasing something. Forcing the victim to retrieve data doesn’t
benefit an attacker because the attacker doesn’t receive the response,
the victim does. As such, CSRF attacks target state-changing requests.
An attacker can use CSRF to obtain the victim’s private data via a
special form of the attack, known as login CSRF. The attacker forces
a non-authenticated user to log in to an account the attacker controls.
If the victim does not realize this, they may add personal data—such as
credit card information—to the account. The attacker can then log back
into the account to view this data, along with the victim’s activity
history on the web application.
It’s sometimes possible to store the CSRF attack on the vulnerable site
itself. Such vulnerabilities are called “stored CSRF flaws”. This can be
accomplished by simply storing an IMG or IFRAME tag in a field that
accepts HTML, or by a more complex cross-site scripting attack. If the
attack can store a CSRF attack in the site, the severity of the attack
is amplified. In particular, the likelihood is increased because the
victim is more likely to view the page containing the attack than some
random page on the Internet. The likelihood is also increased because
the victim is sure to be authenticated to the site already.
### Synonyms
CSRF attacks are also known by a number of other names, including XSRF,
“Sea Surf”, Session Riding, Cross-Site Reference Forgery, and Hostile
Linking. Microsoft refers to this type of attack as a One-Click attack
in their threat modeling process and many places in their online
documentation.
## Prevention measures that do **NOT** work
A number of flawed ideas for defending against CSRF attacks have been
developed over time. Here are a few that we recommend you avoid.
Remember that all cookies, even the *secret* ones, will be submitted
with every request. All authentication tokens will be submitted
regardless of whether or not the end-user was tricked into submitting
the request. Furthermore, session identifiers are simply used by the
application container to associate the request with a specific session
object. The session identifier does not verify that the end-user
intended to submit the request.
### Only accepting POST requests
Applications can be developed to only accept POST requests for the
execution of business logic. The misconception is that since the
attacker cannot construct a malicious link, a CSRF attack cannot be
executed. Unfortunately, this logic is incorrect. There are numerous
methods in which an attacker can trick a victim into submitting a forged
POST request, such as a simple form hosted in an attacker’s Website with
hidden values. This form can be triggered automatically by JavaScript or
can be triggered by the victim who thinks the form will do something
else.
### Multi-Step Transactions
Multi-Step transactions are not an adequate prevention of CSRF. As long
as an attacker can predict or deduce each step of the completed
transaction, then CSRF is possible.
### URL Rewriting
This might be seen as a useful CSRF prevention technique as the attacker
cannot guess the victim’s session ID. However, the user’s session ID is
exposed in the URL. We don’t recommend fixing one security flaw by
introducing another.
### HTTPS
HTTPS by itself does nothing to defend against CSRF.
However, HTTPS should be considered a prerequisite for any preventative
measures to be trustworthy.
This doesn’t work in practice because the referrer header can be easily
spoofed by an attacker. Additionally, some users or browsers might not
send the referrer header due to privacy settings or policies, leading to false
positives. Moreover, there are situations where the referrer can be null, such
as when a user navigates to a site from a bookmark or any other resource without
a traditional url. In these scenarios, legitimate requests could be mistaken as
potential CSRF attacks, which would result in more potential false positive flags.
## Examples
### How does the attack work?
There are numerous ways in which an end user can be tricked into loading
information from or submitting information to a web application. In
order to execute an attack, we must first understand how to generate a
valid malicious request for our victim to execute. Let us consider the
following example: Alice wishes to transfer $100 to Bob using the
*bank.com* web application that is vulnerable to CSRF. Maria, an
attacker, wants to trick Alice into sending the money to Maria instead.
The attack will comprise the following steps:
1. Building an exploit URL or script
2. Tricking Alice into executing the action with [Social Engineering](https://en.wikipedia.org/wiki/Social_engineering_(security))
#### GET scenario
If the application was designed to primarily use GET requests to
transfer parameters and execute actions, the money transfer operation
might be reduced to a request like:
`GET http://bank.com/transfer.do?acct=BOB&amount=100 HTTP/1.1`
Maria now decides to exploit this web application vulnerability using
Alice as the victim. Maria first constructs the following exploit URL
which will transfer $100,000 from Alice’s account to Maria’s account. Maria
takes the original command URL and replaces the beneficiary name with
herself, raising the transfer amount significantly at the same time:
`http://bank.com/transfer.do?acct=MARIA&amount=100000`
The [social engineering](https://en.wikipedia.org/wiki/Social_engineering_(security)) aspect of the
attack tricks Alice into loading this URL when Alice is logged into the
bank application. This is usually done with one of the following
techniques:
- sending an unsolicited email with HTML content
- planting an exploit URL or script on pages that are likely to be visited by the victim while they are also doing online banking
The exploit URL can be disguised as an ordinary link, encouraging the
victim to click it:
`View my Pictures!`
Or as a 0x0 fake image:
``
If this image tag were included in the email, Alice wouldn’t see
anything. However, the browser *will still* submit the request to
bank.com without any visual indication that the transfer has taken
place.
A real life example of CSRF attack on an application using GET was a
[uTorrent
exploit](https://www.ghacks.net/2008/01/17/dos-vulnerability-in-utorrent-and-bittorrent/)
from 2008 that was used on a mass scale to download malware.
#### POST scenario
The only difference between GET and POST attacks is how the attack is
being executed by the victim. Let’s assume the bank now uses POST and
the vulnerable request looks like this:
```
POST http://bank.com/transfer.do HTTP/1.1
acct=BOB&amount=100
```
Such a request cannot be delivered using standard A or IMG tags, but can
be delivered using a FORM tags:
```