mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
Currently, SimplePie uses legacy DOMDocument parsing, which outputs potentially problematic HTML, not correctly sanitised.
This breaks the assumption that one can use something like `<div><!-- some HTML sanitized from SimplePie --></div>`.
```
--- Expected
+++ Actual
@@ @@
'<ul>
- <li><div></div></li>
+ <li><div>
<li>Item</li>
-</ul>'
+</div></li></ul>'
```
When shown in a compliant Web browser, several tags will be wrongly closed, breaking the layout of the client.
This PR patches SimplePie to use the modern parser and code syntax, while maintaining a best-effort polyfill for legacy PHP versions < 8.4.
Depends on:
SimplePie: Upgrade to PHPStan 2
* https://github.com/FreshRSS/simplepie/pull/97
* https://github.com/simplepie/simplepie/pull/939
* https://github.com/simplepie/simplepie/pull/1019
SimplePie: Use PHP 8.4+ \Dom classes and functions
* https://github.com/FreshRSS/simplepie/pull/98
* https://github.com/simplepie/simplepie/issues/1021
SimplePie: Add unit test for HTML5 sanitizing compliance
* https://github.com/simplepie/simplepie/pull/1022
SimplePie: Remove obsolete xml_is_sane and XMLReader fallback
* https://github.com/simplepie/simplepie/pull/1023
CssXPath: Add support for case insensitivity
* https://github.com/phpgt/CssXPath/pull/256
fix https://github.com/FreshRSS/FreshRSS/issues/5201
fix https://github.com/FreshRSS/FreshRSS/issues/6830
fix https://github.com/FreshRSS/FreshRSS/issues/6831
~5% faster for HTML sanitising.
Remove SimpleXML. Remove XMLReader.
134 lines
3.9 KiB
PHP
134 lines
3.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
const FAVICONS_DIR = DATA_PATH . '/favicons/';
|
|
const DEFAULT_FAVICON = PUBLIC_PATH . '/themes/icons/default_favicon.ico';
|
|
|
|
function isImgMime(string $content): bool {
|
|
//Based on https://github.com/ArthurHoaro/favicon/blob/3a4f93da9bb24915b21771eb7873a21bde26f5d1/src/Favicon/Favicon.php#L311-L319
|
|
if ($content == '') {
|
|
return false;
|
|
}
|
|
if (!extension_loaded('fileinfo')) {
|
|
return true;
|
|
}
|
|
$fInfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
if ($fInfo === false) {
|
|
return true;
|
|
}
|
|
$content = finfo_buffer($fInfo, $content);
|
|
$isImage = str_contains($content ?: '', 'image');
|
|
return $isImage;
|
|
}
|
|
|
|
function faviconCachePath(string $url): string {
|
|
return CACHE_PATH . '/' . sha1($url) . '.ico';
|
|
}
|
|
|
|
function searchFavicon(string $url): string {
|
|
$url = trim($url);
|
|
if ($url === '') {
|
|
return '';
|
|
}
|
|
['body' => $html, 'effective_url' => $effective_url, 'fail' => $fail] =
|
|
FreshRSS_http_Util::httpGet($url, cachePath: CACHE_PATH . '/' . sha1($url) . '.html', type: 'html');
|
|
if ($fail || $html === '') {
|
|
return '';
|
|
}
|
|
$dom = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
|
|
$links = $dom->querySelectorAll('link[href][rel~="icon" i]');
|
|
|
|
// Use the base element for relative paths, if there is one
|
|
$baseElement = $dom->querySelector('base[href]');
|
|
$baseUrl = ($baseElement !== null) ? ($baseElement->getAttribute('href') ?? '') : $effective_url;
|
|
|
|
foreach ($links as $link) {
|
|
if (!$link instanceof Dom\Element) {
|
|
continue;
|
|
}
|
|
$href = trim($link->getAttribute('href') ?? '');
|
|
$urlParts = parse_url($effective_url);
|
|
|
|
// Handle protocol-relative URLs by adding the current URL's scheme
|
|
if (substr($href, 0, 2) === '//') {
|
|
$href = ($urlParts['scheme'] ?? 'https') . ':' . $href;
|
|
}
|
|
|
|
$href = \SimplePie\IRI::absolutize($baseUrl, $href);
|
|
if ($href == false) {
|
|
return '';
|
|
}
|
|
|
|
$iri = $href->get_iri();
|
|
if ($iri == false) {
|
|
continue;
|
|
}
|
|
$iri = FreshRSS_http_Util::checkUrl($iri, fixScheme: false);
|
|
if (!is_string($iri) || $iri === '') {
|
|
continue;
|
|
}
|
|
$favicon = FreshRSS_http_Util::httpGet($iri, faviconCachePath($iri), 'ico', curl_options: [
|
|
CURLOPT_REFERER => $effective_url,
|
|
])['body'];
|
|
if (isImgMime($favicon)) {
|
|
return $favicon;
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Downloads a favicon directly from a known image URL (e.g. from a feed's <image><url> or icon field).
|
|
* Returns false without any fallback if the URL does not point to a valid image.
|
|
*/
|
|
function download_favicon_from_image_url(string $imageUrl, string $dest): bool {
|
|
$imageUrl = FreshRSS_http_Util::checkUrl($imageUrl);
|
|
if (!is_string($imageUrl) || $imageUrl === '') {
|
|
return false;
|
|
}
|
|
$favicon = FreshRSS_http_Util::httpGet($imageUrl, faviconCachePath($imageUrl), 'ico')['body'];
|
|
if (!isImgMime($favicon)) {
|
|
return false;
|
|
}
|
|
return file_put_contents($dest, $favicon) > 0;
|
|
}
|
|
|
|
function download_favicon(string $url, string $dest): bool {
|
|
$url = FreshRSS_http_Util::checkUrl($url);
|
|
if (!is_string($url) || $url === '') {
|
|
return @copy(DEFAULT_FAVICON, $dest);
|
|
}
|
|
$favicon = searchFavicon($url);
|
|
if ($favicon == '') {
|
|
$rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url) ?? $url;
|
|
if ($rootUrl != $url) {
|
|
$url = $rootUrl;
|
|
$favicon = searchFavicon($url);
|
|
}
|
|
if ($favicon == '') {
|
|
$link = FreshRSS_http_Util::checkUrl($rootUrl . 'favicon.ico', fixScheme: false) ?: '';
|
|
$favicon = $link === '' ? '' : FreshRSS_http_Util::httpGet($link, faviconCachePath($link), 'ico', curl_options: [
|
|
CURLOPT_REFERER => $url,
|
|
])['body'];
|
|
if (!isImgMime($favicon)) {
|
|
$favicon = '';
|
|
}
|
|
}
|
|
}
|
|
return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
|
|
@copy(DEFAULT_FAVICON, $dest);
|
|
}
|
|
|
|
function contentType(string $ico): string {
|
|
$ico_content_type = 'image/x-icon';
|
|
if (function_exists('mime_content_type')) {
|
|
$ico_content_type = mime_content_type($ico) ?: $ico_content_type;
|
|
}
|
|
switch ($ico_content_type) {
|
|
case 'image/svg':
|
|
$ico_content_type = 'image/svg+xml';
|
|
break;
|
|
}
|
|
return $ico_content_type;
|
|
}
|