CSRF Vulnerabilities in Symfony: A Developer’s Guide
Cross-Site Request Forgery (CSRF) is a sneaky and dangerous vulnerability that tricks users into submitting unintended actions in web applications where they’re authenticated. Symfony, like other PHP frameworks, is not immune unless CSRF protection is properly implemented.
In this article, we’ll walk you through:
- What CSRF is
- How it impacts Symfony applications
- Real coding examples of vulnerable and secure implementations
- How to test your app using our Free Website Vulnerability Scanner
- Bonus: Links and tools to secure your Symfony app
📌 Also read: More security insights on our blog at Pentest Testing Corp.
🔍 What is Cross-Site Request Forgery (CSRF)?
Imagine this: you’re logged into your bank account, and while browsing another site, a hidden script forces your browser to transfer funds. That’s CSRF in action — an attacker hijacks your browser’s authenticated session to perform unwanted actions.
In Symfony, CSRF vulnerabilities often arise in forms, especially when CSRF tokens are not used or validated properly.
⚠️ CSRF Attack Example in Symfony (Vulnerable)
Here’s a basic example of a Symfony form without CSRF protection:
// src/Form/Type/ContactType.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('subject', TextType::class)
->add('message', TextType::class);
}
}
This form is vulnerable because it doesn’t use Symfony’s built-in CSRF protection mechanism.
✅ Secure Symfony Form With CSRF Token
To prevent CSRF, you must include the CSRF token in your form:
// src/Form/Type/SecureContactType.php
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SecureContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('subject', TextType::class)
->add('message', TextType::class)
->add('_token', HiddenType::class, [
'data' => $options['csrf_token'],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([ 'csrf_token' => null, ]);
}
}
And in your controller:
$csrfToken = $this->container->get('security.csrf.token_manager')->getToken('contact_form')->getValue();
$form = $this->createForm(SecureContactType::class, null, [
'csrf_token' => $csrfToken,
]);
🧪 Verifying CSRF Protection
Use Symfony’s CSRF token validator:
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
$submittedToken = $_POST['_token'];
if (!$csrfTokenManager->isTokenValid(new CsrfToken('contact_form', $submittedToken))) {
throw new \RuntimeException('Invalid CSRF token.');
}
🧰 Use Symfony’s Built-In CSRF Protection (Best Practice)
The best approach is enabling CSRF protection globally for forms:
# config/packages/framework.yaml
framework:
csrf_protection: true
Symfony forms will now automatically inject and validate CSRF tokens.
🧑💻 Real-World Scenario: Admin Panel Exploit
Suppose you have an admin page for updating a user’s role:
<form method="POST" action="/admin/change-role">
<input type="hidden" name="user_id" value="42">
<input type="hidden" name="role" value="ROLE_ADMIN">
<input type="submit" value="Update">
</form>
Without a CSRF token, an attacker could trick an admin into clicking a malicious link or submitting a hidden form from another site.
With Symfony CSRF protection enabled, such actions require a valid CSRF token, blocking unauthorized requests.
🧪 Test CSRF and Other Vulnerabilities With Our Free Tool
Want to know if your website is vulnerable to CSRF or other security threats?
📸 Try our Website Vulnerability Scanner:
📸 You’ll get a full vulnerability report to check Website Security that looks like this:
Our tool scans for CSRF, XSS, SQL Injection, and more!
📚 Additional Resources
🏁 Final Thoughts
Cross-Site Request Forgery is a silent but serious threat. Luckily, Symfony provides solid tools to combat it. All you need to do is use them correctly.
Whether you’re building a contact form or managing an admin dashboard, always enable CSRF protection.
And remember — prevention is better than exploitation. Use our Free Website Security Scanner to scan and fix vulnerabilities before attackers find them.