Skip to content

Commit e098c03

Browse files
committed
feat(previews): add file signature check before opening files
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 42240fb commit e098c03

10 files changed

Lines changed: 171 additions & 2 deletions

File tree

lib/private/Preview/Bitmap.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@
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+
* These should correspond to the MIME types *identified* by Imagemagick
44+
* for files to be processed by this provider. These do / will not
45+
* necessarily need to match the MIME types stored in the database
46+
* (which are identified by IMimeTypeDetector).
47+
*
48+
* @return string Regular expression
49+
*/
50+
abstract protected function getAllowedMimeTypes(): string;
51+
52+
53+
/**
54+
* @return list<string>
55+
*/
56+
abstract protected function getMagicStrings(): array;
57+
58+
abstract protected function getImagickFormatHint(): string;
59+
4060
/**
4161
* {@inheritDoc}
4262
*/
@@ -90,8 +110,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
90110
private function getResizedPreview($tmpPath, $maxX, $maxY) {
91111
$bp = new Imagick();
92112

113+
if (!$this->isMagicStringSupported($tmpPath)) {
114+
throw new \Exception('Invalid image type: magic string not recognized');
115+
}
116+
117+
// Validate mime type
118+
$bp->pingImage($this->getImagickFormatHint() . ':' . $tmpPath . '[0]');
119+
$mimeType = $bp->getImageMimeType();
120+
if (!preg_match($this->getAllowedMimeTypes(), $mimeType)) {
121+
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
122+
}
123+
93124
// Layer 0 contains either the bitmap or a flat representation of all vector layers
94-
$bp->readImage($tmpPath . '[0]');
125+
$bp->readImage($this->getImagickFormatHint() . ':' . $tmpPath . '[0]');
95126

96127
$bp = $this->resize($bp, $maxX, $maxY);
97128

@@ -100,6 +131,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
100131
return $bp;
101132
}
102133

134+
private function isMagicStringSupported(string $filepath): bool {
135+
$signatures = $this->getMagicStrings();
136+
if (empty($signatures)) {
137+
return true;
138+
}
139+
$length = array_reduce($signatures, static fn (int $carry, string $signature) => max($carry, strlen($signature)), 0);
140+
$firstBytes = file_get_contents($filepath, false, null, 0, $length);
141+
foreach ($signatures as $signature) {
142+
if (str_starts_with($firstBytes, $signature)) {
143+
return true;
144+
}
145+
}
146+
147+
return false;
148+
}
149+
103150
/**
104151
* Returns a resized \Imagick object
105152
*

lib/private/Preview/Font.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,22 @@ class Font extends Bitmap {
3030
public function getMimeType(): string {
3131
return '/application\/(?:font-sfnt|x-font$)/';
3232
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
protected function getAllowedMimeTypes(): string {
38+
return '/(application|image)\/(?:font-sfnt|x-font|x-otf|x-ttf|x-pfb$)/';
39+
}
40+
41+
protected function getMagicStrings(): array {
42+
return [
43+
"\x00\x01\x00\x00\x00", // TTF
44+
'OTTO', // OTF
45+
];
46+
}
47+
48+
protected function getImagickFormatHint(): string {
49+
return 'ttf';
50+
}
3351
}

lib/private/Preview/HEIC.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,16 @@ 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('heic:' . $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
116-
$bp->readImage($tmpPath . '[0]');
124+
$bp->readImage('heic:' . $tmpPath . '[0]');
117125

118126
// Fix orientation from EXIF
119127
$bp->autoOrient();

lib/private/Preview/Illustrator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,19 @@ class Illustrator extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/illustrator/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
protected function getAllowedMimeTypes(): string {
39+
return '/application\/(illustrator|pdf)/';
40+
}
41+
42+
protected function getMagicStrings(): array {
43+
return ["\x25\x50\x44\x46"];
44+
}
45+
46+
protected function getImagickFormatHint(): string {
47+
return 'ai';
48+
}
3449
}

lib/private/Preview/PDF.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,19 @@ class PDF extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/pdf/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
protected function getAllowedMimeTypes(): string {
39+
return '/application\/pdf/';
40+
}
41+
42+
protected function getMagicStrings(): array {
43+
return ['%PDF-'];
44+
}
45+
46+
protected function getImagickFormatHint(): string {
47+
return 'pdf';
48+
}
3449
}

lib/private/Preview/Photoshop.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,19 @@ class Photoshop extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/x-photoshop/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
protected function getAllowedMimeTypes(): string {
39+
return '/(application|image)\/(x-photoshop|x-psd)/';
40+
}
41+
42+
protected function getMagicStrings(): array {
43+
return ['8BPS'];
44+
}
45+
46+
protected function getImagickFormatHint(): string {
47+
return 'psd';
48+
}
3449
}

lib/private/Preview/Postscript.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,19 @@ class Postscript extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/application\/postscript/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
protected function getAllowedMimeTypes(): string {
39+
return '/application\/postscript/';
40+
}
41+
42+
protected function getMagicStrings(): array {
43+
return ['%!PS'];
44+
}
45+
46+
protected function getImagickFormatHint(): string {
47+
return 'ps';
48+
}
3449
}

lib/private/Preview/SGI.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ class SGI extends Bitmap {
3030
public function getMimeType(): string {
3131
return '/image\/sgi/';
3232
}
33+
34+
protected function getMagicStrings(): array {
35+
return ["\x01\xDA"];
36+
}
37+
38+
protected function getImagickFormatHint(): string {
39+
return 'sgi';
40+
}
3341
}

lib/private/Preview/TGA.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ class TGA extends Bitmap {
3030
public function getMimeType(): string {
3131
return '/image\/t(ar)?ga/';
3232
}
33+
34+
protected function getMagicStrings(): array {
35+
return [];
36+
}
37+
38+
protected function getImagickFormatHint(): string {
39+
return 'tga';
40+
}
3341
}

lib/private/Preview/TIFF.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,24 @@ class TIFF extends Bitmap {
3131
public function getMimeType(): string {
3232
return '/image\/tiff/';
3333
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
protected function getAllowedMimeTypes(): string {
39+
return '/image\/tiff/';
40+
}
41+
42+
protected function getMagicStrings(): array {
43+
return [
44+
"II*\x00",
45+
"MM\x00*",
46+
"II+\x00",
47+
"MM\x00+",
48+
];
49+
}
50+
51+
protected function getImagickFormatHint(): string {
52+
return 'tiff';
53+
}
3454
}

0 commit comments

Comments
 (0)