From 4edd2b9eefc7e6d6b0b678fd0364024c7820c9cc Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 3 Jul 2026 17:43:13 +0530 Subject: [PATCH 1/3] (fix): harden image preview against image-bomb DoS Bump appwrite/base to 1.4.4, which ships a hardened ImageMagick policy (50KP width/height caps, 4GiB disk, 120s time, PS/PDF/SVG/HTTP coders disabled). Reject oversized images before decode with a dedicated storage_image_resolution_exceeded error instead of the opaque storage_file_type_unsupported wrapper. --- Dockerfile | 2 +- app/config/errors.php | 5 +++ src/Appwrite/Extend/Exception.php | 1 + .../Http/Buckets/Files/Preview/Get.php | 13 ++++++++ tests/e2e/Services/Storage/StorageBase.php | 31 ++++++++++++++++++ tests/resources/disk-a/image-bomb.png | Bin 0 -> 138 bytes 6 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/resources/disk-a/image-bomb.png diff --git a/Dockerfile b/Dockerfile index d9f8610ee1..4fed75ac30 100755 --- a/Dockerfile +++ b/Dockerfile @@ -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" diff --git a/app/config/errors.php b/app/config/errors.php index a548d75179..76022eb21f 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -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.', diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index ca114550b7..416d628ee0 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -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'; diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index d856c0456d..0d4955a60b 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -240,6 +240,19 @@ class Get extends Action $decompressionTime = \microtime(true) - $startTime - $downloadTime - $decryptionTime; + $maxWidth = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_WIDTH); + $maxHeight = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_HEIGHT); + $dimensions = \getimagesizefromstring($source); + if ( + $dimensions !== false && + (($maxWidth > 0 && $dimensions[0] > $maxWidth) || ($maxHeight > 0 && $dimensions[1] > $maxHeight)) + ) { + throw new Exception( + Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED, + \sprintf('Image resolution %dx%d exceeds the maximum allowed %dx%d pixels', $dimensions[0], $dimensions[1], $maxWidth, $maxHeight) + ); + } + try { $image = new Image($source); } catch (\Exception $e) { diff --git a/tests/e2e/Services/Storage/StorageBase.php b/tests/e2e/Services/Storage/StorageBase.php index 3f823c38e9..d2f27e4ed9 100644 --- a/tests/e2e/Services/Storage/StorageBase.php +++ b/tests/e2e/Services/Storage/StorageBase.php @@ -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(); diff --git a/tests/resources/disk-a/image-bomb.png b/tests/resources/disk-a/image-bomb.png new file mode 100644 index 0000000000000000000000000000000000000000..060de804e43b93b0da58c93d2d35745461d736da GIT binary patch literal 138 zcmeAS@N?(olHy`uVBq!ia0y~yc$ENTGjae4hJy$BEr3*@r;B4q#jUpo4H<#FLkqt8 h=koF|05Zu0=XhQvGBD0IGJFd%z|+;wWt~$(696&$6>$In literal 0 HcmV?d00001 From 1ad94992aba401bee00494434ac7005fe0cc0a43 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 3 Jul 2026 17:52:34 +0530 Subject: [PATCH 2/3] (fix): reject images exceeding the area limit in preview pre-check --- .../Http/Buckets/Files/Preview/Get.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index 0d4955a60b..a5562281d5 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -242,15 +242,20 @@ class Get extends Action $maxWidth = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_WIDTH); $maxHeight = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_HEIGHT); + $maxArea = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_AREA); $dimensions = \getimagesizefromstring($source); - if ( - $dimensions !== false && - (($maxWidth > 0 && $dimensions[0] > $maxWidth) || ($maxHeight > 0 && $dimensions[1] > $maxHeight)) - ) { - throw new Exception( - Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED, - \sprintf('Image resolution %dx%d exceeds the maximum allowed %dx%d pixels', $dimensions[0], $dimensions[1], $maxWidth, $maxHeight) - ); + if ($dimensions !== false) { + [$width, $height] = $dimensions; + if ( + ($maxWidth > 0 && $width > $maxWidth) || + ($maxHeight > 0 && $height > $maxHeight) || + ($maxArea > 0 && $width * $height > $maxArea) + ) { + throw new Exception( + Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED, + \sprintf('Image resolution %dx%d exceeds the maximum allowed %dx%d or %d total pixels', $width, $height, $maxWidth, $maxHeight, $maxArea) + ); + } } try { From fa04473661408cdd289c6103777015b1a458da50 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 3 Jul 2026 18:06:44 +0530 Subject: [PATCH 3/3] (fix): stop resolution pre-check from clobbering requested crop dimensions --- .../Modules/Storage/Http/Buckets/Files/Preview/Get.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index a5562281d5..7ff22f3744 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -245,15 +245,15 @@ class Get extends Action $maxArea = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_AREA); $dimensions = \getimagesizefromstring($source); if ($dimensions !== false) { - [$width, $height] = $dimensions; + [$sourceWidth, $sourceHeight] = $dimensions; if ( - ($maxWidth > 0 && $width > $maxWidth) || - ($maxHeight > 0 && $height > $maxHeight) || - ($maxArea > 0 && $width * $height > $maxArea) + ($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', $width, $height, $maxWidth, $maxHeight, $maxArea) + \sprintf('Image resolution %dx%d exceeds the maximum allowed %dx%d or %d total pixels', $sourceWidth, $sourceHeight, $maxWidth, $maxHeight, $maxArea) ); } }