Application Security

Prevent Server-Side Request Forgery (SSRF) in PHP

Secure PHP features that fetch remote URLs by validating destinations and blocking access to internal network resources.

Why SSRF is dangerous

A server-side request forgery flaw can make your application send requests to destinations chosen by an attacker. This may expose internal services or cloud metadata endpoints.

Avoid arbitrary URLs

The safest design is to accept a known resource identifier and map it to a server-controlled URL. If users must provide a destination, use a strict hostname allowlist.

Configure cURL defensively

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
]);

This is only a starting point. It does not replace destination allowlisting and IP-range validation.

Validate resolved destinations

Account for DNS resolution, IPv4 and IPv6, redirects, and DNS changes between validation and connection. Block loopback, private, link-local, and other non-public ranges unless explicitly required.

Where possible, restrict outbound network access from the application server.