Search by

johnhenry / craft-ip-guard

johnhenry

Shared address classification for the SSRF guards in John Henry's Craft plugins.

Package info

github.com/john-henry/craft-ip-guard

pkg:composer/johnhenry/craft-ip-guard

Statistics

Installs: 894

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

1.3.0 2026-09-25 17:45 UTC

This package is auto-updated.

Last update: 2026-11-27 15:30:08 UTC


README

Tells a public address from one a server should never be asked to fetch.

It is the address half of an SSRF guard, and nothing else. There is no HTTP client in here, no redirect handling, no exceptions of its own. It answers one question, and the plugin asking it decides what to do with the answer.

The thinking behind it

Any plugin that fetches a URL somebody else supplied needs to know whether that URL points somewhere it has no business going: the machine it is running on, another box on the same private network, or the cloud metadata endpoint at 169.254.169.254 that will hand out credentials to anyone who asks.

That check is a list of address ranges. The list is dull, it comes out of the RFCs, and it changes about never. The trouble is what happens when two plugins each keep their own copy. They do not stay the same. One picks up carrier-grade NAT and the other does not, somebody fixes a gap in the plugin they happened to be working in, and the other one quietly keeps the hole. A guard that is right in one place and wrong in another is worse than one that is wrong in both, because you stop checking.

So the list lives here, on its own, and the plugins read from it. A range added is a range added for all of them.

What it covers

For IPv4: the RFC 1918 private blocks, loopback, link-local (which is where the metadata endpoint sits), this-network, carrier-grade NAT, the IETF protocol assignments, benchmarking, the three documentation blocks (TEST-NET-1, TEST-NET-2 and TEST-NET-3), the old 6to4 relay range (192.88.99.0/24), multicast and the reserved class E space.

For IPv6: loopback, unspecified, unique-local, link-local and multicast.

An IPv6 address can carry an IPv4 one inside it: IPv4-mapped (::ffff:0:0/96), NAT64 (64:ff9b::/96) and 6to4 (2002::/16). Those are taken out and judged as the IPv4 address they reach, however they are written, so ::ffff:7f00:1 is refused the same as 127.0.0.1. A public address written that way still gets through.

PHP's own FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE are applied first. The explicit list is belt and braces over them, because some builds apply those flags less completely than others.

An address that will not parse comes back as private. The caller is deciding whether to open a connection, and the safe answer to "I cannot tell" is no.

Using it

use johnhenry\ipguard\IpRange;

if (IpRange::isPrivate($ip)) {
    // Refuse it, however your plugin refuses things.
}

Name lookups come through Dns, which keeps a failed one quiet without reaching for @:

use johnhenry\ipguard\Dns;

$addresses = Dns::addressesFor($host);

A host that will not resolve is an ordinary answer to a guard like this, not a fault, so it comes back as an empty list. Dns::records() is there for a single record type where you want it.

There is a second check for the install's own sites:

if (IpRange::isOwnSiteOrigin($scheme, $host, $port)) {
    // One of this install's own sites, so the range check does not apply.
}

That one matters more than it looks. A local or intranet install legitimately resolves to a private address, and its own sites come from Craft's site config rather than from anything a visitor typed, so they are exempt. Without that exemption the guard refuses the site it is running on.

It only matches when the scheme, host and port are exactly those of one of the sites. Pass null for the port when the URL has none, and the scheme's default is used. Another port on the same host is a different service, so it gets no exemption. And when Craft has taken @web from the request's Host header, the person who sent the request chose that hostname, so nothing is exempt at all.

IpRange::isOwnSiteHost($host) is still there, but it is the weaker check and you shouldn't use it as the exemption. It compares the hostname alone, so any port on the site's host gets through, and it trusts a base URL that Craft built from the request.

What it does not do

Validating an address is not the whole job. A hostname can answer with a public address when you check it and a private one when you connect, which is DNS rebinding, and no amount of re-checking the name closes it. Closing it means handing the addresses you validated straight to the connection and following redirects yourself, one checked hop at a time. That belongs in the plugin doing the fetching, and both of the ones using this package do it.

Requirements

Craft CMS 5 and PHP 8.2 or later.

Licence

MIT. See LICENSE.md.