image_processing 2.0.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a11a590332eebc08eff9b2cc0ff0436ff8ab42c6bee6e85034232d8e5425700
4
- data.tar.gz: 983201e5b89000e91f299f4f25cfa861673e6a552f5f751d336bf45b12f3b534
3
+ metadata.gz: 5b790f3dd08c310a2413cb8cb6b33a9c602edb3509cdd1de2c8f94ebea702f17
4
+ data.tar.gz: 99d87927f2a6603939332f61292c2b2b582fecc5832b4562a92e8a5170e862d9
5
5
  SHA512:
6
- metadata.gz: 762c39caba0bfba8f5b00eb3334308a0a3fd79f602301e087cc7e53acb38dc416ad8bd547d6779636c966c25fbf689c75d306394ca01a93d8dfe13e5fb902ac6
7
- data.tar.gz: 94e92fff8dddd41a3651ee85e58bdcdc94d293fbacf573e62a2ca560ad9c5546f9e79e9e97736948b7daf65478938e15e2e7ffa8a4d7f8a6b55a7ed24e930f14
6
+ metadata.gz: 00b2e0b0b250473b55e4ca415fd00f1c9fa50458e2c280a4110949838c344cb49c52b5afe023e0c75b8e435460f55517a1aeb8497ae1adf72fdca1de3fab59e2
7
+ data.tar.gz: a87c6791a8dbc06d5e40a6a64aaefd7803a382aac5071e238f7f7bface47d026405603bb6da9cc4534f9472f65d2e70bf4614cd75b9962506781a2818ad81b73
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 2.2.0 (2026-09-29)
2
+
3
+ * Save in the `#convert` format when the destination path has no extension, such as a `/dev/fd/N` path (thanks to @flavorjones)
4
+
5
+ ## 2.1.0 (2026-09-01)
6
+
7
+ * [minimagick] Add `inherit_fds:` so a loader can name an already-open input (thanks to @flavorjones)
8
+
9
+ ## 2.0.3 (2026-08-06)
10
+
11
+ * Prevent remote code execution when operation names come from user input, closing bypasses through the `#operation` meta-builder, `#method_missing`, and nested `#send` calls (reported by @szymonsec)
12
+
13
+ * [minimagick] Prevent remote code execution through unsafe public methods (e.g. `#instance_eval`, `#send`) passed as loader/saver option names
14
+
1
15
  ## 2.0.2 (2026-06-03)
2
16
 
3
17
  * Raise `LoadError` instead of `ImageProcessing::Error` when soft dependencies are missing (@bdewater-thatch)
@@ -86,25 +86,24 @@ module ImageProcessing
86
86
  private
87
87
 
88
88
  # This prevents calling unsafe Ruby core methods such as `Kernel#system`,
89
- # which would allow for remote shell execution.
89
+ # which would allow for remote shell execution. The processor enforces the
90
+ # same guard when operations are dispatched, which also covers paths that
91
+ # bypass this early check (e.g. the #operation meta-builder).
92
+ #
93
+ # Bang names are rejected here because they are Chainable's execution
94
+ # shortcut (see #method_missing), not operations to chain.
90
95
  def invalid_operation?(name)
91
- return true if name.end_with?("!")
92
-
93
- owner = method(name).owner
94
- [BasicObject, Kernel, Object, Module].include?(owner)
95
- rescue NameError
96
- false
96
+ name.end_with?("!") || ImageProcessing.unsafe_method?(self, name)
97
97
  end
98
98
 
99
99
  # Assume that any unknown method names an operation supported by the
100
100
  # processor. Add a bang ("!") if you want processing to be performed.
101
- def method_missing(name, *args, &block)
102
- return super if name.to_s.end_with?("?")
103
- return public_send(name.to_s.chomp("!"), *args, &block).call if name.to_s.end_with?("!")
101
+ def method_missing(name, ...)
102
+ return super if name.end_with?("?")
103
+ return public_send(name.to_s.chomp("!"), ...).call if name.end_with?("!")
104
104
 
105
- operation(name, *args, &block)
105
+ operation(name, ...)
106
106
  end
107
- ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
108
107
 
109
108
  # Empty options which the builder starts with.
