What is Cross-Site Request Forgery (CSRF) and How to Protect Your Website?

Cross-Site Request Forgery (CSRF) is a type of cyberattack that tricks a victim into performing unwanted actions on a web application in which they are authenticated. Often referred to as one-click attacks, session riding, or silent attacks, CSRF exploits the trust that a website has in a user’s browser.
In this post, we’ll dive into what CSRF attacks are, how they work, their potential risks, and the best practices to protect your website from this malicious technique.
What is Cross-Site Request Forgery (CSRF)?
Cross-Site Request Forgery (CSRF) occurs when an attacker tricks an authenticated user into making an unwanted request to a web application, often without the user’s knowledge. These requests could lead to actions such as changing a password, making unauthorized financial transactions, or deleting important files.
CSRF attacks exploit the trust that a web application has in the user’s browser, rather than the user’s trust in the website. Because the victim is authenticated (i.e., logged in) to a site, their session cookies and credentials are automatically sent along with the malicious request, allowing the attacker to impersonate the victim.
How Does a CSRF Attack Work?
1. Attacker’s Crafting of a Malicious Request:
The attacker first crafts a malicious HTTP request that performs an action on a web application. For example, the attacker may create a URL that will trigger a transfer of funds or change the password of a user account.
2. Victim is Tricked into Clicking a Link:
The attacker then tricks the victim into performing the malicious request, usually by embedding it in an email, website, or a social media message. This link may appear to be innocuous, such as a link to a harmless-looking website or an image.
3. Malicious Request is Sent with Authentication Information:
If the victim is logged into a website (like a bank, social media platform, or email account), their session cookies are automatically sent with the malicious request. The web application believes the request is coming from the authenticated user, and the malicious action is executed.
For example, suppose the victim is logged into their banking account. The attacker sends the victim an email with a link that, when clicked, triggers a money transfer from the victim’s account to the attacker’s account. The victim doesn’t know they’ve initiated the transfer, but the bank website processes it as a legitimate action because the victim is authenticated.
Common CSRF Attack Scenarios
CSRF can be used to perform a variety of malicious actions on websites, such as:
- Changing Account Settings: An attacker could trick the victim into changing their email address, password, or other sensitive information.
- Making Financial Transactions: If the victim is logged into their bank account, an attacker could initiate a transfer of funds to an attacker-controlled account.
- Deleting Data: An attacker could exploit CSRF to delete important files or data from a user’s account, causing data loss or corruption.
- Posting Content on Social Media: An attacker could exploit CSRF vulnerabilities to post spam or malicious content to a victim’s social media accounts.
Why is CSRF Dangerous?
1. Unwarranted Actions
CSRF allows attackers to perform actions on behalf of the victim without their consent. Since the malicious requests are sent with the victim’s authentication credentials (e.g., session cookies), the web application cannot distinguish between legitimate and fraudulent requests.
2. No Need for Complex Exploits
CSRF is an easy-to-execute attack that doesn’t require complex technical knowledge. An attacker can simply trick the victim into clicking a malicious link or loading an image with a request embedded in it.
3. Exploitation of Trusted Websites
The attack exploits the trust between a user and a website. Since the victim is already logged in to the website, the attacker doesn’t need to bypass authentication systems.
How to Prevent CSRF Attacks
Fortunately, there are several techniques and best practices you can implement to protect your website from CSRF vulnerabilities:
1. Use Anti-CSRF Tokens
The most effective way to prevent CSRF attacks is by implementing anti-CSRF tokens. This is a unique, unpredictable value added to each HTTP request, which ensures that the request is coming from the user’s legitimate session.
- Each form submitted by the user should contain a hidden field with a token that is checked by the server.
- The token is validated on the server-side to ensure that the request is valid and originates from the correct user.
- If the token is missing or invalid, the request is rejected.
2. SameSite Cookie Attribute
The SameSite cookie attribute is a feature that helps mitigate CSRF by ensuring that cookies are only sent in requests originating from the same domain.
- SameSite=Strict: Cookies are only sent if the request originates from the same site.
- SameSite=Lax: Cookies are sent in top-level navigations but not in cross-site requests (e.g., links).
- SameSite=None; Secure: Cookies are sent in cross-site requests, but only over HTTPS.
By using SameSite cookies, browsers will block cross-site requests from sending authentication cookies, which can help prevent CSRF attacks.
3. Double-Submit Cookies
Another defense mechanism involves sending the same token in both a cookie and in the request. The server checks if both values match. If they don’t, the request is rejected.
4. Check the Referer Header
The Referer header of an HTTP request indicates the origin of the request. By checking the Referer header, the server can ensure that the request is coming from a trusted domain. While this isn’t a foolproof method on its own, it can add an additional layer of protection against CSRF attacks.
5. Require Reauthentication for Critical Actions
For sensitive actions, such as changing account passwords or making financial transactions, require the user to reauthenticate (e.g., by entering their password again) before performing the action. This ensures that even if a CSRF attack occurs, the attacker will not be able to perform critical actions without the victim’s explicit consent.
6. Use CAPTCHA or Two-Factor Authentication (2FA)
For high-risk actions like logging in, changing account settings, or making financial transactions, requiring CAPTCHA or two-factor authentication (2FA) can add an extra layer of security. This helps ensure that actions are being performed by the legitimate user.
Conclusion
Cross-Site Request Forgery (CSRF) is a dangerous vulnerability that can lead to unauthorized actions being taken on behalf of authenticated users. Fortunately, there are effective countermeasures that web developers can implement to protect their applications, including anti-CSRF tokens, SameSite cookies, double-submit cookies, and more.
By following these best practices, organizations can secure their websites from CSRF attacks and ensure that users are protected from malicious actions. Always stay vigilant, monitor your web applications, and implement the latest security measures to safeguard your users and their sensitive data.

Need Help Securing Your Website Against CSRF and Other Vulnerabilities?
If you’re looking to improve the security of your website or application, CyberVolt offers comprehensive web security solutions to protect against CSRF and other common vulnerabilities.
Contact us today to learn how we can help protect your business from cyber threats.