mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
Ref: [GHSA-fc32-79hf-pcmw](https://github.com/FreshRSS/FreshRSS/security/advisories/GHSA-fc32-79hf-pcmw) (considered a bug), https://github.com/curl/curl/issues/23681, https://github.com/FreshRSS/FreshRSS/discussions/9286#discussioncomment-18392518 Fixed issues: * Using wrong default HTTP port for proxies (supposed to be 1080 not 80). * Lack of system configuration support for a mix of `CURLOPT_{PROXY,PROXYTYPE,PROXYPORT}` options, along with user defined feed options. * Normalize cURL proxy options during requests * Use `CURLPROXY_HTTPS` in tests instead of 2 * Fix some more mismatched port cases * Remove changelog Avoid changelog in PRs to reduce conflicts * Minor comments reduction --------- Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
515 lines
26 KiB
PHP
515 lines
26 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
use PHPUnit\Framework\Attributes\DataProvider;
|
||
|
||
/**
|
||
* Tests for FreshRSS_http_Util
|
||
*/
|
||
final class httpUtilTest extends \PHPUnit\Framework\TestCase {
|
||
|
||
#[DataProvider('provideUrlsIgnoringHttps')]
|
||
public function test_compareUrlIgnoringHttps(string $url1, string $url2, bool $expected): void {
|
||
self::assertEquals($expected, FreshRSS_http_Util::compareUrlIgnoringHttps($url1, $url2) === 0);
|
||
}
|
||
|
||
#[DataProvider('provideCurlProxyValues')]
|
||
public function test_sanitizeCurlParamsRejectsMalformedProxy(string $proxy, bool $shouldSurvive): void {
|
||
$sanitized = FreshRSS_http_Util::sanitizeCurlParams([CURLOPT_PROXY => $proxy, CURLOPT_PROXYTYPE => CURLPROXY_HTTP]);
|
||
if ($shouldSurvive) {
|
||
self::assertSame($proxy, $sanitized[CURLOPT_PROXY] ?? null);
|
||
} else {
|
||
self::assertArrayNotHasKey(CURLOPT_PROXY, $sanitized);
|
||
}
|
||
}
|
||
|
||
/** @return array<string,array{string,bool}> */
|
||
public static function provideCurlProxyValues(): array {
|
||
return [
|
||
// A second `://` would otherwise be reduced by a greedy scheme-strip to this
|
||
// trailing "decoy" host for the allowlist/DNS-pinning check, while cURL still
|
||
// received the original, unstripped value as CURLOPT_PROXY.
|
||
'rejects scheme-prefixed decoy host' => ['http://127.0.0.1:8080/://8.8.8.8', false],
|
||
'rejects scheme-less decoy host' => ['127.0.0.1:8080/://198.51.100.10', false],
|
||
// The scheme comes from CURLOPT_PROXYTYPE, not the value itself -- a scheme-containing
|
||
// value is rejected outright rather than normalised.
|
||
'rejects a value carrying its own scheme' => ['http://proxy.example.com:3128', false],
|
||
// cURL treats "host:port/path" as a Unix domain socket for SOCKS proxies and ignores the
|
||
// port, so an allowlisted host:port could otherwise be smuggled into connecting to an
|
||
// arbitrary local socket.
|
||
'rejects a SOCKS Unix-domain-socket path' => ['localhost:9050/path/to.sock', false],
|
||
// Certain characters complicate normalizing of proxy URLs, so they're disallowed.
|
||
'rejects a query delimiter' => ['127.0.0.1?:6379', false],
|
||
'rejects a fragment delimiter' => ['127.0.0.1#:6379', false],
|
||
'rejects a query hiding a decoy host' => ['127.0.0.1?@10.0.0.1:80', false],
|
||
'rejects a fragment hiding a decoy host' => ['8.8.8.8#@127.0.0.1', false],
|
||
'rejects a backslash' => ['127.0.0.1\\@8.8.8.8:80', false],
|
||
'rejects a space' => ['127.0.0.1 :80', false],
|
||
'rejects a tab' => ["127.0.0.1\t:80", false],
|
||
'rejects a newline' => ["127.0.0.1\n:80", false],
|
||
'accepts percent-encoded credentials' => ['user:p%3Fss%23w%40rd@proxy.example.com:3128', true],
|
||
'accepts bare host:port' => ['127.0.0.1:8080', true],
|
||
'accepts hostname:port' => ['proxy.example.com:3128', true],
|
||
'accepts host:port with credentials' => ['user:pass@203.0.113.5:1080', true],
|
||
'accepts an IPv6 proxy' => ['[::1]:8080', true],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @param array<int,mixed> $options
|
||
* @param array<int,mixed> $expected
|
||
*/
|
||
#[DataProvider('provideProxyOptions')]
|
||
public function test_normalizeProxyOptions(array $options, array $expected): void {
|
||
self::assertEquals($expected, FreshRSS_http_Util::normalizeProxyOptions($options));
|
||
}
|
||
|
||
/** @return array<string,array{array<int,mixed>,array<int,mixed>}> */
|
||
public static function provideProxyOptions(): array {
|
||
return [
|
||
'CURLOPT_PROXYPORT is added' => [
|
||
[CURLOPT_PROXY => 'proxy.internal', CURLOPT_PROXYTYPE => CURLPROXY_HTTP, CURLOPT_PROXYPORT => 3128],
|
||
[CURLOPT_PROXY => 'proxy.internal:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'port in CURLOPT_PROXY wins' => [
|
||
[CURLOPT_PROXY => 'proxy.internal:3128', CURLOPT_PROXYPORT => 4000],
|
||
[CURLOPT_PROXY => 'proxy.internal:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'scheme in system config' => [
|
||
[CURLOPT_PROXY => 'http://proxy.internal:3128/'],
|
||
[CURLOPT_PROXY => 'proxy.internal:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'scheme wins over type' => [
|
||
[CURLOPT_PROXY => 'socks5h://user:pass@proxy.internal', CURLOPT_PROXYTYPE => CURLPROXY_HTTP],
|
||
[CURLOPT_PROXY => 'user:pass@proxy.internal:1080', CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME]],
|
||
'scheme with CURLOPT_PROXYPORT' => [
|
||
[CURLOPT_PROXY => 'http://proxy.internal', CURLOPT_PROXYPORT => 3128],
|
||
[CURLOPT_PROXY => 'proxy.internal:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'scheme in uppercase' => [
|
||
[CURLOPT_PROXY => 'HTTP://proxy.internal:3128'],
|
||
[CURLOPT_PROXY => 'proxy.internal:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'HTTPS scheme' => [
|
||
[CURLOPT_PROXY => 'https://proxy.internal'],
|
||
[CURLOPT_PROXY => 'proxy.internal:443', CURLOPT_PROXYTYPE => CURLPROXY_HTTPS]],
|
||
'unsupported scheme is left as is' => [
|
||
[CURLOPT_PROXY => 'ftp://proxy.internal:3128'],
|
||
[CURLOPT_PROXY => 'ftp://proxy.internal:3128']],
|
||
'default port for HTTPS' => [
|
||
[CURLOPT_PROXY => 'proxy.internal', CURLOPT_PROXYTYPE => CURLPROXY_HTTPS],
|
||
[CURLOPT_PROXY => 'proxy.internal:443', CURLOPT_PROXYTYPE => CURLPROXY_HTTPS]],
|
||
'IPv6 without port' => [
|
||
[CURLOPT_PROXY => '[::1]', CURLOPT_PROXYPORT => 3128],
|
||
[CURLOPT_PROXY => '[::1]:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'IPv6 with port' => [
|
||
[CURLOPT_PROXY => '[::1]:3128'],
|
||
[CURLOPT_PROXY => '[::1]:3128', CURLOPT_PROXYTYPE => CURLPROXY_HTTP]],
|
||
'invalid value is left as is' => [
|
||
[CURLOPT_PROXY => 'http://127.0.0.1:8080/://8.8.8.8'],
|
||
[CURLOPT_PROXY => 'http://127.0.0.1:8080/://8.8.8.8']],
|
||
'query is left as is' => [
|
||
[CURLOPT_PROXY => '127.0.0.1?:6379'],
|
||
[CURLOPT_PROXY => '127.0.0.1?:6379']],
|
||
'fragment is left as is' => [
|
||
[CURLOPT_PROXY => 'http://127.0.0.1#:6379'],
|
||
[CURLOPT_PROXY => 'http://127.0.0.1#:6379']],
|
||
'proxy disabled' => [
|
||
[CURLOPT_PROXY => 'proxy.internal', CURLOPT_PROXYTYPE => -1],
|
||
[CURLOPT_PROXY => 'proxy.internal', CURLOPT_PROXYTYPE => -1]],
|
||
'no proxy' => [[CURLOPT_PROXYPORT => 3128], [CURLOPT_PROXYPORT => 3128]],
|
||
];
|
||
}
|
||
|
||
/** An ambiguous feed proxy must be rejected by sanitization, before the port check of `getCurlResolveInfo()`. */
|
||
#[DataProvider('provideAmbiguousFeedProxies')]
|
||
public function test_feedProxyIsNotAmbiguousForAllowlistCheck(string $proxy): void {
|
||
putenv('INTERNAL_HOST_ALLOWLIST=127.0.0.1:80');
|
||
try {
|
||
$options = FreshRSS_http_Util::normalizeProxyOptions(
|
||
FreshRSS_http_Util::sanitizeCurlParams([CURLOPT_PROXY => $proxy, CURLOPT_PROXYTYPE => CURLPROXY_HTTP]));
|
||
self::assertArrayNotHasKey(CURLOPT_PROXY, $options);
|
||
self::assertArrayNotHasKey(CURLOPT_PROXYTYPE, $options);
|
||
} finally {
|
||
putenv('INTERNAL_HOST_ALLOWLIST');
|
||
}
|
||
}
|
||
|
||
/** @return array<string,array{string}> */
|
||
public static function provideAmbiguousFeedProxies(): array {
|
||
return [
|
||
'query with port' => ['127.0.0.1?:6379'],
|
||
'fragment with port' => ['127.0.0.1#:6379'],
|
||
'query without port' => ['127.0.0.1?'],
|
||
'fragment without port' => ['127.0.0.1#'],
|
||
'query then credentials' => ['10.0.0.1?@:6379'],
|
||
];
|
||
}
|
||
|
||
public function test_proxyPortIsUsedByAllowlistCheck(): void {
|
||
putenv('INTERNAL_HOST_ALLOWLIST=127.0.0.1:3128');
|
||
$options = FreshRSS_http_Util::normalizeProxyOptions([CURLOPT_PROXY => '127.0.0.1', CURLOPT_PROXYPORT => 3128]);
|
||
self::assertIsString($options[CURLOPT_PROXY]);
|
||
self::assertSame([], FreshRSS_http_Util::getCurlResolveInfo('http://' . $options[CURLOPT_PROXY]));
|
||
putenv('INTERNAL_HOST_ALLOWLIST');
|
||
}
|
||
|
||
#[DataProvider('provideUrlsForCheckUrl')]
|
||
public function test_checkUrl(string $url, string|false $expected): void {
|
||
self::assertSame($expected, FreshRSS_http_Util::checkUrl($url));
|
||
}
|
||
|
||
/** @return Traversable<string,array{string,string|false}> */
|
||
public static function provideUrlsForCheckUrl(): Traversable {
|
||
yield 'plain' => ['https://example.net/feed?a=1&b=2', 'https://example.net/feed?a=1&b=2'];
|
||
yield 'underscore' => ['https://ex_ample.net/', 'https://ex_ample.net/'];
|
||
yield 'IPv6' => ['https://[2001:db8::1]/feed', 'https://[2001:db8::1]/feed'];
|
||
if (function_exists('idn_to_ascii')) {
|
||
yield 'IDN' => ['https://bücher.example/feed', 'https://xn--bcher-kva.example/feed'];
|
||
yield 'IDN with ideographic full stop' => ['https://日本語。example/', 'https://xn--wgv71a119e.example/'];
|
||
}
|
||
// IDNA (UTS #46) maps fullwidth punctuation to ASCII: must not produce characters absent from the (possibly HTML-encoded) input
|
||
yield 'fullwidth markup in host' => ['https://example.net/"><b>', false];
|
||
yield 'fullwidth path and query in host' => ['https://example.net/path?a=1&b=2', false];
|
||
yield 'fullwidth userinfo in host' => ['https://example.net@example.org/', false];
|
||
}
|
||
|
||
#[\Override]
|
||
protected function tearDown(): void {
|
||
FreshRSS_Context::initSystem(reload: true); // Restore the default internal host allowlist
|
||
$resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
|
||
$resolveOk->setValue(null, []); // Restore the default empty cache
|
||
}
|
||
|
||
#[DataProvider('provideUrlsForRetryAfter')]
|
||
public function test_getRetryAfterFile(string $url1, string $url2, bool $sameFile): void {
|
||
$getRetryAfterFile = new ReflectionMethod(FreshRSS_http_Util::class, 'getRetryAfterFile');
|
||
self::assertSame($sameFile, $getRetryAfterFile->invoke(null, $url1, '') === $getRetryAfterFile->invoke(null, $url2, ''));
|
||
}
|
||
|
||
/** @return array<string,array{string,string,bool}> */
|
||
public static function provideUrlsForRetryAfter(): array {
|
||
return [
|
||
// A public server waits as a whole, per port
|
||
'public server' => ['https://198.51.100.7/feed1', 'https://198.51.100.7/feed2?a=1&b=2', true],
|
||
'public server, other port' => ['https://198.51.100.7/feed', 'https://198.51.100.7:8443/feed', false],
|
||
// A server on a local network waits URL by URL
|
||
'local IP address' => ['http://192.168.1.2/feed1', 'http://192.168.1.2/feed2', false],
|
||
'local domain' => ['http://rss-bridge.lan/?bridge=A', 'http://rss-bridge.lan/?bridge=B', false],
|
||
'local, same URL' => ['http://192.168.1.2/feed', 'http://192.168.1.2/feed', true],
|
||
];
|
||
}
|
||
|
||
#[DataProvider('provideUtf8Html')]
|
||
public function test_enforceHtmlBase_keepsUtf8InScripts(string $html): void {
|
||
$enforceHtmlBase = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHtmlBase');
|
||
$result = $enforceHtmlBase->invoke(null, $html, 'https://example.net/page');
|
||
self::assertIsString($result);
|
||
self::assertStringContainsString('<base href="https://example.net/page">', $result);
|
||
self::assertStringContainsString('<script type="application/json">{"title":"Café’s menu — 日本"}</script>', $result);
|
||
if (str_starts_with($html, "\xEF\xBB\xBF")) {
|
||
self::assertStringStartsWith("\xEF\xBB\xBF", $result);
|
||
}
|
||
}
|
||
|
||
/** @return Traversable<string,array{string}> */
|
||
public static function provideUtf8Html(): Traversable {
|
||
$head = '<html><head><title>T</title></head>';
|
||
$body = '<body><p>Café’s menu — 日本</p><script type="application/json">{"title":"Café’s menu — 日本"}</script></body></html>';
|
||
yield 'meta charset' => ['<!DOCTYPE html><html><head><meta charset="utf-8"><title>T</title></head>' . $body];
|
||
yield 'byte order mark' => ["\xEF\xBB\xBF" . '<!DOCTYPE html>' . $head . $body];
|
||
yield 'byte order mark, late <html>' => ["\xEF\xBB\xBF" . '<!DOCTYPE html>' . str_repeat('<!-- padding -->', 40) . $head . $body];
|
||
yield 'meta http-equiv' => ['<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>T</title></head>' . $body];
|
||
}
|
||
|
||
/** @return Traversable<string,array{string,string,string}> */
|
||
public static function provideLegacyCharsets(): Traversable {
|
||
$isoUtf8 = 'M' . "\xC3\xBC" . 'llerstra' . "\xC3\x9F" . 'e';
|
||
yield 'iso-8859-1, meta charset' => ['iso-8859-1',
|
||
'<!DOCTYPE html><html><head><meta charset="iso-8859-1"></head>' .
|
||
'<body><p>M' . "\xFC" . 'llerstra' . "\xDF" . 'e</p></body></html>', $isoUtf8];
|
||
// A Windows-1252 body whose quotes differ from the ISO-8859-1 mapping
|
||
yield 'windows-1252, smart quotes' => ['windows-1252',
|
||
'<!DOCTYPE html><html><head><meta charset="windows-1252"></head>' .
|
||
'<body><p>' . "\x93" . 'quoted' . "\x94" . '</p></body></html>', "\xE2\x80\x9C" . 'quoted' . "\xE2\x80\x9D"];
|
||
}
|
||
|
||
// Legacy charsets declared over HTTP must be converted to UTF-8 and not just left
|
||
// to the HTML parser, because `saveHTML()` serialises extracted fragments according
|
||
// to the encoding of the document, and downstream steps expecting UTF-8 would
|
||
// therefore fail on the remaining Windows-1252 bytes (e.g. full-content fetching, issue #9648).
|
||
#[DataProvider('provideLegacyCharsets')]
|
||
public function test_enforceHttpEncoding_convertsLegacyCharsetsToUtf8(string $httpCharset, string $html, string $expectedUtf8Text): void {
|
||
$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
|
||
$result = $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=' . $httpCharset);
|
||
self::assertIsString($result);
|
||
// The HTTP-declared encoding is enforced by prefixing a Unicode byte order mark
|
||
self::assertStringStartsWith("\xEF\xBB\xBF", $result);
|
||
self::assertStringContainsString($expectedUtf8Text, $result);
|
||
// The obsolete charset declaration is removed, so that the now UTF-8 bytes are not re-read with the declared legacy charset
|
||
self::assertStringNotContainsString('<meta', $result);
|
||
}
|
||
|
||
public function test_enforceHttpEncoding_legacyCharset_fragmentSerialization(): void {
|
||
$html = '<!DOCTYPE html><html><head><meta charset="iso-8859-1"></head><body><div class="artikel">' .
|
||
'<p>Gr' . "\xF6" . 'nemeyer, oft w' . "\xFC" . 'tend</p></div></body></html>';
|
||
$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
|
||
$html = $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=iso-8859-1');
|
||
self::assertIsString($html);
|
||
|
||
// Extract a fragment the way done when fetching the full content of an article
|
||
$doc = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
|
||
$node = $doc->querySelector('div.artikel>p');
|
||
self::assertNotNull($node);
|
||
self::assertSame('Gr' . "\xC3\xB6" . 'nemeyer, oft w' . "\xC3\xBC" . 'tend', $node->textContent);
|
||
$fragment = $doc->saveHTML($node);
|
||
// The serialised fragment must remain valid UTF-8 and not be corrupted to U+FFFD replacement characters
|
||
self::assertStringContainsString('Gr' . "\xC3\xB6" . 'nemeyer, oft w' . "\xC3\xBC" . 'tend', $fragment);
|
||
self::assertStringNotContainsString("\xEF\xBF\xBD", $fragment);
|
||
}
|
||
|
||
public function test_enforceHttpEncoding_usAsciiHttpCharset_unchanged(): void {
|
||
$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
|
||
$html = '<p>abcdef</p>';
|
||
self::assertSame($html, $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=US-ASCII'));
|
||
}
|
||
|
||
public function test_enforceHttpEncoding_noHttpCharsetLeavesLegacyHtmlUnchanged(): void {
|
||
$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
|
||
$html = '<!DOCTYPE html><html><head><meta charset="iso-8859-1"></head><body><p>M' . "\xFC" . 'ller</p></body></html>';
|
||
// Without an HTTP-declared charset, a legacy-declared body is left untouched, for the HTML parser to handle it
|
||
self::assertSame($html, $enforceHttpEncoding->invoke(null, $html, 'text/html'));
|
||
}
|
||
|
||
public function test_enforceHttpEncoding_utf8HttpCharsetPrefixesByteOrderMark(): void {
|
||
$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
|
||
$html = '<p>M' . "\xC3\xBC" . 'ller</p>';
|
||
self::assertSame("\xEF\xBB\xBF" . $html, $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=utf-8'));
|
||
}
|
||
|
||
#[DataProvider('provideCidrRanges')]
|
||
public function test_checkCIDR(string $ip, string $range, bool $expected): void {
|
||
$checkCIDR = new ReflectionMethod(FreshRSS_http_Util::class, 'checkCIDR');
|
||
self::assertEquals($expected, $checkCIDR->invoke(null, $ip, $range));
|
||
}
|
||
|
||
/** @return list<array{string,string,bool}> */
|
||
public static function provideCidrRanges(): array {
|
||
return [
|
||
// A range without a subnet is a single address
|
||
['192.168.1.1', '192.168.1.1', true],
|
||
['192.168.1.2', '192.168.1.1', false],
|
||
['2001:db8::1', '2001:db8::1', true],
|
||
['2001:db8::2', '2001:db8::1', false],
|
||
|
||
// An explicit subnet keeps working
|
||
['192.168.1.1', '192.168.1.1/32', true],
|
||
['192.168.1.42', '192.168.1.0/24', true],
|
||
['192.168.2.42', '192.168.1.0/24', false],
|
||
['192.168.1.1', '0.0.0.0/0', true],
|
||
['2001:db8::1', '2001:db8::/32', true],
|
||
|
||
// Invalid input is still rejected
|
||
['192.168.1.1', '', false],
|
||
['192.168.1.1', '192.168.1.1/', false],
|
||
['192.168.1.1', '192.168.1.1/33', false],
|
||
['192.168.1.1', '192.168.1.1/abc', false],
|
||
['192.168.1.1', 'gateway.localdomain', false],
|
||
['192.168.1.1', '2001:db8::1', false],
|
||
['not-an-ip', '192.168.1.1', false],
|
||
];
|
||
}
|
||
|
||
/** @return list<array{string,string,bool}> */
|
||
public static function provideUrlsIgnoringHttps(): array {
|
||
return [
|
||
// Only the scheme differs → equal
|
||
['http://www.blogger.com/feeds/1/posts', 'https://www.blogger.com/feeds/1/posts', true],
|
||
['https://example.net/feed.xml?a=1&b=2', 'http://example.net/feed.xml?a=1&b=2', true],
|
||
['HTTP://Example.net/Feed', 'https://Example.net/Feed', true],
|
||
['HTTPS://Example.net/Feed', 'http://Example.net/Feed', true],
|
||
|
||
// Fully identical → equal
|
||
['https://example.net/feed', 'https://example.net/feed', true],
|
||
['', '', true],
|
||
|
||
// Path differs → not equal (scheme-only tolerance must not hide real mismatches)
|
||
['http://example.net/a', 'https://example.net/b', false],
|
||
// Trailing slash is a path difference → not equal
|
||
['http://example.net/', 'https://example.net', false],
|
||
// Host differs → not equal
|
||
['http://a.example.net/feed', 'https://b.example.net/feed', false],
|
||
// Query differs → not equal
|
||
['https://example.net/feed?a=1', 'http://example.net/feed?a=2', false],
|
||
// Non-http(s) schemes are compared as-is
|
||
['ftp://example.net/feed', 'https://example.net/feed', false],
|
||
];
|
||
}
|
||
|
||
public function test_getCurlResolveInfoAcceptsPublicNat64Address(): void {
|
||
FreshRSS_Context::initSystem();
|
||
$resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
|
||
$resolveOk->setValue(null, [
|
||
'example.test' => [
|
||
'192.0.66.96',
|
||
'64:ff9b::c000:4260',
|
||
],
|
||
]);
|
||
|
||
self::assertSame(
|
||
['example.test:443:192.0.66.96,[64:ff9b::c000:4260]'],
|
||
FreshRSS_http_Util::getCurlResolveInfo('https://example.test/feed')
|
||
);
|
||
}
|
||
|
||
#[DataProvider('providePrivateSubnets')]
|
||
public function test_getCurlResolveInfoRejectsPrivateSubnets(string $ip): void {
|
||
FreshRSS_Context::initSystem();
|
||
$resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
|
||
$resolveOk->setValue(null, ['example.test' => [$ip]]);
|
||
self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('https://example.test/feed'));
|
||
}
|
||
|
||
/** @return array<string,array{string}> */
|
||
public static function providePrivateSubnets(): array {
|
||
return [
|
||
// One example per range, ensuring FILTER_FLAG_GLOBAL_RANGE and custom checks are sufficient
|
||
'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
|
||
'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
|
||
'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
|
||
'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
|
||
'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
|
||
];
|
||
}
|
||
|
||
/** 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();
|
||
// A proxy on a private address is rejected by default
|
||
self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://127.0.0.1:1080'));
|
||
|
||
// Once the proxy is allowlisted, no resolve override is needed for an IP address
|
||
FreshRSS_Context::systemConf()->internal_host_allowlist = ['127.0.0.1:1080'];
|
||
self::assertSame([], FreshRSS_http_Util::getCurlResolveInfo('socks5h://127.0.0.1:1080'));
|
||
|
||
// Other proxy ports need their own entry
|
||
FreshRSS_Context::systemConf()->internal_host_allowlist = ['127.0.0.1:8080'];
|
||
self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://127.0.0.1:1080'));
|
||
self::assertSame([], FreshRSS_http_Util::getCurlResolveInfo('https://127.0.0.1:8080'));
|
||
}
|
||
|
||
/** For a hostname proxy, the resolved IP of the proxy is pinned with a resolve entry, to prevent DNS rebinding */
|
||
public function test_getCurlResolveInfoForProxyHostname(): void {
|
||
FreshRSS_Context::initSystem();
|
||
$resolveOk = new ReflectionProperty(FreshRSS_http_Util::class, 'resolve_ok');
|
||
$resolveOk->setValue(null, ['proxy.example.test' => ['198.51.100.7']]);
|
||
FreshRSS_Context::systemConf()->internal_host_allowlist = ['198.51.100.7:1080'];
|
||
|
||
self::assertSame(
|
||
['proxy.example.test:1080:198.51.100.7'],
|
||
FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080')
|
||
);
|
||
|
||
// Applications must not emit resolve entries for an address that is not guaranteed public.
|
||
// TEST-NET-2 is a reserved range, so without an allowlist entry, no entry is produced.
|
||
FreshRSS_Context::systemConf()->internal_host_allowlist = [];
|
||
self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080'));
|
||
|
||
// A genuinely public address produces an entry without allowlisting
|
||
$resolveOk->setValue(null, ['public-proxy.example.test' => ['8.8.8.8']]);
|
||
self::assertSame(
|
||
['public-proxy.example.test:1080:8.8.8.8'],
|
||
FreshRSS_http_Util::getCurlResolveInfo('socks5h://public-proxy.example.test:1080')
|
||
);
|
||
|
||
// A proxy resolving to a private address is rejected unless allowlisted
|
||
$resolveOk->setValue(null, ['proxy.example.test' => ['192.168.1.1']]);
|
||
self::assertNull(FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080'));
|
||
FreshRSS_Context::systemConf()->internal_host_allowlist = ['192.168.1.1:1080'];
|
||
self::assertSame(
|
||
['proxy.example.test:1080:192.168.1.1'],
|
||
FreshRSS_http_Util::getCurlResolveInfo('socks5h://proxy.example.test:1080')
|
||
);
|
||
}
|
||
}
|