Skip to main content

SSRF: when your server follows a stranger's link

Security · · 5 min read

Where this appears

SSRF is when the visitor supplies an address and your server makes the request to it. The feature looks harmless and shows up in almost every project:

  • "Upload an image by link" instead of a file from disk.
  • Webhooks: the user says where notifications should be sent.
  • Link previews in messages and comments.
  • Importing data from a given address, checking whether a site is up, proxies for maps and avatars.

The danger is that the server sits inside a network the visitor cannot reach. It sees the database on a local address, a panel on a closed port, the service interfaces of neighbouring machines. By supplying such an address, the visitor gets the answer from there — through the hands of your own application.

The most valuable target: metadata

Virtual machines in clouds have a metadata service at the local address 169.254.169.254, from the link-local range 169.254.0.0/16. It serves information about the machine and, in a number of cases, temporary access keys. A request to it needs no password: the assumption is that only the machine itself can ask. SSRF does exactly that.

On AWS the second version of the service requires obtaining a token with PUT first and sending it in the X-aws-ec2-metadata-token header, with the token's lifetime set by the X-aws-ec2-metadata-token-ttl-seconds header. That makes the naive attack considerably harder: a plain request to the address is no longer enough. But if your code forwards headers and methods from the visitor's request, the protection is bypassed — so the version of the service alone cannot be relied on.

What to check: the address, not the string

A common mistake is checking the look of the link. It does not work, because there are many ways to write an internal address: a domain name that resolves to a local address; a shortened form; an IPv6 address; a redirect to an internal address issued after the check.

What needs checking is where the connection will actually go:

  1. Allow only http and https. Schemes such as file, gopher and dict are closed outright.
  2. Parse the address, fetch ALL the DNS records and reject if even one falls into a closed range: loopback, link-local, private networks, and their IPv6 counterparts.
  3. Forbid redirects. Otherwise an external address answers with a redirect to an internal one.
  4. Set timeouts and a response size limit — otherwise the feature becomes a port scanner measured by response time.
<?php
$ip = gethostbyname($host);           // one record is not enough
$all = dns_get_record($host, DNS_A | DNS_AAAA);   // all of them are needed
foreach ($all as $r) {
    $addr = $r['ip'] ?? $r['ipv6'];
    if (!filter_var($addr, FILTER_VALIDATE_IP,
            FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
        throw new RuntimeException('address in a closed range');
    }
}

The FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE flags cut off private and reserved ranges, 169.254.0.0/16 among them. Every record returned must be checked: a domain can hand out an external and an internal address at the same time.

Why the check is still not enough

Time passes between the check and the connection, and in that time the DNS answer can change. The technique is called rebinding the answer on a repeat query: the first time the domain returns an external address and passes the check, the second time an internal one, and that is where the connection goes. The code checked one thing and connected to another.

Closing this at the application level is hard. There are two dependable ways, and both sit below the application:

ApproachWhat it doesCost
Connect to the checked addressResolve the name once and pass the address into the connectionNeeds support in the client
Egress ban on the firewallThe application process cannot reach its own networkConfigured apart from the code
A separate host for such requestsA machine with no access to the internal networkOne more host to maintain

For the metadata service there is also a direct measure: forbid requests to 169.254.169.254 from every process except those that genuinely need it.

iptables -I OUTPUT -m owner --uid-owner www-data \
  -d 169.254.169.254 -j REJECT

How to test your own feature

If the project imports by link, test it on three addresses and watch not the text of the response but how long it took and what reached the log:

curl -s -X POST https://example.com/api/import -d 'url=http://169.254.169.254/'
curl -s -X POST https://example.com/api/import -d 'url=http://127.0.0.1:3306/'
curl -s -X POST https://example.com/api/import -d 'url=http://[::1]:6379/'

A response with contents is a finding. But even a difference in response time between a closed and an open port is a leak: that is how an internal network gets mapped. So the refusal must be equally fast for every closed address.

What must not be done with the response

Data fetched by link is foreign input, even when you consider the link trusted. Do not show the response to the visitor in full: that turns SSRF into reading internal pages. Do not parse it as executable markup, and do not save it under a name taken from the response. And do not show the error text — it reveals both the address and the reason for refusal.

What sits next to this

Outbound connections are limited on the firewall: configuring iptables and UFW on a VPS. Containers add a trap of their own, where a port is opened around the rules: Docker bypasses UFW.

Was this article helpful?
Back to Knowledge Base Ask Support