Skip to content

Commit fa35ff6

Browse files
susnuxbackportbot[bot]
authored andcommitted
fix(preview): properly handle encoded content
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 6cbf0f4 commit fa35ff6

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

lib/private/Preview/SVG.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
4848
$svg->setBackgroundColor(new \ImagickPixel('transparent'));
4949

5050
$content = stream_get_contents($file->fopen('r'));
51-
if (substr($content, 0, 5) !== '<?xml') {
52-
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
51+
if ($content === false) {
52+
return null;
5353
}
54-
55-
// Do not parse SVG files with references
56-
if (preg_match('/["\s](xlink:)?href\s*=/i', $content)) {
54+
// check if the file can be processed by this provider
55+
if (!$this->canBeProcessed($content)) {
5756
return null;
5857
}
5958

59+
$content = ltrim($content);
60+
if (substr($content, 0, 5) !== '<?xml') {
61+
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
62+
}
63+
6064
$svg->readImageBlob($content);
6165
$svg->setImageFormat('png32');
6266
} catch (\Exception $e) {
@@ -78,4 +82,30 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
7882
}
7983
return null;
8084
}
85+
86+
/**
87+
* Check if the file can be processed by this provider,
88+
* meaning the SVG is safe to be processed and does not contain any external references.
89+
*/
90+
protected function canBeProcessed(string $content): bool {
91+
// check for allowed encodings and convert if necessary
92+
$encoding = mb_detect_encoding($content, ['UTF-8', 'ISO-2022-JP', 'ISO-8859-1'], true);
93+
if ($encoding === false) {
94+
return false;
95+
} elseif ($encoding !== 'UTF-8') {
96+
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
97+
}
98+
99+
// Strip all non-printable/control characters except newlines/tabs
100+
$content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $content);
101+
if ($content === null) {
102+
return false;
103+
}
104+
105+
// check for any potential external reference (include custom namespace prefix)
106+
if (preg_match('/["\s\']([a-z_][a-z0-9_.-]*:)?href\s*=/i', $content)) {
107+
return false;
108+
}
109+
return true;
110+
}
81111
}

tests/lib/Preview/SVGTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,50 @@ public function testGetThumbnailSVGHref(string $content): void {
7272

7373
self::assertNull($this->provider->getThumbnail($file, 512, 512));
7474
}
75+
76+
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetThumbnailSVGHrefNamespace')]
77+
#[\PHPUnit\Framework\Attributes\RequiresPhpExtension('imagick')]
78+
public function testGetThumbnailSvgHrefNamespace(string $namespace): void {
79+
$handle = fopen('php://temp', 'w+');
80+
fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:' . $namespace . '="http://www.w3.org/1999/xlink">
81+
<image x="0" y="0" ' . $namespace . ':href="fxlogo.png" height="100" width="100" />
82+
</svg>');
83+
rewind($handle);
84+
85+
$file = $this->createMock(File::class);
86+
$file->method('fopen')
87+
->willReturn($handle);
88+
89+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
90+
}
91+
92+
public static function dataGetThumbnailSVGHrefNamespace(): array {
93+
return [
94+
['xlink'],
95+
['foo'],
96+
['_foo'],
97+
['fo_12'],
98+
['foo-bar'],
99+
['Fo_B1-ar'],
100+
];
101+
}
102+
103+
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetThumbnailSvgEncoded')]
104+
#[\PHPUnit\Framework\Attributes\RequiresPhpExtension('imagick')]
105+
public function testGetThumbnailSvgEncoded(string $content): void {
106+
$handle = fopen('php://temp', 'w+');
107+
fwrite($handle, $content);
108+
rewind($handle);
109+
110+
$file = $this->createMock(File::class);
111+
$file->method('fopen')
112+
->willReturn($handle);
113+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
114+
}
115+
116+
public static function dataGetThumbnailSvgEncoded(): array {
117+
return [
118+
'iso-2022-jp' => ["<?xml version=\"1.0\" encoding=\"ISO-2022-JP\"?>\n<svg width=\"700\" height=\"700\" xmlns=\"http://www.w3.org/2000/svg\">\n<i\x1b(Bmage width=\"700\" height=\"700\" h\x1b(Bref=\"text:/proc/cpuinfo\" />\n</svg>"],
119+
];
120+
}
75121
}

0 commit comments

Comments
 (0)