> nur für Tests: feste Auflösung statt DNS */ private static array $fakeHosts = []; /** * @param array> $hosts */ public static function fakeResolve(array $hosts): void { self::$fakeHosts = array_map(fn ($ips) => (array) $ips, $hosts); } /** * @param list $schemes */ public static function isPublicUrl(string $url, array $schemes = ['https', 'http']): bool { $parts = parse_url($url); $scheme = strtolower((string) ($parts['scheme'] ?? '')); $host = strtolower(trim((string) ($parts['host'] ?? ''), '[]')); if (! in_array($scheme, $schemes, true) || $host === '' || isset($parts['user']) || in_array($host, ['localhost', 'localhost.localdomain'], true) || str_ends_with($host, '.localhost') || str_ends_with($host, '.internal') || str_ends_with($host, '.local')) { return false; } $ips = filter_var($host, FILTER_VALIDATE_IP) ? [$host] : self::resolve($host); if ($ips === []) { return false; } foreach ($ips as $ip) { if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { return false; } } return true; } /** * Optionen für den HTTP-Client: höchstens 3 Weiterleitungen, jede wird erneut geprüft. * * @param list $schemes * @return array */ public static function options(array $schemes = ['https', 'http']): array { return [ 'allow_redirects' => [ 'max' => 3, 'protocols' => $schemes, 'on_redirect' => function ($request, $response, UriInterface $uri) use ($schemes) { if (! self::isPublicUrl((string) $uri, $schemes)) { throw new RuntimeException('Weiterleitung auf eine nicht erlaubte Adresse.'); } }, ], ]; } /** * @return list */ private static function resolve(string $host): array { if (isset(self::$fakeHosts[$host])) { return self::$fakeHosts[$host]; } $ips = []; foreach ([DNS_A, DNS_AAAA] as $type) { foreach (@dns_get_record($host, $type) ?: [] as $record) { $ips[] = $record['ip'] ?? $record['ipv6'] ?? null; } } return array_values(array_filter($ips)); } }