Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/ImageProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function run(): void {
$this->testHtmlDocumentsAndTagSoup();
$this->testImageAttributes();
$this->testVideoPosters();
$this->testHtmlEncodedUris();
$this->testUriSchemes();
$this->testExcludedDomains();
} catch (FreshRSS_Context_Exception $e) {
Expand Down Expand Up @@ -188,6 +189,29 @@ private function testVideoPosters(): void {
$this->assertSame(false, $videos[1]->hasAttribute('poster'), 'Videos without poster must remain without poster');
}

/** @throws FreshRSS_Context_Exception */
private function testHtmlEncodedUris(): void {
$url = 'http://images.example/thumbnail.jpg?w=1&h=2';
$this->assertSame(
self::PROXY_URL . $url,
ImageProxyExtension::getProxyImageHtmlUri($url),
'HTML-encoded URLs must be proxied and remain HTML-encoded',
);
$this->assertContains(
'src="' . ImageProxyExtension::getProxyImageHtmlUri($url) . '"',
ImageProxyExtension::swapUris('<img src="' . $url . '">'),
'Proxied HTML-encoded URLs must be found as such in the proxied content',
);

$this->configure(encodeUrl: true);
$this->assertSame(
self::PROXY_URL . rawurlencode('http://images.example/thumbnail.jpg?w=1&h=2'),
ImageProxyExtension::getProxyImageHtmlUri($url),
'HTML-encoded URLs must be decoded before being URL-encoded',
);
$this->configure();
}

/** @throws FreshRSS_Context_Exception */
private function testUriSchemes(): void {
$this->assertSame(
Expand Down
1 change: 1 addition & 0 deletions xExtension-ImageProxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ To use it, upload this entire directory to the FreshRSS `./extensions` directory

## Changelog

* 1.1.1 Proxy the thumbnails shown in the list of articles
* 1.1.0 Add a list of domains that are not proxied
* 1.0.4 Respect the feed option to hide enclosures
* 1.0.3 Proxy the poster images of videos
Expand Down
47 changes: 44 additions & 3 deletions xExtension-ImageProxy/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ public static function getProxyImageUri(string $url): string {
return FreshRSS_Context::userConf()->attributeString('image_proxy_url') . $url;
}

/**
* Same as getProxyImageUri() for the HTML-encoded URLs that FreshRSS stores, e.g. the ones of thumbnails.
* @throws FreshRSS_Context_Exception
*/
public static function getProxyImageHtmlUri(string $url): string {
return htmlspecialchars(self::getProxyImageUri(htmlspecialchars_decode($url, ENT_QUOTES)), ENT_COMPAT, 'UTF-8');
}

/**
* @param array<string> $matches
* @throws FreshRSS_Context_Exception
Expand Down Expand Up @@ -216,10 +224,43 @@ public static function setImageProxyHook(FreshRSS_Entry $entry): FreshRSS_Entry
// The enclosures become part of the content, so only include them if the feed displays them
// We most likely already have the feed object in cache, otherwise make a request
$feed = FreshRSS_Category::findFeed(FreshRSS_Context::categories(), $entry->feedId()) ?? $entry->feed();
$entry->_content(
self::swapUris($entry->content($feed?->attributeBoolean('display_enclosures') ?? true))
);
$withEnclosures = $feed?->attributeBoolean('display_enclosures') ?? true;
$entry->_content(self::swapUris($entry->content($withEnclosures)));
self::swapThumbnailUris($entry, $withEnclosures);

return $entry;
}

/**
* Proxy the thumbnails of the entry and of its enclosures, which are shown in the list of articles.
* @throws FreshRSS_Context_Exception
*/
private static function swapThumbnailUris(FreshRSS_Entry $entry, bool $withEnclosures): void {
$thumbnail = $entry->attributeArray('thumbnail');
if (is_string($thumbnail['url'] ?? null) && $thumbnail['url'] !== '') {
$url = self::getProxyImageHtmlUri($thumbnail['url']);
// When displaying the enclosures, FreshRSS appends the thumbnail to the content unless its URL is found there.
// The content set above already includes the thumbnail, so to avoid showing it twice,
// only proxy it if the proxied URL is found in the content as well (e.g. not if the content only links to it).
if (!$withEnclosures || preg_match('/([\'"])' . preg_quote($url, '/') . '\1/', $entry->content(false)) === 1) {
$thumbnail['url'] = $url;
$entry->_attribute('thumbnail', $thumbnail);
}
}

// The thumbnails of the enclosures are not compared to the content, so they can always be proxied
$enclosures = $entry->attributeArray('enclosures') ?? [];
foreach ($enclosures as $key => $enclosure) {
if (is_array($enclosure) && is_array($enclosure['thumbnails'] ?? null)) {
$enclosure['thumbnails'] = array_map(
fn (mixed $src) => is_string($src) ? self::getProxyImageHtmlUri($src) : $src,
$enclosure['thumbnails']
);
$enclosures[$key] = $enclosure;
}
}
if ($enclosures !== []) {
$entry->_attribute('enclosures', $enclosures);
}
}
}
2 changes: 1 addition & 1 deletion xExtension-ImageProxy/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Image Proxy",
"author": "Frans de Jonge",
"description": "No insecure content warnings or disappearing images.",
"version": "1.1.0",
"version": "1.1.1",
"entrypoint": "ImageProxy",
"type": "user"
}