Symfony is a powerful and widely-used PHP framework—but with great power comes great responsibility. One such overlooked vulnerability in Symfony-based applications is Cache Poisoning.
In this blog, we’ll explore:
- What Cache Poisoning is
- How it affects Symfony apps
- Code examples showing vulnerable and safe implementations
- How to detect it using our website vulnerability scanner online free
- Practical mitigation strategies
➡️ Check out other useful posts at Pentest Testing Blog
🔍 What is HTTP Cache Poisoning?
HTTP Cache Poisoning is a web vulnerability where attackers manipulate cacheable responses (like via reverse proxies such as Varnish, CDN, or even Symfony’s HttpCache) to serve malicious or incorrect content to other users.
Example Scenario:
Imagine you cache this response:
GET /profile?id=123 HTTP/1.1
Host: example.com
Response:
200 OK
Content-Type: text/html
Cache-Control: public, max-age=300
Now an attacker crafts:
GET /profile?id=123 HTTP/1.1
Host: example.com
X-Forwarded-Host: attacker.com
If your cache keys include X-Forwarded-Host
, and Symfony’s HttpCache is not configured properly, this might poison the cache.
⚙️ Vulnerable Symfony Code Example
Let’s see what a vulnerable Symfony controller might look like:
// src/Controller/ProfileController.php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ProfileController extends AbstractController
{
public function profile(Request $request): Response
{
$id = $request->query->get('id');
$response = $this->render('profile/show.html.twig', ['id' => $id]);
$response->setPublic();
$response->setMaxAge(300); // 5 minutes
return $response;
}
}
👎 This controller does not validate headers like Host
, X-Forwarded-Host
, or X-Original-URL
. That’s where cache poisoning slips in.
✅ Safe Symfony Code with Header Normalization
Here’s how to harden it:
// src/EventSubscriber/TrustedHeaderSubscriber.php
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class TrustedHeaderSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => 'sanitizeHeaders',
];
}
public function sanitizeHeaders(RequestEvent $event)
{
$request = $event->getRequest();
// Strip suspicious headers
$request->headers->remove('X-Forwarded-Host');
$request->headers->remove('X-Original-URL');
$request->headers->remove('X-Rewrite-URL');
}
}
This effectively blocks header-based manipulation attempts before they can impact the cache behavior.
🧪 Scan Your Symfony App for Free
Want to find out if your Symfony app is vulnerable to cache poisoning or other threats?
📸 Below is a screenshot of our Website Vulnerability Scanner tool that scans for these and other web vulnerabilities.
Screenshot of the free tools webpage where you can access security assessment tools.
Run a scan in seconds. No login required.
📸 Sample Vulnerability Report to check Website Vulnerability like these and other vulnerabilities
An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.
✅ Identify vulnerabilities
✅ Get severity ratings
✅ View remediation suggestions
👉 Try it now: free.pentesttesting.com
🔐 Real-World Prevention Tips for Symfony
Here are practical ways to prevent cache poisoning in Symfony:
- Normalize headers using an event subscriber
- Avoid caching based on user input like query params
- Use Symfony's ESI or fragments carefully—validate each fragment separately
- Avoid third-party middleware that introduces unvalidated headers
- Scan regularly with our tool foe a Website Security test
🔧 Related Services We Offer
We provide specialized penetration testing services to ensure your Symfony applications are resilient.
1️⃣ Web App Penetration Testing
In-depth analysis of your Symfony apps and beyond.
2️⃣ Partner With Us: Cybersecurity for Your Clients
Are you an agency or SaaS provider? Offer our cybersecurity services under your brand.
📬 Stay Updated: Join Our Newsletter
Receive real-world tips, vulnerability alerts, and technical write-ups like this one.
📰 Subscribe on LinkedIn:
👉 Pentest Testing Corp Newsletter
🔚 Conclusion
Cache poisoning is a serious yet stealthy threat in Symfony apps. It can lead to content spoofing, account hijacking, and privacy leaks—all without compromising the server directly.
With proper header validation, cautious caching, and regular scanning, you can prevent such exploits from affecting your users.
💬 Have you faced cache poisoning in your project? Let us know in the comments on Dev.to!
Want a free scan? DM me or check https://free.pentesttesting.com
Top comments (0)