Skip to content

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

DEV: Read SVG upload dimensions with the opt-in libvips worker (#43454)

Alan Guo Xiang Tanf15bf9887c71
diff --git a/app/models/upload.rb b/app/models/upload.rb
index d73814f1fd3..486d8731994 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -306,15 +306,19 @@ class Upload < ActiveRecord::Base
if extension == "svg"
w, h =
begin
- ImageMagick.identify(
- "-ping",
- "-format",
- "%w %h",
- "MSVG:#{path}",
- operation: :upload_svg_dimensions,
- read: [path],
- timeout: MAX_IDENTIFY_SECONDS,
- ).split(" ")
+ if GlobalSetting.enable_vips_image_processing
+ DiscourseVips.svg_dimensions(input_path: path, timeout: MAX_IDENTIFY_SECONDS)
+ else
+ ImageMagick.identify(
+ "-ping",
+ "-format",
+ "%w %h",
+ "MSVG:#{path}",
+ operation: :upload_svg_dimensions,
+ read: [path],
+ timeout: MAX_IDENTIFY_SECONDS,
+ ).split(" ")
+ end
rescue StandardError
[0, 0]
end
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index 7119d3b27bb..b864bcdc3e3 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -3,6 +3,9 @@
require_relative "discourse_vips/client"
module DiscourseVips
+ SVG_DIMENSIONS_TIMEOUT_SECONDS = 3
+ private_constant :SVG_DIMENSIONS_TIMEOUT_SECONDS
+
def self.version
Client.call(["version"], operation: :vips_version)
end
@@ -23,6 +26,11 @@ module DiscourseVips
)
end
+ def self.svg_dimensions(input_path:, timeout:)
+ timeout = [timeout, SVG_DIMENSIONS_TIMEOUT_SECONDS].min
+ Client.call(["svg-dimensions", input_path], operation: :upload_svg_dimensions, timeout:)
+ end
+
# Uses GIF/WebP frame counts and HEIF/AVIF image collections.
# APNG and timed HEIF/AVIF sequences are unsupported.
def self.animated?(input_path:, timeout:)
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 438f1841f5f..40b03b6e79f 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -190,24 +190,31 @@ class UploadCreator
if @image_info.type.to_s == "svg"
w, h = [0, 0]
- # identify can behave differently depending on how it's compiled and
- # what programs (e.g. inkscape) are installed on your system.
- # 'MSVG:' forces ImageMagick to use internal routines and behave
- # consistently whether it's running from our docker container or not
begin
w, h =
- ImageMagick
- .identify(
- "-ping",
- "-format",
- "%w %h",
- "MSVG:#{@file.path}",
- operation: :upload_svg_dimensions,
- read: [@file.path],
+ if GlobalSetting.enable_vips_image_processing
+ DiscourseVips.svg_dimensions(
+ input_path: @file.path,
timeout: Upload::MAX_IDENTIFY_SECONDS,
)
- .split(" ")
- .map(&:to_i)
+ else
+ # identify can behave differently depending on how it's compiled and
+ # what programs (e.g. inkscape) are installed on your system.
+ # 'MSVG:' forces ImageMagick to use internal routines and behave
+ # consistently whether it's running from our docker container or not
+ ImageMagick
+ .identify(
+ "-ping",
+ "-format",
+ "%w %h",
+ "MSVG:#{@file.path}",
+ operation: :upload_svg_dimensions,
+ read: [@file.path],
+ timeout: Upload::MAX_IDENTIFY_SECONDS,
+ )
+ .split(" ")
+ .map(&:to_i)
+ end
rescue StandardError
# use default 0, 0
end
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index cfa0bf65c65..7f2b9a9cccb 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -27,6 +27,9 @@ class DiscourseVipsWorker
WARM_OPERATIONS = %w[text gravity flatten pngsave thumbnail].freeze
private_constant :WARM_OPERATIONS
+ SVG_DIMENSIONS_RLIMITS = { cpu_seconds: 1, memory_bytes: 1024 * 1024 * 1024 }.freeze
+ private_constant :SVG_DIMENSIONS_RLIMITS
+
def self.warm
WARM_OPERATIONS.each do |name|
operation = Vips.vips_operation_new(name)
@@ -50,14 +53,17 @@ class DiscourseVipsWorker
animated(*arguments)
when "heif-to-jpeg"
heif_to_jpeg(input_path: arguments[0], output_path: arguments[1])
+ when "svg-dimensions"
+ svg_dimensions(*arguments)
else
raise ArgumentError, "unsupported libvips operation"
end
end
- def initialize(read_paths: [], write_paths: [], &action)
+ def initialize(read_paths: [], write_paths: [], rlimits: {}, &action)
@read_paths = read_paths
@write_paths = write_paths
+ @rlimits = rlimits
@action = action
end
@@ -72,6 +78,10 @@ class DiscourseVipsWorker
}
end
+ def sandbox_rlimits(defaults:)
+ defaults.merge(@rlimits)
+ end
+
def self.version
new { Vips.version_string }
end
@@ -176,6 +186,22 @@ class DiscourseVipsWorker
end
private_class_method :heif_to_jpeg
+ def self.svg_dimensions(input_path)
+ new(read_paths: [input_path], rlimits: SVG_DIMENSIONS_RLIMITS) do
+ raise ArgumentError, "invalid SVG dimensions arguments" if !input_path
+
+ block_loaders(allowed: %w[VipsForeignLoadSvg])
+ begin
+ Vips.concurrency_set(1)
+ image = Vips::Image.svgload(input_path, dpi: 96, scale: 0.75)
+ [image.width, image.height]
+ rescue Vips::Error => error
+ raise InvalidImage, error.message
+ end
+ end
+ end
+ private_class_method :svg_dimensions
+
def self.normalize_color(image)
image = image.colourspace(:srgb)
image = (image * 255).round(:rint) if %i[float double].include?(image.format)
@@ -304,7 +330,7 @@ class DiscourseVipsWorker
timeout: Float(request.fetch("timeout")),
env: child_environment(scratch),
unsetenv_others: true,
- rlimits: RLIMITS,
+ rlimits: operation.sandbox_rlimits(defaults: RLIMITS),
seccomp_deny_network: LINUX_PLATFORM,
on_unsupported: :run_without_landlock,
) do |stdout, _stderr|
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index c9da77a57bb..06ce125fe6c 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -171,6 +171,131 @@ RSpec.describe DiscourseVips do
end
end
+ describe ".svg_dimensions" do
+ it "returns explicit SVG dimensions" do
+ input_path = file_from_fixtures("image.svg").path
+
+ expect(described_class.svg_dimensions(input_path:, timeout: 5)).to eq([100, 50])
+ end
+
+ it "rounds fractional SVG dimensions" do
+ input_path = file_from_fixtures("tiny.svg").path
+
+ expect(described_class.svg_dimensions(input_path:, timeout: 5)).to eq([115, 86])
+ end
+
+ it "returns large SVG dimensions" do
+ input_path = file_from_fixtures("massive.svg").path
+
+ expect(described_class.svg_dimensions(input_path:, timeout: 5)).to eq([11_520, 11_615])
+ end
+
+ it "uses the viewBox when the SVG has no explicit dimensions" do
+ file =
+ file_from_contents(
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 90"/>',
+ "viewbox.svg",
+ )
+
+ dimensions = described_class.svg_dimensions(input_path: file.path, timeout: 5)
+
+ expect(dimensions).to eq([120, 90])
+ end
+
+ it "rejects a zero-sized SVG with surrounding viewBox whitespace" do
+ file =
+ file_from_contents(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" viewBox=" 0 0 120 90 "/>',
+ "whitespace-viewbox.svg",
+ )
+
+ expect { described_class.svg_dimensions(input_path: file.path, timeout: 5) }.to raise_error(
+ DiscourseVips::InvalidImage,
+ )
+ end
+
+ it "rejects a zero-sized SVG without a viewBox" do
+ file =
+ file_from_contents(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"/>',
+ "zero.svg",
+ )
+
+ expect { described_class.svg_dimensions(input_path: file.path, timeout: 5) }.to raise_error(
+ DiscourseVips::InvalidImage,
+ )
+ end
+
+ it "rejects an SVG with zero width" do
+ file =
+ file_from_contents(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="0" height="60" viewBox="0 0 120 90"/>',
+ "zero-width.svg",
+ )
+
+ expect { described_class.svg_dimensions(input_path: file.path, timeout: 5) }.to raise_error(
+ DiscourseVips::InvalidImage,
+ )
+ end
+
+ it "rejects an SVG with zero height" do
+ file =
+ file_from_contents(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="80" height="0" viewBox="0 0 120 90"/>',
+ "zero-height.svg",
+ )
+
+ expect { described_class.svg_dimensions(input_path: file.path, timeout: 5) }.to raise_error(
+ DiscourseVips::InvalidImage,
+ )
+ end
+
+ it "rejects non-SVG images" do
+ expect {
+ described_class.svg_dimensions(
+ input_path: file_from_fixtures("cropped.png").path,
+ timeout: 5,
+ )
+ }.to raise_error(DiscourseVips::InvalidImage)
+ end
+
+ it "rejects malformed SVGs" do
+ file = file_from_contents('<svg width="100" height="50">', "invalid.svg")
+
+ expect { described_class.svg_dimensions(input_path: file.path, timeout: 5) }.to raise_error(
+ DiscourseVips::InvalidImage,
+ )
+ end
+
+ it "bounds dimensionless SVG filter graphs and keeps the worker available" do
+ filter_primitives =
+ 10_000
+ .times
+ .map do |index|
+ input = index.zero? ? "SourceGraphic" : "blur#{index - 1}"
+ %(<feGaussianBlur in="#{input}" result="blur#{index}" stdDeviation="5"/>)
+ end
+ .join
+ svg = <<~SVG
+ <svg xmlns="http://www.w3.org/2000/svg">
+ <defs><filter id="dense">#{filter_primitives}</filter></defs>
+ <rect width="300" height="100" fill="#ff0000" filter="url(#dense)"/>
+ </svg>
+ SVG
+ file = file_from_contents(svg, "dimensionless-dense-filter.svg")
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+
+ expect { described_class.svg_dimensions(input_path: file.path, timeout: 10) }.to raise_error(
+ DiscourseVips::Error,
+ "libvips operation failed",
+ ) { |error| expect(error).not_to be_a(DiscourseVips::OperationTimeout) }
+
+ elapsed_seconds = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at
+ expect(elapsed_seconds).to be < 2.5
+ expect(described_class.version).to match(/\A\d+\.\d+\.\d+\z/)
+ end
+ end
+
describe "worker lifecycle" do
it "recovers after the worker exits unexpectedly" do
described_class.version
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index 00ee4d5dba9..5b3d2be2549 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -933,57 +933,86 @@ RSpec.describe UploadCreator do
expect(FastImage.size(stored_path)).to eq([1, 1])
end
end
- end
- describe "svg sizes expressed in units other than pixels" do
- let(:tiny_svg_filename) { "tiny.svg" }
- let(:tiny_svg_file) { file_from_fixtures(tiny_svg_filename) }
+ context "when reading SVG upload dimensions" do
+ shared_examples "SVG upload dimensions" do |expected_zero_dimensions|
+ let(:tiny_svg_filename) { "tiny.svg" }
+ let(:tiny_svg_file) { file_from_fixtures(tiny_svg_filename) }
- let(:massive_svg_filename) { "massive.svg" }
- let(:massive_svg_file) { file_from_fixtures(massive_svg_filename) }
+ let(:massive_svg_filename) { "massive.svg" }
+ let(:massive_svg_file) { file_from_fixtures(massive_svg_filename) }
- let(:zero_sized_svg_filename) { "zero_sized.svg" }
- let(:zero_sized_svg_file) { file_from_fixtures(zero_sized_svg_filename) }
+ let(:zero_sized_svg_filename) { "zero_sized.svg" }
+ let(:zero_sized_svg_file) { file_from_fixtures(zero_sized_svg_filename) }
- it "remains viewable when a dimension is fractional" do
- upload =
- UploadCreator.new(tiny_svg_file, tiny_svg_filename, force_optimize: true).create_for(
- user.id,
- )
+ it "remains viewable when a dimension is fractional" do
+ upload =
+ UploadCreator.new(tiny_svg_file, tiny_svg_filename, force_optimize: true).create_for(
+ user.id,
+ )
- expect(upload.width).to be > 50
- expect(upload.height).to be > 50
+ expect(upload.width).to be > 50
+ expect(upload.height).to be > 50
- expect(upload.thumbnail_width).to be <= SiteSetting.max_image_width
- expect(upload.thumbnail_height).to be <= SiteSetting.max_image_height
- end
+ expect(upload.thumbnail_width).to be <= SiteSetting.max_image_width
+ expect(upload.thumbnail_height).to be <= SiteSetting.max_image_height
+ end
- it "does not exceed the maximum thumbnail size" do
- upload =
- UploadCreator.new(massive_svg_file, massive_svg_filename, force_optimize: true).create_for(
- user.id,
- )
+ it "does not exceed the maximum thumbnail size" do
+ upload =
+ UploadCreator.new(
+ massive_svg_file,
+ massive_svg_filename,
+ force_optimize: true,
+ ).create_for(user.id)
- expect(upload.width).to be > 50
- expect(upload.height).to be > 50
+ expect(upload.width).to be > 50
+ expect(upload.height).to be > 50
- expect(upload.thumbnail_width).to be <= SiteSetting.max_image_width
- expect(upload.thumbnail_height).to be <= SiteSetting.max_image_height
- end
+ expect(upload.thumbnail_width).to be <= SiteSetting.max_image_width
+ expect(upload.thumbnail_height).to be <= SiteSetting.max_image_height
+ end
- it "handles files with a zero dimension" do
- upload =
- UploadCreator.new(
- zero_sized_svg_file,
- zero_sized_svg_filename,
- force_optimize: true,
- ).create_for(user.id)
+ it "stores the detected dimensions for a zero-sized SVG" do
+ upload =
+ UploadCreator.new(
+ zero_sized_svg_file,
+ zero_sized_svg_filename,
+ force_optimize: true,
+ ).create_for(user.id)
- expect(upload.width).to be > 50
- expect(upload.height).to be > 50
+ expect(upload).to be_persisted
+ expect([upload.width, upload.height]).to eq(expected_zero_dimensions)
- expect(upload.thumbnail_width).to be <= SiteSetting.max_image_width
- expect(upload.thumbnail_height).to be <= SiteSetting.max_image_height
+ expect([upload.thumbnail_width, upload.thumbnail_height]).to eq(expected_zero_dimensions)
+ end
+
+ it "stores zero dimensions when the SVG has no usable dimensions" do
+ file =
+ file_from_contents(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"/>',
+ "zero.svg",
+ )
+
+ upload = described_class.new(file, "zero.svg").create_for(user.id)
+
+ expect(upload).to be_persisted
+ expect([upload.width, upload.height]).to eq([0, 0])
+ expect([upload.thumbnail_width, upload.thumbnail_height]).to eq([0, 0])
+ end
+ end
+
+ context "with libvips disabled" do
+ before { global_setting :enable_vips_image_processing, false }
+
+ include_examples "SVG upload dimensions", [120, 90]
+ end
+
+ context "with libvips enabled" do
+ before { global_setting :enable_vips_image_processing, true }
+
+ include_examples "SVG upload dimensions", [0, 0]
+ end
end
end
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 6c7d9dc9697..c3bea94e064 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -262,6 +262,57 @@ RSpec.describe Upload do
expect(upload.thumbnail_height).to eq(500)
end
+ describe "#fix_dimensions!" do
+ shared_examples "SVG dimension repair" do |expected_zero_dimensions|
+ it "persists SVG dimensions and thumbnail dimensions" do
+ upload = Fabricate(:upload, extension: "svg", width: nil, height: nil)
+ file = file_from_fixtures("tiny.svg")
+ upload.update!(url: Discourse.store.store_upload(file, upload))
+
+ upload.fix_dimensions!
+
+ expect(
+ upload.reload.attributes.slice("width", "height", "thumbnail_width", "thumbnail_height"),
+ ).to eq("width" => 115, "height" => 86, "thumbnail_width" => 115, "thumbnail_height" => 86)
+ end
+
+ it "stores the detected dimensions for a zero-sized SVG" do
+ upload = Fabricate(:upload, extension: "svg", width: nil, height: nil)
+ file = file_from_fixtures("zero_sized.svg")
+ upload.update!(url: Discourse.store.store_upload(file, upload))
+
+ upload.fix_dimensions!
+
+ expect([upload.reload.width, upload.height]).to eq(expected_zero_dimensions)
+ expect([upload.thumbnail_width, upload.thumbnail_height]).to eq(expected_zero_dimensions)
+ end
+
+ it "stores zero dimensions when SVG dimension detection fails" do
+ upload = Fabricate(:upload, extension: "svg", width: nil, height: nil)
+ file = file_from_contents("invalid SVG", "invalid.svg")
+ upload.update!(url: Discourse.store.store_upload(file, upload))
+
+ upload.fix_dimensions!
+
+ expect(
+ upload.reload.attributes.slice("width", "height", "thumbnail_width", "thumbnail_height"),
+ ).to eq("width" => 0, "height" => 0, "thumbnail_width" => 0, "thumbnail_height" => 0)
+ end
+ end
+
+ context "with libvips disabled" do
+ before { global_setting :enable_vips_image_processing, false }
+
+ include_examples "SVG dimension repair", [120, 90]
+ end
+
+ context "with libvips enabled" do
+ before { global_setting :enable_vips_image_processing, true }
+
+ include_examples "SVG dimension repair", [0, 0]
+ end
+ end
+
it "dimension calculation returns nil on missing image" do
SiteSetting.max_image_megapixels = 85
upload = UploadCreator.new(huge_image, "image.png").create_for(user_id)