mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
* Use keyed hash (HMAC-SHA256) for favicon cache keys The favicon cache key was a CRC-32 checksum of the salt-prefixed feed website URL (or feed icon URL). CRC-32 is linear, so collisions for equal-length inputs are independent of the salt prefix: a user could compute offline a website URL for their own feed colliding into another feed's cache slot, overwrite the shared data/favicons/<hash>.txt, and have the unauthenticated public favicon endpoint fetch and serve their image to every user of the instance. Replace the checksum with a keyed hash (HMAC-SHA256, instance salt as key, truncated to 16 hex chars) so the cache key cannot be forged without the secret salt. * Preserve legacy custom favicons when upgrading cache keys * Base64 hash (shorter) and native copy --------- Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr> Co-authored-by: Inverle <inverle@proton.me>
82 lines
2.5 KiB
PHP
82 lines
2.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require dirname(__DIR__) . '/constants.php';
|
|
require LIB_PATH . '/lib_rss.php'; //Includes class autoloader
|
|
require LIB_PATH . '/favicons.php';
|
|
require LIB_PATH . '/http-conditional.php';
|
|
|
|
FreshRSS_Context::initSystem();
|
|
if (!FreshRSS_Context::hasSystemConf()) {
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
die('Invalid system init!');
|
|
}
|
|
$frameAncestors = FreshRSS_Context::systemConf()->attributeString('csp.frame-ancestors') ?? "'none'";
|
|
header("Content-Security-Policy: default-src 'none'; frame-ancestors $frameAncestors; sandbox");
|
|
header('X-Content-Type-Options: nosniff');
|
|
|
|
$no_cache = file_exists(DATA_PATH . '/no-cache.txt');
|
|
|
|
function show_default_favicon(int $cacheSeconds = 3600): void {
|
|
global $no_cache;
|
|
$default_mtime = @filemtime(DEFAULT_FAVICON) ?: 0;
|
|
if ($no_cache || !httpConditional($default_mtime, $cacheSeconds, 2)) {
|
|
header('Content-Type: image/x-icon');
|
|
header('Content-Disposition: attachment; filename="default_favicon.ico"');
|
|
readfile(DEFAULT_FAVICON);
|
|
}
|
|
}
|
|
|
|
$id = $_GET['h'] ?? '0';
|
|
if (!is_string($id) || preg_match('/\\A[A-Za-z0-9_-]{1,11}\\z/', $id) !== 1) {
|
|
$id = '0';
|
|
}
|
|
|
|
$txt = FAVICONS_DIR . $id . '.txt';
|
|
$ico = FAVICONS_DIR . $id . '.ico';
|
|
|
|
$ico_mtime = @filemtime($ico) ?: 0;
|
|
$txt_mtime = @filemtime($txt) ?: 0;
|
|
|
|
$is_custom_favicon = $ico_mtime != false && $txt_mtime == false;
|
|
|
|
if (($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (rand(15, 20) * 86400))) && !$is_custom_favicon) {
|
|
if ($txt_mtime == false) {
|
|
show_default_favicon(1800);
|
|
exit();
|
|
}
|
|
|
|
// no ico file or we should download a new one.
|
|
$url = file_get_contents($txt);
|
|
if ($url === false) {
|
|
show_default_favicon(1800);
|
|
exit();
|
|
}
|
|
$url = FreshRSS_http_Util::checkUrl($url) ?: '';
|
|
if ($url === '') {
|
|
show_default_favicon(1800);
|
|
exit();
|
|
}
|
|
|
|
// Try downloading the URL as a direct image first (e.g. from a feed's <image><url>),
|
|
// then fall back to HTML favicon search if it is not a valid image.
|
|
if (!download_favicon_from_image_url($url, $ico) && !download_favicon($url, $ico)) {
|
|
// Download failed
|
|
if ($ico_mtime == false) {
|
|
show_default_favicon(86400);
|
|
exit();
|
|
}
|
|
|
|
touch($ico);
|
|
}
|
|
}
|
|
|
|
if ($no_cache || !httpConditional($ico_mtime, rand(14, 21) * 86400, 2)) {
|
|
$ico_content_type = contentType($ico);
|
|
header('Content-Type: ' . $ico_content_type);
|
|
header('Content-Disposition: attachment; filename="' . $id . '.ico"');
|
|
if (!$no_cache && isset($_GET['t'])) {
|
|
header('Cache-Control: immutable');
|
|
}
|
|
readfile($ico);
|
|
}
|