DEV Community

Cover image for Preventing DNS Rebinding in Symfony – Guide & Example
Pentest Testing Corp
Pentest Testing Corp

Posted on

Preventing DNS Rebinding in Symfony – Guide & Example

What is a DNS Rebinding Attack?

A DNS rebinding attack tricks a victim’s browser into believing a malicious domain is your trusted domain (e.g. localhost, 127.0.0.1). Once the DNS entry changes to point to your internal server, the attacker can interact with private network services.

Preventing DNS Rebinding in Symfony – Guide & Example

Why Symfony Apps Are at Risk

Symfony-based web applications often expose endpoints not specifically hardened for internal access. A successful DNS rebinding exploit can allow cross-domain requests into these internal routes, bypassing authentication or accessing private APIs.


🧠 How DNS Rebinding Works

  1. Attacker registers attacker.com.
  2. Victim visits attacker.com/browser.js.
  3. The DNS initially resolves to a public server controlled by attacker.
  4. After loading, the attacker updates the DNS to resolve to 127.0.0.1.
  5. The script now interacts with your local Symfony app.

💻 Symfony Defense: Secure Request Headers

Use Symfony's HTTP foundation to inspect and reject suspicious requests:

// src/EventListener/DnsRebindingListener.php
namespace App\EventListener;

use Symfony\Component\HttpKernel\Event\RequestEvent;

class DnsRebindingListener
{
    public function onKernelRequest(RequestEvent $event): void
    {
        $req = $event->getRequest();
        $host = $req->headers->get('host');

        // Only allow your domain(s)
        $allowedHosts = ['example.com', 'api.example.com'];
        if (!in_array($host, $allowedHosts, true)) {
            throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(
                "Invalid host: $host"
            );
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Register the listener:

# config/services.yaml
services:
    App\EventListener\DnsRebindingListener:
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Enter fullscreen mode Exit fullscreen mode

🔐 Reinforce with Security Headers

Configure trusted origins and anti-CSRF settings in Symfony:

# config/packages/framework.yaml
framework:
    router:
        strict_requirements: null
    trusted_hosts: ['^example\.com$']
    session:
        cookie_secure: auto
        cookie_samesite: lax
    csrf_protection: true
Enter fullscreen mode Exit fullscreen mode

Consider adding these via middleware or server config:

# .htaccess or server block
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set X-Frame-Options DENY
Enter fullscreen mode Exit fullscreen mode

✅ Testing with Pentest Testing Corp’s Free Tool

You can test for DNS rebinding vulnerabilities using our free Website Vulnerability Scanner:

  1. Visit: https://free.pentesttesting.com/
  2. Enter your Symfony application URL
  3. Review the interactive results

Screenshot of the Website Vulnerability Scanner tool homepage:

Screenshot of the free tools webpage where you can access security assessment tools.Screenshot of the free tools webpage where you can access security assessment tools.

Example assessment report to check Website Vulnerability:

An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.An Example of a vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities.


🛠️ Advanced Configuration Example

// src/Middleware/HostnameValidatorMiddleware.php
namespace App\Middleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class HostnameValidatorMiddleware
{
    private array $allowedHosts;

    public function __construct(array $allowedHosts)
    {
        $this->allowedHosts = $allowedHosts;
    }

    public function __invoke(Request $request, callable $next): Response
    {
        if (!in_array($request->getHost(), $this->allowedHosts, true)) {
            throw new AccessDeniedHttpException("Invalid Host: {$request->getHost()}");
        }
        return $next($request);
    }
}
Enter fullscreen mode Exit fullscreen mode

Register with Service Container and add to the security/firewall pipeline.


🌟 Additional Pentest Testing Corp Resources


📰 Stay Updated

Subscribe for new guides on Symfony security:
🔗 Subscribe on LinkedIn


🔚 Conclusion

DNS rebinding is stealthy but preventable. Rely on domain validation, strict headers, and active testing like Pentest Testing Corp’s free tool for Website Security test. Stay ahead of threats by combining technical safeguards with proactive vulnerability assessment.

Top comments (0)