Skip to content

Commit f3cb440

Browse files
Merge pull request #62268 from nextcloud/backport/62202/stable28
[stable28] fix(preview): properly handle encoded content
2 parents b8d6251 + 469d4ca commit f3cb440

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
@@ -45,15 +45,19 @@ public function getMimeType(): string {
4545
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
4646
try {
4747
$content = stream_get_contents($file->fopen('r'));
48-
if (substr($content, 0, 5) !== '<?xml') {
49-
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
48+
if ($content === false) {
49+
return null;
5050
}
51-
52-
// Do not parse SVG files with references
53-
if (preg_match('/["\s](xlink:)?href\s*=/i', $content)) {
51+
// check if the file can be processed by this provider
52+
if (!$this->canBeProcessed($content)) {
5453
return null;
5554
}
5655

56+
$content = ltrim($content);
57+
if (substr($content, 0, 5) !== '<?xml') {
58+
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
59+
}
60+
5761
$svg = new \Imagick();
5862

5963
$svg->pingImageBlob($content);
@@ -87,4 +91,30 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8791
}
8892
return null;
8993
}
94+
95+
/**
96+
* Check if the file can be processed by this provider,
97+
* meaning the SVG is safe to be processed and does not contain any external references.
98+
*/
99+
protected function canBeProcessed(string $content): bool {
100+
// check for allowed encodings and convert if necessary
101+
$encoding = mb_detect_encoding($content, ['ISO-2022-JP', 'UTF-8', 'ISO-8859-1'], true);
102+
if ($encoding === false) {
103+
return false;
104+
} elseif ($encoding !== 'UTF-8') {
105+
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
106+
}
107+
108+
// Strip all non-printable/control characters except newlines/tabs
109+
$content = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $content);
110+
if ($content === null) {
111+
return false;
112+
}
113+
114+
// check for any potential external reference (include custom namespace prefix)
115+
if (preg_match('/["\s\']([a-z_][a-z0-9_.-]*:)?href\s*=/i', $content)) {
116+
return false;
117+
}
118+
return true;
119+
}
90120
}

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)