Skip to content

Commit 98a9a2d

Browse files
Altahrimbackportbot[bot]
authored andcommitted
feat(previews): add file signature check before opening files
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 42240fb commit 98a9a2d

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

lib/private/Preview/Bitmap.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
9191
$bp = new Imagick();
9292

9393
// Layer 0 contains either the bitmap or a flat representation of all vector layers
94-
$bp->readImage($tmpPath . '[0]');
94+
$bp->readImage($this->getImagickFormatHint() . ':' . $tmpPath . '[0]');
9595

9696
$bp = $this->resize($bp, $maxX, $maxY);
9797

@@ -100,6 +100,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
100100
return $bp;
101101
}
102102

103+
private function isMagicStringSupported(string $filepath): bool {
104+
$signatures = $this->getMagicStrings();
105+
if (empty($signatures)) {
106+
return true;
107+
}
108+
$length = array_reduce($signatures, static fn (int $carry, string $signature) => max($carry, strlen($signature)), 0);
109+
$firstBytes = file_get_contents($filepath, false, null, 0, $length);
110+
foreach ($signatures as $signature) {
111+
if (str_starts_with($firstBytes, $signature)) {
112+
return true;
113+
}
114+
}
115+
116+
return false;
117+
}
118+
103119
/**
104120
* Returns a resized \Imagick object
105121
*

lib/private/Preview/HEIC.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
113113
$bp = new \Imagick();
114114

115115
// Layer 0 contains either the bitmap or a flat representation of all vector layers
116-
$bp->readImage($tmpPath . '[0]');
116+
$bp->readImage('heic:' . $tmpPath . '[0]');
117117

118118
// Fix orientation from EXIF
119119
$bp->autoOrient();

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
}

0 commit comments

Comments
 (0)