Lesson 9 of 14 · 20m
When Your Server Becomes the Client: SSRF and Stream Wrappers
URL fetches reach 169.254.169.254 and localhost; how spellings, redirects, and rebinding beat string checks.
When PHP calls file_get_contents($url) or Guzzle's get($url), the URL is an instruction, not data — it names a TCP service your server will connect to and read from. Everything reachable from the box is reachable through your feature: localhost, RFC1918 neighbors, the cloud metadata endpoint, the Redis that has no password because it's "internal". SSRF is the bug where the attacker picks the destination.
Why string validation fails
The obvious check — parse_url($url) and require http/https — passes everything that matters:
- Loopback has many spellings.
http://2130706433/(decimal),http://0x7f000001/(hex),http://0177.0.0.1/(octal),http://[::1]/,http://127.1/— glibc'sinet_atonresolves them all to 127.0.0.1, andFILTER_VALIDATE_URLaccepts every one. - Redirects re-roll the dice. You checked
example.com;example.comrepliesLocation: http://169.254.169.254/latest/meta-data/and cURL happily follows. - DNS is time-of-check, time-of-use.
gethostbyname()returns a safe IP at validation; the fetch resolves again and gets the attacker's second answer. Rebinding is a TTL trick, not exotic tooling. - The scheme list is longer than you think. PHP's wrappers include
php://,phar://,ftp://,compress.zlib://. cURL's protocol list is longer —gopher,dict,ldap.gopher://writes raw TCP, which is how SSRF reaches Redis command injection and FastCGI payloads through a "URL fetch".
169.254.169.254 is the address with the case study behind it. Capital One, 2019: a web application firewall's feature issued requests containing an attacker-influenced URL; the response to 169.254.169.254/latest/meta-data/iam/security-credentials/ handed back IAM credentials, and 100 million customer records walked out through S3. The fine was $80 million. AWS's answer was IMDSv2 — a session-token handshake the metadata endpoint now requires — which helps exactly as much as your SSRF filter helps: an attacker who controls the request can usually fetch a token first. That endpoint is the poster child, but the reachable list on any real box is longer: Redis on 6379, Elasticsearch on 9200, the FPM status page, Prometheus, the Kubernetes API on 10.96.0.1. GCP and Azure put their own metadata on the same address — the magic IP is a cloud convention, not an AWS quirk.
The file-side sibling
php://filter is the same primitive pointed at your own disk: php://filter/convert.base64-encode/resource=config.php returns your source base64'd — bypassing "don't include PHP files" because it never executes them, it reads them. Synacktiv's php_filter_chains research (Charles Fol, 2023) pushed this further, showing filter chains alone can reach RCE-class primitives. Any function that takes a filename and does I/O on it is a candidate — file_get_contents, include, parse_ini_file, simplexml_load_file.
What actually fixes it
In order of what they buy you:
- Egress allowlist. The real fix lives at the firewall/proxy: the app may only reach enumerated hosts. Everything else is partial.
allow_url_fopen=0when nothing legitimately reads remote URLs — removesfile_get_contents('http://…')as a primitive entirely.- Resolve-then-pin in code.
parse_url, resolve the host, validate the resolved IP against loopback/link-local/RFC1918/metadata ranges, then pin the resolution withCURLOPT_RESOLVEso the fetch uses the IP you checked — closing the rebinding gap. - Bound cURL's reach:
CURLOPT_PROTOCOLS_STR='http,https',CURLOPT_REDIR_PROTOCOLS_STRthe same,CURLOPT_FOLLOWLOCATIONoff or re-validate every hop,CURLOPT_CONNECTTIMEOUT/CURLOPT_TIMEOUTalways. - Don't surface failure internals. An error body from an internal service leaks headers and banners. Return "fetch failed", not the response.
For real outbound work use Guzzle — same rules, better defaults — and timeout every call. An outbound request without a timeout is a worker you can't get back.
Try this
Stand up a listener on localhost (nc -l 127.0.0.1 8090), point your URL-fetch feature at http://2130706433:8090/, and watch it connect. Then fix it with the resolve-and-pin path above and watch the same request die at validation.
Pitfalls
FILTER_VALIDATE_URLvalidates syntax, not destination —http://0x7f000001/passes.- IPv6 literals, IPv4-mapped IPv6 (
::ffff:7f00:1), trailing-dot hosts (localhost.) — enumerate the spellings in your blocklist or lose. - Webhooks and "check this URL" features are SSRF by design; treat them as the most dangerous endpoint you ship.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PHP 8.6 Finally Turns On the Session Settings You Were Told to Set in 2006
Every PHP hardening guide for two decades has told you to flip the same three session settings. PHP 8.6, due November 19, 2026, flips them for you: strict mode on, HttpOnly on, SameSite set to Lax.
Money Is Not a Float: Decimals, Rounding and Cents in PHP, Python and JavaScript
I ran the same money math through PHP 8.6, Python 3.15 and Node 26. They round 2.675 differently, bcadd('0.1', '0.2') returns 0, PHP's BcMath\Number turns a float 0.1 into 0 with only a deprecation notice, and JSON.parse quietly changes 9007199254740993 to ...992.
PHP OPcache, Preloading and the JIT, Measured on Laravel: One Setting Is 18x, One Costs You 19%, and the JIT Does Nothing
I ran Laravel 13 on PHP 8.6 FPM under six OPcache configurations and timed every one. OPcache itself was worth 18x, revalidate_freq=0 quietly cost 19%, preloading added 9%, and the JIT that doubles Zend's CPU benchmark moved a real page by 1%.
One Slow Call, Three Runtimes: How PHP-FPM, Python ASGI and Node Fall Over When a Dependency Takes 200 ms
I put the same 200 ms wait behind PHP-FPM, FastAPI on uvicorn and a plain Node server, then hit each one with 50 concurrent requests. Handled correctly, the async runtimes served 205-211 req/s against FPM's 19.6. One blocking call in the wrong place took both of them down to 4.98, slower than...