Skip to content

A self-hosted media server and file management system

fix: image downloads are missing security checks

aminnausinb4db561a0886
diff --git a/app/Exceptions/Images/InvalidImageDataException.php b/app/Exceptions/Images/InvalidImageDataException.php
new file mode 100644
index 00000000..7702ad29
--- /dev/null
+++ b/app/Exceptions/Images/InvalidImageDataException.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Exceptions\Images;
+
+use Exception;
+
+class InvalidImageDataException extends Exception {
+ private array $context = [];
+
+ public function __construct(string $message, ?array $context = []) {
+ $this->context = $context ?? [];
+ parent::__construct($message);
+ }
+
+ public function getContext(): array {
+ return $this->context;
+ }
+}
diff --git a/app/Rules/ImageUpdateRules.php b/app/Rules/ImageUpdateRules.php
index e524964a..5daef8e7 100644
--- a/app/Rules/ImageUpdateRules.php
+++ b/app/Rules/ImageUpdateRules.php
@@ -8,7 +8,6 @@ class ImageUpdateRules {
'image_id' => [
'required_if:mode,existing',
'integer',
- 'exists:images,id',
],
'image' => [
@@ -16,7 +15,7 @@ class ImageUpdateRules {
'file',
'image',
'mimes:' . implode(',', config('media.uploads.allowed_extensions', ['jpeg', 'jpg', 'png', 'webp'])),
- 'max:' . config('media.uploads.max_upload_size', 10240),
+ 'max:' . config('media.uploads.max_size_kb', 10240),
],
'url' => [
diff --git a/app/Services/Images/ImageService.php b/app/Services/Images/ImageService.php
index 7e4a42fd..d663df19 100644
--- a/app/Services/Images/ImageService.php
+++ b/app/Services/Images/ImageService.php
@@ -7,6 +7,7 @@ use App\Data\Images\ImageUpdateData;
use App\Enums\ImageSource;
use App\Enums\ImageType;
use App\Enums\ImageVariant;
+use App\Exceptions\Images\InvalidImageDataException;
use App\Models\Image;
use App\Models\Metadata;
use App\Models\Series;
@@ -14,6 +15,7 @@ use Bepsvpt\Blurhash\Facades\BlurHash;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Http\Client\Response;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@@ -156,22 +158,34 @@ class ImageService {
try {
$fmt = 'webp';
+ $url = $this->validateUrl($url, $debugInfo);
+ if (! $url) {
+ return null;
+ }
+
[$relativeOutputPath, $absoluteOutputPath] = $this->resolvePath($owner, $imageType, $fmt, null, true);
- $response = Http::get($url);
+ $response = Http::timeout(10)->connectTimeout(5)->retry(1, 250)->withOptions(['allow_redirects' => false])->accept('image/*')->get($url);
if (! $response->successful()) {
- Log::warning('Failed to download image', $debugInfo);
+ throw new InvalidImageDataException('Failed to download image');
+ }
+ $imageData = $this->validateDownloadedImage($response, $debugInfo);
+
+ if (! $imageData) {
return null;
}
- Storage::disk('public')->put($relativeOutputPath, $response->body());
+ $webpData = $this->convertToWebp($imageData);
+ if (! $webpData) {
+ throw new InvalidImageDataException('Failed to convert downloaded image to webp');
+ }
- if (! file_exists($absoluteOutputPath) || filesize($absoluteOutputPath) === 0) {
- Log::warning('Failed to download image, file not created', [...$debugInfo, 'outputPath' => $absoluteOutputPath]);
+ Storage::disk('public')->put($relativeOutputPath, $webpData);
- return null;
+ if (! file_exists($absoluteOutputPath) || filesize($absoluteOutputPath) === 0) {
+ throw new InvalidImageDataException('Failed to download image, file not created', ['outputPath' => $absoluteOutputPath]);
}
return $this->persistImage(
@@ -186,6 +200,13 @@ class ImageService {
sourceUrl: $url
)
);
+ } catch (InvalidImageDataException $th) {
+ Log::warning($th->getMessage(), [
+ ...$debugInfo,
+ ...$th->getContext(),
+ ]);
+
+ return null;
} catch (\Throwable $th) {
Log::warning('Failed to download image', [...$debugInfo, 'error' => $th->getMessage()]);
@@ -273,16 +294,15 @@ class ImageService {
return;
}
- if (! $data->isAdmin && $owner->images()->whereIn('id', $data->deletedIds)->where('user_id', '!=', $data->user->id)->exists()) {
- throw new AuthorizationException('You do not own one or more of the images you are trying to delete.');
- }
-
$affected = $owner->images()
->whereIn('id', $data->deletedIds)
->whereIn('image_source', [ImageSource::UPLOADED->value, ImageSource::DOWNLOADED->value])
- ->when(! $data->isAdmin, fn ($q) => $q->where('user_id', $data->user->id))
->whereNull('replaced_at')
- ->get(['id', 'path', 'image_type', 'image_source']);
+ ->get(['id', 'path', 'image_type', 'image_source', 'user_id']);
+
+ if (! $data->isAdmin && $affected->where('user_id', '!=', $data->user->id)->isNotEmpty()) {
+ throw new AuthorizationException('You do not own one or more of the images you are trying to delete.');
+ }
if ($affected->isEmpty()) {
return;
@@ -333,7 +353,7 @@ class ImageService {
)
);
} catch (\Throwable $th) {
- Log::warning('Failed to migrate image', ['path' => $filePath, 'owner' => $owner->class, 'error' => $th->getMessage()]);
+ Log::warning('Failed to migrate image', ['path' => $filePath, 'owner' => $owner::class, 'error' => $th->getMessage()]);
return null;
}
@@ -384,6 +404,7 @@ class ImageService {
}
$uuid = $owner->uuid ?? throw new \InvalidArgumentException('Owner must have a UUID');
+ $fileSize = filesize($data->absolutePath);
[$width, $height] = getimagesize($data->absolutePath);
if ($overwrite) {
@@ -402,7 +423,7 @@ class ImageService {
'width' => $width,
'height' => $height,
- 'size' => filesize($data->absolutePath),
+ 'size' => $fileSize,
'path' => $data->relativePath,
'format' => $data->format,
@@ -415,7 +436,7 @@ class ImageService {
// Only replace generated / extracted / auto downloaded files
// User uploaded / url downloaded images should not be replaced?
- $autoSources = [ImageSource::GENERATED, ImageSource::EMBEDDED, ImageSource::DOWNLOADED];
+ $autoSources = [ImageSource::GENERATED, ImageSource::EMBEDDED];
if (in_array($data->source, $autoSources) || $forceReplace) {
Image::where([
@@ -437,7 +458,7 @@ class ImageService {
'width' => $width,
'height' => $height,
- 'size' => filesize($data->absolutePath),
+ 'size' => $fileSize,
'path' => $data->relativePath,
'format' => $data->format,
@@ -453,4 +474,85 @@ class ImageService {
*/
return Storage::disk($disk)->url($relativePath);
}
+
+ protected function validateUrl(string $url, array $debugInfo): ?string {
+ try {
+ $scheme = strtolower(parse_url(trim($url), PHP_URL_SCHEME) ?? '');
+ if (! in_array($scheme, ['http', 'https'], true)) {
+ throw new InvalidImageDataException('Invalid URL scheme', ['scheme' => $scheme]);
+ }
+
+ $host = strtolower(parse_url(trim($url), PHP_URL_HOST) ?? '');
+ if (! $host) {
+ throw new InvalidImageDataException('Missing URL host');
+ }
+
+ if (config('media.image_downloads.security.allow_private_network_urls')) {
+ return $url;
+ }
+
+ if (in_array($host, ['localhost', '127.0.0.1', '::1'], true)) {
+ throw new InvalidImageDataException('Blocked localhost image URL');
+ }
+
+ // Filters raw ip addresses that are not public
+ if (filter_var($host, FILTER_VALIDATE_IP) && ! filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
+ throw new InvalidImageDataException('Blocked private image URL', ['host' => $host]);
+ }
+
+ return $url;
+ } catch (InvalidImageDataException $th) {
+ Log::warning($th->getMessage(), [
+ ...$debugInfo,
+ ...$th->getContext(),
+ ]);
+
+ return null;
+ }
+ }
+
+ protected function validateDownloadedImage(Response $response, array $debugInfo): ?string {
+ try {
+ $contentType = strtolower(explode(';', $response->header('content-type'))[0]);
+
+ // Filter mime/content type
+ if (! str_starts_with($contentType, 'image/')) {
+ throw new InvalidImageDataException('Downloaded file is not an image', ['content_type' => $contentType]);
+ }
+
+ $body = $response->body();
+ $maxBytes = config('media.image_downloads.max_size_kb', 10240) * 1024;
+
+ // Filter against max size
+ if (strlen($body) > $maxBytes) {
+ throw new InvalidImageDataException('Downloaded image exceeds size limit', ['size_bytes' => strlen($body)]);
+ }
+
+ // Filter image bytes
+ if (! @getimagesizefromstring($body)) {
+ throw new InvalidImageDataException('Downloaded content is not a valid image');
+ }
+
+ return $body;
+ } catch (InvalidImageDataException $th) {
+ Log::warning($th->getMessage(), [
+ ...$debugInfo,
+ ...$th->getContext(),
+ ]);
+
+ return null;
+ }
+ }
+
+ protected function convertToWebp(string $imageData): ?string {
+ $source = @imagecreatefromstring($imageData);
+ if (! $source) {
+ return null;
+ }
+
+ ob_start();
+ imagewebp($source, null, 85);
+
+ return ob_get_clean() ?: null;
+ }
}
diff --git a/config/media.php b/config/media.php
index ab589cb3..e4c97b9b 100644
--- a/config/media.php
+++ b/config/media.php
@@ -4,6 +4,12 @@ return [
'downloads' => [
'max_size_mb' => env('MAX_DOWNLOAD_SIZE_MB', 1024 * 6),
],
+ 'image_downloads' => [
+ 'security' => [
+ 'allow_private_network_urls' => env('MEDIA_ALLOW_PRIVATE_URLS', true),
+ ],
+ 'max_size_kb' => 1024 * 20,
+ ],
'storage' => [
'prefix' => 'storage/media/',
],
@@ -22,6 +28,6 @@ return [
],
'uploads' => [
'allowed_extensions' => ['jpeg', 'jpg', 'png', 'webp'],
- 'max_upload_size' => 1024 * 10,
+ 'max_size_kb' => 1024 * 10,
],
];