$allowedDevelopmentHosts * Exact host names allowed for HTTP/private-network service requests. * This is intended only for isolated local development environments. */ public function __construct( private array $allowedDevelopmentHosts = [], ) {} /** * Returns TRUE for a policy-compliant HTTPS URL or an allowlisted dev host. */ public function isAllowedServiceUrl(string $url): bool { if ($this->isAllowedHttpsUrl($url)) { return TRUE; } $parts = $this->parseUrl($url); if ( $parts === NULL || !\in_array(\strtolower((string) ($parts['scheme'] ?? '')), ['http', 'https'], TRUE) || !\is_string($parts['host'] ?? NULL) || isset($parts['user']) || isset($parts['pass']) ) { return FALSE; } $host = $this->normalizeHost($parts['host']); $allowed = \array_map($this->normalizeHost(...), $this->allowedDevelopmentHosts); return $host !== '' && \in_array($host, $allowed, TRUE); } /** * Rejects a disallowed allowed service url. */ public function assertAllowedServiceUrl(string $url): void { if (!$this->isAllowedServiceUrl($url)) { throw new \InvalidArgumentException(\sprintf('Refusing disallowed URL "%s" as an AT Protocol service.', $url)); } } /** * Returns TRUE only for syntactically valid HTTPS URLs with allowed hosts. */ public function isAllowedHttpsUrl(string $url): bool { $parts = $this->parseUrl($url); if ( $parts === NULL || \strtolower((string) ($parts['scheme'] ?? '')) !== 'https' || !\is_string($parts['host'] ?? NULL) || $parts['host'] === '' || isset($parts['user']) || isset($parts['pass']) ) { return FALSE; } return $this->isAllowedHost($parts['host']); } /** * Rejects a disallowed allowed https url. */ public function assertAllowedHttpsUrl(string $url): void { if (!$this->isAllowedHttpsUrl($url)) { throw new \InvalidArgumentException(\sprintf('Refusing disallowed URL "%s".', $url)); } } /** * @return array|null */ private static function parseUrl(string $url): ?array { try { $parts = \parse_url($url); } // ValueError is an expected invalid-input result at this API boundary. // @mago-expect analysis:avoid-catching-error catch (\ValueError) { return NULL; } return \is_array($parts) ? $parts : NULL; } private function isAllowedHost(string $host): bool { $host = $this->normalizeHost($host); if ($host === '' || \strpbrk($host, "/?#@ \t\r\n") !== FALSE) { return FALSE; } if ( $host === 'localhost' || \str_ends_with($host, '.localhost') || \str_ends_with($host, '.local') || \str_ends_with($host, '.internal') ) { return FALSE; } if (\filter_var($host, FILTER_VALIDATE_IP) !== FALSE) { return $this->isAllowedIpAddress($host); } // Some resolvers accept abbreviated, octal, or hexadecimal IPv4 forms // (for example, 127.1). Do not let those aliases fall through as DNS // hostnames and bypass the IP address policy. if (\preg_match('/^(?:0[xX][0-9A-Fa-f]+|[0-9]+)(?:\.(?:0[xX][0-9A-Fa-f]+|[0-9]+))*$/D', $host) === 1) { return FALSE; } return ( \preg_match( '/^(?=.{1,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/', $host, ) === 1 ); } private static function normalizeHost(string $host): string { return \strtolower(\trim($host, "[] \t\r\n")); } private function isAllowedIpAddress(string $address): bool { if (\filter_var($address, FILTER_VALIDATE_IP) === FALSE) { return FALSE; } $translatedIpv4 = $this->translatedIpv4Address($address); if ($translatedIpv4 !== NULL && !$this->isAllowedIpAddress($translatedIpv4)) { return FALSE; } $blocked = \str_contains($address, ':') ? [ '::/96', '::ffff:0:0/96', '64:ff9b:1::/48', '100::/64', '2001::/23', '2001:db8::/32', '2002::/16', '3fff::/20', '5f00::/16', 'fc00::/7', 'fec0::/10', 'fe80::/10', 'ff00::/8', ] : [ '0.0.0.0/8', '10.0.0.0/8', '100.64.0.0/10', '127.0.0.0/8', '169.254.0.0/16', '172.16.0.0/12', '192.0.0.0/24', '192.0.2.0/24', '192.88.99.0/24', '192.168.0.0/16', '198.18.0.0/15', '198.51.100.0/24', '203.0.113.0/24', '224.0.0.0/4', '240.0.0.0/4', ]; foreach ($blocked as $cidr) { if ($this->addressInCidr($address, $cidr)) { return FALSE; } } return TRUE; } private function translatedIpv4Address(string $address): ?string { if (!$this->addressInCidr($address, '64:ff9b::/96')) { return NULL; } $addressBytes = \inet_pton($address); if ($addressBytes === FALSE) { return NULL; } $ipv4 = \inet_ntop(\substr($addressBytes, -4)); return \is_string($ipv4) ? $ipv4 : NULL; } private static function addressInCidr(string $address, string $cidr): bool { [$network, $prefix] = \explode('/', $cidr, 2); $addressBytes = \inet_pton($address); $networkBytes = \inet_pton($network); if ($addressBytes === FALSE || $networkBytes === FALSE || \strlen($addressBytes) !== \strlen($networkBytes)) { return FALSE; } $bits = (int) $prefix; $wholeBytes = \intdiv($bits, 8); $remainingBits = $bits % 8; if ($wholeBytes > 0 && \substr($addressBytes, 0, $wholeBytes) !== \substr($networkBytes, 0, $wholeBytes)) { return FALSE; } if ($remainingBits === 0) { return TRUE; } $mask = (0xFF << (8 - $remainingBits)) & 0xFF; return (\ord($addressBytes[$wholeBytes]) & $mask) === (\ord($networkBytes[$wholeBytes]) & $mask); } }