diff --git a/.typos.toml b/.typos.toml index ebf23717..46ce411e 100644 --- a/.typos.toml +++ b/.typos.toml @@ -17,6 +17,7 @@ extend-exclude = [ "*/i18n/az", "*/i18n/de", "*/i18n/fr", + "*/i18n/nl", "*/i18n/pl", "bin/", "node_modules/", diff --git a/tests/ImageProxyTest.php b/tests/ImageProxyTest.php index 8049d7db..ad07f6fa 100644 --- a/tests/ImageProxyTest.php +++ b/tests/ImageProxyTest.php @@ -68,6 +68,7 @@ public function run(): void { $this->testImageAttributes(); $this->testVideoPosters(); $this->testUriSchemes(); + $this->testExcludedDomains(); } catch (FreshRSS_Context_Exception $e) { throw new RuntimeException('ImageProxy configuration failed', 0, $e); } finally { @@ -77,7 +78,7 @@ public function run(): void { fwrite(STDOUT, "ImageProxy regression tests passed ({$this->assertions} assertions).\n"); } - private function configure(bool $includeScheme = true, bool $encodeUrl = false): void { + private function configure(bool $includeScheme = true, bool $encodeUrl = false, string $excludedDomains = ''): void { ImageProxyTestContext::configure([ 'image_proxy_url' => self::PROXY_URL, 'image_proxy_scheme_http' => true, @@ -85,6 +86,7 @@ private function configure(bool $includeScheme = true, bool $encodeUrl = false): 'image_proxy_scheme_default' => 'auto', 'image_proxy_scheme_include' => $includeScheme, 'image_proxy_url_encode' => $encodeUrl, + 'image_proxy_excluded_domains' => $excludedDomains, ]); } @@ -209,6 +211,46 @@ private function testUriSchemes(): void { ); } + /** @throws FreshRSS_Context_Exception */ + private function testExcludedDomains(): void { + $this->configure(excludedDomains: " Intranet.example\r\n*.cdn.example 192.168.1.10\n.dot.example [::1] star.example.*\n\n"); + $notProxied = [ + 'http://intranet.example/a.png' => 'Excluded domains must not be proxied', + 'https://INTRANET.example/a.png' => 'Excluded domains must be matched case-insensitively', + 'http://images.intranet.example/a.png' => 'Subdomains of excluded domains must not be proxied', + 'http://cdn.example/a.png' => 'Wildcard entries must match the domain itself', + 'http://img.cdn.example/a.png' => 'Wildcard entries must match subdomains', + 'http://img.dot.example/a.png' => 'Entries with a leading dot must match subdomains', + 'http://192.168.1.10:8080/a.png' => 'Excluded IP addresses must not be proxied, whatever the port', + 'http://[::1]/a.png' => 'Excluded IPv6 addresses must not be proxied', + '//intranet.example/a.png' => 'Protocol-relative URLs of excluded domains must not be proxied', + ]; + foreach ($notProxied as $url => $message) { + $this->assertSame($url, ImageProxyExtension::getProxyImageUri($url), $message); + } + + $proxied = [ + 'http://example/a.png' => 'Parent domains of excluded domains must be proxied', + 'http://myintranet.example/a.png' => 'Domains merely ending with an excluded domain must be proxied', + 'http://intranet.example.org/a.png' => 'Domains merely starting with an excluded domain must be proxied', + 'http://star.example/a.png' => 'A trailing wildcard is not supported and must not match', + 'http://192.168.1.100/a.png' => 'Other IP addresses must be proxied', + 'http://images.example/intranet.example.png' => 'Only the host must be compared to excluded domains', + ]; + foreach ($proxied as $url => $message) { + $this->assertSame(self::PROXY_URL . $url, ImageProxyExtension::getProxyImageUri($url), $message); + } + + $images = $this->images(ImageProxyExtension::swapUris( + '')); + $this->assertSame('http://intranet.example/a.png', $images[0]->getAttribute('src'), 'Images of excluded domains must keep their src'); + $this->assertSame( + 'http://intranet.example/b.png 1x, ' . self::PROXY_URL . 'http://images.example/c.png 2x', + $images[0]->getAttribute('srcset'), + 'Only srcset candidates of non-excluded domains must be proxied', + ); + } + private function textContent(string $html): string { $doc = $this->parseFragment($html); $body = $doc->getElementsByTagName('body')->item(0); diff --git a/xExtension-ImageProxy/README.md b/xExtension-ImageProxy/README.md index 81ae273e..2a7bbce3 100644 --- a/xExtension-ImageProxy/README.md +++ b/xExtension-ImageProxy/README.md @@ -6,6 +6,7 @@ To use it, upload this entire directory to the FreshRSS `./extensions` directory ## Changelog +* 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 * 1.0.1 Replace deprecated HTML entity conversion on PHP 8.2+ @@ -27,6 +28,10 @@ To use it, upload this entire directory to the FreshRSS `./extensions` directory * `url_encode` (default: `1`): whether to URL-encode (RFC 3986) the proxied URL +* `excluded_domains` (default: empty): domains whose images are never proxied, one per line, e.g. `intranet.example` and `192.168.1.10`; + subdomains are matched too, so `example.org` also covers `images.example.org`. + This is useful for images served from internal hosts that the proxy cannot reach, or from domains that the proxy refuses to serve + ## Proxy Settings By default this extension will use the [wsrv.nl](https://wsrv.nl) image caching and resizing proxy, but instead you can supply your own proxy URL in the settings. An example URL would look like ``https://images.example.com/?url=``. diff --git a/xExtension-ImageProxy/configure.phtml b/xExtension-ImageProxy/configure.phtml index 1042e30e..9828ef00 100644 --- a/xExtension-ImageProxy/configure.phtml +++ b/xExtension-ImageProxy/configure.phtml @@ -52,6 +52,15 @@ attributeBool('image_proxy_url_encode') ? 'checked' : '' ?>> +
+ +
+ +

+
+
diff --git a/xExtension-ImageProxy/extension.php b/xExtension-ImageProxy/extension.php index 4b35a07f..d7d97fa6 100644 --- a/xExtension-ImageProxy/extension.php +++ b/xExtension-ImageProxy/extension.php @@ -10,6 +10,7 @@ final class ImageProxyExtension extends Minz_Extension { private const SCHEME_DEFAULT = 'auto'; private const SCHEME_INCLUDE = false; private const URL_ENCODE = true; + private const EXCLUDED_DOMAINS = ''; /** * @throws FreshRSS_Context_Exception @@ -46,6 +47,10 @@ public function init(): void { FreshRSS_Context::userConf()->_attribute('image_proxy_url_encode', self::URL_ENCODE); $save = true; } + if (FreshRSS_Context::userConf()->attributeString('image_proxy_excluded_domains') === null) { + FreshRSS_Context::userConf()->_attribute('image_proxy_excluded_domains', self::EXCLUDED_DOMAINS); + $save = true; + } if ($save) { FreshRSS_Context::userConf()->save(); } @@ -66,15 +71,39 @@ public function handleConfigureAction(): void { Minz_Request::paramString('image_proxy_scheme_default', plaintext: true) ?: self::SCHEME_DEFAULT); FreshRSS_Context::userConf()->_attribute('image_proxy_scheme_include', Minz_Request::paramBoolean('image_proxy_scheme_include')); FreshRSS_Context::userConf()->_attribute('image_proxy_url_encode', Minz_Request::paramBoolean('image_proxy_url_encode')); + FreshRSS_Context::userConf()->_attribute('image_proxy_excluded_domains', Minz_Request::paramString('image_proxy_excluded_domains', plaintext: true)); FreshRSS_Context::userConf()->save(); } } + /** + * Whether the host is one of the domains excluded from proxying, or a subdomain of one of them. + * @throws FreshRSS_Context_Exception + */ + private static function isExcludedHost(string $host): bool { + $host = trim(strtolower($host), '[].'); + if ($host === '') { + return false; + } + $excludedDomains = strtolower(FreshRSS_Context::userConf()->attributeString('image_proxy_excluded_domains') ?? ''); + $domains = preg_split('/\s+/', $excludedDomains, flags: PREG_SPLIT_NO_EMPTY) ?: []; + foreach ($domains as $domain) { + $domain = ltrim(trim($domain, '[]'), '*.'); // Accept `*.example.org`, `.example.org`, and `[::1]` + if ($domain !== '' && ($host === $domain || str_ends_with($host, '.' . $domain))) { + return true; + } + } + return false; + } + /** * @throws FreshRSS_Context_Exception */ public static function getProxyImageUri(string $url): string { $parsed_url = parse_url($url); + if (isset($parsed_url['host']) && self::isExcludedHost($parsed_url['host'])) { + return $url; + } $scheme = $parsed_url['scheme'] ?? ''; if ($scheme === 'http') { if (!FreshRSS_Context::userConf()->attributeBool('image_proxy_scheme_http')) { diff --git a/xExtension-ImageProxy/i18n/de/ext.php b/xExtension-ImageProxy/i18n/de/ext.php index 5edfd73a..b1363978 100644 --- a/xExtension-ImageProxy/i18n/de/ext.php +++ b/xExtension-ImageProxy/i18n/de/ext.php @@ -7,6 +7,8 @@ 'scheme_https' => 'HTTPS-Proxy', 'scheme_default' => 'Proxy protokoll-relative URL', 'scheme_include' => 'http*:// in die URL einfügen', - 'url_encode' => 'URL-Prozentkodierung' + 'url_encode' => 'URL-Prozentkodierung', + 'excluded_domains' => 'Diese Domains nicht über den Proxy laden', + 'excluded_domains_help' => 'Eine Domain pro Zeile. Subdomains sind ebenfalls eingeschlossen: example.org gilt zum Beispiel auch für images.example.org.', ), ); diff --git a/xExtension-ImageProxy/i18n/en/ext.php b/xExtension-ImageProxy/i18n/en/ext.php index 1bca58ac..a2c6f322 100644 --- a/xExtension-ImageProxy/i18n/en/ext.php +++ b/xExtension-ImageProxy/i18n/en/ext.php @@ -7,6 +7,8 @@ 'scheme_https' => 'Proxy HTTPS', 'scheme_default' => 'Proxy protocol-relative URL', 'scheme_include' => 'Include http*:// in URL', - 'url_encode' => 'Encode the URL' + 'url_encode' => 'Encode the URL', + 'excluded_domains' => 'Do not proxy these domains', + 'excluded_domains_help' => 'One domain per line. Subdomains are included as well: for instance, example.org also matches images.example.org.', ), ); diff --git a/xExtension-ImageProxy/i18n/fr/ext.php b/xExtension-ImageProxy/i18n/fr/ext.php index a04583ff..a7ef066c 100644 --- a/xExtension-ImageProxy/i18n/fr/ext.php +++ b/xExtension-ImageProxy/i18n/fr/ext.php @@ -7,6 +7,8 @@ 'scheme_https' => 'Proxy HTTPS', 'scheme_default' => 'Proxy URLs sans protocole', 'scheme_include' => 'Inclure http*:// dans l\'URL', - 'url_encode' => 'Encoder l\'URL' + 'url_encode' => 'Encoder l\'URL', + 'excluded_domains' => 'Ne pas utiliser le proxy pour ces domaines', + 'excluded_domains_help' => 'Un domaine par ligne. Les sous-domaines sont également inclus : par exemple, example.org correspond aussi à images.example.org.', ), ); diff --git a/xExtension-ImageProxy/i18n/nl/ext.php b/xExtension-ImageProxy/i18n/nl/ext.php index 5c113151..7f4d5622 100644 --- a/xExtension-ImageProxy/i18n/nl/ext.php +++ b/xExtension-ImageProxy/i18n/nl/ext.php @@ -7,6 +7,8 @@ 'scheme_https' => 'Https proxyen', 'scheme_default' => 'Protocol-relatieve url’s proxyen', 'scheme_include' => 'http*:// in url opnemen', - 'url_encode' => 'Url encoderen' + 'url_encode' => 'Url encoderen', + 'excluded_domains' => 'Deze domeinen niet proxyen', + 'excluded_domains_help' => 'Eén domein per regel. Subdomeinen zijn ook inbegrepen: example.org geldt bijvoorbeeld ook voor images.example.org.', ), ); diff --git a/xExtension-ImageProxy/i18n/tr/ext.php b/xExtension-ImageProxy/i18n/tr/ext.php index 184645b4..16cc015f 100644 --- a/xExtension-ImageProxy/i18n/tr/ext.php +++ b/xExtension-ImageProxy/i18n/tr/ext.php @@ -8,5 +8,7 @@ 'scheme_default' => 'Belirtilmemiş Vekil URL Şeması', 'scheme_include' => 'Bağlantıya http*:// ekle', 'url_encode' => 'Bağlantıyı kodla', + 'excluded_domains' => 'Do not proxy these domains', // TODO + 'excluded_domains_help' => 'One domain per line. Subdomains are included as well: for instance, example.org also matches images.example.org.', // TODO ), ); diff --git a/xExtension-ImageProxy/metadata.json b/xExtension-ImageProxy/metadata.json index 0663a117..1d755b1c 100644 --- a/xExtension-ImageProxy/metadata.json +++ b/xExtension-ImageProxy/metadata.json @@ -2,7 +2,7 @@ "name": "Image Proxy", "author": "Frans de Jonge", "description": "No insecure content warnings or disappearing images.", - "version": "1.0.4", + "version": "1.1.0", "entrypoint": "ImageProxy", "type": "user" }