mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12779 from appwrite/fix-image-bomb-preview-dos
Harden image preview against image-bomb DoS
This commit is contained in:
6 files changed
+56
-1
No files matched your search
+1
-1
@@ -12,7 +12,7 @@ RUN composer install --ignore-platform-reqs --optimize-autoloader \
|
||||
--no-plugins --no-scripts --prefer-dist \
|
||||
`if [ "$TESTING" != "true" ]; then echo "--no-dev"; fi`
|
||||
|
||||
FROM appwrite/base:1.4.3 AS base
|
||||
FROM appwrite/base:1.4.4 AS base
|
||||
|
||||
LABEL maintainer="team@appwrite.io"
|
||||
|
||||
|
||||
@@ -512,6 +512,11 @@ return [
|
||||
'description' => 'The given file extension is not supported.',
|
||||
'code' => 400,
|
||||
],
|
||||
Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED => [
|
||||
'name' => Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED,
|
||||
'description' => 'The image resolution exceeds the maximum allowed for processing.',
|
||||
'code' => 400,
|
||||
],
|
||||
Exception::STORAGE_INVALID_FILE_SIZE => [
|
||||
'name' => Exception::STORAGE_INVALID_FILE_SIZE,
|
||||
'description' => 'The file size is either not valid or exceeds the maximum allowed size. Please check the file or the value of the _APP_STORAGE_LIMIT environment variable.',
|
||||
|
||||
@@ -151,6 +151,7 @@ class Exception extends \Exception
|
||||
public const string STORAGE_DEVICE_NOT_FOUND = 'storage_device_not_found';
|
||||
public const string STORAGE_FILE_EMPTY = 'storage_file_empty';
|
||||
public const string STORAGE_FILE_TYPE_UNSUPPORTED = 'storage_file_type_unsupported';
|
||||
public const string STORAGE_IMAGE_RESOLUTION_EXCEEDED = 'storage_image_resolution_exceeded';
|
||||
public const string STORAGE_INVALID_FILE_SIZE = 'storage_invalid_file_size';
|
||||
public const string STORAGE_INVALID_FILE = 'storage_invalid_file';
|
||||
public const string STORAGE_BUCKET_ALREADY_EXISTS = 'storage_bucket_already_exists';
|
||||
|
||||
@@ -240,6 +240,24 @@ class Get extends Action
|
||||
|
||||
$decompressionTime = \microtime(true) - $startTime - $downloadTime - $decryptionTime;
|
||||
|
||||
$maxWidth = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_WIDTH);
|
||||
$maxHeight = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_HEIGHT);
|
||||
$maxArea = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_AREA);
|
||||
$dimensions = \getimagesizefromstring($source);
|
||||
if ($dimensions !== false) {
|
||||
[$sourceWidth, $sourceHeight] = $dimensions;
|
||||
if (
|
||||
($maxWidth > 0 && $sourceWidth > $maxWidth) ||
|
||||
($maxHeight > 0 && $sourceHeight > $maxHeight) ||
|
||||
($maxArea > 0 && $sourceWidth * $sourceHeight > $maxArea)
|
||||
) {
|
||||
throw new Exception(
|
||||
Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED,
|
||||
\sprintf('Image resolution %dx%d exceeds the maximum allowed %dx%d or %d total pixels', $sourceWidth, $sourceHeight, $maxWidth, $maxHeight, $maxArea)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$image = new Image($source);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -892,6 +892,37 @@ trait StorageBase
|
||||
$this->assertEquals(404, $file8['headers']['status-code']);
|
||||
}
|
||||
|
||||
public function testFilePreviewOversized(): void
|
||||
{
|
||||
$data = $this->setupBucketFile();
|
||||
$bucketId = $data['bucketId'];
|
||||
|
||||
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([
|
||||
'content-type' => 'multipart/form-data',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'fileId' => ID::unique(),
|
||||
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/disk-a/image-bomb.png'), 'image/png', 'image-bomb.png'),
|
||||
'permissions' => [
|
||||
Permission::read(Role::any()),
|
||||
Permission::update(Role::any()),
|
||||
Permission::delete(Role::any()),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(201, $file['headers']['status-code']);
|
||||
$this->assertNotEmpty($file['body']['$id']);
|
||||
|
||||
$preview = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $file['body']['$id'] . '/preview', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()));
|
||||
|
||||
$this->assertEquals(400, $preview['headers']['status-code']);
|
||||
$this->assertEquals(Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED, $preview['body']['type']);
|
||||
$this->assertStringContainsString('60000x1', $preview['body']['message']);
|
||||
}
|
||||
|
||||
public function testFilePreviewCache(): void
|
||||
{
|
||||
$data = $this->setupBucketFile();
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 138 B |
Reference in new issue
Block a user