In the world of web security, not all attacks involve breaking passwords or injecting code. Some attacks exploit something much simpler—user trust.

One of the most dangerous examples of this is the CSRF (Cross-Site Request Forgery) attack.

CSRF attacks trick users into performing actions they never intended, such as changing passwords, transferring money, or updating account settings.

What makes CSRF particularly dangerous is that it uses legitimate user sessions, making it harder to detect.

In this complete guide, you’ll learn what CSRF is, how it works with real examples, and how to prevent it effectively.


โš ๏ธ What is a CSRF Attack?

CSRF (Cross-Site Request Forgery) is a type of attack where a malicious website tricks a user’s browser into sending unauthorized requests to another website where the user is already authenticated.

๐Ÿ‘‰ In simple terms:
The attacker forces a logged-in user to perform an action without their knowledge.


๐Ÿ’ฃ How CSRF Works

A CSRF attack relies on three key elements:

  1. The user is logged into a website
  2. The website uses cookies for authentication
  3. The attacker tricks the user into sending a request

๐Ÿ” Example Scenario

Imagine a user is logged into a banking website.

The attacker creates a malicious link:

If the user clicks or loads this page:

๐Ÿ‘‰ The browser automatically sends the request
๐Ÿ‘‰ The bank processes it as a valid request

Result:
๐Ÿ’ธ Money gets transferred without user consent


๐Ÿšจ Impact of CSRF Attacks

CSRF attacks can lead to:

  • Unauthorized transactions
  • Account changes
  • Password updates
  • Data manipulation

๐Ÿ‘‰ In critical systems, this can cause financial and reputational damage.


๐Ÿ” Types of CSRF Attacks


๐Ÿ”ด 1. GET-Based CSRF

Uses simple URLs to perform actions.


๐Ÿ”ด 2. POST-Based CSRF

Uses forms to send hidden requests.


๐Ÿ”ด 3. Login CSRF

Forces users to log into attacker-controlled accounts.


๐Ÿงช Real Example (Form-Based CSRF)

 


 

 

 

 

๐Ÿ‘‰ If user is logged in, password gets changed automatically


๐Ÿ›ก๏ธ How to Prevent CSRF Attacks


โœ… 1. Use CSRF Tokens (MOST IMPORTANT)

Generate a unique token for each request.

Example:

$_SESSION['csrf_token'] = bin2hex(random_bytes(32));

Verify it on form submission.


โœ… 2. Use SameSite Cookies

Set cookies with:

SameSite=Strict

๐Ÿ‘‰ Prevents cross-site requests


โœ… 3. Validate HTTP Referer & Origin

Check if request comes from trusted source.


โœ… 4. Use POST Instead of GET

Sensitive actions should not use GET requests.


โœ… 5. Require User Interaction

Add confirmation steps for critical actions.


โœ… 6. Implement CAPTCHA

Prevents automated attacks.


โœ… 7. Use Secure Frameworks

Frameworks like:

  • CodeIgniter
  • Laravel

have built-in CSRF protection.


๐Ÿ”ง How to Fix CSRF Vulnerability


Step 1: Identify Vulnerable Actions

Check:

  • Forms
  • API requests
  • Sensitive operations

Step 2: Add CSRF Tokens

Ensure every request includes a valid token.


Step 3: Validate Requests

Reject requests without valid tokens.


Step 4: Secure Cookies

Use:

  • HttpOnly
  • Secure
  • SameSite

Step 5: Test Again

Use tools to confirm protection.


๐Ÿงช Testing for CSRF

Use tools like:

  • Burp Suite
  • OWASP ZAP

Manual testing:

  • Remove token and test request

โšก Secure Coding Best Practices

  • Never trust requests blindly
  • Use CSRF tokens everywhere
  • Validate origin
  • Secure session handling

๐ŸŒ Real-World Impact

Many major platforms have faced CSRF vulnerabilities.

๐Ÿ‘‰ Common reasons:

  • Missing tokens
  • Poor validation

๐Ÿ“Š SEO & Business Impact

If your site is vulnerable:

  • User trust decreases
  • Data breaches occur
  • SEO ranking drops

๐Ÿš€ Developer Tips (For You ๐Ÿ‘)

Since you use PHP & CodeIgniter:

๐Ÿ‘‰ Enable CSRF protection in CI4:


 

public $CSRFProtection = true;

๐Ÿ‘‰ Always use:

  • Built-in security features

๐Ÿ“Š Conclusion

CSRF attacks are dangerous because they exploit user trust rather than system weaknesses.

However, they are easy to prevent with proper implementation of CSRF tokens and secure practices.

By following the techniques in this guide, you can protect your web applications effectively.

In 2026, secure authentication and request validation are essential for every developer.

FAQs

What is a CSRF attack?

An attack that tricks users into performing unwanted actions.


How to prevent CSRF?

Use CSRF tokens and validate requests.


Is CSRF still relevant?

Yes, it remains a major vulnerability.