Better sanitize domains (#9611)

Fix https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-3ch8-46c3-xr9h
This commit is contained in:
Alexandre Alapetite authored and GitHub committed 2026-11-22 21:57:54 +01:00
1 parent 6320ff8af7
commit dcd40cb7d4
2 files changed
+40

No files matched your search

+7
View File
@@ -327,6 +327,13 @@ final class FreshRSS_http_Util {
if (in_array('*', $internal_host_allowlist, true)) {
return []; // Disables SSRF checks entirely (unsafe)
}
if (filter_var($host, FILTER_VALIDATE_IP) === false) {
// cURL percent-decodes the host, converts non-ASCII to Punycode, and reads a host ending in a number as IPv4 (e.g. 0x7f.1).
// Refuse such hosts, as cURL would contact another name or address than the one checked and pinned here.
if (preg_match('/^[a-z0-9._-]+$/D', $host) !== 1 || preg_match('/(^|\.)(\d+|0x[0-9a-f]*)\.?$/D', $host) === 1) {
return false;
}
}
$resolve_str = "$host:$port:";
$ips_ok = [];
+33
View File
@@ -218,6 +218,39 @@ final class httpUtilTest extends \PHPUnit\Framework\TestCase {
];
}
/** cURL would rewrite these hosts, and contact another name or address than the one checked and pinned */
#[DataProvider('provideHostsRewrittenByCurl')]
public function test_getCurlResolveInfoRejectsHostsRewrittenByCurl(string $host): void {
FreshRSS_Context::initSystem();
$resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
$resolveOk->setValue(null, [strtolower($host) => ['192.0.66.96']]);
self::assertFalse(FreshRSS_http_Util::getCurlResolveInfo("http://$host/feed"));
self::assertFalse(FreshRSS_http_Util::getCurlResolveInfo("socks5h://$host:1080"));
}
/** @return array<string,array{string}> */
public static function provideHostsRewrittenByCurl(): array {
return [
'percent-encoded underscore' => ['a%5F.example.test'], // cURL resolves a_.example.test
'percent-encoded UTF-8' => ['%C3%A9.example.test'], // cURL resolves xn--9ca.example.test
'non-ASCII' => ['é.example.test'], // cURL resolves xn--9ca.example.test
'IPv4 as one number' => ['2130706433'], // cURL connects to 127.0.0.1
'IPv4 as hexadecimal' => ['0x7f.1'], // cURL connects to 127.0.0.1
'IPv4 as octal' => ['0177.0.0.1'], // cURL connects to 127.0.0.1
'IPv4 with fewer parts' => ['127.1'], // cURL connects to 127.0.0.1
];
}
/** Ordinary host names are still resolved and pinned as written */
public function test_getCurlResolveInfoAcceptsHostsUsedAsWrittenByCurl(): void {
FreshRSS_Context::initSystem();
$resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
foreach (['a_b-c.example.test', 'xn--9ca.example.test', '1.example.test', 'example.7z', 'example.test.'] as $host) {
$resolveOk->setValue(null, [$host => ['192.0.66.96']]);
self::assertSame(["$host:80:192.0.66.96"], FreshRSS_http_Util::getCurlResolveInfo("http://$host/feed"));
}
}
/** Socks schemes default to port 1080, and an IP address proxy produces no resolve entry */
public function test_getCurlResolveInfoForProxyAddress(): void {
FreshRSS_Context::initSystem();