Skip to content

Commit e5e2b15

Browse files
Merge pull request #62273 from nextcloud/backport/62202/stable23
[stable23] fix(preview): properly handle encoded content
2 parents d340f70 + 37d619c commit e5e2b15

3 files changed

Lines changed: 124 additions & 8 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, ['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

@@ -31,7 +34,7 @@ abstract class Provider extends \Test\TestCase {
3134
protected $width;
3235
/** @var int */
3336
protected $height;
34-
/** @var \OC\Preview\Provider */
37+
/** @var IProviderV2|PreviewProvider */
3538
protected $provider;
3639
/** @var int */
3740
protected $maxWidth = 1024;
@@ -138,8 +141,10 @@ protected function prepareTestFile($fileName, $fileContent) {
138141
* @return bool|\OCP\IImage
139142
*/
140143
private function getPreview($provider) {
141-
$file = new File(\OC::$server->getRootFolder(), $this->rootView, $this->imgPath);
142-
$preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
144+
$file = new File(\OC::$server->get(IRootFolder::class), $this->rootView, $this->imgPath);
145+
$preview = ($provider instanceof PreviewProvider)
146+
? $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp, $this->rootView)
147+
: $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
143148

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

tests/lib/Preview/SVGTest.php

Lines changed: 81 additions & 0 deletions
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
*
@@ -43,4 +45,83 @@ protected function setUp(): void {
4345
$this->markTestSkipped('No SVG provider present');
4446
}
4547
}
48+
49+
public function dataGetThumbnailSVGHref(): array {
50+
return [
51+
['href'],
52+
[' href'],
53+
["\nhref"],
54+
['xlink:href'],
55+
[' xlink:href'],
56+
["\nxlink:href"],
57+
];
58+
}
59+
60+
/**
61+
* @dataProvider dataGetThumbnailSVGHref
62+
* @requires extension imagick
63+
*/
64+
public function testGetThumbnailSVGHref(string $content): void {
65+
$handle = fopen('php://temp', 'w+');
66+
fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
67+
<image x="0" y="0"' . $content . '="fxlogo.png" height="100" width="100" />
68+
</svg>');
69+
rewind($handle);
70+
71+
$file = $this->createMock(File::class);
72+
$file->method('fopen')
73+
->willReturn($handle);
74+
75+
self::assertNull($this->provider->getThumbnail($file, 512, 512));
76+
}
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+
}
46127
}

0 commit comments

Comments
 (0)