Skip to content

Commit

Permalink
FIX: Ignore invalid images when shrinking uploads (discourse#25346)
Browse files Browse the repository at this point in the history
  • Loading branch information
CvX authored Jan 22, 2024
1 parent ae2d9de commit 5278734
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/shrink_uploaded_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def perform
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"
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/shrink_uploaded_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@

expect(result).to be(false)
end

it "returns false if the image is invalid" do
post = Fabricate(:post, raw: "<img src='#{upload.url}'>")
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
Expand Down

0 comments on commit 5278734

Please sign in to comment.