A platform for community discussion. Free, open, simple.
FIX: automatically timeout long running image magick commands (#12670)
Sam5deda5ef3ef8
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rbindex e32f15a703c..228b3dc54be 100644--- a/app/models/optimized_image.rb+++ b/app/models/optimized_image.rb@@ -306,9 +306,10 @@ class OptimizedImage < ActiveRecord::BaseendMAX_PNGQUANT_SIZE = 500_000+ MAX_CONVERT_SECONDS = 20def self.convert_with(instructions, to, opts = {})- Discourse::Utils.execute_command("nice", "-n", "10", *instructions)+ Discourse::Utils.execute_command("nice", "-n", "10", *instructions, timeout: MAX_CONVERT_SECONDS)allow_pngquant = to.downcase.ends_with?(".png") && File.size(to) < MAX_PNGQUANT_SIZEFileHelper.optimize_image!(to, allow_pngquant: allow_pngquant)diff --git a/app/models/upload.rb b/app/models/upload.rbindex bb6ba200174..0f1902c21be 100644--- a/app/models/upload.rb+++ b/app/models/upload.rb@@ -13,6 +13,7 @@ class Upload < ActiveRecord::BaseSHA1_LENGTH = 40SEEDED_ID_THRESHOLD = 0URL_REGEX ||= /(\/original\/\dX[\/\.\w]*\/(\h+)[\.\w]*)/+ MAX_IDENTIFY_SECONDS = 5belongs_to :userbelongs_to :access_control_post, class_name: 'Post'@@ -225,7 +226,7 @@ class Upload < ActiveRecord::Basebeginif extension == 'svg'- w, h = Discourse::Utils.execute_command("identify", "-format", "%w %h", path).split(' ') rescue [0, 0]+ w, h = Discourse::Utils.execute_command("identify", "-format", "%w %h", path, timeout: MAX_IDENTIFY_SECONDS).split(' ') rescue [0, 0]elsew, h = FastImage.new(path, raise_on_failure: true).sizeend@@ -274,7 +275,7 @@ class Upload < ActiveRecord::Baseenddef target_image_quality(local_path, test_quality)- @file_quality ||= Discourse::Utils.execute_command("identify", "-format", "%Q", local_path).to_i rescue 0+ @file_quality ||= Discourse::Utils.execute_command("identify", "-format", "%Q", local_path, timeout: MAX_IDENTIFY_SECONDS).to_i rescue 0if @file_quality == 0 || @file_quality > test_qualitytest_qualitydiff --git a/lib/discourse.rb b/lib/discourse.rbindex 5224b2aac2f..12f89a9c010 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -95,7 +95,13 @@ module Discourseprivate- def execute_command(*command, failure_message: "", success_status_codes: [0], chdir: ".")+ def execute_command(*command, timeout: nil, failure_message: "", success_status_codes: [0], chdir: ".")+ if timeout+ # will send a TERM after timeout+ # will send a KILL after timeout * 2+ command = ["timeout", "-k", "#{timeout.to_f * 2}", timeout.to_s] + command+ end+stdout, stderr, status = Open3.capture3(*command, chdir: chdir)if !status.exited? || !success_status_codes.include?(status.exitstatus)diff --git a/lib/theme_store/git_importer.rb b/lib/theme_store/git_importer.rbindex 37fb49e6cdf..553016b07f3 100644--- a/lib/theme_store/git_importer.rb+++ b/lib/theme_store/git_importer.rb@@ -82,7 +82,7 @@ class ThemeStore::GitImporterelseDiscourse::Utils.execute_command("git", "clone", @url, @temp_folder)end- rescue RuntimeError => err+ rescue RuntimeErrorraise RemoteTheme::ImportError.new(I18n.t("themes.import_error.git"))endenddiff --git a/lib/upload_creator.rb b/lib/upload_creator.rbindex bd9cadcee92..63364e7b4f1 100644--- a/lib/upload_creator.rb+++ b/lib/upload_creator.rb@@ -125,7 +125,15 @@ class UploadCreatorif is_imageif @image_info.type.to_s == 'svg'- w, h = Discourse::Utils.execute_command("identify", "-format", "%w %h", @file.path).split(' ') rescue [0, 0]+ w, h = [0, 0]++ begin+ w, h = Discourse::Utils+ .execute_command("identify", "-format", "%w %h", @file.path, timeout: Upload::MAX_IDENTIFY_SECONDS)+ .split(' ')+ rescue+ # use default 0, 0+ endelsew, h = @image_info.sizeend@@ -264,6 +272,7 @@ class UploadCreatorextract_image_info!end+ MAX_CONVERT_FORMAT_SECONDS = 20def execute_convert(from, to, opts = {})command = ["convert",@@ -277,7 +286,11 @@ class UploadCreatorcommand << "-quality" << opts[:quality].to_s if opts[:quality]command << to- Discourse::Utils.execute_command(*command, failure_message: I18n.t("upload.png_to_jpg_conversion_failure_message"))+ Discourse::Utils.execute_command(+ *command,+ failure_message: I18n.t("upload.png_to_jpg_conversion_failure_message"),+ timeout: MAX_CONVERT_FORMAT_SECONDS+ )enddef should_alter_quality?@@ -354,13 +367,14 @@ class UploadCreator@image_info.orientation.to_i > 1end+ MAX_FIX_ORIENTATION_TIME = 5def fix_orientation!path = @file.pathOptimizedImage.ensure_safe_paths!(path)path = OptimizedImage.prepend_decoder!(path, nil, filename: "image.#{@image_info.type}")- Discourse::Utils.execute_command('convert', path, '-auto-orient', path)+ Discourse::Utils.execute_command('convert', path, '-auto-orient', path, timeout: MAX_FIX_ORIENTATION_TIME)extract_image_info!end@@ -458,7 +472,7 @@ class UploadCreatorOptimizedImage.ensure_safe_paths!(@file.path)command = ["identify", "-format", "%n\\n", @file.path]- frames = Discourse::Utils.execute_command(*command).to_i rescue 1+ frames = Discourse::Utils.execute_command(*command, timeout: Upload::MAX_IDENTIFY_SECONDS).to_i rescue 1frames > 1elsediff --git a/spec/components/discourse_spec.rb b/spec/components/discourse_spec.rbindex fe8db27fa19..6f1b294733c 100644--- a/spec/components/discourse_spec.rb+++ b/spec/components/discourse_spec.rb@@ -398,6 +398,12 @@ describe Discourse doexpect(Discourse::Utils.execute_command("pwd", chdir: "plugins").strip).to eq("#{Rails.root.to_s}/plugins")end+ it "supports timeouts" do+ expect do+ Discourse::Utils.execute_command("sleep", "999999999999", timeout: 0.001)+ end.to raise_error(RuntimeError)+ end+it "works with a block" doDiscourse::Utils.execute_command do |runner|expect(runner.exec("pwd").strip).to eq(Rails.root.to_s)