$lookup every address the name answers with; lookup() over the system resolver by default, a test hands in its own * @return non-empty-list a name with no answer is refused rather than returned as []; CurlPin's $ips requires non-empty, and PHPStan holds the call site to it (an entry built from [] would be `host:port:`, which libcurl cannot parse) * @throws AddressRefused with a translated message naming the host */ public static function resolve(string $host, string $url = '', ?\Closure $lookup = null): array { $host = strtolower(trim($host, '.')); $literal = trim($host, '[]'); if ($literal === '') { throw new AddressRefused(__('The address has no host name.', 'alpaca-bot')); } $addresses = SpecialPurposeAddress::isAddress($literal) ? [$literal] : ($lookup ?? self::lookup(...))($host); if ($addresses === []) { /* translators: %s: a host name */ throw new AddressRefused(sprintf(__('%s does not resolve, or its lookup failed.', 'alpaca-bot'), $host)); } foreach ($addresses as $address) { if (!SpecialPurposeAddress::isAddress($address)) { /* translators: %s: a host name */ throw new AddressRefused(sprintf(__('%s answered with something that is not an address.', 'alpaca-bot'), $host)); } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Deliberately core's own filter, not a hook of ours: a site that has already told WordPress a private host is reachable should not have to say it again here. if (SpecialPurposeAddress::match($address) !== null && !apply_filters('http_request_host_is_external', false, $host, $url)) { /* translators: 1: a host name, 2: an IP address */ throw new AddressRefused(sprintf(__('%1$s resolves to %2$s, a private, local or other special-purpose address.', 'alpaca-bot'), $host, $address)); } } return $addresses; } /** * Every address a name answers with, both families, through the system resolver: A through * gethostbynamel(), AAAA through dns_get_record(). All of them are checked and all of them * are handed back (resolve() says why), so both families have to be read. Cost: * two lookups, and a resolver that times out costs its timeout. For web_fetch the A one is * normally a cache hit, since core's wp_http_validate_url() has just made it; the AAAA one * is a query either way, no core version having asked for it, and a caller that has not been * through core's HTTP API first, as Mcp\Egress has not, pays for both. dns_get_record() warns * as well as returning false on a failed query, and a warning here is a refused request, not * an error worth logging, hence the suppression. * * A lookup that failed is not a lookup that answered "none". Either function returns false * when the query could not be made (a timeout, a nameserver that drops the question), and * that is answered with an empty list, which resolve() refuses as it refuses a name that * does not resolve. Read as "no records" instead, a false from the AAAA half would let a name * whose nameserver answers A with a public address and drops AAAA queries through on the A * alone. What the refusal costs: a legitimate host behind a resolver that fails one of the * two queries cannot be reached until the resolver answers. * * @param null|\Closure(string): (list|false) $a the A lookup; gethostbynamel() by default * @param null|\Closure(string): (array|false) $aaaa the AAAA lookup; dns_get_record($host, DNS_AAAA) by default * @return list every address, A first, or [] when the name has none or either lookup failed */ public static function lookup(string $host, ?\Closure $a = null, ?\Closure $aaaa = null): array { $a ??= static fn(string $host): array|false => gethostbynamel($host); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- dns_get_record() emits a PHP warning as well as returning false when a lookup fails. The false is checked below and refuses the request; the warning would only leak resolver detail into the response of an SSRF guard. $aaaa ??= static fn(string $host): array|false => @dns_get_record($host, DNS_AAAA); $v4 = $a($host); $v6 = $aaaa($host); if ($v4 === false || $v6 === false) { return []; } $addresses = array_values(array_filter($v4, 'is_string')); foreach ($v6 as $record) { if (is_array($record) && is_string($record['ipv6'] ?? null)) { $addresses[] = $record['ipv6']; } } return $addresses; } }