From 5278734fe2675dde3a4eaabff0a35d2488acbd9a Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 22 Jan 2024 12:10:29 +0100 Subject: [PATCH] FIX: Ignore invalid images when shrinking uploads (#25346) --- lib/shrink_uploaded_image.rb | 6 +++++- spec/lib/shrink_uploaded_image_spec.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/shrink_uploaded_image.rb b/lib/shrink_uploaded_image.rb index f46a3b72073..042a9fcce3f 100644 --- a/lib/shrink_uploaded_image.rb +++ b/lib/shrink_uploaded_image.rb @@ -40,7 +40,11 @@ class ShrinkUploadedImage return false end - w, h = FastImage.size(path, timeout: 15, raise_on_failure: true) + begin + w, h = FastImage.size(path, timeout: 15, raise_on_failure: true) + rescue FastImage::SizeNotFound + return false + end if !w || !h log "Invalid image dimensions after resizing" diff --git a/spec/lib/shrink_uploaded_image_spec.rb b/spec/lib/shrink_uploaded_image_spec.rb index 58b5522a69d..06f0076a0ce 100644 --- a/spec/lib/shrink_uploaded_image_spec.rb +++ b/spec/lib/shrink_uploaded_image_spec.rb @@ -110,6 +110,21 @@ RSpec.describe ShrinkUploadedImage do expect(result).to be(false) end + + it "returns false if the image is invalid" do + post = Fabricate(:post, raw: "") + post.link_post_uploads + FastImage.stubs(:size).raises(FastImage::SizeNotFound.new) + + result = + ShrinkUploadedImage.new( + upload: upload, + path: Discourse.store.path_for(upload), + max_pixels: 10_000, + ).perform + + expect(result).to be(false) + end end context "when S3 uploads are enabled" do