4
0

httpUtil.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. <?php
  2. declare(strict_types=1);
  3. final class FreshRSS_http_Util {
  4. private const RETRY_AFTER_PATH = DATA_PATH . '/Retry-After/';
  5. private const NAT64_PREFIX_RFC6052 = '64:ff9b::/96';
  6. private const NAT64_PREFIX_RFC8215 = '64:ff9b:1::/48';
  7. /** @var array<string, string[]> $resolve_ok */
  8. private static array $resolve_ok = [];
  9. /** @var array<string, bool> $retry_after_domain_wide */
  10. private static array $retry_after_domain_wide = [];
  11. private static function getRetryAfterFile(string $url, string $proxy): string {
  12. $domain = parse_url($url, PHP_URL_HOST);
  13. if (!is_string($domain) || $domain === '') {
  14. return '';
  15. }
  16. // Once per host, as serverIsPublic() may resolve it
  17. $domainWide = self::$retry_after_domain_wide[$domain] ??= Minz_Request::serverIsPublic($url, allowDnsResolve: $proxy === '');
  18. $port = parse_url($url, PHP_URL_PORT);
  19. if (is_int($port)) {
  20. $domain .= ':' . $port;
  21. }
  22. return self::RETRY_AFTER_PATH . urlencode($domain) .
  23. ($domainWide ? '' : '_' . hash('sha256', $url)) .
  24. (empty($proxy) ? '' : '_' . urlencode($proxy)) . '.txt';
  25. }
  26. /**
  27. * Clean up old Retry-After files
  28. */
  29. private static function cleanRetryAfters(): void {
  30. if (!is_dir(self::RETRY_AFTER_PATH)) {
  31. return;
  32. }
  33. $files = glob(self::RETRY_AFTER_PATH . '*.txt', GLOB_NOSORT);
  34. if ($files === false) {
  35. return;
  36. }
  37. foreach ($files as $file) {
  38. if (@filemtime($file) < time()) {
  39. @unlink($file);
  40. }
  41. }
  42. }
  43. /**
  44. * Check whether the URL needs to wait for a Retry-After period.
  45. * @return int The timestamp of when the Retry-After expires, or 0 if not set.
  46. */
  47. public static function getRetryAfter(string $url, string $proxy): int {
  48. if (rand(0, 30) === 1) { // Remove old files once in a while
  49. self::cleanRetryAfters();
  50. }
  51. $txt = self::getRetryAfterFile($url, $proxy);
  52. if ($txt === '') {
  53. return 0;
  54. }
  55. $retryAfter = @filemtime($txt) ?: 0;
  56. if ($retryAfter <= 0) {
  57. return 0;
  58. }
  59. if ($retryAfter < time()) {
  60. @unlink($txt);
  61. return 0;
  62. }
  63. return $retryAfter;
  64. }
  65. /**
  66. * Store the HTTP Retry-After header value of an HTTP `429 Too Many Requests` or `503 Service Unavailable` response.
  67. */
  68. public static function setRetryAfter(string $url, string $proxy, string $retryAfter): int {
  69. $txt = self::getRetryAfterFile($url, $proxy);
  70. if ($txt === '') {
  71. return 0;
  72. }
  73. $limits = FreshRSS_Context::systemConf()->limits;
  74. if (ctype_digit($retryAfter)) {
  75. $retryAfter = time() + (int)$retryAfter;
  76. } else {
  77. $retryAfter = \SimplePie\Misc::parse_date($retryAfter) ?:
  78. (time() + max(600, $limits['retry_after_default'] ?? 0));
  79. }
  80. $retryAfter = min($retryAfter, time() + max(3600, $limits['retry_after_max'] ?? 0));
  81. @mkdir(self::RETRY_AFTER_PATH);
  82. if (!touch($txt, $retryAfter)) {
  83. Minz_Log::error('Failed to set Retry-After for ' . $url);
  84. return 0;
  85. }
  86. return $retryAfter;
  87. }
  88. /**
  89. * @param array<mixed> $curl_params
  90. * @return array<mixed>
  91. */
  92. public static function sanitizeCurlParams(array $curl_params): array {
  93. $safe_params = [
  94. CURLOPT_COOKIE,
  95. CURLOPT_COOKIEFILE,
  96. CURLOPT_FOLLOWLOCATION, // We filter this value later, only allowing `false`
  97. CURLOPT_HTTPHEADER,
  98. CURLOPT_MAXREDIRS,
  99. CURLOPT_POST,
  100. CURLOPT_POSTFIELDS,
  101. CURLOPT_PROXY,
  102. CURLOPT_PROXYTYPE,
  103. CURLOPT_USERAGENT,
  104. ];
  105. foreach ($curl_params as $k => $_) {
  106. if (!in_array($k, $safe_params, true)) {
  107. unset($curl_params[$k]);
  108. continue;
  109. }
  110. // Allow only an empty value just to enable the libcurl cookie engine
  111. if ($k === CURLOPT_COOKIEFILE) {
  112. $curl_params[$k] = '';
  113. }
  114. // Remove HTTP authentication headers problematic for security
  115. if ($k === CURLOPT_HTTPHEADER && is_array($curl_params[$k])) {
  116. $curl_params[$k] = array_filter($curl_params[$k],
  117. fn($header) => is_string($header) && !preg_match('/^(Remote[-_\s]*User|X[-_\s]*WebAuth[-_\s]*User)\\s*:/i', $header));
  118. }
  119. // Forbid '/' in CURLOPT_PROXY [user[:pass]@]host[:port] (the scheme comes from CURLOPT_PROXYTYPE),
  120. // to reject e.g. 'http://example.net/://decoy' and 'localhost:9050/path/to.sock'
  121. if ($k === CURLOPT_PROXY && (!is_string($curl_params[$k]) || str_contains($curl_params[$k], '/'))) {
  122. unset($curl_params[$k], $curl_params[CURLOPT_PROXYTYPE]);
  123. }
  124. }
  125. return $curl_params;
  126. }
  127. private static function idn_to_puny(string $url): string {
  128. if (function_exists('idn_to_ascii')) {
  129. $idn = parse_url($url, PHP_URL_HOST);
  130. if (is_string($idn) && $idn != '') {
  131. $puny = idn_to_ascii($idn);
  132. $pos = strpos($url, $idn);
  133. if ($pos !== false && is_string($puny) && preg_match('/^[a-z0-9._-]+$/iD', $puny) === 1) {
  134. $url = substr_replace($url, $puny, $pos, strlen($idn));
  135. }
  136. }
  137. }
  138. return $url;
  139. }
  140. public static function checkUrl(string $url, bool $fixScheme = true): string|false {
  141. $url = trim($url);
  142. if ($url == '') {
  143. return '';
  144. }
  145. if ($fixScheme && preg_match('#^https?://#i', $url) !== 1) {
  146. $url = 'https://' . ltrim($url, '/');
  147. }
  148. $url = self::idn_to_puny($url); // https://bugs.php.net/bug.php?id=53474
  149. $urlRelaxed = str_replace('_', 'z', $url); //PHP discussion #64948 Underscore
  150. if (is_string(filter_var($urlRelaxed, FILTER_VALIDATE_URL))) {
  151. return $url;
  152. } else {
  153. return false;
  154. }
  155. }
  156. /**
  157. * Remove the charset meta information of an HTML document, e.g.:
  158. * `<meta charset="..." />`
  159. * `<meta http-equiv="Content-Type" content="text/html; charset=...">`
  160. */
  161. private static function stripHtmlMetaCharset(string $html): string {
  162. return preg_replace('/<meta\s[^>]*charset\s*=\s*[^>]+>/i', '', $html, 1) ?? '';
  163. }
  164. /**
  165. * Set an XML preamble to enforce the HTML content type charset received by HTTP.
  166. * @param string $html the raw downloaded HTML content
  167. * @param string $contentType an HTTP Content-Type such as 'text/html; charset=utf-8'
  168. * @return string an HTML string with Unicode BOM encoding information
  169. */
  170. private static function enforceHttpEncoding(string $html, string $contentType = ''): string {
  171. $httpCharset = preg_match('/\bcharset=([0-9a-z_-]{2,12})$/i', $contentType, $matches) === 1 ? $matches[1] : '';
  172. if ($httpCharset == '') {
  173. // No charset defined by HTTP
  174. if (preg_match('/<meta\s[^>]*charset\s*=[\s\'"]*UTF-?8\b/i', substr($html, 0, 2048))) {
  175. // Detect UTF-8 even if declared too deep in HTML for DOMDocument
  176. $httpCharset = 'UTF-8';
  177. } else {
  178. // Do nothing
  179. return $html;
  180. }
  181. }
  182. $httpCharsetNormalized = \SimplePie\Misc::encoding($httpCharset);
  183. if (in_array($httpCharsetNormalized, ['windows-1252', 'US-ASCII'], true)) {
  184. // Default charset for HTTP, do nothing
  185. return $html;
  186. }
  187. if (substr($html, 0, 3) === "\xEF\xBB\xBF" || // UTF-8 BOM
  188. substr($html, 0, 2) === "\xFF\xFE" || // UTF-16 Little Endian BOM
  189. substr($html, 0, 2) === "\xFE\xFF" || // UTF-16 Big Endian BOM
  190. substr($html, 0, 4) === "\xFF\xFE\x00\x00" || // UTF-32 Little Endian BOM
  191. substr($html, 0, 4) === "\x00\x00\xFE\xFF") { // UTF-32 Big Endian BOM
  192. // Existing byte order mark, do nothing
  193. return $html;
  194. }
  195. if (preg_match('/^<[?]xml[^>]+encoding\b/', substr($html, 0, 64))) {
  196. // Existing XML declaration, do nothing
  197. return $html;
  198. }
  199. if ($httpCharsetNormalized !== 'UTF-8') {
  200. // Try to change encoding to UTF-8 using mbstring or iconv or intl
  201. $utf8 = \SimplePie\Misc::change_encoding($html, $httpCharsetNormalized, 'UTF-8');
  202. if (is_string($utf8)) {
  203. $html = self::stripHtmlMetaCharset($utf8);
  204. $httpCharsetNormalized = 'UTF-8';
  205. }
  206. }
  207. if ($httpCharsetNormalized === 'UTF-8') {
  208. // Save encoding information as Unicode BOM
  209. return "\xEF\xBB\xBF" . $html;
  210. }
  211. // Give up
  212. return $html;
  213. }
  214. /**
  215. * Set an HTML base URL to the HTML content if there is none.
  216. * @param string $html the raw downloaded HTML content
  217. * @param string $href the HTML base URL
  218. * @return string an HTML string
  219. */
  220. private static function enforceHtmlBase(string $html, string $href): string {
  221. $doc = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
  222. $documentElement = $doc->documentElement;
  223. if ($documentElement === null) {
  224. return '';
  225. }
  226. if ($doc->querySelector('base') === null) {
  227. $base = $doc->createElement('base');
  228. $base->setAttribute('href', $href);
  229. $head = $doc->querySelector('head');
  230. if ($head instanceof Dom\Element) {
  231. $head->insertBefore($base, $head->firstChild);
  232. } else {
  233. $documentElement->insertBefore($base, $documentElement->firstChild);
  234. }
  235. }
  236. $saved = $doc->saveHTML();
  237. if ($saved === '') {
  238. return $html;
  239. }
  240. // Re-add the Unicode byte order mark, for downstream charset detection
  241. return (str_starts_with($html, "\xEF\xBB\xBF") ? "\xEF\xBB\xBF" : '') . $saved;
  242. }
  243. public static function compareURLOrigins(string $url1, string $url2): bool {
  244. $url1 = parse_url(strtolower($url1));
  245. $url2 = parse_url(strtolower($url2));
  246. if ($url1 === false || $url2 === false) {
  247. return false;
  248. }
  249. foreach ([&$url1, &$url2] as &$url) {
  250. $url['port'] ??= match ($url['scheme'] ?? '') {
  251. 'http' => 80,
  252. 'https' => 443,
  253. default => 0,
  254. };
  255. }
  256. return ($url1['scheme'] ?? '') === ($url2['scheme'] ?? '') &&
  257. ($url1['host'] ?? '') === ($url2['host'] ?? '') &&
  258. ($url1['port'] ?? '') === ($url2['port'] ?? '');
  259. }
  260. /**
  261. * Return 0 if values on either side are equal ignoring the HTTP vs HTTPS differences, or 1/-1 if they differ.
  262. */
  263. public static function compareUrlIgnoringHttps(string $url1, string $url2): int {
  264. $normalizeScheme = static fn(string $url): string => preg_replace('#^https?://#i', '//', trim($url)) ?? $url;
  265. return $normalizeScheme($url1) <=> $normalizeScheme($url2);
  266. }
  267. /**
  268. * Returns a value for CURLOPT_RESOLVE as an array, null if no allowed IPs were found, false if the domain failed to resolve.
  269. *
  270. * @return array<string>|null|false
  271. */
  272. public static function getCurlResolveInfo(string $url): array|null|false {
  273. $url = strtolower($url);
  274. $parsed = parse_url($url);
  275. if ($parsed === false) {
  276. return false;
  277. }
  278. $host = $parsed['host'] ?? null;
  279. $scheme = $parsed['scheme'] ?? null;
  280. if ($host === null || $scheme === null) {
  281. return false;
  282. }
  283. if (str_starts_with($host, '[') && str_ends_with($host, ']')) {
  284. if (strlen($host) === 2) {
  285. return false;
  286. }
  287. $host = substr($host, 1, strlen($host) - 2);
  288. }
  289. $internal_host_allowlist = getenv('INTERNAL_HOST_ALLOWLIST');
  290. if (is_string($internal_host_allowlist) && $internal_host_allowlist !== '') {
  291. $internal_host_allowlist = preg_split('/\s+/', $internal_host_allowlist, -1, PREG_SPLIT_NO_EMPTY);
  292. }
  293. if (!is_array($internal_host_allowlist) || empty($internal_host_allowlist)) {
  294. $internal_host_allowlist = FreshRSS_Context::systemConf()->internal_host_allowlist;
  295. }
  296. $port = parse_url($url)['port'] ?? match ($scheme) {
  297. 'http' => 80,
  298. 'https' => 443,
  299. 'socks4' => 1080,
  300. 'socks4a' => 1080,
  301. 'socks5' => 1080,
  302. 'socks5h' => 1080,
  303. default => 0,
  304. };
  305. if (in_array('*', $internal_host_allowlist, true)) {
  306. return []; // Disables SSRF checks entirely (unsafe)
  307. }
  308. if (filter_var($host, FILTER_VALIDATE_IP) === false) {
  309. // cURL percent-decodes the host, converts non-ASCII to Punycode, and reads a host ending in a number as IPv4 (e.g. 0x7f.1).
  310. // Refuse such hosts, as cURL would contact another name or address than the one checked and pinned here.
  311. if (preg_match('/^[a-z0-9._-]+$/D', $host) !== 1 || preg_match('/(^|\.)(\d+|0x[0-9a-f]*)\.?$/D', $host) === 1) {
  312. return false;
  313. }
  314. }
  315. $resolve_str = "$host:$port:";
  316. $ips_ok = [];
  317. $ips = [];
  318. $records = [];
  319. if (filter_var($host, FILTER_VALIDATE_IP) !== false) {
  320. $ips[] = $host;
  321. } elseif (isset(self::$resolve_ok[$host])) {
  322. $ips = self::$resolve_ok[$host];
  323. } else {
  324. $records = @dns_get_record($host, DNS_A + DNS_AAAA);
  325. if ($records === false) {
  326. return false;
  327. }
  328. foreach ($records as $record) {
  329. $ip = $record['ip'] ?? $record['ipv6'];
  330. if (is_string($ip)) {
  331. $ips[] = $ip;
  332. }
  333. }
  334. self::$resolve_ok[$host] = $ips;
  335. }
  336. $cidr_allowlist = array_filter($internal_host_allowlist, fn($v, $_) => str_contains($v, '/'), ARRAY_FILTER_USE_BOTH);
  337. foreach ($ips as $ip) {
  338. $allowlist_str = "$ip:$port";
  339. $add_ip = $ip;
  340. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
  341. $allowlist_str = "[$ip]:$port";
  342. $add_ip = "[$ip]";
  343. }
  344. foreach ($cidr_allowlist as $cidr) {
  345. if (self::checkCIDR($ip, $cidr)) {
  346. $ips_ok[] = $add_ip;
  347. continue 2;
  348. }
  349. }
  350. if (in_array($allowlist_str, $internal_host_allowlist, true) ||
  351. in_array("$host:$port", $internal_host_allowlist, true)) {
  352. $ips_ok[] = $add_ip;
  353. continue;
  354. }
  355. // NAT64 addresses use the RFC 6052 well-known prefix.
  356. // Validate the embedded IPv4 address because the NAT64 IPv6 address itself
  357. // is globally routable even when it maps to a private/reserved IPv4 address.
  358. if (self::checkCIDR($ip, self::NAT64_PREFIX_RFC6052)) {
  359. $packed = @inet_pton($ip);
  360. if ($packed === false || strlen($packed) !== 16) {
  361. continue;
  362. }
  363. $embedded_ipv4 = @inet_ntop(substr($packed, 12, 4));
  364. if ($embedded_ipv4 === false) {
  365. continue;
  366. }
  367. if (filter_var($embedded_ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE) === false) {
  368. continue;
  369. }
  370. $ips_ok[] = $add_ip;
  371. continue;
  372. }
  373. // `FILTER_FLAG_GLOBAL_RANGE` does not reject the NAT64 prefixes
  374. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_GLOBAL_RANGE) === false ||
  375. self::checkCIDR($ip, self::NAT64_PREFIX_RFC8215)) {
  376. continue;
  377. }
  378. $ips_ok[] = $add_ip;
  379. }
  380. if (count($ips_ok) > 0) {
  381. if (count($records) > 0 || isset(self::$resolve_ok[$host])) {
  382. $resolve_str .= implode(',', $ips_ok);
  383. return [$resolve_str];
  384. }
  385. if (filter_var($host, FILTER_VALIDATE_IP) !== false) {
  386. // No resolve overrides since the URL only contained an IP, not a domain
  387. return [];
  388. }
  389. }
  390. if (count($ips) === 0) {
  391. return false;
  392. }
  393. return null;
  394. }
  395. /**
  396. * Whether cURL sends the request as a HTTP POST (`CURLOPT_POST` or `CURLOPT_POSTFIELDS`)
  397. * @param array<mixed> $user_options User-defined cURL options derived from `curl_params`
  398. * @param array<mixed> $curl_options Internal cURL options
  399. */
  400. private static function isPost(array $user_options, array $curl_options): bool {
  401. return !empty($user_options[CURLOPT_POST]) || !empty($curl_options[CURLOPT_POST]) ||
  402. isset($user_options[CURLOPT_POSTFIELDS]) || isset($curl_options[CURLOPT_POSTFIELDS]);
  403. }
  404. /**
  405. * @param non-empty-string $url
  406. * @param string|null $cachePath path to cache file, or `null` to disable caching
  407. * @param string $type {html,ico,json,opml,xml}
  408. * @param array<string,mixed> $attributes May contain user-defined cURL options in `$attributes['curl_params']`
  409. * @param array<int,mixed> $curl_options Internal overrides of cURL options
  410. * @return array{body:string,effective_url:string,redirect_count:int,fail:bool,status:int,error:string,method:string}
  411. * `status` is the HTTP response code (e.g. 200, 404), or a custom negative value:
  412. * * `-200` served from local cache;
  413. * * `-429` blocked by active `Retry-After` period;
  414. * * `-500` `curl_init()` failure, or request refused by a security check.
  415. *
  416. * `method` is the HTTP method used for the request producing `status` ('GET' or 'POST').
  417. */
  418. public static function httpGet(string $url, ?string $cachePath = null, string $type = 'html', array $attributes = [], array $curl_options = []): array {
  419. $options = is_array($attributes['curl_params'] ?? null) ?
  420. self::sanitizeCurlParams($attributes['curl_params']) : []; // User-defined cURL options
  421. $c_method = self::isPost($options, $curl_options) ? 'POST' : 'GET';
  422. if (!\SimplePie\Misc::is_remote_uri($url)) {
  423. Minz_Log::warning('Error fetching content: malformed URL “' . $url . '“');
  424. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  425. }
  426. $limits = FreshRSS_Context::systemConf()->limits;
  427. $feed_timeout = empty($attributes['timeout']) || !is_numeric($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  428. if ($cachePath !== null) {
  429. $cacheMtime = @filemtime($cachePath);
  430. if ($cacheMtime !== false && $cacheMtime > time() - intval($limits['cache_duration'])) {
  431. $body = @file_get_contents($cachePath);
  432. if ($body != false) {
  433. if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
  434. syslog(LOG_DEBUG, 'FreshRSS uses cache for ' . \SimplePie\Misc::url_remove_credentials($url));
  435. }
  436. return ['body' => $body, 'effective_url' => $url, 'redirect_count' => 0, 'fail' => false, 'status' => -200,
  437. 'error' => '', 'method' => $c_method];
  438. }
  439. }
  440. }
  441. if (rand(0, 30) === 1) { // Remove old cache once in a while
  442. cleanCache(CLEANCACHE_HOURS);
  443. }
  444. $accept = '';
  445. $proxy = is_string(FreshRSS_Context::systemConf()->curl_options[CURLOPT_PROXY] ?? null) ? FreshRSS_Context::systemConf()->curl_options[CURLOPT_PROXY] : '';
  446. $proxy_type = is_int(FreshRSS_Context::systemConf()->curl_options[CURLOPT_PROXYTYPE] ?? null) ?
  447. FreshRSS_Context::systemConf()->curl_options[CURLOPT_PROXYTYPE] : 0;
  448. if (!empty($options)) {
  449. $proxy = is_string($options[CURLOPT_PROXY] ?? null) ? $options[CURLOPT_PROXY] : $proxy;
  450. $proxy_type = is_int($options[CURLOPT_PROXYTYPE] ?? null) ? $options[CURLOPT_PROXYTYPE] : $proxy_type;
  451. if (is_array($options[CURLOPT_HTTPHEADER] ?? null)) {
  452. // Add Accept header if it is not set
  453. if (preg_grep('/^Accept\\s*:/i', $options[CURLOPT_HTTPHEADER]) === false) {
  454. $options[CURLOPT_HTTPHEADER][] = 'Accept: ' . $accept;
  455. }
  456. }
  457. }
  458. $proxy_type = is_int($curl_options[CURLOPT_PROXYTYPE] ?? null) ? $curl_options[CURLOPT_PROXYTYPE] : $proxy_type;
  459. // CURLOPT_PROXY is already scheme-less by the time it reaches here:
  460. $proxy = is_string($curl_options[CURLOPT_PROXY] ?? null) ? $curl_options[CURLOPT_PROXY] : $proxy;
  461. if (($retryAfter = FreshRSS_http_Util::getRetryAfter($url, $proxy)) > 0) {
  462. Minz_Log::warning('For that domain, will first retry after ' . date('c', $retryAfter) . '. ' . \SimplePie\Misc::url_remove_credentials($url));
  463. return ['body' => '', 'effective_url' => $url, 'redirect_count' => 0, 'fail' => true, 'status' => -429, 'error' => '', 'method' => $c_method];
  464. }
  465. if (FreshRSS_Context::systemConf()->simplepie_syslog_enabled) {
  466. syslog(LOG_INFO, 'FreshRSS ' . $c_method . ' ' . $type . ' ' . \SimplePie\Misc::url_remove_credentials($url));
  467. }
  468. switch ($type) {
  469. case 'json':
  470. $accept = 'application/json,application/feed+json,application/javascript;q=0.9,text/javascript;q=0.8,*/*;q=0.7';
  471. break;
  472. case 'opml':
  473. $accept = 'text/x-opml,text/xml;q=0.9,application/xml;q=0.9,*/*;q=0.8';
  474. break;
  475. case 'xml':
  476. $accept = 'application/xml,application/xhtml+xml,text/xml;q=0.9,*/*;q=0.8';
  477. break;
  478. case 'ico':
  479. $accept = 'image/x-icon,image/vnd.microsoft.icon,image/ico,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.1';
  480. break;
  481. case 'html':
  482. default:
  483. $accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  484. break;
  485. }
  486. $original_url = $url;
  487. $fail = false;
  488. $redirs = 0;
  489. $max_redirs = $curl_options[CURLOPT_MAXREDIRS] ?? $options[CURLOPT_MAXREDIRS] ?? FreshRSS_Context::systemConf()->curl_options[CURLOPT_MAXREDIRS] ?? null;
  490. if (!is_int($max_redirs)) {
  491. $max_redirs = 4;
  492. }
  493. while (true) {
  494. $c_method = self::isPost($options, $curl_options) ? 'POST' : 'GET'; // Can change after a redirect has changed the method
  495. $url = is_string($url) ? $url : '';
  496. $resolve = [];
  497. if ($proxy === '') {
  498. $resolve = self::getCurlResolveInfo($url);
  499. if ($resolve === null) {
  500. Minz_Log::warning('Fetching this URL is not allowed, because the host’s IP is not in the allowlist [' .
  501. \SimplePie\Misc::url_remove_credentials($url) . ']');
  502. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  503. } elseif ($resolve === false) {
  504. Minz_Log::warning('Error during HTTP fetch: unable to resolve hostname! ' . \SimplePie\Misc::url_remove_credentials($url));
  505. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  506. }
  507. if (!empty($resolve)) {
  508. $curl_options[CURLOPT_RESOLVE] = $resolve; // Prevent DNS rebinding
  509. }
  510. } else {
  511. defined('CURLPROXY_HTTPS') or define('CURLPROXY_HTTPS', 2); // Compatibility cURL 7.51
  512. $proxy_scheme = match ($proxy_type) {
  513. CURLPROXY_HTTP => 'http',
  514. CURLPROXY_HTTPS => 'https',
  515. CURLPROXY_SOCKS4 => 'socks4',
  516. CURLPROXY_SOCKS4A => 'socks4a',
  517. CURLPROXY_SOCKS5 => 'socks5',
  518. CURLPROXY_SOCKS5_HOSTNAME => 'socks5h',
  519. default => null,
  520. };
  521. if ($proxy_scheme === null) {
  522. // Unsupported proxy type
  523. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  524. }
  525. $proxy_url = "$proxy_scheme://$proxy"; // CURLOPT_PROXY ($proxy) is formatted as user:pass@hostname:port, with the part before @ being optional
  526. $resolve = self::getCurlResolveInfo($proxy_url);
  527. if ($resolve === null) {
  528. Minz_Log::warning('Failed to fetch this URL, because the proxy’s IP is not in the allowlist [' .
  529. \SimplePie\Misc::url_remove_credentials($url) . '] [' .
  530. \SimplePie\Misc::url_remove_credentials($proxy_url) . ']');
  531. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  532. } elseif ($resolve === false) {
  533. Minz_Log::warning('Error during HTTP fetch: unable to resolve the proxy’s hostname! ' . \SimplePie\Misc::url_remove_credentials($proxy_url));
  534. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  535. }
  536. if (!empty($resolve)) {
  537. // Only for the proxy domain, socks4a and socks5h DNS queries will be passed through the proxy.
  538. // For the other proxy protocols, note that IPs for internal domains can be leaked,
  539. // since the domain is resolved outside of the proxy.
  540. $curl_options[CURLOPT_RESOLVE] = $resolve;
  541. }
  542. }
  543. // TODO: Implement HTTP 1.1 conditional GET If-Modified-Since
  544. $ch = curl_init();
  545. if ($ch === false || $url === '') {
  546. return ['body' => '', 'effective_url' => '', 'redirect_count' => 0, 'fail' => true, 'status' => -500, 'error' => '', 'method' => $c_method];
  547. }
  548. curl_setopt_array($ch, [
  549. CURLOPT_URL => $url,
  550. CURLOPT_HTTPHEADER => ['Accept: ' . $accept],
  551. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  552. CURLOPT_CONNECTTIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  553. CURLOPT_TIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  554. CURLOPT_RETURNTRANSFER => true,
  555. CURLOPT_ACCEPT_ENCODING => '', //Enable all encodings
  556. //CURLOPT_VERBOSE => 1, // To debug sent HTTP headers
  557. ]);
  558. curl_setopt_array($ch, $options);
  559. curl_setopt_array($ch, FreshRSS_Context::systemConf()->curl_options);
  560. $responseHeaders = '';
  561. curl_setopt($ch, CURLOPT_HEADERFUNCTION, function (\CurlHandle $ch, string $header) use (&$responseHeaders) {
  562. if (trim($header) !== '') { // Skip e.g. separation with trailer headers
  563. $responseHeaders .= $header;
  564. }
  565. return strlen($header);
  566. });
  567. if (isset($attributes['ssl_verify'])) {
  568. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, empty($attributes['ssl_verify']) ? 0 : 2);
  569. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (bool)$attributes['ssl_verify']);
  570. if (empty($attributes['ssl_verify'])) {
  571. curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1');
  572. }
  573. }
  574. if (defined('CURLOPT_PROTOCOLS_STR') && is_int(CURLOPT_PROTOCOLS_STR)) {
  575. $curl_options[CURLOPT_PROTOCOLS_STR] = 'http,https';
  576. if (defined('CURLOPT_REDIR_PROTOCOLS_STR') && is_int(CURLOPT_REDIR_PROTOCOLS_STR)) {
  577. $curl_options[CURLOPT_REDIR_PROTOCOLS_STR] = 'http,https';
  578. }
  579. } elseif (defined('CURLPROTO_HTTP') && defined('CURLPROTO_HTTPS')) {
  580. // Legacy PHP 8.2-
  581. if (defined('CURLOPT_PROTOCOLS')) {
  582. $curl_options[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
  583. }
  584. if (defined('CURLOPT_REDIR_PROTOCOLS')) {
  585. $curl_options[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
  586. }
  587. }
  588. curl_setopt_array($ch, $curl_options);
  589. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // We handle HTTP redirections manually for security
  590. $body = curl_exec($ch);
  591. $c_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  592. $c_content_type = '' . curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  593. $c_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  594. $c_error = curl_error($ch);
  595. $headers = [];
  596. if ($body !== false) {
  597. $responseHeaders .= "\r\n";
  598. $responseHeaders = \SimplePie\HTTP\Parser::prepareHeaders($responseHeaders);
  599. $parser = new \SimplePie\HTTP\Parser($responseHeaders);
  600. if ($parser->parse()) {
  601. $headers = $parser->headers;
  602. }
  603. }
  604. if (in_array($c_status, [301, 302, 303, 307, 308], true)) {
  605. // Handle the redirect by making another request
  606. $location = \SimplePie\Misc::absolutize_url($headers['location'] ?? $url, $url);
  607. if ($location === false || !\SimplePie\Misc::is_remote_uri($location)) {
  608. Minz_Log::warning('Invalid redirect location: malformed URL “' . ($headers['location'] ?? $url) . '“');
  609. break;
  610. }
  611. if (!self::compareURLOrigins($url, $location)) {
  612. unset($curl_options[CURLOPT_COOKIE]);
  613. unset($curl_options[CURLOPT_USERPWD]);
  614. unset($options[CURLOPT_COOKIE]);
  615. unset($options[CURLOPT_USERPWD]);
  616. if (is_array($options[CURLOPT_HTTPHEADER] ?? null)) {
  617. $options[CURLOPT_HTTPHEADER] = array_filter($options[CURLOPT_HTTPHEADER], fn(mixed $header): bool =>
  618. is_string($header) && !preg_match('/^(Cookie|Authorization)\\s*:/i', $header));
  619. }
  620. if (is_array($curl_options[CURLOPT_HTTPHEADER] ?? null)) {
  621. $curl_options[CURLOPT_HTTPHEADER] = array_filter($curl_options[CURLOPT_HTTPHEADER], fn(mixed $header): bool =>
  622. is_string($header) && !preg_match('/^(Cookie|Authorization)\\s*:/i', $header));
  623. }
  624. }
  625. if ($max_redirs >= 0) {
  626. $redirs++;
  627. }
  628. if ($redirs > $max_redirs) {
  629. Minz_Log::warning('Error fetching content: Too many redirects were hit [' . \SimplePie\Misc::url_remove_credentials($original_url) . ']');
  630. break;
  631. }
  632. if (self::isPost($options, $curl_options) &&
  633. in_array($c_status, [301, 302, 303], true)) { // Not for 307 and 308, which must not change the HTTP method
  634. unset($curl_options[CURLOPT_POST]);
  635. unset($curl_options[CURLOPT_POSTFIELDS]);
  636. unset($options[CURLOPT_POST]);
  637. unset($options[CURLOPT_POSTFIELDS]);
  638. if (is_array($options[CURLOPT_HTTPHEADER] ?? null)) {
  639. $options[CURLOPT_HTTPHEADER] = array_filter($options[CURLOPT_HTTPHEADER], fn(mixed $header): bool =>
  640. is_string($header) && !str_starts_with(strtolower(trim($header)), 'content-type:'));
  641. }
  642. if (is_array($curl_options[CURLOPT_HTTPHEADER] ?? null)) {
  643. $curl_options[CURLOPT_HTTPHEADER] = array_filter($curl_options[CURLOPT_HTTPHEADER], fn(mixed $header): bool =>
  644. is_string($header) && !str_starts_with(strtolower(trim($header)), 'content-type:'));
  645. }
  646. }
  647. $url = $location;
  648. continue;
  649. }
  650. $fail = $c_status < 200 || $c_status >= 300 || $c_error != '' || $body === false;
  651. if ($fail) {
  652. $body = '';
  653. Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url);
  654. if (in_array($c_status, [429, 503], true)) {
  655. $retryAfter = FreshRSS_http_Util::setRetryAfter($url, $proxy, $headers['retry-after'] ?? '');
  656. if ($c_status === 429) {
  657. $errorMessage = 'HTTP 429 Too Many Requests! [' . \SimplePie\Misc::url_remove_credentials($url) . ']';
  658. } elseif ($c_status === 503) {
  659. $errorMessage = 'HTTP 503 Service Unavailable! [' . \SimplePie\Misc::url_remove_credentials($url) . ']';
  660. }
  661. if ($retryAfter > 0) {
  662. $errorMessage .= ' We may retry after ' . date('c', $retryAfter);
  663. }
  664. }
  665. } elseif (!is_string($body) || strlen($body) === 0) { // TODO: Implement HTTP 410 Gone
  666. $body = '';
  667. } else {
  668. if (in_array($type, ['html', 'json', 'opml', 'xml'], true)) {
  669. $body = trim($body, " \n\r\t\v"); // Do not trim \x00 to avoid breaking a BOM
  670. }
  671. if (in_array($type, ['html', 'xml', 'opml'], true)) {
  672. $body = self::enforceHttpEncoding($body, $c_content_type);
  673. }
  674. if (in_array($type, ['html'], true)) {
  675. if (stripos($c_content_type, 'text/plain') !== false) {
  676. // Plain text to be displayed as preformatted text. Prefixed with UTF-8 BOM
  677. $body = "\xEF\xBB\xBF" . '<pre class="text-plain">' . htmlspecialchars($body, ENT_NOQUOTES, 'UTF-8') . '</pre>';
  678. } else {
  679. $body = self::enforceHtmlBase($body, $c_effective_url);
  680. }
  681. }
  682. }
  683. break;
  684. }
  685. if ($cachePath !== null && file_put_contents($cachePath, $body) === false) {
  686. Minz_Log::warning("Error saving cache for $url");
  687. }
  688. return ['body' => is_string($body) ? $body : '', 'effective_url' => $c_effective_url, 'redirect_count' => $redirs,
  689. 'fail' => $fail, 'status' => $c_status, 'error' => $c_error, 'method' => $c_method];
  690. }
  691. /**
  692. * Converts an IP (v4 or v6) to a binary representation using inet_pton
  693. *
  694. * @param string $ip the IP to convert
  695. * @return string a binary representation of the specified IP
  696. */
  697. private static function ipToBits(string $ip): string {
  698. $binaryip = '';
  699. foreach (str_split(inet_pton($ip) ?: '') as $char) {
  700. $binaryip .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
  701. }
  702. return $binaryip;
  703. }
  704. /**
  705. * Check if an ip belongs to the provided range (in CIDR format)
  706. *
  707. * @param string $ip the IP that we want to verify (ex: 192.168.16.1)
  708. * @param string $range the range to check against (ex: 192.168.16.0/24)
  709. * @return bool true if the IP is in the range, otherwise false
  710. */
  711. private static function checkCIDR(string $ip, string $range): bool {
  712. $binary_ip = self::ipToBits($ip);
  713. if ($binary_ip === '') {
  714. return false;
  715. }
  716. $split = explode('/', $range);
  717. $subnet = $split[0] ?? '';
  718. if ($subnet == '') {
  719. return false;
  720. }
  721. $binary_subnet = self::ipToBits($subnet);
  722. if ($binary_subnet === '') {
  723. return false;
  724. }
  725. if (strlen($binary_ip) !== strlen($binary_subnet)) {
  726. return false; // Do not mix IPv4 and IPv6
  727. }
  728. $max_mask_bits = str_contains($ip, ':') ? 128 : 32;
  729. // A range without a subnet is a single address, like Apache’s mod_remoteip accepts
  730. $mask_bits_str = $split[1] ?? (string)$max_mask_bits;
  731. if (!ctype_digit($mask_bits_str)) {
  732. return false;
  733. }
  734. $mask_bits = (int)$mask_bits_str;
  735. if ($mask_bits < 0 || $mask_bits > $max_mask_bits) {
  736. return false; // Reject invalid mask bits lengths
  737. }
  738. if ($mask_bits === 0) {
  739. return true;
  740. }
  741. $ip_net_bits = substr($binary_ip, 0, $mask_bits);
  742. $subnet_bits = substr($binary_subnet, 0, $mask_bits);
  743. return $ip_net_bits === $subnet_bits;
  744. }
  745. /**
  746. * Check if the client (e.g. last proxy) is allowed to send unsafe headers.
  747. * This uses the `TRUSTED_PROXY` environment variable or the `trusted_sources` configuration option to get an array of the authorized ranges,
  748. * The connection IP is obtained from the `CONN_REMOTE_ADDR`
  749. * (if available, to be robust even when using Apache mod_remoteip) or `REMOTE_ADDR` environment variables.
  750. * @return bool true if the sender’s IP is in one of the ranges defined in the configuration, else false
  751. */
  752. public static function checkTrustedIP(): bool {
  753. if (!FreshRSS_Context::hasSystemConf()) {
  754. return false;
  755. }
  756. $remoteIp = Minz_Request::connectionRemoteAddress();
  757. if ($remoteIp === '') {
  758. return false;
  759. }
  760. $trusted = getenv('TRUSTED_PROXY');
  761. if ($trusted != 0 && is_string($trusted)) {
  762. $trusted = preg_split('/\s+/', $trusted, -1, PREG_SPLIT_NO_EMPTY);
  763. }
  764. if (!is_array($trusted) || empty($trusted)) {
  765. $trusted = FreshRSS_Context::systemConf()->trusted_sources;
  766. }
  767. foreach ($trusted as $cidr) {
  768. if (self::checkCIDR($remoteIp, $cidr)) {
  769. return true;
  770. }
  771. }
  772. return false;
  773. }
  774. public static function httpAuthUser(bool $onlyTrusted = true): string {
  775. $auths = array_unique(array_filter(
  776. array_intersect_key($_SERVER, ['REMOTE_USER' => '', 'REDIRECT_REMOTE_USER' => '', 'HTTP_REMOTE_USER' => '', 'HTTP_X_WEBAUTH_USER' => '']),
  777. fn($value) => is_string($value) && $value !== ''
  778. ));
  779. if (count($auths) > 1) {
  780. Minz_Log::warning('Multiple HTTP authentication headers!');
  781. return '';
  782. }
  783. if (!empty($_SERVER['REMOTE_USER']) && is_string($_SERVER['REMOTE_USER'])) {
  784. return $_SERVER['REMOTE_USER'];
  785. }
  786. if (!empty($_SERVER['REDIRECT_REMOTE_USER']) && is_string($_SERVER['REDIRECT_REMOTE_USER'])) {
  787. return $_SERVER['REDIRECT_REMOTE_USER'];
  788. }
  789. if (!$onlyTrusted || self::checkTrustedIP()) {
  790. if (!empty($_SERVER['HTTP_REMOTE_USER']) && is_string($_SERVER['HTTP_REMOTE_USER'])) {
  791. return $_SERVER['HTTP_REMOTE_USER'];
  792. }
  793. if (!empty($_SERVER['HTTP_X_WEBAUTH_USER']) && is_string($_SERVER['HTTP_X_WEBAUTH_USER'])) {
  794. return $_SERVER['HTTP_X_WEBAUTH_USER'];
  795. }
  796. }
  797. return '';
  798. }
  799. }