Skip to content
Merged
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
1 change: 1 addition & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extend-exclude = [
"*/i18n/az",
"*/i18n/de",
"*/i18n/fr",
"*/i18n/nl",
"*/i18n/pl",
"bin/",
"node_modules/",
Expand Down
44 changes: 43 additions & 1 deletion tests/ImageProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -77,14 +78,15 @@ 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,
'image_proxy_scheme_https' => true,
'image_proxy_scheme_default' => 'auto',
'image_proxy_scheme_include' => $includeScheme,
'image_proxy_url_encode' => $encodeUrl,
'image_proxy_excluded_domains' => $excludedDomains,
]);
}

Expand Down Expand Up @@ -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(
'<img src="http://intranet.example/a.png" srcset="http://intranet.example/b.png 1x, http://images.example/c.png 2x">'));
$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);
Expand Down
5 changes: 5 additions & 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.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+
Expand All @@ -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=``.
Expand Down
9 changes: 9 additions & 0 deletions xExtension-ImageProxy/configure.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
<?= FreshRSS_Context::userConf()->attributeBool('image_proxy_url_encode') ? 'checked' : '' ?>>
</div>
</div>
<div class="form-group">
<label class="group-name" for="image_proxy_excluded_domains"><?= _t('ext.imageproxy.excluded_domains'); ?></label>
<div class="group-controls">
<textarea name="image_proxy_excluded_domains" id="image_proxy_excluded_domains" class="w100"><?=
htmlspecialchars(FreshRSS_Context::userConf()->attributeString('image_proxy_excluded_domains') ?? '', ENT_COMPAT, 'UTF-8')
?></textarea>
<p class="help"><?= _t('ext.imageproxy.excluded_domains_help'); ?></p>
</div>
</div>

<div class="form-group form-actions">
<div class="group-controls">
Expand Down
29 changes: 29 additions & 0 deletions xExtension-ImageProxy/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand All @@ -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')) {
Expand Down
4 changes: 3 additions & 1 deletion xExtension-ImageProxy/i18n/de/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
),
);
4 changes: 3 additions & 1 deletion xExtension-ImageProxy/i18n/en/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
),
);
4 changes: 3 additions & 1 deletion xExtension-ImageProxy/i18n/fr/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
),
);
4 changes: 3 additions & 1 deletion xExtension-ImageProxy/i18n/nl/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
),
);
2 changes: 2 additions & 0 deletions xExtension-ImageProxy/i18n/tr/ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
);
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.0.4",
"version": "1.1.0",
"entrypoint": "ImageProxy",
"type": "user"
}