Skip to content

Commit ffe31a5

Browse files
committed
fix(preview): check mime type before processing with Imagick
Signed-off-by: Varun Patil <varunpatil@ucla.edu>
1 parent 1fb5486 commit ffe31a5

File tree

13 files changed

+106
-13
lines changed

13 files changed

+106
-13
lines changed

lib/private/Preview/Bitmap.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
* @package OC\Preview
3838
*/
3939
abstract class Bitmap extends ProviderV2 {
40+
/**
41+
* List of MIME types that this preview provider is allowed to process.
42+
*
43+
* @return string Regular expression
44+
*/
45+
public function getAllowedMimeTypes(): string {
46+
return '/.*/';
47+
}
48+
4049
/**
4150
* {@inheritDoc}
4251
*/
@@ -90,6 +99,13 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
9099
private function getResizedPreview($tmpPath, $maxX, $maxY) {
91100
$bp = new Imagick();
92101

102+
// Validate mime type
103+
$bp->pingImage($tmpPath . '[0]');
104+
$mimeType = $bp->getImageMimeType();
105+
if (!preg_match($this->getAllowedMimeTypes(), $mimeType)) {
106+
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
107+
}
108+
93109
// Layer 0 contains either the bitmap or a flat representation of all vector layers
94110
$bp->readImage($tmpPath . '[0]');
95111

lib/private/Preview/Font.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ class Font extends Bitmap {
3030
public function getMimeType(): string {
3131
return '/application\/(?:font-sfnt|x-font$)/';
3232
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getAllowedMimeTypes(): string {
38+
return '/(application|image)\/(?:font-sfnt|x-font|x-otf|x-ttf|x-pfb$)/';
39+
}
3340
}

lib/private/Preview/HEIC.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class HEIC extends ProviderV2 {
4444
* {@inheritDoc}
4545
*/
4646
public function getMimeType(): string {
47-
return '/image\/hei(f|c)/';
47+
return '/image\/(x-)?hei(f|c)/';
4848
}
4949

5050
/**
@@ -112,6 +112,14 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
112112
private function getResizedPreview($tmpPath, $maxX, $maxY) {
113113
$bp = new \Imagick();
114114

115+
// Some HEIC files just contain (or at least are identified as) other formats
116+
// like JPEG. We just need to check if the image is safe to process.
117+
$bp->pingImage($tmpPath . '[0]');
118+
$mimeType = $bp->getImageMimeType();
119+
if (!preg_match('/^image\/(x-)?(png|jpeg|gif|bmp|tiff|webp|hei(f|c)|avif)$/', $mimeType)) {
120+
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
121+
}
122+
115123
// Layer 0 contains either the bitmap or a flat representation of all vector layers
116124
$bp->readImage($tmpPath . '[0]');
117125

lib/private/Preview/IMagickSupport.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public function supportsFormat(string $format): bool {
3737
$this->cache->set($format, $cached);
3838
return $formatSupported;
3939
}
40+
41+
public static function isSafeMimeType(string $mimeType): bool {
42+
return preg_match('/^image\/(png|jpeg|gif|bmp|tiff|webp|hei(f|c))$/', $mimeType) > 0;
43+
}
4044
}

lib/private/Preview/Illustrator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ class Illustrator extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/illustrator/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function getAllowedMimeTypes(): string {
39+
return '/application\/(illustrator|pdf)/';
40+
}
3441
}

lib/private/Preview/PDF.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ class PDF extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/pdf/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function getAllowedMimeTypes(): string {
39+
return '/application\/pdf/';
40+
}
3441
}

lib/private/Preview/Photoshop.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ class Photoshop extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/x-photoshop/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function getAllowedMimeTypes(): string {
39+
return '/(application|image)\/(x-photoshop|x-psd)/';
40+
}
3441
}

lib/private/Preview/Postscript.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ class Postscript extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/postscript/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function getAllowedMimeTypes(): string {
39+
return '/application\/postscript/';
40+
}
3441
}

lib/private/Preview/SGI.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class SGI extends Bitmap {
2828
* {@inheritDoc}
2929
*/
3030
public function getMimeType(): string {
31-
return '/image\/sgi/';
31+
return '/image\/(x-)?sgi/';
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getAllowedMimeTypes(): string {
38+
return '/image\/(x-)?sgi/';
3239
}
3340
}

lib/private/Preview/SVG.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ public function getMimeType(): string {
4444
*/
4545
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
4646
try {
47-
$svg = new \Imagick();
48-
$svg->setBackgroundColor(new \ImagickPixel('transparent'));
49-
5047
$content = stream_get_contents($file->fopen('r'));
5148
if (substr($content, 0, 5) !== '<?xml') {
5249
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
@@ -57,13 +54,25 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
5754
return null;
5855
}
5956

57+
$svg = new \Imagick();
58+
59+
$svg->pingImageBlob($content);
60+
$mimeType = $svg->getImageMimeType();
61+
if (!preg_match($this->getMimeType(), $mimeType)) {
62+
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
63+
}
64+
65+
$svg->setBackgroundColor(new \ImagickPixel('transparent'));
6066
$svg->readImageBlob($content);
6167
$svg->setImageFormat('png32');
6268
} catch (\Exception $e) {
63-
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [
64-
'exception' => $e,
65-
'app' => 'core',
66-
]);
69+
\OC::$server->get(LoggerInterface::class)->error(
70+
'File: ' . $file->getPath() . ' Imagick says:',
71+
[
72+
'exception' => $e,
73+
'app' => 'core',
74+
]
75+
);
6776
return null;
6877
}
6978

0 commit comments

Comments
 (0)