Explorar o código

Harden SSRF guard: reject remaining reserved/special-use IP ranges (#9621)

* Reject remaining special-use ranges in the SSRF guard

FILTER_FLAG_GLOBAL_RANGE still accepts several reserved ranges on
supported PHP versions. Reject those explicitly, including when an
IPv4 address is embedded in the public NAT64 prefix, and keep
legitimate public NAT64 addresses allowed.

Co-authored-by: aplaton216 <aplaton216@users.noreply.github.com>

* Refactor

Fix https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-2983-h3r5-qv6j
Fix https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-8f2g-vq96-ffxw

* Doc PHP upstream

https://github.com/php/php-src/pull/25426

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: aplaton216 <aplaton216@users.noreply.github.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
aplaton216 hai 7 horas
pai
achega
f94c101621
Modificáronse 3 ficheiros con 87 adicións e 14 borrados
  1. 1 0
      CREDITS.md
  2. 35 5
      app/Utils/httpUtil.php
  3. 51 9
      tests/app/Utils/httpUtilTest.php

+ 1 - 0
CREDITS.md

@@ -38,6 +38,7 @@ People are sorted by name so please keep this order.
 * [Annika Backstrom](https://github.com/abackstrom): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:abackstrom), [Web](https://sixohthree.com/)
 * [Anton Smirnov](https://github.com/arokettu): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:arokettu), [Web](https://sandfox.me/)
 * [Antonio Santos](https://github.com/Otolock): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:Otolock), [Web](https://otolock.net/)
+* [aplaton216](https://github.com/aplaton216): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:aplaton216)
 * [Arslan-TR](https://github.com/Arslan-TR): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:Arslan-TR)
 * [ArthurHoaro](https://github.com/ArthurHoaro): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:ArthurHoaro)
 * [Artem Netsvetaev](https://github.com/netsvetoch): [contributions](https://github.com/FreshRSS/FreshRSS/pulls?q=is:pr+author:netsvetoch)

+ 35 - 5
app/Utils/httpUtil.php

@@ -5,7 +5,23 @@ final class FreshRSS_http_Util {
 
 	private const RETRY_AFTER_PATH = DATA_PATH . '/Retry-After/';
 	private const NAT64_PREFIX_RFC6052 = '64:ff9b::/96';
-	private const NAT64_PREFIX_RFC8215 = '64:ff9b:1::/48';
+
+	/**
+	 * `FILTER_FLAG_GLOBAL_RANGE` does not reject everything yet:
+	 * https://github.com/php/php-src/pull/25426
+	 * @var list<string>
+	 */
+	private const ADDITIONAL_NON_GLOBAL_RANGES = [
+		'3fff::/20',	// RFC 9637 (Documentation)
+		'5f00::/16',	// RFC 9602 (SRv6 SIDs)
+		'64:ff9b:1::/48',	// RFC 8215 (Local-Use IPv4/IPv6 Translation Prefix)
+		'100:0:0:1::/64',	// RFC 9780 (Dummy IPv6 Prefix)
+		'192.88.99.0/24',	// RFC 7526 (deprecated 6to4 anycast)
+		'224.0.0.0/4',	// RFC 1112 (IPv4 multicast)
+		'2002::/16',	// RFC 3056 (6to4)
+		'fec0::/10',	// RFC 3879 (deprecated site-local)
+		'ff00::/8',	// RFC 4291 (IPv6 multicast)
+	];
 	/** @var array<string, string[]> $resolve_ok */
 	private static array $resolve_ok = [];
 	/** @var array<string, bool> $retry_after_domain_wide */
@@ -391,7 +407,7 @@ final class FreshRSS_http_Util {
 					continue;
 				}
 
-				if (filter_var($embedded_ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE) === false) {
+				if (self::isNonGlobalIp($embedded_ipv4)) {
 					continue;
 				}
 
@@ -399,9 +415,7 @@ final class FreshRSS_http_Util {
 				continue;
 			}
 
-			// `FILTER_FLAG_GLOBAL_RANGE` does not reject the NAT64 prefixes
-			if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE) === false ||
-				self::checkCIDR($ip, self::NAT64_PREFIX_RFC8215)) {
+			if (self::isNonGlobalIp($ip)) {
 				continue;
 			}
 
@@ -803,6 +817,22 @@ final class FreshRSS_http_Util {
 		return $ip_net_bits === $subnet_bits;
 	}
 
+	/**
+	 * Whether $ip is outside the global answer ranges
+	 */
+	private static function isNonGlobalIp(string $ip): bool {
+		if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE) === false) {
+			return true;
+		}
+		// `FILTER_FLAG_GLOBAL_RANGE` does not reject all special-use ranges
+		foreach (self::ADDITIONAL_NON_GLOBAL_RANGES as $range) {
+			if (self::checkCIDR($ip, $range)) {
+				return true;
+			}
+		}
+		return false;
+	}
+
 	/**
 	 * Check if the client (e.g. last proxy) is allowed to send unsafe headers.
 	 * This uses the `TRUSTED_PROXY` environment variable or the `trusted_sources` configuration option to get an array of the authorized ranges,

+ 51 - 9
tests/app/Utils/httpUtilTest.php

@@ -200,21 +200,63 @@ final class httpUtilTest extends \PHPUnit\Framework\TestCase {
 	public static function providePrivateSubnets(): array {
 		return [
 			// One example per range, ensuring FILTER_FLAG_GLOBAL_RANGE and custom checks are sufficient
-			'127.0.0.0/8' => ['127.0.0.1'],	// RFC1700 (Loopback)
+			'0.0.0.0/8' => ['0.0.0.5'],	// RFC5735
+			'3fff::/20' => ['3fff::1'],	// RFC9637 (Documentation)
+			'5f00::/16' => ['5f00::1'],	// RFC9602 (SRv6 SIDs)
 			'10.0.0.0/8' => ['10.0.0.5'],	// RFC1918
-			'192.168.0.0/16' => ['192.168.1.5'],	// RFC1918
-			'172.16.0.0/12' => ['172.16.5.5'],	// RFC1918
+			'64:ff9b:1::/48' => ['64:ff9b:1::c000:4260'],	// RFC8215 (Local-Use IPv4/IPv6 Translation Prefix)
+			'64:ff9b::/96' => ['64:ff9b::a9fe:a9fe'],	// RFC6052 (IPv4/IPv6 Translator, NAT64)
+			'100.64.0.0/10' => ['100.64.5.5'],	// RFC6598 (Shared Address Space of Carrier-Grade NAT)
+			'100:0:0:1::/64' => ['100:0:0:1::1'],	// RFC9780 (Dummy IPv6 Prefix)
+			'127.0.0.0/8' => ['127.0.0.1'],	// RFC1700 (Loopback)
 			'169.254.0.0/16' => ['169.254.5.5'],	// RFC3927
-			'0.0.0.0/8' => ['0.0.0.5'],	// RFC5735
+			'172.16.0.0/12' => ['172.16.5.5'],	// RFC1918
+			'192.0.0.0/24' => ['192.0.0.170'],	// RFC6890 (IETF Protocol Assignments)
+			'192.0.2.0/24' => ['192.0.2.1'],	// RFC5737 (TEST-NET-1)
+			'192.88.99.0/24' => ['192.88.99.1'],	// RFC7526 (deprecated 6to4 anycast)
+			'192.168.0.0/16' => ['192.168.1.5'],	// RFC1918
+			'198.18.0.0/15' => ['198.18.0.1'],	// RFC2544 (Benchmarking)
+			'198.18.0.0/15 upper' => ['198.19.255.255'],
+			'198.51.100.0/24' => ['198.51.100.1'],	// RFC5737 (TEST-NET-2)
+			'203.0.113.0/24' => ['203.0.113.1'],	// RFC5737 (TEST-NET-3)
+			'224.0.0.0/4' => ['224.0.0.1'],	// RFC1112 (IPv4 multicast)
+			'224.0.0.0/4 upper' => ['239.255.255.255'],
 			'240.0.0.0/4' => ['240.0.0.5'],	// RFC1112
-			'100.64.0.0/10' => ['100.64.5.5'],	// RFC6598 (Shared Address Space of Carrier-Grade NAT)
+			'2001::/32' => ['2001::1'],	// RFC4380 (Teredo)
+			'2002::/16' => ['2002::1'],	// RFC3056 (6to4)
+			'2002::/16 upper' => ['2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff'],
+			'::/128' => ['::'],	// Unspecified address
 			'::1/128' => ['::1'],	// Loopback
+			'::ffff:0:0/96' => ['::ffff:10.0.0.5'],	// IPv4 translations
 			'fc00::/7' => ['fc00::5'],	// Unique Local Address
 			'fe80::/10' => ['fe80::5'],	// Link Local Address
-			'::ffff:0:0/96' => ['::ffff:10.0.0.5'],	// IPv4 translations
-			'64:ff9b::/96' => ['64:ff9b::a9fe:a9fe'],	// RFC6052 (IPv4/IPv6 Translator, NAT64)
-			'64:ff9b:1::/48' => ['64:ff9b:1::c000:4260'],	// RFC8215 (Local-Use IPv4/IPv6 Translation Prefix)
-			'::/128' => ['::'],	// Unspecified address
+			'fec0::/10' => ['fec0::1'],	// RFC3879 (deprecated site-local)
+			'fec0::/10 upper' => ['feff::1'],
+			'ff00::/8' => ['ff02::1'],	// RFC4291 (IPv6 multicast)
+		];
+	}
+
+	#[DataProvider('providePublicAddresses')]
+	public function test_getCurlResolveInfoAcceptsPublicAddresses(string $ip): void {
+		FreshRSS_Context::initSystem();
+		$url = str_contains($ip, ':') ? "https://[$ip]/feed" : "https://$ip/feed";
+		self::assertSame([], FreshRSS_http_Util::getCurlResolveInfo($url));
+	}
+
+	/** @return array<string,array{string}> */
+	public static function providePublicAddresses(): array {
+		return [
+			'public IPv4 alternate' => ['1.1.1.1'],
+			'public IPv4' => ['8.8.8.8'],
+			'below deprecated 6to4 anycast' => ['192.88.98.1'],
+			'above deprecated 6to4 anycast' => ['192.88.100.1'],
+			'below IPv4 multicast' => ['223.255.255.255'],
+			'public IPv6' => ['2001:4860:4860::8888'],
+			'outside 6to4' => ['2003::1'],
+			'outside documentation prefix' => ['3fff:1000::1'],
+			'outside SRv6 prefix' => ['5f01::1'],
+			'outside dummy IPv6 prefix' => ['100:0:0:2::1'],
+			'public NAT64' => ['64:ff9b::c000:4260'],	// RFC6052 embedding 192.0.66.96
 		];
 	}