From 8ac7f6a0350a1d425fbacb8f086015edfa2bbdc4 Mon Sep 17 00:00:00 2001 From: glubsy Date: Sat, 23 Jan 2021 18:30:19 +0000 Subject: [PATCH] Limit thumbnail generation * This fixes a denial-of-service exploit that would allow the client to generate an infinite number of thumbnails and fill up the storage completely. Since the client had control over the requested thumbnail sizes, it could make arbitrary requests for thumbnails, and every time the backend did not find an already generated thumbnail with the specified sizes, it would happily generate a new one. * Remove the ability of the client to decide thumbnail dimensions and only let the back-end do this by reading the configuration. * Limit the number of generated thumbnails per file to only one, with "landscape" dimensions (4/3). * Use CSS "object-fit" property to adjust displaying of landscape thumbnails into squares. Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit --- src/_h5ai/private/conf/options.json | 2 +- src/_h5ai/private/php/core/class-context.php | 5 ++++- src/_h5ai/private/php/ext/class-thumb.php | 4 ++-- src/_h5ai/public/css/lib/view/view.less | 2 ++ src/_h5ai/public/js/lib/ext/thumbnails.js | 5 ----- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/_h5ai/private/conf/options.json b/src/_h5ai/private/conf/options.json index bccde678..191cdf7b 100644 --- a/src/_h5ai/private/conf/options.json +++ b/src/_h5ai/private/conf/options.json @@ -355,7 +355,7 @@ - mov: array of strings - doc: array of strings - delay: number, delay in milliseconds after "dom-ready" before thumb-requesting starts - - size: number, size in pixel of the generated thumbnails + - size: number, height in pixel of the generated thumbnails - seek: number, percentage of total video duration to seek into - exif: boolean, use included EXIF thumbs if possible - chunksize: int, number of thumbs per request diff --git a/src/_h5ai/private/php/core/class-context.php b/src/_h5ai/private/php/core/class-context.php index 0a72cea5..64c9b72c 100644 --- a/src/_h5ai/private/php/core/class-context.php +++ b/src/_h5ai/private/php/core/class-context.php @@ -23,6 +23,9 @@ class Context { $this->options = Json::load($this->setup->get('CONF_PATH') . '/options.json'); + $this->thumbnail_height = $this->options['thumbnails']['size'] ?? 240; + $this->thumbnail_width = floor($this->thumbnail_height * (4 / 3)); + $this->passhash = $this->query_option('passhash', ''); $this->options['hasCustomPasshash'] = strcasecmp($this->passhash, Context::$DEFAULT_PASSHASH) !== 0; unset($this->options['passhash']); @@ -250,7 +253,7 @@ class Context { foreach ($requests as $req) { $thumb = new Thumb($this); - $hrefs[] = $thumb->thumb($req['type'], $req['href'], $req['width'], $req['height']); + $hrefs[] = $thumb->thumb($req['type'], $req['href']); } return $hrefs; diff --git a/src/_h5ai/private/php/ext/class-thumb.php b/src/_h5ai/private/php/ext/class-thumb.php index 0174a607..224d05ab 100644 --- a/src/_h5ai/private/php/ext/class-thumb.php +++ b/src/_h5ai/private/php/ext/class-thumb.php @@ -25,7 +25,7 @@ class Thumb { } } - public function thumb($type, $source_href, $width, $height) { + public function thumb($type, $source_href) { $source_path = $this->context->to_path($source_href); if (!file_exists($source_path) || Util::starts_with($source_path, $this->setup->get('CACHE_PUB_PATH'))) { return null; @@ -49,7 +49,7 @@ class Thumb { $capture_path = $source_path; } - return $this->thumb_href($capture_path, $width, $height); + return $this->thumb_href($capture_path, $this->context->thumbnail_width, $this->context->thumbnail_height); } private function thumb_href($source_path, $width, $height) { diff --git a/src/_h5ai/public/css/lib/view/view.less b/src/_h5ai/public/css/lib/view/view.less index aef93ecb..1be5170d 100644 --- a/src/_h5ai/public/css/lib/view/view.less +++ b/src/_h5ai/public/css/lib/view/view.less @@ -48,6 +48,8 @@ .thumb { max-width: none; max-height: none; + object-fit: cover; + object-position: 50% 50%; } } diff --git a/src/_h5ai/public/js/lib/ext/thumbnails.js b/src/_h5ai/public/js/lib/ext/thumbnails.js index d71898cc..d92c1455 100644 --- a/src/_h5ai/public/js/lib/ext/thumbnails.js +++ b/src/_h5ai/public/js/lib/ext/thumbnails.js @@ -13,7 +13,6 @@ const settings = Object.assign({ exif: false, chunksize: 20 }, allsettings.thumbnails); -const landscapeRatio = 4 / 3; const queueItem = (queue, item) => { @@ -35,7 +34,6 @@ const queueItem = (queue, item) => { queue.push({ type, href: item.absHref, - ratio: 1, callback: src => { if (src && item.$view) { item.thumbSquare = src; @@ -51,7 +49,6 @@ const queueItem = (queue, item) => { queue.push({ type, href: item.absHref, - ratio: landscapeRatio, callback: src => { if (src && item.$view) { item.thumbRational = src; @@ -67,8 +64,6 @@ const requestQueue = queue => { return { type: req.type, href: req.href, - width: Math.round(settings.size * req.ratio), - height: settings.size }; });