Fetching a feed from a URL you don’t trust

RSS to JSON can now refuse feed URLs that resolve to private addresses, so a stranger can’t point your server at its own network.

A terminal showing RSS to JSON refusing three addresses, including the cloud metadata address written as IPv6, each with the message “Refusing to connect ... private or reserved address”. A terminal showing RSS to JSON refusing three addresses, including the cloud metadata address written as IPv6, each with the message “Refusing to connect ... private or reserved address”.

When a visitor types a feed URL into a form, the request comes from the server, and the server can reach places the visitor can’t: an admin page on 127.0.0.1, a service on the private network, or 169.254.169.254, where cloud servers read their own settings and, on some setups, their credentials. That’s server-side request forgery.

RSS to JSON reads a feed from a URL and gives back an array ready for json_encode(), and version 2.1 added block_private_ips for exactly this case. It’s for any URL that comes from someone you don’t trust. I left it off by default, so existing code behaves as before.

Which addresses count as private

The list lives in src/IpAddress.php. I wrote the ranges out rather than rely on PHP’s FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE, whose coverage has changed between PHP versions. The IPv4 list has fifteen ranges, including these:

'10.0.0.0/8',       // Private
'100.64.0.0/10',    // Carrier-grade NAT (includes 100.100.100.200, Alibaba Cloud metadata)
'127.0.0.0/8',      // Loopback
'169.254.0.0/16',   // Link-local (includes 169.254.169.254, AWS/GCP/Azure metadata)
'172.16.0.0/12',    // Private
'192.168.0.0/16',   // Private

The others are the “this network” block, the block for IETF protocol assignments, the documentation and benchmarking ranges, the old 6to4 relay range, multicast, and the reserved block that ends at 255.255.255.255. IPv6 has its own list: the unspecified and loopback addresses, unique local addresses (where AWS puts its IPv6 metadata address, fd00:ec2::254), link-local, site-local, multicast, documentation, discard and Teredo.

An IPv6 address can also carry an IPv4 address inside it. ::ffff:169.254.169.254 is the metadata address written as IPv6, and a check that only looked at the IPv6 list would let it through. Four IPv6 ranges are marked as wrapping IPv4, and for those the embedded four bytes are checked against the IPv4 list:

private const EMBEDDED_V4 = [
    '::ffff:0:0/96' => 12,  // IPv4-mapped
    '::/96' => 12,          // IPv4-compatible (deprecated)
    '64:ff9b::/96' => 12,   // NAT64
    '2002::/16' => 2,       // 6to4
];

The number is the byte where the IPv4 address starts. 6to4 puts it straight after the first two bytes, so 2002:7f00:0001::1 is 127.0.0.1 in disguise.

Addresses are compared as bytes, after inet_pton(), and anything that function can’t parse is treated as not public. localhost, the decimal form 2130706433 and 999.1.1.1 all fail.

Resolve first, and check every address

A host name isn’t an address. checkUrl() resolves it before anything connects: gethostbynamel() for IPv4 and dns_get_record() for IPv6. Whoever controls a domain controls its DNS, and can return one public address and one private one, which is why every address has to pass:

$addresses = $isIpLiteral ? [$host] : $this->resolveHost($host);
if ($addresses === []) {
    $this->lastError = "Could not resolve host: $host";
    return null;
}
foreach ($addresses as $address) {
    if (!$this->isAddressAllowed($address)) {
        $this->lastError = sprintf('Refusing to connect to %s (%s): private or reserved address', $host, $address);
        return null;
    }
}

Before the lookup, some URLs are refused outright in this mode. A URL with a user name and password, whitespace or a backslash goes, because PHP’s parse_url() and libcurl can disagree about which part of it is the host. A URL that ends in \@169.254.169.254/ is the kind of thing that rule is for. Host names that aren’t plain ASCII, such as local%68ost, go too. libcurl decodes a percent-encoded or Unicode host name before it connects. The pin described below is keyed on the name as written. It wouldn’t apply, and curl would make its own lookup of the decoded name, which the check never saw. International domains still work in their xn-- form.

Pinning the connection

Checking the address and then letting curl connect means two DNS lookups, and the answer can change between them. That’s DNS rebinding: the attacker’s DNS server answers the first lookup with a public address and a very short lifetime, and the second with 127.0.0.1. The check passes, and curl connects somewhere else.

