Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
94 lines
2.9 KiB
PHP
94 lines
2.9 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
use Psr\Http\Message\UriInterface;
|
||
use RuntimeException;
|
||
|
||
/**
|
||
* Ausgehende Anfragen an Adressen, die aus Inhalten oder Einstellungen stammen:
|
||
* nie an interne Ziele (localhost, private Netze, Link-Local, Metadaten-Dienste) –
|
||
* auch nicht über Weiterleitungen (Schutz vor SSRF).
|
||
*/
|
||
final class SafeOutbound
|
||
{
|
||
/** @var array<string, list<string>> nur für Tests: feste Auflösung statt DNS */
|
||
private static array $fakeHosts = [];
|
||
|
||
/**
|
||
* @param array<string, string|list<string>> $hosts
|
||
*/
|
||
public static function fakeResolve(array $hosts): void
|
||
{
|
||
self::$fakeHosts = array_map(fn ($ips) => (array) $ips, $hosts);
|
||
}
|
||
|
||
/**
|
||
* @param list<string> $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<string> $schemes
|
||
* @return array<string, mixed>
|
||
*/
|
||
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<string>
|
||
*/
|
||
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));
|
||
}
|
||
}
|