A platform for community discussion. Free, open, simple.
FEATURE: add `system_user_max_attachment_size_kb` site setting (#28351)
Guhyoun Nam9c1812e071b9
diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rbindex 1956db8184d..de80ba88212 100644--- a/app/controllers/uploads_controller.rb+++ b/app/controllers/uploads_controller.rb@@ -25,7 +25,6 @@ class UploadsController < ApplicationControllerdef create# capture current user for block later onme = current_user-RateLimiter.new(current_user,"uploads-per-minute",@@ -228,7 +227,7 @@ class UploadsController < ApplicationController"upload.attachments.too_large_humanized",max_size:ActiveSupport::NumberHelper.number_to_human_size(- SiteSetting.max_attachment_size_kb.kilobytes,+ UploadsController.max_attachment_size_for_user(current_user).kilobytes,),),)@@ -277,7 +276,7 @@ class UploadsController < ApplicationControllerif url.present? && is_apimaximum_upload_size = [SiteSetting.max_image_size_kb,- SiteSetting.max_attachment_size_kb,+ UploadsController.max_attachment_size_for_user(current_user),].max.kilobytestempfile =begin@@ -319,12 +318,20 @@ class UploadsController < ApplicationControllerprivate+ def self.max_attachment_size_for_user(user)+ if user.id == Discourse::SYSTEM_USER_ID && !SiteSetting.system_user_max_attachment_size_kb.zero?+ SiteSetting.system_user_max_attachment_size_kb+ else+ SiteSetting.max_attachment_size_kb+ end+ end+# We can preemptively check size for attachments, but not for (most) images# as they may be further reduced in size by UploadCreator (at this point# they may have already been reduced in size by preprocessors)def attachment_too_big?(file_name, file_size)!FileHelper.is_supported_image?(file_name) &&- file_size >= SiteSetting.max_attachment_size_kb.kilobytes+ file_size >= UploadsController.max_attachment_size_for_user(current_user).kilobytesend# Gifs are not resized on the client and not reduced in size by UploadCreatordiff --git a/config/site_settings.yml b/config/site_settings.ymlindex 2399d5344ce..8d7c7747afa 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1490,6 +1490,11 @@ files:default: 4096max: 1024000type: file_size_restriction+ system_user_max_attachment_size_kb:+ default: 0+ max: 4096000+ type: file_size_restriction+ hidden: truemax_image_megapixels:default: 40min: 5diff --git a/lib/validators/upload_validator.rb b/lib/validators/upload_validator.rbindex 3ff057ce0ae..d85068a1d18 100644--- a/lib/validators/upload_validator.rb+++ b/lib/validators/upload_validator.rb@@ -146,7 +146,14 @@ class UploadValidator < ActiveModel::Validatorif upload.for_exportSiteSetting.max_export_file_size_kbelse- SiteSetting.get("max_#{type}_size_kb")+ if upload.user&.id == Discourse::SYSTEM_USER_ID && type == "attachment"+ [+ SiteSetting.get("system_user_max_attachment_size_kb"),+ SiteSetting.get("max_attachment_size_kb"),+ ].max+ else+ SiteSetting.get("max_#{type}_size_kb")+ endendmax_size_bytes = max_size_kb.kilobytesdiff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rbindex 1bca77e3db7..13025ebab28 100644--- a/spec/requests/uploads_controller_spec.rb+++ b/spec/requests/uploads_controller_spec.rb@@ -2,6 +2,7 @@RSpec.describe UploadsController dofab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:system_user) { Discourse.system_user }describe "#create" doit "requires you to be logged in" do@@ -238,6 +239,75 @@ RSpec.describe UploadsController doexpect(message).to contain_exactly(I18n.t("upload.images.size_not_found"))endend++ context "when system user is logged in" do+ before { sign_in(system_user) }++ let(:text_file) { Rack::Test::UploadedFile.new(File.new("#{Rails.root}/LICENSE.txt")) }++ it "properly returns errors if system_user_max_attachment_size_kb is not set" do+ SiteSetting.authorized_extensions = "*"+ SiteSetting.max_attachment_size_kb = 1++ post "/uploads.json", params: { file: text_file, type: "composer" }++ expect(response.status).to eq(422)+ errors = response.parsed_body["errors"]+ expect(errors.first).to eq(+ I18n.t("upload.attachments.too_large_humanized", max_size: "1 KB"),+ )+ end++ it "should accept large files if system user" do+ SiteSetting.authorized_extensions = "*"+ SiteSetting.system_user_max_attachment_size_kb = 421_730++ post "/uploads.json", params: { file: text_file, type: "composer" }+ expect(response.status).to eq(200)+ end++ it "should fail to accept large files if system user system_user_max_attachment_size_kb setting is low" do+ SiteSetting.authorized_extensions = "*"+ SiteSetting.max_attachment_size_kb = 1+ SiteSetting.system_user_max_attachment_size_kb = 1++ post "/uploads.json", params: { file: text_file, type: "composer" }++ expect(response.status).to eq(422)+ errors = response.parsed_body["errors"]+ expect(errors.first).to eq(+ I18n.t("upload.attachments.too_large_humanized", max_size: "1 KB"),+ )+ end++ it "should fail to accept large files if system user system_user_max_attachment_size_kb setting is low and general setting is low" do+ SiteSetting.authorized_extensions = "*"+ SiteSetting.max_attachment_size_kb = 10+ SiteSetting.system_user_max_attachment_size_kb = 5++ post "/uploads.json", params: { file: text_file, type: "composer" }++ expect(response.status).to eq(422)+ errors = response.parsed_body["errors"]+ expect(errors.first).to eq(+ I18n.t("upload.attachments.too_large_humanized", max_size: "10 KB"),+ )+ end++ it "should fail to accept large files if attachment_size settings are low" do+ SiteSetting.authorized_extensions = "*"+ SiteSetting.max_attachment_size_kb = 1+ SiteSetting.system_user_max_attachment_size_kb = 10++ post "/uploads.json", params: { file: text_file, type: "composer" }++ expect(response.status).to eq(422)+ errors = response.parsed_body["errors"]+ expect(errors.first).to eq(+ I18n.t("upload.attachments.too_large_humanized", max_size: "10 KB"),+ )+ end+ endenddef upload_file(file, folder = "images")