From dcd40cb7d44ea4a8eb262b0032346e2728a470f1 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sun, 22 Nov 2026 21:57:54 +0100 Subject: [PATCH] Better sanitize domains (#9611) Fix https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-3ch8-46c3-xr9h --- app/Utils/httpUtil.php | 7 +++++++ tests/app/Utils/httpUtilTest.php | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/Utils/httpUtil.php b/app/Utils/httpUtil.php index 044b4bf4b..d33233e70 100644 --- a/app/Utils/httpUtil.php +++ b/app/Utils/httpUtil.php @@ -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 = []; diff --git a/tests/app/Utils/httpUtilTest.php b/tests/app/Utils/httpUtilTest.php index 3573174aa..97dfa1496 100644 --- a/tests/app/Utils/httpUtilTest.php +++ b/tests/app/Utils/httpUtilTest.php @@ -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 */ + 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();