To close that gap, curl never does its own lookup, and CURLOPT_RESOLVE hands it the address that was checked, for that host and port:

if ($target['ip'] !== null) {
    // A proxy would make its own connection, out of our sight.
    curl_setopt($ch, CURLOPT_PROXY, '');

    // Connect to the address that was checked, not a fresh lookup.
    // An IP literal involves no lookup, so there is nothing to pin.
    if (filter_var($target['host'], FILTER_VALIDATE_IP) === false) {
        $address = strpos($target['ip'], ':') === false ? $target['ip'] : '[' . $target['ip'] . ']';
        curl_setopt($ch, CURLOPT_RESOLVE, [sprintf('%s:%d:%s', $target['host'], $target['port'], $address)]);
    }
}

The host name stays in the URL, and the Host header and the TLS certificate check still use it. When a host has both kinds of address, the pin prefers IPv4, which every version of libcurl accepts in CURLOPT_RESOLVE. The empty proxy setting matters too. curl picks up proxy settings from the environment, and a proxy would make its own connection, out of the check’s sight.

An IP literal isn’t pinned at all. There’s no lookup to protect. An IPv6 literal would also produce a CURLOPT_RESOLVE entry that libcurl can’t parse, and every URL with one would fail.

On PHP 8.4 and later, with libcurl 7.80 or later, there’s a second line of defence. CURLOPT_PREREQFUNCTION runs once curl has connected and before it sends the request, with the address it connected to, and the same check can abort the request there. Older versions get a check of CURLINFO_PRIMARY_IP afterwards, which throws the response away. By then the request has already been sent, which makes it a backstop behind the pin.

Redirects

curl can follow redirects itself, but then the second hop would skip every check above. With CURLOPT_FOLLOWLOCATION off, fetchRemoteFeed() follows redirects in its own loop. Each new URL goes back through checkUrl(): the scheme, the lookup, the addresses and a fresh pin. Only then is it requested.

The new URL comes from CURLINFO_REDIRECT_URL, and a relative redirect arrives as a full URL. A feed that redirects to http://169.254.169.254/latest/meta-data/ is refused at the second hop, and so is one that redirects to http://[::ffff:169.254.169.254]/. Five redirects are allowed by default, and one deadline covers the whole fetch, which stops a chain of slow redirects from stretching the ten-second timeout.

Some of this applies with the option off. Only http and https are fetched, for the first URL and for every redirect: a redirect to file:///etc/passwd, gopher:// or dict:// is refused either way. curl is also limited to those two protocols, as a second check.

A test server on loopback

The remote tests run against PHP’s built-in web server on 127.0.0.1, which is exactly the kind of address the option refuses. The class has a hook for this, the protected isAddressAllowed(), and the tests use a subclass that lets 127.0.0.1 through and nothing else on loopback:

return new class (['block_private_ips' => true]) extends RSSToJson {
    protected function isAddressAllowed(string $ip): bool
    {
        return $ip === '127.0.0.1' || parent::isAddressAllowed($ip);
    }
};

A redirect test can start at the test server and check that a hop to 10.0.0.1 or 127.0.0.2 is refused. The two redirects to the metadata address have a test each, and the tests list localhost, 2130706433 and 999.1.1.1, which inet_pton() can’t parse. The rebinding defence has its own test, with a subclass whose isAddressAllowed() says yes the first time and no after that, as if DNS had changed between the check and the connection. The fetch has to fail, with the connected address in the error. The suite has 112 tests, and PHPStan checks the code at its strictest level.

Where untrusted input belongs

The option covers fetching. parse() treats anything that isn’t a valid URL as a file path, and PHP’s file functions accept stream wrappers, so untrusted input belongs in parseUrl(), which refuses anything but http and https.

Using the option

The package is on Packagist, and the source on GitHub:

composer require tomgould/rsstojson
use TomGould\RSSToJson\RSSToJson;

$parser = new RSSToJson(['block_private_ips' => true]);
$feed = $parser->parseUrl($urlFromTheForm);

if ($feed === null) {
    // For example: Refusing to connect to localhost (127.0.0.1): private or reserved address
    echo $parser->getLastError(), PHP_EOL;
}

To let one internal feed server through, extend the class and override isAddressAllowed(string $ip): bool, the way the tests do.

More on RSS to JSON

All writing about RSS to JSON

Featured projects

All projects

More writing

All writing