- by x32x01 ||
In this thread, we’ll break down one of the most dangerous and common web security issues in modern applications: CSRF (Cross-Site Request Forgery) 🔥. You’ll learn how attackers exploit it to silently change user passwords and how to properly defend your applications.
At first glance, this seems fine 👍
But there’s a critical missing question:
That means if a user is logged in, any request - legit or malicious - will include their session automatically.
When the victim opens this page while logged in:
Here’s what’s wrong:
This ensures only legitimate requests from your frontend are accepted.
This adds an extra layer of protection before allowing password changes.
Use this instead ✅:
Because secure: false allows cookies to be sent over HTTP, which can be intercepted easily 🚨
If your app relies only on sessions without CSRF protection, attackers can silently perform actions on behalf of users without them ever noticing.
What’s the problem with the password change endpoint? 🔐
We have a simple endpoint that allows users to change their password. The developer only checks if the user is logged in using a session: PHP:
if (!req.session.user) But there’s a critical missing question:
The application blindly trusts the session, which opens the door to CSRF attacks 🧨Is this request really coming from the actual user or from an external attacker?
How CSRF attack works ⚠️
Browsers automatically attach cookies (including session cookies) to every request sent to a trusted domain.That means if a user is logged in, any request - legit or malicious - will include their session automatically.
Normal request example:
Code:
POST /change-password
new_password=newPass123 Attack scenario 💣
An attacker creates a malicious webpage: HTML:
<form action="https://victim.com/change-password" method="POST">
<input type="hidden" name="new_password" value="hacked123">
</form>
<script>
document.forms[0].submit();
</script> - The browser automatically sends the session cookie 🍪
- The server thinks the request is legitimate
- The password gets changed without the user’s consent 😱
Why this code is dangerous 🚨
In the original implementation: JavaScript:
new_password = req.body.new_password - No confirmation of current password
- No CSRF protection at all
- Full trust in session authentication
Impact of CSRF attacks 💥
CSRF vulnerabilities can lead to serious damage, including:- Unauthorized password changes 🔑
- Full account takeover
- Performing actions without user consent
- Abuse of admin or privileged sessions
How to fix CSRF vulnerabilities 🛡️
1. Use CSRF tokens
Generate a unique token for each session and validate it on every sensitive request: PHP:
const csrf = require('csurf');
app.use(csrf()); 2. Require current password confirmation
PHP:
if (users[username].password !== req.body.current_password) {
return res.status(403).send("Wrong current password");
} 3. Use secure cookies
Avoid this ❌: JavaScript:
secure: false JavaScript:
secure: true Best security practices 🔒
To properly secure your web applications:- Always use CSRF protection middleware
- Enable SameSite cookie attributes
- Enforce HTTPS across the entire app
- Require re-authentication for sensitive actions
- Validate request origin when possible
Conclusion 🧠
CSRF attacks don’t break your server directly - they exploit the trust between the browser and your application.If your app relies only on sessions without CSRF protection, attackers can silently perform actions on behalf of users without them ever noticing.
Last edited: