Skip to content

Commit 1ebf144

Browse files
committed
fix(preview): properly handle encoded content
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 6eec91a commit 1ebf144

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
@@ -29,15 +29,19 @@ public function getMimeType(): string {
2929
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
3030
try {
3131
$content = stream_get_contents($file->fopen('r'));
32-
if (substr($content, 0, 5) !== '<?xml') {
33-
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
32+
if ($content === false) {
33+
return null;
3434
}
35-
36-
// Do not parse SVG files with references
37-
if (preg_match('/["\s](xlink:)?href\s*=/i', $content)) {
35+
// check if the file can be processed by this provider
36+
if (!$this->canBeProcessed($content)) {
3837
return null;
3938
}
4039

40+
$content = ltrim($content);
41+
if (substr($content, 0, 5) !== '<?xml') {
42+
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
43+
}
44+
4145
$svg = new \Imagick();
4246

4347
$svg->pingImageBlob($content);
@@ -71,4 +75,30 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
7175
}
7276
return null;
7377
}
78+
79+
/**
80+
* Check if the file can be processed by this provider,
81+
* meaning the SVG is safe to be processed and does not contain any external references.
82+
*/
83+
protected function canBeProcessed(string $content): bool {
84+
// check for allowed encodings and convert if necessary
85+
$encoding = mb_detect_encoding($content, ['UTF-8', 'ISO-2022-JP', 'ISO-8859-1'], true);
86+
if ($encoding === false) {
87+
return false;
88+
} elseif ($encoding !== 'UTF-8') {
89+
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
90+
}
91+
92+
// Strip all non-printable/control characters except newlines/tabs
93+
$content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $content);
94+
if ($content === null) {
95+
return false;
96+
}
97+
98+
// check for any potential external reference (include custom namespace prefix)
99+
if (preg_match('/["\s\']([a-z_][a-z0-9_.-]*:)?href\s*=/i', $content)) {
100+
return false;
101+
}
102+
return true;
103+
}
74104
}

tests/lib/Preview/SVGTest.php

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

6262
self::assertNull($this->provider->getThumbnail($file, 512, 512));
6363
}
64+
65+
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetThumbnailSVGHrefNamespace')]
66+
#[\PHPUnit\Framework\Attributes\RequiresPhpExtension('imagick')]
67+
public function testGetThumbnailSvgHrefNamespace(string $namespace): void {
68+
$handle = fopen('php://temp', 'w+');
69+
fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:' . $namespace . '="http://www.w3.org/1999/xlink">
70+
<image x="0" y="0" ' . $namespace . ':href="fxlogo.png" height="100" width="100" />
71+
</svg>');
72+
rewind($handle);
73+
74+
$file = $this->createMock(File::class);
75+
$file->method('fopen')
76+
->willReturn($handle);
77+
78+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
79+
}
80+
81+
public static function dataGetThumbnailSVGHrefNamespace(): array {
82+
return [
83+
['xlink'],
84+
['foo'],
85+
['_foo'],
86+
['fo_12'],
87+
['foo-bar'],
88+
['Fo_B1-ar'],
89+
];
90+
}
91+
92+
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetThumbnailSvgEncoded')]
93+
#[\PHPUnit\Framework\Attributes\RequiresPhpExtension('imagick')]
94+
public function testGetThumbnailSvgEncoded(string $content): void {
95+
$handle = fopen('php://temp', 'w+');
96+
fwrite($handle, $content);
97+
rewind($handle);
98+
99+
$file = $this->createMock(File::class);
100+
$file->method('fopen')
101+
->willReturn($handle);
102+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
103+
}
104+
105+
public static function dataGetThumbnailSvgEncoded(): array {
106+
return [
107+
'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>"],
108+
];
109+
}
64110
}

0 commit comments

Comments
 (0)