Skip to content

Commit 02cf4f6

Browse files
Merge pull request #62270 from nextcloud/backport/62202/stable26
[stable26] fix(preview): properly handle encoded content
2 parents 8b450d0 + f27836b commit 02cf4f6

3 files changed

Lines changed: 96 additions & 9 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, ['ISO-2022-JP', 'UTF-8', '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/Provider.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
namespace Test\Preview;
2323

2424
use OC\Files\Node\File;
25+
use OC\Preview\Provider as PreviewProvider;
26+
use OCP\Files\IRootFolder;
27+
use OCP\Preview\IProviderV2;
2528

2629
abstract class Provider extends \Test\TestCase {
2730
/** @var string */
@@ -30,7 +33,7 @@ abstract class Provider extends \Test\TestCase {
3033
protected $width;
3134
/** @var int */
3235
protected $height;
33-
/** @var \OC\Preview\Provider */
36+
/** @var IProviderV2|PreviewProvider */
3437
protected $provider;
3538
/** @var int */
3639
protected $maxWidth = 1024;
@@ -137,8 +140,10 @@ protected function prepareTestFile($fileName, $fileContent) {
137140
* @return bool|\OCP\IImage
138141
*/
139142
private function getPreview($provider) {
140-
$file = new File(\OC::$server->getRootFolder(), $this->rootView, $this->imgPath);
141-
$preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
143+
$file = new File(\OC::$server->get(IRootFolder::class), $this->rootView, $this->imgPath);
144+
$preview = ($provider instanceof PreviewProvider)
145+
? $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp, $this->rootView)
146+
: $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
142147

143148
if (get_class($this) === BitmapTest::class && $preview === null) {
144149
$this->markTestSkipped('An error occured while operating with Imagick.');

tests/lib/Preview/SVGTest.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
namespace Test\Preview;
2323

24+
use OCP\Files\File;
25+
2426
/**
2527
* Class SVGTest
2628
*
@@ -66,10 +68,60 @@ public function testGetThumbnailSVGHref(string $content): void {
6668
</svg>');
6769
rewind($handle);
6870

69-
$file = $this->createMock(\OCP\Files\File::class);
71+
$file = $this->createMock(File::class);
7072
$file->method('fopen')
7173
->willReturn($handle);
7274

7375
self::assertNull($this->provider->getThumbnail($file, 512, 512));
7476
}
77+
78+
/**
79+
* @dataProvider dataGetThumbnailSVGHrefNamespace
80+
* @requires extension imagick
81+
*/
82+
public function testGetThumbnailSvgHrefNamespace(string $namespace): void {
83+
$handle = fopen('php://temp', 'w+');
84+
fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:' . $namespace . '="http://www.w3.org/1999/xlink">
85+
<image x="0" y="0" ' . $namespace . ':href="fxlogo.png" height="100" width="100" />
86+
</svg>');
87+
rewind($handle);
88+
89+
$file = $this->createMock(File::class);
90+
$file->method('fopen')
91+
->willReturn($handle);
92+
93+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
94+
}
95+
96+
public static function dataGetThumbnailSVGHrefNamespace(): array {
97+
return [
98+
['xlink'],
99+
['foo'],
100+
['_foo'],
101+
['fo_12'],
102+
['foo-bar'],
103+
['Fo_B1-ar'],
104+
];
105+
}
106+
107+
/**
108+
* @dataProvider dataGetThumbnailSvgEncoded
109+
* @requires extension imagick
110+
*/
111+
public function testGetThumbnailSvgEncoded(string $content): void {
112+
$handle = fopen('php://temp', 'w+');
113+
fwrite($handle, $content);
114+
rewind($handle);
115+
116+
$file = $this->createMock(File::class);
117+
$file->method('fopen')
118+
->willReturn($handle);
119+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
120+
}
121+
122+
public static function dataGetThumbnailSvgEncoded(): array {
123+
return [
124+
'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>"],
125+
];
126+
}
75127
}

0 commit comments

Comments
 (0)