4
0

httpUtilTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. /**
  5. * Tests for FreshRSS_http_Util
  6. */
  7. final class httpUtilTest extends \PHPUnit\Framework\TestCase {
  8. #[DataProvider('provideUrlsIgnoringHttps')]
  9. public function test_compareUrlIgnoringHttps(string $url1, string $url2, bool $expected): void {
  10. self::assertEquals($expected, FreshRSS_http_Util::compareUrlIgnoringHttps($url1, $url2) === 0);
  11. }
  12. #[\Override]
  13. protected function tearDown(): void {
  14. FreshRSS_Context::initSystem(reload: true); // Restore the default internal host allowlist
  15. $resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
  16. $resolveOk->setValue(null, []); // Restore the default empty cache
  17. }
  18. #[DataProvider('provideUrlsForRetryAfter')]
  19. public function test_getRetryAfterFile(string $url1, string $url2, bool $sameFile): void {
  20. $getRetryAfterFile = new ReflectionMethod(FreshRSS_http_Util::class, 'getRetryAfterFile');
  21. self::assertSame($sameFile, $getRetryAfterFile->invoke(null, $url1, '') === $getRetryAfterFile->invoke(null, $url2, ''));
  22. }
  23. /** @return array<string,array{string,string,bool}> */
  24. public static function provideUrlsForRetryAfter(): array {
  25. return [
  26. // A public server waits as a whole, per port
  27. 'public server' => ['https://198.51.100.7/feed1', 'https://198.51.100.7/feed2?a=1&b=2', true],
  28. 'public server, other port' => ['https://198.51.100.7/feed', 'https://198.51.100.7:8443/feed', false],
  29. // A server on a local network waits URL by URL
  30. 'local IP address' => ['http://192.168.1.2/feed1', 'http://192.168.1.2/feed2', false],
  31. 'local domain' => ['http://rss-bridge.lan/?bridge=A', 'http://rss-bridge.lan/?bridge=B', false],
  32. 'local, same URL' => ['http://192.168.1.2/feed', 'http://192.168.1.2/feed', true],
  33. ];
  34. }
  35. #[DataProvider('provideUtf8Html')]
  36. public function test_enforceHtmlBase_keepsUtf8InScripts(string $html): void {
  37. $enforceHtmlBase = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHtmlBase');
  38. $result = $enforceHtmlBase->invoke(null, $html, 'https://example.net/page');
  39. self::assertIsString($result);
  40. self::assertStringContainsString('<base href="https://example.net/page">', $result);
  41. self::assertStringContainsString('<script type="application/json">{"title":"Café’s menu — 日本"}</script>', $result);
  42. if (str_starts_with($html, "\xEF\xBB\xBF")) {
  43. self::assertStringStartsWith("\xEF\xBB\xBF", $result);
  44. }
  45. }
  46. /** @return Traversable<string,array{string}> */
  47. public static function provideUtf8Html(): Traversable {
  48. $head = '<html><head><title>T</title></head>';
  49. $body = '<body><p>Café’s menu — 日本</p><script type="application/json">{"title":"Café’s menu — 日本"}</script></body></html>';
  50. yield 'meta charset' => ['<!DOCTYPE html><html><head><meta charset="utf-8"><title>T</title></head>' . $body];
  51. yield 'byte order mark' => ["\xEF\xBB\xBF" . '<!DOCTYPE html>' . $head . $body];
  52. yield 'byte order mark, late <html>' => ["\xEF\xBB\xBF" . '<!DOCTYPE html>' . str_repeat('<!-- padding -->', 40) . $head . $body];
  53. yield 'meta http-equiv' => ['<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>T</title></head>' . $body];
  54. }
  55. #[DataProvider('provideCidrRanges')]
  56. public function test_checkCIDR(string $ip, string $range, bool $expected): void {
  57. $checkCIDR = new ReflectionMethod(FreshRSS_http_Util::class, 'checkCIDR');
  58. self::assertEquals($expected, $checkCIDR->invoke(null, $ip, $range));
  59. }
  60. /** @return list<array{string,string,bool}> */
  61. public static function provideCidrRanges(): array {
  62. return [
  63. // A range without a subnet is a single address
  64. ['192.168.1.1', '192.168.1.1', true],
  65. ['192.168.1.2', '192.168.1.1', false],
  66. ['2001:db8::1', '2001:db8::1', true],
  67. ['2001:db8::2', '2001:db8::1', false],
  68. // An explicit subnet keeps working
  69. ['192.168.1.1', '192.168.1.1/32', true],
  70. ['192.168.1.42', '192.168.1.0/24', true],
  71. ['192.168.2.42', '192.168.1.0/24', false],
  72. ['192.168.1.1', '0.0.0.0/0', true],
  73. ['2001:db8::1', '2001:db8::/32', true],
  74. // Invalid input is still rejected
  75. ['192.168.1.1', '', false],
  76. ['192.168.1.1', '192.168.1.1/', false],
  77. ['192.168.1.1', '192.168.1.1/33', false],
  78. ['192.168.1.1', '192.168.1.1/abc', false],
  79. ['192.168.1.1', 'gateway.localdomain', false],
  80. ['192.168.1.1', '2001:db8::1', false],
  81. ['not-an-ip', '192.168.1.1', false],
  82. ];
  83. }
  84. /** @return list<array{string,string,bool}> */
  85. public static function provideUrlsIgnoringHttps(): array {
  86. return [
  87. // Only the scheme differs → equal
  88. ['http://www.blogger.com/feeds/1/posts', 'https://www.blogger.com/feeds/1/posts', true],
  89. ['https://example.net/feed.xml?a=1&b=2', 'http://example.net/feed.xml?a=1&b=2', true],
  90. ['HTTP://Example.net/Feed', 'https://Example.net/Feed', true],
  91. ['HTTPS://Example.net/Feed', 'http://Example.net/Feed', true],
  92. // Fully identical → equal
  93. ['https://example.net/feed', 'https://example.net/feed', true],
  94. ['', '', true],
  95. // Path differs → not equal (scheme-only tolerance must not hide real mismatches)
  96. ['http://example.net/a', 'https://example.net/b', false],
  97. // Trailing slash is a path difference → not equal
  98. ['http://example.net/', 'https://example.net', false],
  99. // Host differs → not equal
  100. ['http://a.example.net/feed', 'https://b.example.net/feed', false],
  101. // Query differs → not equal
  102. ['https://example.net/feed?a=1', 'http://example.net/feed?a=2', false],
  103. // Non-http(s) schemes are compared as-is
  104. ['ftp://example.net/feed', 'https://example.net/feed', false],
  105. ];
  106. }
  107. public function test_getCurlResolveInfoAcceptsPublicNat64Address(): void {
  108. FreshRSS_Context::initSystem();
  109. $resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
  110. $resolveOk->setValue(null, [
  111. 'example.test' => [
  112. '192.0.66.96',
  113. '64:ff9b::c000:4260',
  114. ],
  115. ]);
  116. self::assertSame(
  117. ['example.test:443:192.0.66.96,[64:ff9b::c000:4260]'],
  118. FreshRSS_http_Util::getCurlResolveInfo('https://example.test/feed')
  119. );
  120. }
  121. #[DataProvider('providePrivateSubnets')]
  122. public function test_getCurlResolveInfoRejectsPrivateSubnets(string $ip): void {
  123. FreshRSS_Context::initSystem();
  124. $resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
  125. $resolveOk->setValue(null, ['example.test' => [$ip]]);
  126. self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('https://example.test/feed'));
  127. }
  128. /** @return array<string,array{string}> */
  129. public static function providePrivateSubnets(): array {
  130. return [
  131. // One example per range, ensuring FILTER_FLAG_GLOBAL_RANGE and custom checks are sufficient
  132. '127.0.0.0/8' => ['127.0.0.1'], // RFC1700 (Loopback)
  133. '10.0.0.0/8' => ['10.0.0.5'], // RFC1918
  134. '192.168.0.0/16' => ['192.168.1.5'], // RFC1918
  135. '172.16.0.0/12' => ['172.16.5.5'], // RFC1918
  136. '169.254.0.0/16' => ['169.254.5.5'], // RFC3927
  137. '0.0.0.0/8' => ['0.0.0.5'], // RFC5735
  138. '240.0.0.0/4' => ['240.0.0.5'], // RFC1112
  139. '100.64.0.0/10' => ['100.64.5.5'], // RFC6598 (Shared Address Space of Carrier-Grade NAT)
  140. '::1/128' => ['::1'], // Loopback
  141. 'fc00::/7' => ['fc00::5'], // Unique Local Address
  142. 'fe80::/10' => ['fe80::5'], // Link Local Address
  143. '::ffff:0:0/96' => ['::ffff:10.0.0.5'], // IPv4 translations
  144. '64:ff9b::/96' => ['64:ff9b::a9fe:a9fe'], // RFC6052 (IPv4/IPv6 Translator, NAT64)
  145. '64:ff9b:1::/48' => ['64:ff9b:1::c000:4260'], // RFC8215 (Local-Use IPv4/IPv6 Translation Prefix)
  146. '::/128' => ['::'], // Unspecified address
  147. ];
  148. }
  149. /** Socks schemes default to port 1080, and an IP address proxy produces no resolve entry */
  150. public function test_getCurlResolveInfoForProxyAddress(): void {
  151. FreshRSS_Context::initSystem();
  152. // A proxy on a private address is rejected by default
  153. self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://127.0.0.1:1080'));
  154. // Once the proxy is allowlisted, no resolve override is needed for an IP address
  155. FreshRSS_Context::systemConf()->internal_host_allowlist = ['127.0.0.1:1080'];
  156. self::assertSame([], FreshRSS_http_Util::getCurlResolveInfo('socks5h://127.0.0.1:1080'));
  157. // Other proxy ports need their own entry
  158. FreshRSS_Context::systemConf()->internal_host_allowlist = ['127.0.0.1:8080'];
  159. self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://127.0.0.1:1080'));
  160. self::assertSame([], FreshRSS_http_Util::getCurlResolveInfo('https://127.0.0.1:8080'));
  161. }
  162. /** For a hostname proxy, the resolved IP of the proxy is pinned with a resolve entry, to prevent DNS rebinding */
  163. public function test_getCurlResolveInfoForProxyHostname(): void {
  164. FreshRSS_Context::initSystem();
  165. $resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
  166. $resolveOk->setValue(null, ['proxy.example.test' => ['198.51.100.7']]);
  167. FreshRSS_Context::systemConf()->internal_host_allowlist = ['198.51.100.7:1080'];
  168. self::assertSame(
  169. ['proxy.example.test:1080:198.51.100.7'],
  170. FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080')
  171. );
  172. // Applications must not emit resolve entries for an address that is not guaranteed public.
  173. // TEST-NET-2 is a reserved range, so without an allowlist entry, no entry is produced.
  174. FreshRSS_Context::systemConf()->internal_host_allowlist = [];
  175. self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080'));
  176. // A genuinely public address produces an entry without allowlisting
  177. $resolveOk->setValue(null, ['public-proxy.example.test' => ['8.8.8.8']]);
  178. self::assertSame(
  179. ['public-proxy.example.test:1080:8.8.8.8'],
  180. FreshRSS_http_Util::getCurlResolveInfo('socks5h://public-proxy.example.test:1080')
  181. );
  182. // A proxy resolving to a private address is rejected unless allowlisted
  183. $resolveOk->setValue(null, ['proxy.example.test' => ['192.168.1.1']]);
  184. self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080'));
  185. FreshRSS_Context::systemConf()->internal_host_allowlist = ['192.168.1.1:1080'];
  186. self::assertSame(
  187. ['proxy.example.test:1080:192.168.1.1'],
  188. FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080')
  189. );
  190. }
  191. }