Skip to content

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

DEV: Security hardening of image processing write permissions (#43876)

Alan Guo Xiang Tan057cdbfb7ffb
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index d1605b326bc..c5701b9cc26 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -353,8 +353,6 @@ class OptimizedImage < ActiveRecord::Base
quality: opts[:quality],
strip_metadata: SiteSetting.strip_image_metadata,
timeout: MAX_CONVERT_SECONDS,
- read: [from],
- write: [File.dirname(to)],
)
optimize_image(to: to)
rescue => error
@@ -386,8 +384,6 @@ class OptimizedImage < ActiveRecord::Base
quality: opts[:quality],
strip_metadata: SiteSetting.strip_image_metadata,
timeout: MAX_CONVERT_SECONDS,
- read: [from],
- write: [File.dirname(to)],
)
optimize_image(to: to)
rescue => error
@@ -435,8 +431,6 @@ class OptimizedImage < ActiveRecord::Base
operation: :optimized_image_downsize,
strip_metadata: SiteSetting.strip_image_metadata,
timeout: MAX_CONVERT_SECONDS,
- read: [from],
- write: [File.dirname(to)],
)
optimize_image(to: to)
rescue => error
@@ -452,16 +446,20 @@ class OptimizedImage < ActiveRecord::Base
private_constant :INSTRUCTION_METHODS
def self.optimize(operation, from, to, dimensions, opts = {})
- instructions = public_send(INSTRUCTION_METHODS.fetch(operation), from, to, dimensions, opts)
+ instructions = nil
begin
- ImageMagick.magick(
- *instructions,
- operation: operation,
- read: [from],
- write: [File.dirname(to)],
- nice: 10,
- timeout: MAX_CONVERT_SECONDS,
- )
+ ImageProcessing::OutputFile.write(to) do |temporary_path|
+ instructions =
+ public_send(INSTRUCTION_METHODS.fetch(operation), from, temporary_path, dimensions, opts)
+ ImageMagick.magick(
+ *instructions,
+ operation: operation,
+ read: [from],
+ write: [temporary_path],
+ nice: 10,
+ timeout: MAX_CONVERT_SECONDS,
+ )
+ end
optimize_image(to: to)
rescue => error
handle_optimization_error(error: error, to: to, opts: opts, instructions: instructions)
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index 666730c7cfe..199897fd5d8 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require_relative "discourse_vips/client"
-require "tempfile"
+require "image_processing/output_file"
module DiscourseVips
SVG_DIMENSIONS_TIMEOUT_SECONDS = 3
@@ -15,12 +15,14 @@ module DiscourseVips
end
def self.letter_avatar(letter:, output_path:, background_color:, font:, font_path:)
- Client.call(
- ["letter-avatar", letter, output_path, background_color, font, font_path],
- operation: :letter_avatar_render,
- read: ["/etc/fonts", "/var/cache/fontconfig", font_path],
- write: [File.dirname(output_path)],
- )
+ ImageProcessing::OutputFile.write(output_path) do |temporary_path|
+ Client.call(
+ ["letter-avatar", letter, temporary_path, background_color, font, font_path],
+ operation: :letter_avatar_render,
+ read: ["/etc/fonts", "/var/cache/fontconfig", font_path],
+ write: [temporary_path],
+ )
+ end
end
def self.dominant_color(input_path:, timeout:)
@@ -57,62 +59,67 @@ module DiscourseVips
)
end
- def self.heif_to_jpeg(input_path:, output_path:, quality:, timeout:, read:, write:)
- Client.call(
- ["heif-to-jpeg", input_path, output_path, quality],
- operation: :upload_heif_to_jpeg,
- read:,
- write:,
- timeout:,
- )
+ def self.heif_to_jpeg(input_path:, output_path:, quality:, timeout:)
+ ImageProcessing::OutputFile.write(output_path) do |temporary_path|
+ Client.call(
+ ["heif-to-jpeg", input_path, temporary_path, quality],
+ operation: :upload_heif_to_jpeg,
+ read: [input_path],
+ write: [temporary_path],
+ timeout:,
+ )
+ end
end
- def self.reencode_jpeg(input_path:, output_path:, quality:, timeout:, read:, write:)
- Client.call(
- ["reencode-jpeg", input_path, output_path, quality],
- operation: :upload_jpeg_reencoding,
- timeout:,
- read:,
- write:,
- )
+ def self.reencode_jpeg(input_path:, output_path:, quality:, timeout:)
+ ImageProcessing::OutputFile.write(output_path) do |temporary_path|
+ Client.call(
+ ["reencode-jpeg", input_path, temporary_path, quality],
+ operation: :upload_jpeg_reencoding,
+ read: [input_path],
+ write: [temporary_path],
+ timeout:,
+ )
+ end
end
- def self.png_to_jpeg(input_path:, output_path:, quality:, timeout:, read:, write:)
- Client.call(
- ["png-to-jpeg", input_path, output_path, quality],
- operation: :upload_png_to_jpeg,
- read:,
- write:,
- timeout:,
- )
+ def self.png_to_jpeg(input_path:, output_path:, quality:, timeout:)
+ ImageProcessing::OutputFile.write(output_path) do |temporary_path|
+ Client.call(
+ ["png-to-jpeg", input_path, temporary_path, quality],
+ operation: :upload_png_to_jpeg,
+ read: [input_path],
+ write: [temporary_path],
+ timeout:,
+ )
+ end
end
def self.svg_to_png(
input_path:,
output_path:,
- read:,
- write:,
+ asset_paths: [],
timeout: SVG_TO_PNG_TIMEOUT_SECONDS,
operation: :svg_to_png,
nice: nil
)
timeout = [timeout, SVG_TO_PNG_TIMEOUT_SECONDS].min
- Client.call(
- ["svg-to-png", input_path, output_path],
- operation:,
- read: ["/etc/fonts", "/var/cache/fontconfig", *read],
- write:,
- timeout:,
- nice:,
- )
+ ImageProcessing::OutputFile.write(output_path) do |temporary_path|
+ Client.call(
+ ["svg-to-png", input_path, temporary_path],
+ operation:,
+ read: [input_path, "/etc/fonts", "/var/cache/fontconfig", *asset_paths],
+ write: [temporary_path],
+ timeout:,
+ nice:,
+ )
+ end
end
def self.thumbnail(
input_path:,
output_path:,
timeout:,
- read:,
- write:,
operation:,
input_format: nil,
output_format: nil,
@@ -132,16 +139,12 @@ module DiscourseVips
"provide exactly one resize target: scale, width and height, or max_pixels"
end
- Tempfile.create(
- ["thumbnail-", File.extname(output_path)],
- File.dirname(output_path),
- ) do |output|
- output.close
+ ImageProcessing::OutputFile.write(output_path) do |temporary_path|
Client.call(
[
"thumbnail",
input_path,
- output.path,
+ temporary_path,
width,
height,
scale,
@@ -156,12 +159,11 @@ module DiscourseVips
output_format,
],
operation:,
- read:,
- write:,
+ read: [input_path],
+ write: [temporary_path],
timeout:,
nice: 10,
)
- FileUtils.copy_file(output.path, output_path)
end
nil
end
diff --git a/lib/image_processing/output_file.rb b/lib/image_processing/output_file.rb
new file mode 100644
index 00000000000..822e09d7c91
--- /dev/null
+++ b/lib/image_processing/output_file.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require "tempfile"
+
+module ImageProcessing
+ module OutputFile
+ def self.write(path)
+ Tempfile.create(["image-output-", File.extname(path)]) do |output|
+ result = yield output.path
+ output.rewind
+
+ File.open(path, "wb") { |destination| IO.copy_stream(output, destination) }
+
+ result
+ end
+ end
+ end
+end
diff --git a/lib/letter_avatar.rb b/lib/letter_avatar.rb
index 8a01bb5f9b9..2dcdfb3f952 100644
--- a/lib/letter_avatar.rb
+++ b/lib/letter_avatar.rb
@@ -95,27 +95,29 @@ class LetterAvatar
font = macos? ? "Helvetica" : "NimbusSans-Regular"
vertical_offset = font == "Helvetica" ? 26 : 34
- ImageMagick.magick(
- "-size",
- "#{FULLSIZE}x#{FULLSIZE}",
- "xc:#{to_rgb(color)}",
- "-pointsize",
- POINTSIZE.to_s,
- "-fill",
- "#FFFFFFCC",
- "-font",
- font,
- "-gravity",
- "Center",
- "-annotate",
- "-0+#{vertical_offset}",
- letter,
- "-depth",
- "8",
- filename,
- operation: :letter_avatar_render,
- write: [File.dirname(filename)],
- )
+ ImageProcessing::OutputFile.write(filename) do |temporary_path|
+ ImageMagick.magick(
+ "-size",
+ "#{FULLSIZE}x#{FULLSIZE}",
+ "xc:#{to_rgb(color)}",
+ "-pointsize",
+ POINTSIZE.to_s,
+ "-fill",
+ "#FFFFFFCC",
+ "-font",
+ font,
+ "-gravity",
+ "Center",
+ "-annotate",
+ "-0+#{vertical_offset}",
+ letter,
+ "-depth",
+ "8",
+ temporary_path,
+ operation: :letter_avatar_render,
+ write: [temporary_path],
+ )
+ end
end
filename
diff --git a/lib/topic_og_image_generator.rb b/lib/topic_og_image_generator.rb
index 1aa0565a9f4..631376e9a26 100644
--- a/lib/topic_og_image_generator.rb
+++ b/lib/topic_og_image_generator.rb
@@ -328,19 +328,19 @@ class TopicOgImageGenerator
DiscourseVips.svg_to_png(
input_path: svg_path,
output_path: png_path,
- read: [svg_path],
- write: [directory],
operation: :topic_og_asset_render,
)
else
- ImageMagick.magick(
- "MSVG:#{svg_path}",
- png_path,
- operation: :topic_og_asset_render,
- read: [svg_path],
- write: [directory],
- timeout: 10,
- )
+ ImageProcessing::OutputFile.write(png_path) do |temporary_path|
+ ImageMagick.magick(
+ "MSVG:#{svg_path}",
+ temporary_path,
+ operation: :topic_og_asset_render,
+ read: [svg_path],
+ write: [temporary_path],
+ timeout: 10,
+ )
+ end
end
return png_path if File.exist?(png_path)
@@ -387,29 +387,30 @@ class TopicOgImageGenerator
DiscourseVips.svg_to_png(
input_path: svg_path,
output_path: png_path,
- read: [dir],
- write: [dir],
+ asset_paths: [dir],
operation: :topic_og_render,
nice: 10,
)
else
- ImageMagick.magick(
- "-background",
- "none",
- "-size",
- "#{OG_WIDTH}x#{OG_HEIGHT}",
- "MSVG:#{svg_path}",
- "-depth",
- "8",
- "-define",
- "png:compression-level=9",
- png_path,
- operation: :topic_og_render,
- read: [dir],
- write: [dir],
- nice: 10,
- timeout: 20,
- )
+ ImageProcessing::OutputFile.write(png_path) do |temporary_path|
+ ImageMagick.magick(
+ "-background",
+ "none",
+ "-size",
+ "#{OG_WIDTH}x#{OG_HEIGHT}",
+ "MSVG:#{svg_path}",
+ "-depth",
+ "8",
+ "-define",
+ "png:compression-level=9",
+ temporary_path,
+ operation: :topic_og_render,
+ read: [dir],
+ write: [temporary_path],
+ nice: 10,
+ timeout: 20,
+ )
+ end
end
return nil unless File.exist?(png_path)
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 24a1c1a6fb0..0286174f9c6 100644
--- a/lib/upload_creator.rb
+++ b/lib/upload_creator.rb
@@ -353,7 +353,6 @@ class UploadCreator
OptimizedImage.ensure_safe_paths!(from, to)
read = [@file.path]
- write = [jpeg_tempfile.path]
if GlobalSetting.enable_vips_image_processing
DiscourseVips.heif_to_jpeg(
@@ -361,15 +360,16 @@ class UploadCreator
output_path: to,
quality: SiteSetting.image_quality,
timeout: MAX_CONVERT_FORMAT_SECONDS,
- read:,
- write:,
)
else
- begin
- execute_convert(from, to, {}, read:, write:)
- rescue StandardError
- # retry with debugging enabled
- execute_convert(from, to, { debug: true }, read:, write:)
+ ImageProcessing::OutputFile.write(to) do |temporary_path|
+ write = [temporary_path]
+ begin
+ execute_convert(from, temporary_path, {}, read:, write:)
+ rescue StandardError
+ # retry with debugging enabled
+ execute_convert(from, temporary_path, { debug: true }, read:, write:)
+ end
end
end
@@ -497,19 +497,19 @@ class UploadCreator
output_path: @file.path,
quality: SiteSetting.ImageQuality.recompress_original_jpg_quality,
timeout: MAX_FIX_ORIENTATION_TIME,
- read: [@file.path],
- write: [@file.path],
)
else
- ImageMagick.magick(
- path,
- "-auto-orient",
- path,
- operation: :upload_auto_orient,
- read: [@file.path],
- write: [@file.path, File.dirname(@file.path)],
- timeout: MAX_FIX_ORIENTATION_TIME,
- )
+ ImageProcessing::OutputFile.write(@file.path) do |temporary_path|
+ ImageMagick.magick(
+ path,
+ "-auto-orient",
+ "jpeg:#{temporary_path}",
+ operation: :upload_auto_orient,
+ read: [@file.path],
+ write: [temporary_path],
+ timeout: MAX_FIX_ORIENTATION_TIME,
+ )
+ end
end
extract_image_info!
@@ -678,21 +678,21 @@ class UploadCreator
output_path: to,
quality:,
timeout: MAX_CONVERT_FORMAT_SECONDS,
- read: [from],
- write: [jpeg_tempfile.path],
)
else
from = OptimizedImage.prepend_decoder!(from, nil, filename: "image.#{@image_info.type}")
- to = OptimizedImage.prepend_decoder!(to)
opts = { quality: }
read = [@file.path]
- write = [jpeg_tempfile.path]
- begin
- execute_convert(from, to, opts, read:, write:)
- rescue StandardError
- # retry with debugging enabled
- execute_convert(from, to, opts.merge(debug: true), read:, write:)
+ ImageProcessing::OutputFile.write(to) do |temporary_path|
+ converted_path = OptimizedImage.prepend_decoder!(temporary_path)
+ write = [temporary_path]
+ begin
+ execute_convert(from, converted_path, opts, read:, write:)
+ rescue StandardError
+ # retry with debugging enabled
+ execute_convert(from, converted_path, opts.merge(debug: true), read:, write:)
+ end
end
end
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index b548c025039..631981b9c63 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -1,11 +1,9 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
-require "fileutils"
require "landlock"
require "msgpack"
require "socket"
-require "tempfile"
require "tmpdir"
require "vips"
@@ -231,10 +229,7 @@ class DiscourseVipsWorker
block_loaders(allowed: %w[VipsForeignLoadJpeg])
begin
image = Vips::Image.jpegload(input_path, access: :random)
- Tempfile.create(%w[reencoded .jpg]) do |reencoded_file|
- save_jpeg(image, input_path:, output_path: reencoded_file.path, quality:)
- FileUtils.copy_file(reencoded_file.path, output_path)
- end
+ save_jpeg(image, input_path:, output_path:, quality:)
nil
rescue Vips::Error => error
raise InvalidImage, error.message
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index 41f9812f51c..aced04e84dc 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -17,8 +17,6 @@ RSpec.describe DiscourseVips do
output_path: File.join(directory, "output.jpg"),
quality: SiteSetting.image_quality,
timeout: 20,
- read: [input_path],
- write: [File.dirname(File.join(directory, "output.jpg"))],
)
end
@@ -130,8 +128,6 @@ RSpec.describe DiscourseVips do
output_path:,
quality: SiteSetting.image_quality,
timeout: 20,
- read: [input_path],
- write: [File.dirname(output_path)],
)
expect(FastImage.type(output_path)).to eq(:jpeg)
@@ -151,14 +147,7 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "converted.jpg")
- described_class.heif_to_jpeg(
- input_path:,
- output_path:,
- quality: 95,
- timeout: 20,
- read: [input_path],
- write: [directory],
- )
+ described_class.heif_to_jpeg(input_path:, output_path:, quality: 95, timeout: 20)
expect(described_class.dominant_color(input_path: output_path, timeout: 5)).to eq("FFFFFF")
end
@@ -176,8 +165,6 @@ RSpec.describe DiscourseVips do
output_path:,
quality: SiteSetting.image_quality,
timeout: 20,
- read: [input_path],
- write: [File.dirname(output_path)],
)
expect(FastImage.type(output_path)).to eq(:jpeg)
@@ -198,16 +185,12 @@ RSpec.describe DiscourseVips do
output_path: lower_quality_path,
quality: 40,
timeout: 20,
- read: [input_path],
- write: [File.dirname(lower_quality_path)],
)
described_class.heif_to_jpeg(
input_path:,
output_path: higher_quality_path,
quality: 95,
timeout: 20,
- read: [input_path],
- write: [File.dirname(higher_quality_path)],
)
expect(File.size(lower_quality_path)).to be < File.size(higher_quality_path)
@@ -227,8 +210,6 @@ RSpec.describe DiscourseVips do
output_path:,
quality: SiteSetting.image_quality,
timeout: 20,
- read: [input_path],
- write: [File.dirname(output_path)],
)
}.to raise_error(DiscourseVips::InvalidImage)
@@ -237,28 +218,19 @@ RSpec.describe DiscourseVips do
end
end
- it "rejects overwriting the source image" do
- original_content = File.binread(file_from_fixtures("should_be_jpeg.heic").path)
-
+ it "converts the source image to JPEG in place" do
Dir.mktmpdir do |directory|
input_path = File.join(directory, "source.heic")
- File.binwrite(input_path, original_content)
+ FileUtils.cp(file_from_fixtures("should_be_jpeg.heic").path, input_path)
- expect {
- described_class.heif_to_jpeg(
- input_path:,
- output_path: input_path,
- quality: SiteSetting.image_quality,
- timeout: 20,
- read: [input_path],
- write: [File.dirname(input_path)],
- )
- }.to raise_error(
- DiscourseVips::Error,
- "JPEG conversion requires separate input and output files",
+ described_class.heif_to_jpeg(
+ input_path:,
+ output_path: input_path,
+ quality: SiteSetting.image_quality,
+ timeout: 20,
)
- expect(File.binread(input_path)).to eq(original_content)
+ expect(FastImage.type(input_path)).to eq(:jpeg)
end
end
@@ -275,8 +247,6 @@ RSpec.describe DiscourseVips do
output_path:,
quality: SiteSetting.image_quality,
timeout: 0.05,
- read: [input_path],
- write: [File.dirname(output_path)],
)
}.to raise_error(DiscourseVips::OperationTimeout)
end
@@ -429,8 +399,6 @@ RSpec.describe DiscourseVips do
output_path:,
quality: SiteSetting.ImageQuality.png_to_jpg_quality,
timeout: 5,
- read: [input_path],
- write: [directory],
)
expect(FastImage.type(output_path)).to eq(:jpeg)
@@ -457,8 +425,6 @@ RSpec.describe DiscourseVips do
output_path:,
quality: SiteSetting.ImageQuality.png_to_jpg_quality,
timeout: 5,
- read: [input_path],
- write: [File.dirname(output_path)],
)
}.to raise_error(DiscourseVips::InvalidImage)
expect(File.exist?(output_path)).to eq(false)
@@ -477,14 +443,7 @@ RSpec.describe DiscourseVips do
output_path = File.join(directory, "converted.jpg")
input_path = file_from_fixtures("logo.jpg").path
- described_class.reencode_jpeg(
- input_path:,
- output_path:,
- quality: 40,
- timeout: 5,
- read: [input_path],
- write: [File.dirname(output_path)],
- )
+ described_class.reencode_jpeg(input_path:, output_path:, quality: 40, timeout: 5)
expect(FastImage.type(output_path)).to eq(:jpeg)
expect(FastImage.size(output_path)).to eq(FastImage.size(input_path))
@@ -494,8 +453,6 @@ RSpec.describe DiscourseVips do
output_path: higher_quality_path,
quality: 95,
timeout: 5,
- read: [input_path],
- write: [File.dirname(higher_quality_path)],
)
expect(File.size(output_path)).to be < File.size(higher_quality_path)
end
@@ -514,8 +471,6 @@ RSpec.describe DiscourseVips do
output_path: input_path,
quality: 95,
timeout: 5,
- read: [input_path],
- write: [input_path],
)
}.to raise_error(DiscourseVips::InvalidImage)
expect(File.binread(input_path)).to eq(original)
@@ -615,12 +570,7 @@ RSpec.describe DiscourseVips do
</svg>
SVG
- described_class.svg_to_png(
- input_path:,
- output_path:,
- read: [directory],
- write: [output_directory],
- )
+ described_class.svg_to_png(input_path:, output_path:, asset_paths: [directory])
png = ChunkyPNG::Image.from_file(output_path)
expect(png[20, 20]).to eq(ChunkyPNG::Color.rgb(255, 0, 0))
@@ -645,12 +595,7 @@ RSpec.describe DiscourseVips do
</svg>
SVG
- described_class.svg_to_png(
- input_path:,
- output_path:,
- read: [input_path, allowed_path],
- write: [output_path],
- )
+ described_class.svg_to_png(input_path:, output_path:, asset_paths: [allowed_path])
png = ChunkyPNG::Image.from_file(output_path)
expect([png[20, 20], png[60, 20]]).to eq(
@@ -659,25 +604,6 @@ RSpec.describe DiscourseVips do
end
end
- it "raises an error when reading the input is not permitted" do
- skip "Landlock is not supported" if !Discourse::SafeExec.landlock_supported?
-
- file = file_from_fixtures("tiny.svg")
-
- Dir.mktmpdir do |directory|
- output_path = File.join(directory, "output.png")
-
- expect {
- described_class.svg_to_png(
- input_path: file.path,
- output_path:,
- read: [],
- write: [directory],
- )
- }.to raise_error(DiscourseVips::Error)
- end
- end
-
it "renders transparent areas against a white background" do
svg = <<~SVG
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="4">
@@ -689,12 +615,7 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "output.png")
- described_class.svg_to_png(
- input_path: file.path,
- output_path:,
- read: [file.path],
- write: [directory],
- )
+ described_class.svg_to_png(input_path: file.path, output_path:)
png = ChunkyPNG::Image.from_file(output_path)
expect([png[2, 2], png[10, 2]]).to eq(
@@ -709,18 +630,13 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "output.png")
- expect {
- described_class.svg_to_png(
- input_path: file.path,
- output_path:,
- read: [file.path],
- write: [directory],
- )
- }.to raise_error(DiscourseVips::Error)
+ expect { described_class.svg_to_png(input_path: file.path, output_path:) }.to raise_error(
+ DiscourseVips::Error,
+ )
end
end
- it "raises an error when input and output refer to the same file" do
+ it "renders PNG output when input and output refer to the same file" do
svg = '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"/>'
Dir.mktmpdir do |directory|
@@ -729,14 +645,10 @@ RSpec.describe DiscourseVips do
File.write(input_path, svg)
File.link(input_path, output_path)
- expect {
- described_class.svg_to_png(
- input_path:,
- output_path:,
- read: [input_path],
- write: [directory],
- )
- }.to raise_error(DiscourseVips::Error, "SVG input and PNG output must be different files")
+ described_class.svg_to_png(input_path:, output_path:)
+
+ expect(FastImage.type(input_path)).to eq(:png)
+ expect(FastImage.size(output_path)).to eq([10, 10])
end
end
@@ -751,14 +663,10 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "output.png")
- expect {
- described_class.svg_to_png(
- input_path: file.path,
- output_path:,
- read: [file.path],
- write: [directory],
- )
- }.to raise_error(DiscourseVips::Error, /SVG exceeds/)
+ expect { described_class.svg_to_png(input_path: file.path, output_path:) }.to raise_error(
+ DiscourseVips::Error,
+ /SVG exceeds/,
+ )
end
end
@@ -772,39 +680,13 @@ RSpec.describe DiscourseVips do
Dir.mktmpdir do |directory|
output_path = File.join(directory, "output.png")
- described_class.svg_to_png(
- input_path: file.path,
- output_path:,
- read: [file.path],
- write: [directory],
- )
+ described_class.svg_to_png(input_path: file.path, output_path:)
png = ChunkyPNG::Image.from_file(output_path)
expect([png.width, png.height]).to eq([600, 200])
expect(png[png.width / 2, png.height / 2]).to eq(ChunkyPNG::Color.rgb(255, 0, 0))
end
end
-
- it "raises an error when writing the output is not permitted" do
- skip "Landlock is not supported" if !Discourse::SafeExec.landlock_supported?
-
- file =
- file_from_contents(
- '<svg xmlns="http://www.w3.org/2000/svg" width="60" height="20"/>',
- "input.svg",
- )
-
- Dir.mktmpdir do |directory|
- expect {
- described_class.svg_to_png(
- input_path: file.path,
- output_path: File.join(directory, "output.png"),
- read: [file.path],
- write: [],
- )
- }.to raise_error(DiscourseVips::Error)
- end
- end
end
describe ".thumbnail" do
@@ -826,8 +708,6 @@ RSpec.describe DiscourseVips do
sharpen: true,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
expect(FastImage.size(output_path)).to eq([100, 50])
@@ -847,8 +727,6 @@ RSpec.describe DiscourseVips do
crop: :centre,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
expect(FastImage.size(destination_path)).to eq([100, 50])
@@ -865,8 +743,6 @@ RSpec.describe DiscourseVips do
height: 50,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::InvalidImage)
end
@@ -881,8 +757,6 @@ RSpec.describe DiscourseVips do
height: 50,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::Error, "unsupported input format")
end
@@ -896,8 +770,6 @@ RSpec.describe DiscourseVips do
height: 50,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::Error)
end
@@ -912,8 +784,6 @@ RSpec.describe DiscourseVips do
quality: 75,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::Error)
end
@@ -928,8 +798,6 @@ RSpec.describe DiscourseVips do
size: :invalid,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(
DiscourseVips::Error,
@@ -949,8 +817,6 @@ RSpec.describe DiscourseVips do
height: 50,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::InvalidImage)
@@ -969,8 +835,6 @@ RSpec.describe DiscourseVips do
height: 50,
timeout: 10,
operation: :optimized_image_resize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::InvalidImage)
end
@@ -983,8 +847,6 @@ RSpec.describe DiscourseVips do
sharpen: true,
timeout: 20,
operation: :optimized_image_downsize,
- read: [input_path],
- write: [directory],
)
expect(FastImage.size(output_path)).to eq([122, 33])
@@ -1001,8 +863,6 @@ RSpec.describe DiscourseVips do
max_pixels:,
timeout: 20,
operation: :optimized_image_downsize,
- read: [input_path],
- write: [directory],
)
output_width, output_height = FastImage.size(output_path)
@@ -1020,8 +880,6 @@ RSpec.describe DiscourseVips do
height: 100,
timeout: 20,
operation: :optimized_image_downsize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(
ArgumentError,
@@ -1037,8 +895,6 @@ RSpec.describe DiscourseVips do
scale: 0,
timeout: 20,
operation: :optimized_image_downsize,
- read: [input_path],
- write: [directory],
)
}.to raise_error(DiscourseVips::Error, "invalid resize scale")
end
diff --git a/spec/lib/image_processing/output_file_spec.rb b/spec/lib/image_processing/output_file_spec.rb
new file mode 100644
index 00000000000..89c993cb09e
--- /dev/null
+++ b/spec/lib/image_processing/output_file_spec.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+require "image_processing/output_file"
+
+RSpec.describe ImageProcessing::OutputFile do
+ describe ".write" do
+ let(:destination) { Tempfile.new(%w[destination .png], binmode: true) }
+ let(:image_contents) { File.binread(file_from_fixtures("logo.png").path) }
+
+ before do
+ destination.write("original")
+ destination.flush
+ end
+
+ after { destination.close! }
+
+ it "makes successful output available through the open destination file" do
+ described_class.write(destination.path) { |path| File.binwrite(path, image_contents) }
+
+ destination.rewind
+ expect(destination.read).to eq(image_contents)
+ end
+
+ it "preserves the destination contents when processing fails" do
+ expect {
+ described_class.write(destination.path) do |path|
+ File.binwrite(path, "partial")
+ raise "rendering failed"
+ end
+ }.to raise_error(RuntimeError, "rendering failed")
+
+ expect(File.binread(destination.path)).to eq("original")
+ end
+ end
+end
diff --git a/spec/support/image_orientation_helpers.rb b/spec/support/image_orientation_helpers.rb
index cd9090fe157..c005d9c28a8 100644
--- a/spec/support/image_orientation_helpers.rb
+++ b/spec/support/image_orientation_helpers.rb
@@ -26,13 +26,15 @@ module ImageOrientationHelpers
Dir.mktmpdir do |directory|
png_path = File.join(directory, "stored.png")
- ImageMagick.magick(
- stored_path,
- png_path,
- operation: :upload_format_conversion,
- read: [stored_path],
- write: [directory],
- )
+ ImageProcessing::OutputFile.write(png_path) do |temporary_path|
+ ImageMagick.magick(
+ stored_path,
+ temporary_path,
+ operation: :upload_format_conversion,
+ read: [stored_path],
+ write: [temporary_path],
+ )
+ end
stored_image = ChunkyPNG::Image.from_file(png_path)
Array.new(rows) do |row_index|