110
109
  DEFAULT_OPTIONS = {
@@ -9,11 +9,20 @@ module ImageProcessing
9
9
  module MiniMagick
10
10
  extend Chainable
11
11
 
12
- def self.convert_shim(&block)
12
+ # mini_magick gained `inherit_fds:` on MiniMagick::Shell#execute in 5.4.0.
13
+ INHERIT_FDS_MINIMUM_VERSION = Gem::Version.new("5.4.0")
14
+
15
+ def self.convert_shim(inherit_fds: nil, &block)
16
+ if inherit_fds && ::MiniMagick.version < INHERIT_FDS_MINIMUM_VERSION
17
+ raise LoadError, "The `inherit_fds` loader option requires mini_magick #{INHERIT_FDS_MINIMUM_VERSION} or newer, but mini_magick #{::MiniMagick.version} is loaded. Please upgrade the gem."
18
+ end
19
+
20
+ options = inherit_fds ? { inherit_fds: inherit_fds } : {}
21
+
13
22
  if ::MiniMagick.respond_to?(:convert)
14
- ::MiniMagick.convert(&block)
23
+ ::MiniMagick.convert(**options, &block)
15
24
  else
16
- ::MiniMagick::Tool::Convert.new(&block)
25
+ ::MiniMagick::Tool::Convert.new(**options, &block)
17
26
  end
18
27
  end
19
28
 
@@ -37,12 +46,16 @@ module ImageProcessing
37
46
  # Initializes the image on disk into a MiniMagick::Tool object. Accepts
38
47
  # additional options related to loading the image (e.g. geometry).
39
48
  # Additionally auto-orients the image to be upright.
40
- def self.load_image(path_or_magick, loader: nil, page: nil, geometry: nil, auto_orient: true, **options)
49
+ # `inherit_fds` names IO objects the tool inherits, so the source may be a
50
+ # `/dev/fd/N` path. The source stays a path, so `loader`, `page` and
51
+ # `geometry` still apply to it, which they would not if the caller passed
52
+ # a pre-built MiniMagick::Tool carrying the descriptor.
53
+ def self.load_image(path_or_magick, loader: nil, page: nil, geometry: nil, auto_orient: true, inherit_fds: nil, **options)
41
54
  if path_or_magick.is_a?(::MiniMagick::Tool)
42
55
  magick = path_or_magick
43
56
  else
44
57
  source_path = path_or_magick
45
- magick = ::ImageProcessing::MiniMagick.convert_shim
58
+ magick = ::ImageProcessing::MiniMagick.convert_shim(inherit_fds: inherit_fds)
46
59
 
47
60
  Utils.apply_options(magick, **options)
48
61
 
@@ -60,11 +73,21 @@ module ImageProcessing
60
73
 
61
74
  # Calls the built ImageMagick command to perform processing and save
62
75
  # the result to disk. Accepts additional options related to saving the
63
- # image (e.g. quality).
64
- def self.save_image(magick, destination_path, allow_splitting: false, **options)
76
+ # image (e.g. quality). `format` names the output format when given,
77
+ # otherwise ImageMagick infers it from the path's extension.
78
+ def self.save_image(magick, destination_path, format = nil, allow_splitting: false, **options)
65
79
  Utils.apply_options(magick, **options)
66
80
 
67
- magick << destination_path
81
+ if format
82
+ # ImageMagick reads everything before the first colon as the
83
+ # format, so a format carrying a colon could redirect the output.
84
+ format = format.to_s
85
+ fail Error, "invalid format: #{format.inspect}" unless format.ascii_only? && format.match?(/\A\w+\z/)
86
+
87
+ magick << "#{format}:#{destination_path}"
88
+ else
89
+ magick << destination_path
90
+ end
68
91
  magick.call
69
92
 
70
93
  Utils.disallow_split_layers!(destination_path) unless allow_splitting
@@ -225,6 +248,13 @@ module ImageProcessing
225
248
  # Applies options from the provided hash.
226
249
  def apply_options(magick, define: {}, **options)
227
250
  options.each do |option, value|
251
+ # Loader/saver option names may come from user input, so guard
252
+ # against dispatching to unsafe Ruby core methods (e.g. #send,
253
+ # #instance_eval) that public_send does not block on its own.
254
+ if ImageProcessing.unsafe_method?(magick, option)
255
+ raise Error, "#{option.inspect} is not a valid option"
256
+ end
257
+
228
258
  case value
229
259
  when true, nil then magick.public_send(option)
230
260
  when false then magick.public_send(option).+
@@ -21,7 +21,11 @@ module ImageProcessing
21
21
  call_processor
22
22
  elsif destination
23
23
  handle_destination do
24
- call_processor(destination: destination)
24
+ if format && !determine_format(destination)
25
+ call_processor(destination: destination, format: format)
26
+ else
27
+ call_processor(destination: destination)
28
+ end
25
29
  end
26
30
  else
27
31
  create_tempfile do |tempfile|
@@ -1,7 +1,7 @@
1
1
  module ImageProcessing
2
2
  # Abstract class inherited by individual processors.
3
3
  class Processor
4
- def self.call(source:, loader:, operations:, saver:, destination: nil)
4
+ def self.call(source:, loader:, operations:, saver:, destination: nil, format: nil)
5
5
  unless source.is_a?(String) || source.is_a?(self::ACCUMULATOR_CLASS)
6
6
  fail Error, "invalid source: #{source.inspect}"
7
7
  end
@@ -19,7 +19,9 @@ module ImageProcessing
19
19
  accumulator = apply_operation(accumulator, operation)
20
20
  end
21
21
 
22
- if destination
22
+ if destination && format
23
+ save_image(accumulator, destination, format, **saver)
24
+ elsif destination
23
25
  save_image(accumulator, destination, **saver)
24
26
  else
25
27
  accumulator
@@ -53,6 +55,15 @@ module ImageProcessing
53
55
  # operation directly on the accumulator object. This provides a common
54
56
  # umbrella above defined macros and direct operations.
55
57
  def apply_operation(name, *args, &block)
58
+ # Chainable performs the same check when operations are built, but that
59
+ # guard only sees the outer builder method name, so it can be routed
60
+ # around via the #operation meta-builder, #method_missing, or a nested
61
+ # #send. This is the single point every operation is finally dispatched
62
+ # through, so enforcing the guard here closes all of those paths.
63
+ if ImageProcessing.unsafe_method?(self, name)
64
+ fail Error, "#{name.inspect} is not a valid operation"
65
+ end
66
+
56
67
  receiver = respond_to?(name) ? self : @accumulator
57
68
 
58
69
  if args.last.is_a?(Hash)
@@ -1,3 +1,3 @@
1
1
  module ImageProcessing
2
- VERSION = "2.0.2"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -56,12 +56,24 @@ module ImageProcessing
56
56
 
57
57
  # Writes the Vips::Image object to disk. This starts the processing
58
58
  # pipeline defined in the Vips::Image object. Accepts additional
59
- # saver-specific options (e.g. quality).
60
- def self.save_image(image, path, saver: nil, quality: nil, **options)
59
+ # saver-specific options (e.g. quality). The saver is chosen by `format`
60
+ # when given, otherwise by the path's extension.
61
+ def self.save_image(image, path, format = nil, saver: nil, quality: nil, **options)
61
62
  options[:Q] = quality if quality
62
63
 
63
64
  if saver
64
65
  image.public_send(:"#{saver}save", path, **options)
66
+ elsif format
67
+ # Savers such as heifsave pick their encoding from the suffix (AV1
68
+ # for ".avif"), so save through a target, which takes the suffix
69
+ # separately from the path.
70
+ saver = ::Vips.vips_foreign_find_save_target(".#{format}")
71
+ fail ::Vips::Error, "No known saver for '#{format}'." unless saver
72
+
73
+ filename = ::Vips.p2str(::Vips.vips_filename_get_filename(path))
74
+ option_string = ::Vips.p2str(::Vips.vips_filename_get_options(path))
75
+ options = Utils.select_valid_options(saver, options)
76
+ image.write_to_target(::Vips::Target.new_to_file(filename), ".#{format}#{option_string}", **options)
65
77
  else
66
78
  options = Utils.select_valid_saver_options(path, options)
67
79
  image.write_to_file(path, **options)
@@ -7,6 +7,25 @@ require "image_processing/version"
7
7
  module ImageProcessing
8
8
  Error = Class.new(StandardError)
9
9
 
10
+ # Method owners considered unsafe to dispatch to when a method name comes
11
+ # from user input, because they expose Ruby's metaprogramming and shell
12
+ # primitives (e.g. Kernel#send, Kernel#eval, BasicObject#instance_eval,
13
+ # Kernel#system). Any name whose implementation is inherited from one of
14
+ # these should never be treated as an image operation or a loader/saver
15
+ # option.
16
+ UNSAFE_METHOD_OWNERS = [BasicObject, Kernel, Object, Module].freeze
17
+
18
+ # Whether calling +name+ on +receiver+ would dispatch to an unsafe Ruby core
19
+ # method rather than a genuine operation. Used as a single, shared guard at
20
+ # every place where a user-provided name is dispatched via +public_send+.
21
+ def self.unsafe_method?(receiver, name)
22
+ UNSAFE_METHOD_OWNERS.include?(receiver.method(name.to_s).owner)
23
+ rescue NameError
24
+ # An unknown name is not a Ruby core method, so it is safe to forward to
25
+ # the receiver (which will reject it if it is not a real operation).
26
+ false
27
+ end
28
+
10
29
  autoload :MiniMagick, "image_processing/mini_magick"
11
30
  autoload :Vips, "image_processing/vips"
12
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_processing
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  - !ruby/object:Gem::Version
106
106
  version: '0'
107
107
  requirements: []
108
- rubygems_version: 4.0.3
108
+ rubygems_version: 4.0.13
109
109
  specification_version: 4
110
110
  summary: High-level wrapper for processing images for the web with ImageMagick or
111
111
  libvips.