Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Harden imagemagick execution

David Taylorca5a7e061675
diff --git a/app/models/upload.rb b/app/models/upload.rb
index 559332d7025..415f6d4ccc8 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -311,7 +311,7 @@ class Upload < ActiveRecord::Base
"-ping",
"-format",
"%w %h",
- path,
+ "MSVG:#{path}",
timeout: MAX_IDENTIFY_SECONDS,
).split(" ")
rescue StandardError
diff --git a/config/imagemagick/policy.xml b/config/imagemagick/policy.xml
new file mode 100644
index 00000000000..ce4640d67fa
--- /dev/null
+++ b/config/imagemagick/policy.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Loaded via MAGICK_CONFIGURE_PATH, set in config/initializers/003-imagemagick.rb.
+-->
+<policymap>
+ <policy domain="resource" name="memory" value="1GiB"/>
+ <policy domain="resource" name="map" value="2GiB"/>
+ <policy domain="resource" name="area" value="4GP"/>
+ <policy domain="resource" name="disk" value="8GiB"/>
+ <policy domain="resource" name="width" value="64KP"/>
+ <policy domain="resource" name="height" value="64KP"/>
+ <policy domain="resource" name="list-length" value="512"/>
+ <policy domain="resource" name="time" value="300"/>
+
+ <!-- no external delegate programs (Ghostscript, ffmpeg, ...) -->
+ <policy domain="delegate" rights="none" pattern="*"/>
+
+ <!-- no image filters -->
+ <policy domain="filter" rights="none" pattern="*"/>
+
+ <!-- block stdio/fd pipes, /etc, traversal, and @ file indirection -->
+ <policy domain="path" rights="none" pattern="-"/>
+ <policy domain="path" rights="none" pattern="fd:*"/>
+ <policy domain="path" rights="none" pattern="/etc/*"/>
+ <policy domain="path" rights="none" pattern="*../*"/>
+ <policy domain="path" rights="none" pattern="@*"/>
+
+ <!-- deny all coders, then allow only the formats Discourse uses -->
+ <policy domain="coder" rights="none" pattern="*"/>
+ <!-- upload/thumbnail raster formats -->
+ <policy domain="coder" rights="read|write" pattern="{GIF,JPEG,JPG,PNG,WEBP,AVIF,ICO}"/>
+ <!-- HEIC/HEIF: UploadCreator#convert_heif! transcodes to JPEG -->
+ <policy domain="coder" rights="read|write" pattern="{HEIC,HEIF}"/>
+ <!-- MSVG: SVG dimension reads; the plain SVG coder stays denied -->
+ <policy domain="coder" rights="read|write" pattern="MSVG"/>
+ <!-- XC: LetterAvatar canvas -->
+ <policy domain="coder" rights="read|write" pattern="XC"/>
+ <!-- ICC/ICM: RT_sRGB.icm "-profile" on resize/crop/downsize -->
+ <policy domain="coder" rights="read|write" pattern="{ICC,ICM}"/>
+ <!-- HISTOGRAM/INFO: Upload#calculate_dominant_color! -->
+ <policy domain="coder" rights="read|write" pattern="{HISTOGRAM,INFO}"/>
+
+ <policy domain="system" name="symlink" rights="none" pattern="follow"/>
+</policymap>
diff --git a/config/initializers/003-imagemagick.rb b/config/initializers/003-imagemagick.rb
new file mode 100644
index 00000000000..241ad4f35a7
--- /dev/null
+++ b/config/initializers/003-imagemagick.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+# Point ImageMagick at Discourse's security policy (config/imagemagick/policy.xml).
+# Child processes inherit this env var, so it covers every identify/magick/convert
+# call. Refuse to boot if it points somewhere else, since overriding it would
+# bypass the policy. Our own value is allowed (the app may boot more than once in
+# a process tree, e.g. parallel test workers).
+path = Rails.root.join("config/imagemagick").to_s
+
+if ENV["MAGICK_CONFIGURE_PATH"] && ENV["MAGICK_CONFIGURE_PATH"] != path
+ raise "MAGICK_CONFIGURE_PATH must not be set externally; Discourse manages it " \
+ "to enforce its ImageMagick security policy (config/imagemagick/policy.xml)."
+end
+
+ENV["MAGICK_CONFIGURE_PATH"] = path
diff --git a/plugins/discourse-ai/lib/utils/pdf_to_text.rb b/plugins/discourse-ai/lib/utils/pdf_to_text.rb
index f581f950700..8eb25007cf2 100644
--- a/plugins/discourse-ai/lib/utils/pdf_to_text.rb
+++ b/plugins/discourse-ai/lib/utils/pdf_to_text.rb
@@ -60,65 +60,6 @@ class DiscourseAi::Utils::PdfToText
require "pdf/reader"
- page_number = 0
- PDF::Reader.open(pdf_path) do |reader|
- reader.pages.each do |page|
- page_number += 1
- llm_decorate(page_number: page_number, text: page.text, pdf_path: pdf_path) do |chunk|
- yield chunk
- end
- end
- end
- end
-
- def llm_decorate(page_number:, text:, pdf_path:)
- raise "Must be called with block" if !block_given?
- if !@llm_model
- yield text
- return
- end
-
- begin
- temp_dir = Dir.mktmpdir("discourse-pdf-#{SecureRandom.hex(8)}")
- output_path = File.join(temp_dir, "page-#{page_number}.png")
-
- # Extract specific page using ImageMagick
- # image magick uses 0 based page numbers
- command = [
- "magick",
- "-density",
- "300",
- "#{pdf_path}[#{page_number - 1}]",
- "-background",
- "white",
- "-auto-orient",
- "-quality",
- "85",
- output_path,
- ]
-
- Discourse::Utils.execute_command(
- *command,
- failure_message: "Failed to convert PDF page #{page_number} to image",
- timeout: 30,
- )
-
- # TODO - we are creating leftover uploads, they will be cleaned up
- # but maybe we should just keep them around?
- upload =
- UploadCreator.new(File.open(output_path), "page-#{page_number}.png").create_for(@user&.id)
-
- DiscourseAi::Utils::ImageToText
- .new(
- upload: upload,
- llm_model: @llm_model,
- user: @user,
- guidance_text: text,
- execution_context: @execution_context,
- )
- .extract_text { |chunk| yield chunk }
- ensure
- FileUtils.rm_rf(temp_dir) if temp_dir
- end
+ PDF::Reader.open(pdf_path) { |reader| reader.pages.each { |page| yield page.text } }
end
end
diff --git a/plugins/discourse-ai/spec/lib/utils/pdf_to_text_spec.rb b/plugins/discourse-ai/spec/lib/utils/pdf_to_text_spec.rb
index 8d59c9c011c..3375550d150 100644
--- a/plugins/discourse-ai/spec/lib/utils/pdf_to_text_spec.rb
+++ b/plugins/discourse-ai/spec/lib/utils/pdf_to_text_spec.rb
@@ -1,8 +1,6 @@
# frozen_string_literal: true
RSpec.describe DiscourseAi::Utils::PdfToText do
- fab!(:llm_model)
- fab!(:user)
let(:pdf) { plugin_file_from_fixtures("2-page.pdf", "rag") }
let(:upload) { UploadCreator.new(pdf, "2-page.pdf").create_for(Discourse.system_user.id) }
@@ -20,49 +18,4 @@ RSpec.describe DiscourseAi::Utils::PdfToText do
expect(pages).to eq(["Page 1", "Page 2"])
end
end
-
- context "when improving PDF extraction with LLM" do
- def imagemagick_with_ghostscript?
- system("magick", "-version", out: File::NULL, err: File::NULL) &&
- system("gs", "--version", out: File::NULL, err: File::NULL)
- end
-
- it "can properly simulate a file" do
- skip "Requires imagemagick with ghostscript support" unless imagemagick_with_ghostscript?
-
- responses = [
- "<chunk>Page 1: LLM chunk 1</chunk><chunk>Page 1: LLM chunk 2</chunk>",
- "<chunk>Page 2: LLM chunk 3</chunk>",
- ]
-
- pages = []
- DiscourseAi::Completions::Llm.with_prepared_responses(responses) do |_, _, _prompts|
- file = described_class.as_fake_file(upload: upload, user: user, llm_model: llm_model)
-
- while content = file.read(100_000)
- pages << content
- end
- end
-
- expect(pages).to eq(["Page 1: LLM chunk 1", "Page 1: LLM chunk 2", "Page 2: LLM chunk 3"])
- end
-
- it "works as expected" do
- skip "Requires imagemagick with ghostscript support" unless imagemagick_with_ghostscript?
-
- pdf_to_text = described_class.new(upload: upload, user: user, llm_model: llm_model)
- pages = []
-
- responses = [
- "<chunk>Page 1: LLM chunk 1</chunk><chunk>Page 1: LLM chunk 2</chunk>",
- "<chunk>Page 2: LLM chunk 3</chunk>",
- ]
-
- DiscourseAi::Completions::Llm.with_prepared_responses(responses) do |_, _, _prompts|
- pdf_to_text.extract_text { |page| pages << page }
- end
-
- expect(pages).to eq(["Page 1: LLM chunk 1", "Page 1: LLM chunk 2", "Page 2: LLM chunk 3"])
- end
- end
end
diff --git a/spec/initializers/imagemagick_policy_spec.rb b/spec/initializers/imagemagick_policy_spec.rb
new file mode 100644
index 00000000000..5db996c808b
--- /dev/null
+++ b/spec/initializers/imagemagick_policy_spec.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+# Guards config/imagemagick/policy.xml. A malformed policy fails open silently
+# (e.g. a stray backtick drops every rule after it), so assert it is parsed and
+# enforced.
+RSpec.describe "ImageMagick security policy" do
+ it "loads the coder allowlist" do
+ policy = Discourse::Utils.execute_command("magick", "-list", "policy")
+
+ expect(policy).to match(/Policy: Coder/i)
+ expect(policy).to include("MSVG")
+ end
+
+ it "blocks a coder that is not on the allowlist" do
+ png = Rails.root.join("spec/fixtures/images/logo.png").to_s
+
+ expect { Discourse::Utils.execute_command("identify", "TIFF:#{png}") }.to raise_error(
+ Discourse::Utils::CommandError,
+ /not (allowed|authorized) by the security policy/,
+ )
+ end
+end
diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb
index 8fd9a424e18..a58c0d02e91 100644
--- a/spec/models/optimized_image_spec.rb
+++ b/spec/models/optimized_image_spec.rb
@@ -280,12 +280,6 @@ RSpec.describe OptimizedImage do
expect(File.read(Discourse.store.path_for(resized))).to eq(
File.read(Discourse.store.path_for(upload)),
)
-
- resized = upload.get_optimized_image(50, 50, format: "gif", raise_on_error: true)
- expect(resized.extension).to eq(".gif")
- # lets ensure we have a gif with the identify tool
- path = Shellwords.escape(Discourse.store.path_for(resized))
- expect(`identify -format %m #{path}`.strip).to eq("GIF")
end
context "when using an internal store" do