SimplePieCustom.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. declare(strict_types=1);
  3. final class FreshRSS_SimplePieCustom extends \SimplePie\SimplePie
  4. {
  5. /**
  6. * @param array<string,mixed> $attributes
  7. * @param array<int,mixed> $curl_options
  8. * @throws FreshRSS_Context_Exception
  9. */
  10. public function __construct(array $attributes = [], array $curl_options = []) {
  11. parent::__construct();
  12. $limits = FreshRSS_Context::systemConf()->limits;
  13. $this->get_registry()->register(\SimplePie\File::class, FreshRSS_SimplePieFetch::class);
  14. $this->set_useragent(FRESHRSS_USERAGENT);
  15. $this->set_cache_name_function('sha1'); // @phpstan-ignore method.deprecated
  16. $this->set_cache_location(CACHE_PATH); // @phpstan-ignore method.deprecated
  17. $this->set_cache_duration($limits['cache_duration'], $limits['cache_duration_min'], $limits['cache_duration_max']);
  18. $this->enable_order_by_date(false);
  19. $feed_timeout = empty($attributes['timeout']) || !is_numeric($attributes['timeout']) ? 0 : (int)$attributes['timeout'];
  20. $this->set_timeout($feed_timeout > 0 ? $feed_timeout : $limits['timeout']);
  21. $curl_options = array_replace(FreshRSS_Context::systemConf()->curl_options, $curl_options);
  22. if (isset($attributes['ssl_verify'])) {
  23. $curl_options[CURLOPT_SSL_VERIFYHOST] = empty($attributes['ssl_verify']) ? 0 : 2;
  24. $curl_options[CURLOPT_SSL_VERIFYPEER] = (bool)$attributes['ssl_verify'];
  25. if (empty($attributes['ssl_verify'])) {
  26. $curl_options[CURLOPT_SSL_CIPHER_LIST] = 'DEFAULT@SECLEVEL=1';
  27. }
  28. }
  29. $attributes['curl_params'] = FreshRSS_http_Util::sanitizeCurlParams(is_array($attributes['curl_params'] ?? null) ? $attributes['curl_params'] : []);
  30. if (!empty($attributes['curl_params']) && is_array($attributes['curl_params'])) {
  31. foreach ($attributes['curl_params'] as $co => $v) {
  32. if (is_int($co)) {
  33. $curl_options[$co] = $v;
  34. }
  35. }
  36. }
  37. if (!empty($curl_options[CURLOPT_PROXYTYPE]) && ($curl_options[CURLOPT_PROXYTYPE] < 0 || $curl_options[CURLOPT_PROXYTYPE] === 3)) {
  38. // 3 is legacy for NONE
  39. unset($curl_options[CURLOPT_PROXYTYPE]);
  40. if (isset($curl_options[CURLOPT_PROXY])) {
  41. unset($curl_options[CURLOPT_PROXY]);
  42. }
  43. }
  44. $this->set_curl_options($curl_options);
  45. $this->strip_comments(true);
  46. $this->rename_attributes(['id', 'class']);
  47. $this->allow_aria_attr(true);
  48. $this->allow_data_attr(true);
  49. $this->allowed_html_attributes([
  50. // HTML
  51. 'dir',
  52. 'draggable',
  53. 'hidden',
  54. 'lang',
  55. 'role',
  56. 'title',
  57. // MathML
  58. 'displaystyle',
  59. 'mathsize',
  60. 'scriptlevel',
  61. ]);
  62. $this->allowed_html_elements_with_attributes([
  63. // HTML
  64. 'a' => ['href', 'hreflang', 'type'],
  65. 'abbr' => [],
  66. 'acronym' => [],
  67. 'address' => [],
  68. // 'area' => [], // TODO: support <area> after rewriting ids with a format like #ugc-<insert original id here> (maybe)
  69. 'article' => [],
  70. 'aside' => [],
  71. 'audio' => ['controlslist', 'loop', 'muted', 'src'],
  72. 'b' => [],
  73. 'bdi' => [],
  74. 'bdo' => [],
  75. 'big' => [],
  76. 'blink' => [],
  77. 'blockquote' => ['cite'],
  78. 'br' => ['clear'],
  79. 'button' => ['disabled'],
  80. 'canvas' => ['width', 'height'],
  81. 'caption' => ['align'],
  82. 'center' => [],
  83. 'cite' => [],
  84. 'code' => [],
  85. 'col' => ['span', 'align', 'valign', 'width'],
  86. 'colgroup' => ['span', 'align', 'valign', 'width'],
  87. 'data' => ['value'],
  88. 'datalist' => [],
  89. 'dd' => [],
  90. 'del' => ['cite', 'datetime'],
  91. 'details' => ['open'],
  92. 'dfn' => [],
  93. 'dialog' => [],
  94. 'dir' => [],
  95. 'div' => ['align'],
  96. 'dl' => [],
  97. 'dt' => [],
  98. 'em' => [],
  99. 'fieldset' => ['disabled'],
  100. 'figcaption' => [],
  101. 'figure' => [],
  102. 'footer' => [],
  103. 'h1' => [],
  104. 'h2' => [],
  105. 'h3' => [],
  106. 'h4' => [],
  107. 'h5' => [],
  108. 'h6' => [],
  109. 'header' => [],
  110. 'hgroup' => [],
  111. 'hr' => ['align', 'noshade', 'size', 'width'],
  112. 'i' => [],
  113. 'iframe' => ['src', 'align', 'frameborder', 'longdesc', 'marginheight', 'marginwidth', 'scrolling'],
  114. 'image' => ['src', 'alt', 'width', 'height', 'align', 'border', 'hspace', 'longdesc', 'vspace'],
  115. 'img' => ['src', 'alt', 'width', 'height', 'align', 'border', 'hspace', 'longdesc', 'vspace'],
  116. 'ins' => ['cite', 'datetime'],
  117. 'kbd' => [],
  118. 'label' => [],
  119. 'legend' => [],
  120. 'li' => ['value', 'type'],
  121. 'main' => [],
  122. // 'map' => [], // TODO: support <map> after rewriting ids with a format like #ugc-<insert original id here> (maybe)
  123. 'mark' => [],
  124. 'marquee' => ['behavior', 'direction', 'height', 'hspace', 'loop', 'scrollamount', 'scrolldelay', 'truespeed', 'vspace', 'width'],
  125. 'menu' => [],
  126. 'meter' => ['value', 'min', 'max', 'low', 'high', 'optimum'],
  127. 'nav' => [],
  128. 'nobr' => [],
  129. // 'noembed' => [], // <embed> is not allowed, so we want to display the contents of <noembed>
  130. 'noframes' => [],
  131. // 'noscript' => [], // From the perspective of the feed content, JS isn't allowed so we want to display the contents of <noscript>
  132. 'ol' => ['reversed', 'start', 'type'],
  133. 'optgroup' => ['disabled', 'label'],
  134. 'option' => ['disabled', 'label', 'selected', 'value'],
  135. 'output' => [],
  136. 'p' => ['align'],
  137. 'picture' => [],
  138. // 'plaintext' => [], // Can't be closed. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/plaintext
  139. 'pre' => ['width', 'wrap'],
  140. 'progress' => ['max', 'value'],
  141. 'q' => ['cite'],
  142. 'rb' => [],
  143. 'rp' => [],
  144. 'rt' => [],
  145. 'rtc' => [],
  146. 'ruby' => [],
  147. 's' => [],
  148. 'samp' => [],
  149. 'search' => [],
  150. 'section' => [],
  151. 'select' => ['disabled', 'multiple', 'size'],
  152. 'small' => [],
  153. 'source' => ['type', 'src', 'media', 'height', 'width'],
  154. 'span' => [],
  155. 'strike' => [],
  156. 'strong' => [],
  157. 'sub' => [],
  158. 'summary' => [],
  159. 'sup' => [],
  160. 'table' => ['align', 'border', 'cellpadding', 'cellspacing', 'rules', 'summary', 'width'],
  161. 'tbody' => ['align', 'char', 'charoff', 'valign'],
  162. 'td' => ['colspan', 'headers', 'rowspan', 'abbr', 'align', 'height', 'scope', 'valign', 'width'],
  163. 'textarea' => ['cols', 'disabled', 'maxlength', 'minlength', 'placeholder', 'readonly', 'rows', 'wrap'],
  164. 'tfoot' => ['align', 'valign'],
  165. 'th' => ['abbr', 'colspan', 'rowspan', 'scope', 'align', 'height', 'valign', 'width'],
  166. 'thead' => ['align', 'valign'],
  167. 'time' => ['datetime'],
  168. 'tr' => ['align', 'valign'],
  169. 'track' => ['default', 'kind', 'srclang', 'label', 'src'],
  170. 'tt' => [],
  171. 'u' => [],
  172. 'ul' => ['type'],
  173. 'var' => [],
  174. 'video' => ['src', 'poster', 'controlslist', 'height', 'loop', 'muted', 'playsinline', 'width'],
  175. 'wbr' => [],
  176. 'xmp' => [],
  177. // MathML
  178. 'maction' => ['actiontype', 'selection'],
  179. 'math' => ['display'],
  180. 'menclose' => ['notation'],
  181. 'merror' => [],
  182. 'mfenced' => ['close', 'open', 'separators'],
  183. 'mfrac' => ['denomalign', 'linethickness', 'numalign'],
  184. 'mi' => ['mathvariant'],
  185. 'mmultiscripts' => ['subscriptshift', 'superscriptshift'],
  186. 'mn' => [],
  187. 'mo' => ['accent', 'fence', 'form', 'largeop', 'lspace', 'maxsize', 'minsize', 'movablelimits', 'rspace', 'separator', 'stretchy', 'symmetric'],
  188. 'mover' => ['accent'],
  189. 'mpadded' => ['depth', 'height', 'lspace', 'voffset', 'width'],
  190. 'mphantom' => [],
  191. 'mprescripts' => [],
  192. 'mroot' => [],
  193. 'mrow' => [],
  194. 'ms' => [],
  195. 'mspace' => ['depth', 'height', 'width'],
  196. 'msqrt' => [],
  197. 'msub' => [],
  198. 'msubsup' => ['subscriptshift', 'superscriptshift'],
  199. 'msup' => ['superscriptshift'],
  200. 'mtable' => ['align', 'columnalign', 'columnlines', 'columnspacing', 'frame', 'framespacing', 'rowalign', 'rowlines', 'rowspacing', 'width'],
  201. 'mtd' => ['columnspan', 'rowspan', 'columnalign', 'rowalign'],
  202. 'mtext' => [],
  203. 'mtr' => ['columnalign', 'rowalign'],
  204. 'munder' => ['accentunder'],
  205. 'munderover' => ['accent', 'accentunder'],
  206. // TODO: Support SVG after sanitizing and URL rewriting of xlink:href
  207. ]);
  208. $this->strip_attributes([
  209. 'data-auto-leave-validation',
  210. 'data-leave-validation',
  211. 'data-no-leave-validation',
  212. 'data-original',
  213. ]);
  214. $this->add_attributes([
  215. 'audio' => ['controls' => 'controls', 'preload' => 'none'],
  216. 'iframe' => [
  217. 'allow' => 'accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share',
  218. 'sandbox' => 'allow-scripts allow-same-origin',
  219. ],
  220. 'video' => ['controls' => 'controls', 'preload' => 'none'],
  221. ]);
  222. $this->set_url_replacements([
  223. 'a' => 'href',
  224. 'area' => 'href',
  225. 'audio' => 'src',
  226. 'blockquote' => 'cite',
  227. 'del' => 'cite',
  228. 'form' => 'action',
  229. 'iframe' => 'src',
  230. 'img' => [
  231. 'longdesc',
  232. 'src',
  233. ],
  234. 'image' => [
  235. 'longdesc',
  236. 'src',
  237. ],
  238. 'input' => 'src',
  239. 'ins' => 'cite',
  240. 'q' => 'cite',
  241. 'source' => 'src',
  242. 'track' => 'src',
  243. 'video' => [
  244. 'poster',
  245. 'src',
  246. ],
  247. ]);
  248. $https_domains = [];
  249. $force = @file(FRESHRSS_PATH . '/force-https.default.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  250. if (is_array($force)) {
  251. $https_domains = array_merge($https_domains, $force);
  252. }
  253. $force = @file(DATA_PATH . '/force-https.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  254. if (is_array($force)) {
  255. $https_domains = array_merge($https_domains, $force);
  256. }
  257. // Remove whitespace and comments starting with # / ;
  258. $https_domains = preg_replace('%\\s+|[\/#;].*$%', '', $https_domains) ?? $https_domains;
  259. $https_domains = array_filter($https_domains, fn(string $v) => $v !== '');
  260. $this->set_https_domains($https_domains);
  261. }
  262. public static function sanitizeHTML(string $data, string $base = '', ?int $maxLength = null): string {
  263. if ($data === '' || ($maxLength !== null && $maxLength <= 0)) {
  264. return '';
  265. }
  266. if ($maxLength !== null) {
  267. $data = mb_strcut($data, 0, $maxLength, 'UTF-8');
  268. }
  269. /** @var FreshRSS_SimplePieCustom|null $simplePie */
  270. static $simplePie = null;
  271. if ($simplePie === null) {
  272. $simplePie = new static();
  273. $simplePie->enable_cache(false);
  274. $simplePie->init();
  275. }
  276. $sanitized = $simplePie->sanitize->sanitize($data, \SimplePie\SimplePie::CONSTRUCT_HTML, $base);
  277. if (!is_string($sanitized)) {
  278. return '';
  279. }
  280. $result = html_only_entity_decode($sanitized);
  281. if ($maxLength !== null && strlen($result) > $maxLength) {
  282. //Sanitizing has made the result too long so try again shorter
  283. $data = mb_strcut($result, 0, (2 * $maxLength) - strlen($result) - 2, 'UTF-8');
  284. return self::sanitizeHTML($data, $base, $maxLength);
  285. }
  286. return $result;
  287. }
  288. }