Skip to content

Commit 12673f7

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

2 files changed

Lines changed: 114 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 (stripos($content, 'xlink:href') !== false) {
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,83 @@ protected function setUp(): void {
4343
$this->markTestSkipped('No SVG provider present');
4444
}
4545
}
46+
47+
public function dataGetThumbnailSVGHref(): array {
48+
return [
49+
['href'],
50+
[' href'],
51+
["\nhref"],
52+
['xlink:href'],
53+
[' xlink:href'],
54+
["\nxlink:href"],
55+
];
56+
}
57+
58+
/**
59+
* @dataProvider dataGetThumbnailSVGHref
60+
* @requires extension imagick
61+
*/
62+
public function testGetThumbnailSVGHref(string $content): void {
63+
$handle = fopen('php://temp', 'w+');
64+
fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
65+
<image x="0" y="0"' . $content . '="fxlogo.png" height="100" width="100" />
66+
</svg>');
67+
rewind($handle);
68+
69+
$file = $this->createMock(\OCP\Files\File::class);
70+
$file->method('fopen')
71+
->willReturn($handle);
72+
73+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
74+
}
75+
76+
/**
77+
* @dataProvider dataGetThumbnailSVGHrefNamespace
78+
* @requires extension imagick
79+
*/
80+
public function testGetThumbnailSvgHrefNamespace(string $namespace): void {
81+
$handle = fopen('php://temp', 'w+');
82+
fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:' . $namespace . '="http://www.w3.org/1999/xlink">
83+
<image x="0" y="0" ' . $namespace . ':href="fxlogo.png" height="100" width="100" />
84+
</svg>');
85+
rewind($handle);
86+
87+
$file = $this->createMock(File::class);
88+
$file->method('fopen')
89+
->willReturn($handle);
90+
91+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
92+
}
93+
94+
public static function dataGetThumbnailSVGHrefNamespace(): array {
95+
return [
96+
['xlink'],
97+
['foo'],
98+
['_foo'],
99+
['fo_12'],
100+
['foo-bar'],
101+
['Fo_B1-ar'],
102+
];
103+
}
104+
105+
/**
106+
* @dataProvider dataGetThumbnailSvgEncoded
107+
* @requires extension imagick
108+
*/
109+
public function testGetThumbnailSvgEncoded(string $content): void {
110+
$handle = fopen('php://temp', 'w+');
111+
fwrite($handle, $content);
112+
rewind($handle);
113+
114+
$file = $this->createMock(File::class);
115+
$file->method('fopen')
116+
->willReturn($handle);
117+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
118+
}
119+
120+
public static function dataGetThumbnailSvgEncoded(): array {
121+
return [
122+
'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>"],
123+
];
124+
}
46125
}

0 commit comments

Comments
 (0)