From ba66e192ff7eb56f473576c0cefe7dece1f2b734 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 15 Jul 2026 00:13:30 +0200 Subject: [PATCH] fix(preview): properly handle encoded content fix(preview): properly handle encoded content Signed-off-by: Ferdinand Thiessen [skip ci] --- lib/private/Preview/SVG.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/private/Preview/SVG.php b/lib/private/Preview/SVG.php index 690a6b50ffe74..f292a2c55ae45 100644 --- a/lib/private/Preview/SVG.php +++ b/lib/private/Preview/SVG.php @@ -48,8 +48,12 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { $svg->setBackgroundColor(new \ImagickPixel('transparent')); $content = stream_get_contents($file->fopen('r')); - if (substr($content, 0, 5) !== '' . $content; + if ($content === false) { + return null; + } + // check if the file can be processed by this provider + if (!$this->canBeProcessed($content)) { + return null; } // Do not parse SVG files with references @@ -78,4 +82,30 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { } return null; } + + /** + * Check if the file can be processed by this provider, + * meaning the SVG is safe to be processed and does not contain any external references. + */ + protected function canBeProcessed(string $content): bool { + // check for allowed encodings and convert if necessary + $encoding = mb_detect_encoding($content, ['UTF-8', 'ISO-2022-JP', 'ISO-8859-1'], true); + if ($encoding === false) { + return false; + } elseif ($encoding !== 'UTF-8') { + $content = mb_convert_encoding($content, 'UTF-8', $encoding); + } + + // Strip all non-printable/control characters except newlines/tabs + $content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $content); + if ($content === null) { + return false; + } + + // check for any potential external reference (include custom namespace prefix) + if (preg_match('/["\s\']([a-z_][a-z0-9_.-]*:)?href\s*=/i', $content)) { + return false; + } + return true; + } }