Преглед на файлове

Better sanitize domains (#9611)

Fix https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-3ch8-46c3-xr9h
Alexandre Alapetite преди 9 часа
родител
ревизия
dcd40cb7d4
променени са 2 файла, в които са добавени 40 реда и са изтрити 0 реда
  1. 7 0
      app/Utils/httpUtil.php
  2. 33 0
      tests/app/Utils/httpUtilTest.php

+ 7 - 0
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 = [];

+ 33 - 0
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<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();