Cross-Site Request Forgery (CSRF)

What is CSRF

Cross-Site Request Forgery (CSRF) is an attack that tricks the victim into submitting a malicious request. It uses the identity and privileges of the victim to perform an undesired function on their behalf. For most sites, browser requests automatically include any credentials associated with the site, such as the user's session cookie, IP address, etc. Therefore, if the user is authenticated to the site, the site cannot distinguish between legitimate requests and forged requests.


CSRF Attack Techniques

There are several techniques that attackers use to exploit CSRF vulnerabilities:


Preventing CSRF

Preventing CSRF involves using anti-CSRF tokens, checking the HTTP Referer header, and using the SameSite cookie attribute. Anti-CSRF tokens can be included in requests and verified by the server. They must be unpredictable and securely generated. The HTTP Referer header can be checked by the server to see if the request is made from an authorized page. The SameSite cookie attribute can be used to disable third-party usage for a particular cookie, helping protect against CSRF attacks.