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
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ jobs:
- name: PHPStan
run: composer run-script phpstan

- name: PHPStan compatibility PHP < 8.4
run: composer run-script phpstan-php83

# NPM tests

- name: Uses Node.js
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@
"phpcs": "phpcs . -s",
"phpcbf": "phpcbf . -p -s",
"phpstan": "phpstan analyse .",
"phpstan-php83": "phpstan analyse -c phpstan-php83.neon .",
"phpstan-third-party": "phpstan analyse -c phpstan-third-party.neon .",
"test-image-proxy": "php tests/ImageProxyTest.php",
"test": [
"@php-lint",
"@phtml-lint",
"@test-image-proxy",
"@phpcs",
"@phpstan"
"@phpstan",
"@phpstan-php83"
],
"fix": [
"@phpcbf"
Expand Down
36 changes: 36 additions & 0 deletions phpstan-php83.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Compatibility PHP < 8.4, due to new class `Dom\` introduced in PHP 8.4+
parameters:
phpVersion:
min: 80100 # PHP 8.1
max: 80399 # PHP 8.3
level: 9 # https://phpstan.org/user-guide/rule-levels
fileExtensions:
- php
- phtml
paths:
- ../FreshRSS
- .
excludePaths:
analyse:
- ../FreshRSS
analyseAndScan:
- .git/*?
- node_modules/*?
- symbolic/*?
- third-party/*?
- tmp/*?
- vendor/
reportUnmatchedIgnoredErrors: false
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- TYPE_GIT
ignoreErrors:
# `Dom\` used by PHP 8.4+
-
identifier: class.notFound
messages:
- '#^Class Dom\\\S+ not found\.$#'
- '#^Parameter \S+ of .+ has invalid type Dom\\\S+\.$#'
- '#^.+ has invalid return type Dom\\\S+\.$#'
- '#^Property \S+ has unknown class Dom\\\S+ as its type\.$#'
- '#^Call to method \S+ on an unknown class Dom\\\S+\.$#'
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
phpVersion:
min: 80100 # PHP 8.1
min: 80400 # PHP 8.4 (PHP 8.1+ covered by phpstan-php83.neon)
max: 80599 # PHP 8.5
level: 10 # https://phpstan.org/user-guide/rule-levels
fileExtensions:
Expand Down
18 changes: 16 additions & 2 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->testHtml5Parsing();
$this->testUriSchemes();
$this->testExcludedDomains();
} catch (FreshRSS_Context_Exception $e) {
Expand Down Expand Up @@ -143,8 +144,8 @@ private function testImageAttributes(): void {

$this->assertSame(3, count($images), 'All image elements must remain present');
$this->assertSame(
self::PROXY_URL . 'http://images.example/one%20caf%C3%A9.jpg',
$images[0]->getAttribute('src'),
self::PROXY_URL . 'http://images.example/one café.jpg',
rawurldecode($images[0]->getAttribute('src')), // The legacy parser of PHP < 8.4 percent-encodes URLs when serialising
'Unicode image URLs must be proxied',
);
$this->assertSame(
Expand Down Expand Up @@ -188,6 +189,19 @@ private function testVideoPosters(): void {
$this->assertSame(false, $videos[1]->hasAttribute('poster'), 'Videos without poster must remain without poster');
}

/** @throws FreshRSS_Context_Exception */
private function testHtml5Parsing(): void {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really a point to testing these things? It'll already be included as part of https://github.com/lexbor/lexbor/tree/e651d6bce83a6601dff37261374f0545e910b1c1/test (the underlying library in PHP)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, the picture and loose text cases are really the parser's job. I dropped them and kept only the body test, since stripping the body is done in our code.

if (!class_exists('Dom\HTMLDocument')) {
return; // The HTML5 parser is only available in PHP 8.4+
}

$this->assertSame(
'<p>Body text</p>',
ImageProxyExtension::swapUris('<!doctype html><html><body class="page"><p>Body text</p></body></html>'),
'The <body> element must not be returned, even with attributes',
);
}

/** @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 Use the standard HTML5 parser of PHP 8.4+ when available
* 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
54 changes: 35 additions & 19 deletions xExtension-ImageProxy/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,16 @@ public static function getSrcSetUris(array $matches): string {
}

/**
* Proxies the images of a parsed HTML document: `src` and `srcset` of `<img>` and `poster` of `<video>`.
* @throws FreshRSS_Context_Exception
*/
public static function swapUris(string $content): string {
if ($content === '') {
return $content;
}

$doc = new DOMDocument();
libxml_use_internal_errors(true); // prevent tag soup errors from showing
$content = mb_encode_numericentity($content, [0x80, 0x10FFFF, 0, ~0], 'UTF-8');
$doc->loadHTML($content);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
if (!($img instanceof DOMElement)) {
private static function swapDocumentUris(DOMDocument|Dom\HTMLDocument $doc): void {
foreach ($doc->getElementsByTagName('img') as $img) {
if (!($img instanceof DOMElement || $img instanceof Dom\Element)) {
continue;
}
if ($img->hasAttribute('src')) {
$src = $img->getAttribute('src');
$src = $img->getAttribute('src');
if ($src != '') { // `''` with DOMElement, `null` with Dom\Element
$newSrc = self::getProxyImageUri($src);
/*
Due to the URL change, FreshRSS is not aware of already rendered enclosures.
Expand All @@ -176,8 +168,8 @@ public static function swapUris(string $content): string {
$img->setAttribute('data-xextension-imageproxy-original-src', $src);
$img->setAttribute('src', $newSrc);
}
if ($img->hasAttribute('srcset')) {
$srcSet = $img->getAttribute('srcset');
$srcSet = $img->getAttribute('srcset');
if ($srcSet != '') {
$newSrcSet = preg_replace_callback('/(?:([^\s,]+)(\s*(?:\s+\d+[wx])(?:,\s*)?))/', fn (array $matches) => self::getSrcSetUris($matches), $srcSet);
if ($newSrcSet != null) {
$img->setAttribute('data-xextension-imageproxy-original-srcset', $srcSet);
Expand All @@ -187,15 +179,39 @@ public static function swapUris(string $content): string {
}

// Video posters are images too; keep the original URL for the same reason as above
$videos = $doc->getElementsByTagName('video');
foreach ($videos as $video) {
if (!($video instanceof DOMElement) || !$video->hasAttribute('poster')) {
foreach ($doc->getElementsByTagName('video') as $video) {
if (!($video instanceof DOMElement || $video instanceof Dom\Element)) {
continue;
}
$poster = $video->getAttribute('poster');
if ($poster == '') {
continue;
}
$video->setAttribute('data-xextension-imageproxy-original-poster', $poster);
$video->setAttribute('poster', self::getProxyImageUri($poster));
}
}

/**
* @throws FreshRSS_Context_Exception
*/
public static function swapUris(string $content): string {
if ($content === '') {
return $content;
}

if (class_exists('Dom\HTMLDocument')) { // PHP 8.4+
$doc = Dom\HTMLDocument::createFromString($content, LIBXML_NOERROR, overrideEncoding: 'UTF-8');
self::swapDocumentUris($doc);
return $doc->body->innerHTML ?? '';
}

// PHP < 8.4
$doc = new DOMDocument();
libxml_use_internal_errors(true); // prevent tag soup errors from showing
$content = mb_encode_numericentity($content, [0x80, 0x10FFFF, 0, ~0], 'UTF-8');
$doc->loadHTML($content);
self::swapDocumentUris($doc);

$body = $doc->getElementsByTagName('body')->item(0);

Expand Down
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.2.0",
"entrypoint": "ImageProxy",
"type": "user"
}