add list of allowed hosts in notes images

Co-Authored-By: n1rwana <93197434+n1rwana@users.noreply.github.com>
This commit is contained in:
mrilyew 2025-06-08 11:08:53 +03:00
parent ec5dee371c
commit 3cef6e4824
5 changed files with 69 additions and 9 deletions

View file

@ -6,12 +6,42 @@ namespace openvk\Web\Models\Entities;
use HTMLPurifier_Config; use HTMLPurifier_Config;
use HTMLPurifier; use HTMLPurifier;
use HTMLPurifier_Filter;
class SecurityFilter extends HTMLPurifier_Filter
{
public function preFilter($html, $config, $context)
{
$html = preg_replace_callback(
'/<img[^>]*src\s*=\s*["\']([^"\']*)["\'][^>]*>/i',
function ($matches) {
$originalSrc = $matches[1];
$src = $originalSrc;
if (OPENVK_ROOT_CONF["openvk"]["preferences"]["notes"]["disableHotlinking"] ?? true) {
if (!str_contains($src, "/image.php?url=")) {
$src = '/image.php?url=' . base64_encode($originalSrc);
} /*else {
$src = preg_replace_callback('/(.*)\/image\.php\?url=(.*)/i', function ($matches) {
return base64_decode($matches[2]);
}, $src);
}*/
}
return str_replace($originalSrc, $src, $matches[0]);
},
$html
);
return $html;
}
}
class Note extends Postable class Note extends Postable
{ {
protected $tableName = "notes"; protected $tableName = "notes";
protected function renderHTML(): string protected function renderHTML(?string $content = null): string
{ {
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$config->set("Attr.AllowedClasses", []); $config->set("Attr.AllowedClasses", []);
@ -78,8 +108,10 @@ class Note extends Postable
$config->set("Attr.AllowedClasses", [ $config->set("Attr.AllowedClasses", [
"underline", "underline",
]); ]);
$config->set('Filter.Custom', [new SecurityFilter()]);
$source = null; $source = $content;
if (!$source) {
if (is_null($this->getRecord())) { if (is_null($this->getRecord())) {
if (isset($this->changes["source"])) { if (isset($this->changes["source"])) {
$source = $this->changes["source"]; $source = $this->changes["source"];
@ -89,6 +121,7 @@ class Note extends Postable
} else { } else {
$source = $this->getRecord()->source; $source = $this->getRecord()->source;
} }
}
$purifier = new HTMLPurifier($config); $purifier = new HTMLPurifier($config);
return $purifier->purify($source); return $purifier->purify($source);
@ -117,7 +150,7 @@ class Note extends Postable
$this->save(); $this->save();
} }
return $cached; return $this->renderHTML($cached);
} }
public function getSource(): string public function getSource(): string

View file

@ -176,4 +176,26 @@ final class InternalAPIPresenter extends OpenVKPresenter
exit(''); exit('');
} }
} }
public function renderImageFilter()
{
$is_enabled = OPENVK_ROOT_CONF["openvk"]["preferences"]["notes"]["disableHotlinking"] ?? true;
$allowed_hosts = OPENVK_ROOT_CONF["openvk"]["preferences"]["notes"]["allowedHosts"] ?? [];
$url = $this->requestParam("url");
$url = base64_decode($url);
if (!$is_enabled) {
$this->redirect($url);
}
$url_parsed = parse_url($url);
$host = $url_parsed['host'];
if (in_array($host, $allowed_hosts)) {
$this->redirect($url);
} else {
$this->redirect('/assets/packages/static/openvk/img/fn_placeholder.jpg');
}
}
} }

View file

@ -413,6 +413,8 @@ routes:
handler: "InternalAPI->getPhotosFromPost" handler: "InternalAPI->getPhotosFromPost"
- url: "/iapi/getPostTemplate/{num}_{num}" - url: "/iapi/getPostTemplate/{num}_{num}"
handler: "InternalAPI->getPostTemplate" handler: "InternalAPI->getPostTemplate"
- url: "/image.php"
handler: "InternalAPI->imageFilter"
- url: "/tour" - url: "/tour"
handler: "About->tour" handler: "About->tour"
- url: "/fave" - url: "/fave"

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -60,6 +60,9 @@ openvk:
exposeOriginalURLs: true exposeOriginalURLs: true
newsfeed: newsfeed:
ignoredSourcesLimit: 50 ignoredSourcesLimit: 50
notes:
disableHotlinking: true
allowedHosts: []
wall: wall:
christian: false christian: false
anonymousPosting: anonymousPosting: