image_processing 2.1.0 → 2.2.1

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: a52c42f1463cc15caf0dfe57bc8a5efeb464006a2a29bbf1b8c842ada339e4aa
4
- data.tar.gz: 2344e1015b02f4b24363371d8877bdcc4fb28b9fb820e15d2e2446f7874443c6
3
+ metadata.gz: 3f36b1a00b35a05403a2c6dd77f94912dbbca311cb6939556cbb7e9eb66cd702
4
+ data.tar.gz: bce160c3fcff5a046447c334c4c5440386080d355b0899c30b5b1ea6e89922c8
5
5
  SHA512:
6
- metadata.gz: 90c29ed45d475cc54f814a368e894504f5bc012835ef989d3678568dfb3cd29aca144e88cb17d102ffc82dcbd94c4d46de7d9a5fbc0474c0f6b15c481a1f4bb3
7
- data.tar.gz: 8a7f991cf00725e1d5618add34f9b57814e2e273d8b9575f49e61701e80259ac8c2cd61028cdaefc6b4a6694a8a922d2246f5f6b8f7ee7184ff31bd0f8644813
6
+ metadata.gz: 61b02322af114f81116e9f3600d5f331cacb8b6c299a78a15c9891d56beb92ee56f4181b3349b64087bd3d801aa90029ee31e5e897847ea4bb9739589bcc3305
7
+ data.tar.gz: ba62cf185ca3ac4a19582936c3fbca176b7e8312185fd3505438b247e839a877ac53781cf518311b6a5ff3601111529393a00649b0e395b5b42bbeec3de27f8d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 2.2.1 (2026-11-21)
2
+
3
+ * Prevent remote code execution when operation names or MiniMagick loader/saver option names come from user input and a library such as ActiveSupport adds methods like `Object#try` to every object (reported by @hgrgr)
4
+
5
+ * [minimagick] Allow loader/saver options named like private Kernel methods again, such as `format:` and `print:`
6
+
7
+ ## 2.2.0 (2026-09-29)
8
+
9
+ * Save in the `#convert` format when the destination path has no extension, such as a `/dev/fd/N` path (thanks to @flavorjones)
10
+
1
11
  ## 2.1.0 (2026-09-01)
2
12
 
3
13
  * [minimagick] Add `inherit_fds:` so a loader can name an already-open input (thanks to @flavorjones)
@@ -85,17 +85,27 @@ module ImageProcessing
85
85
 
86
86
  private
87
87
 
88
- # This prevents calling unsafe Ruby core methods such as `Kernel#system`,
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).
88
+ # Operation names may come from user input, so only allow Chainable's own
89
+ # builder methods and unknown names (which become processor operations via
90
+ # #method_missing). This prevents dispatching to methods such as
91
+ # `Kernel#send`, `BasicObject#instance_eval` or ActiveSupport's
92
+ # `Object#try`, which would allow remote code execution. The processor
93
+ # enforces the same guard when operations are dispatched, which also covers
94
+ # paths that bypass this early check (e.g. the #operation meta-builder).
92
95
  #
93
96
  # Bang names are rejected here because they are Chainable's execution
94
- # shortcut (see #method_missing), not operations to chain.
97
+ # shortcut (see #method_missing), not operations to chain. #branch and
98
+ # #call are rejected because they would let input overwrite internal
99
+ # builder options or the destination path.
95
100
  def invalid_operation?(name)
96
- name.end_with?("!") || ImageProcessing.unsafe_method?(self, name)
101
+ name.end_with?("!") ||
102
+ NON_OPERATION_METHODS.include?(name.to_s) ||
103
+ !ImageProcessing.allowed_method?(self, name, [Chainable])
97
104
  end
98
105
 
106
+ # Chainable methods that must not be reachable through #apply.
107
+ NON_OPERATION_METHODS = %w[branch call].freeze
108
+
99
109
  # Assume that any unknown method names an operation supported by the
100
110
  # processor. Add a bang ("!") if you want processing to be performed.
101
111
  def method_missing(name, ...)
@@ -73,11 +73,21 @@ module ImageProcessing
73
73
 
74
74
  # Calls the built ImageMagick command to perform processing and save
75
75
  # the result to disk. Accepts additional options related to saving the
76
- # image (e.g. quality).
77
- 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)
78
79
  Utils.apply_options(magick, **options)
79
80
 
80
- 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
81
91
  magick.call
82
92
 
83
93
  Utils.disallow_split_layers!(destination_path) unless allow_splitting
@@ -238,10 +248,11 @@ module ImageProcessing
238
248
  # Applies options from the provided hash.
239
249
  def apply_options(magick, define: {}, **options)
240
250
  options.each do |option, value|
241
- # Loader/saver option names may come from user input, so guard
242
- # against dispatching to unsafe Ruby core methods (e.g. #send,
243
- # #instance_eval) that public_send does not block on its own.
244
- if ImageProcessing.unsafe_method?(magick, option)
251
+ # Loader/saver option names may come from user input, so only
252
+ # dispatch to MiniMagick::Tool's own methods or CLI options handled
253
+ # by #method_missing, never to methods such as #instance_eval,
254
+ # #send or ActiveSupport's #try.
255
+ unless ImageProcessing.allowed_method?(magick, option, [::MiniMagick::Tool])
245
256
  raise Error, "#{option.inspect} is not a valid option"
246
257
  end
247
258
 
@@ -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
@@ -58,12 +60,20 @@ module ImageProcessing
58
60
  # around via the #operation meta-builder, #method_missing, or a nested
59
61
  # #send. This is the single point every operation is finally dispatched
60
62
  # through, so enforcing the guard here closes all of those paths.
61
- if ImageProcessing.unsafe_method?(self, name)
63
+ #
64
+ # Macros defined by the processor are called on the processor; anything
65
+ # else is forwarded to the accumulator, but only if the accumulator's
66
+ # class defines it or handles it via #method_missing. Methods inherited
67
+ # from elsewhere (Ruby core, or modules mixed into every object such as
68
+ # ActiveSupport's Object#try) are rejected.
69
+ if respond_to?(name) && method(name).owner <= ImageProcessing::Processor
70
+ receiver = self
71
+ elsif ImageProcessing.allowed_method?(@accumulator, name, [self.class::ACCUMULATOR_CLASS])
72
+ receiver = @accumulator
73
+ else
62
74
  fail Error, "#{name.inspect} is not a valid operation"
63
75
  end
64
76
 
65
- receiver = respond_to?(name) ? self : @accumulator
66
-
67
77
  if args.last.is_a?(Hash)
68
78
  kwargs = args.pop
69
79
  receiver.public_send(name, *args, **kwargs, &block)
@@ -1,3 +1,3 @@
1
1
  module ImageProcessing
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.1"
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,23 +7,24 @@ 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)
10
+ # Whether +name+ can be safely dispatched on +receiver+ when the name comes
11
+ # from user input. A name is allowed if the receiver doesn't define it, in
12
+ # which case it falls through to #method_missing and becomes an image
13
+ # operation, or if it's defined by one of +allowed_owners+ (or a subclass).
14
+ #
15
+ # Any other method is rejected: Ruby core methods such as Kernel#send or
16
+ # BasicObject#instance_eval, but also methods that other libraries mix into
17
+ # every object, such as ActiveSupport's Object#try. Allowing only known
18
+ # owners keeps the guard correct regardless of what else is loaded.
19
+ #
20
+ # Private methods are ignored, because public_send can't call them and
21
+ # forwards them to #method_missing instead. This keeps CLI options named
22
+ # like private Kernel methods (e.g. -format, -print) working.
23
+ def self.allowed_method?(receiver, name, allowed_owners)
24
+ owner = receiver.public_method(name.to_s).owner
25
+ allowed_owners.any? { |allowed_owner| owner <= allowed_owner }
23
26
  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
+ true
27
28
  end
28
29
 
29
30
  autoload :MiniMagick, "image_processing/mini_magick"
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.1.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić