uploads: add limits on max image dimensions.

This commit is contained in:
evazion
2018-03-25 18:05:52 -05:00
parent be0c2cfcfa
commit c0c41dd369
3 changed files with 37 additions and 0 deletions

View File

@@ -66,6 +66,18 @@ class Upload < ApplicationRecord
end
end
def validate_dimensions
resolution = image_width * image_height
if resolution > Danbooru.config.max_image_resolution
raise "image resolution is too large (resolution: #{(resolution / 1_000_000.0).round(1)} megapixels (#{image_width}x#{image_height}); max: #{Danbooru.config.max_image_resolution / 1_000_000} megapixels)"
elsif image_width > Danbooru.config.max_image_width
raise "image width is too large (width: #{image_width}; max width: #{Danbooru.config.max_image_width})"
elsif image_height > Danbooru.config.max_image_height
raise "image height is too large (height: #{image_height}; max height: #{Danbooru.config.max_image_height})"
end
end
def rating_given
if rating.present?
return true
@@ -120,6 +132,7 @@ class Upload < ApplicationRecord
self.tag_string = "#{tag_string} #{automatic_tags}"
self.image_width, self.image_height = calculate_dimensions
validate_dimensions
save
end

View File

@@ -179,6 +179,21 @@ module Danbooru
35.megabytes
end
# Maximum resolution (width * height) of an upload. Default: 441 megapixels (21000x21000 pixels).
def max_image_resolution
21000 * 21000
end
# Maximum width of an upload.
def max_image_width
40000
end
# Maximum height of an upload.
def max_image_height
40000
end
def member_comment_time_threshold
1.week.ago
end

View File

@@ -212,6 +212,15 @@ class UploadTest < ActiveSupport::TestCase
assert_equal("", @upload.automatic_tags)
end
end
context "that is too large" do
should "should fail validation" do
Danbooru.config.stubs(:max_image_resolution).returns(31*31)
@upload = FactoryGirl.create(:upload, file: upload_file("test/files/test-static-32x32.gif"))
@upload.process!
assert_match(/image resolution is too large/, @upload.status)
end
end
end
should "process completely for a pixiv ugoira" do