image_processing 2.2.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: 5b790f3dd08c310a2413cb8cb6b33a9c602edb3509cdd1de2c8f94ebea702f17
4
- data.tar.gz: 99d87927f2a6603939332f61292c2b2b582fecc5832b4562a92e8a5170e862d9
3
+ metadata.gz: 3f36b1a00b35a05403a2c6dd77f94912dbbca311cb6939556cbb7e9eb66cd702
4
+ data.tar.gz: bce160c3fcff5a046447c334c4c5440386080d355b0899c30b5b1ea6e89922c8
5
5
  SHA512:
6
- metadata.gz: 00b2e0b0b250473b55e4ca415fd00f1c9fa50458e2c280a4110949838c344cb49c52b5afe023e0c75b8e435460f55517a1aeb8497ae1adf72fdca1de3fab59e2
7
- data.tar.gz: a87c6791a8dbc06d5e40a6a64aaefd7803a382aac5071e238f7f7bface47d026405603bb6da9cc4534f9472f65d2e70bf4614cd75b9962506781a2818ad81b73
6
+ metadata.gz: 61b02322af114f81116e9f3600d5f331cacb8b6c299a78a15c9891d56beb92ee56f4181b3349b64087bd3d801aa90029ee31e5e897847ea4bb9739589bcc3305
7
+ data.tar.gz: ba62cf185ca3ac4a19582936c3fbca176b7e8312185fd3505438b247e839a877ac53781cf518311b6a5ff3601111529393a00649b0e395b5b42bbeec3de27f8d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
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
+
1
7
  ## 2.2.0 (2026-09-29)
2
8
 
3
9
  * Save in the `#convert` format when the destination path has no extension, such as a `/dev/fd/N` path (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, ...)
@@ -248,10 +248,11 @@ module ImageProcessing
248
248
  # Applies options from the provided hash.
249
249
  def apply_options(magick, define: {}, **options)
250
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)
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])
255
256
  raise Error, "#{option.inspect} is not a valid option"
256
257
  end
257
258
 
@@ -60,12 +60,20 @@ module ImageProcessing
60
60
  # around via the #operation meta-builder, #method_missing, or a nested
61
61
  # #send. This is the single point every operation is finally dispatched
62
62
  # through, so enforcing the guard here closes all of those paths.
63
- 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
64
74
  fail Error, "#{name.inspect} is not a valid operation"
65
75
  end
66
76
 
67
- receiver = respond_to?(name) ? self : @accumulator
68
-
69
77
  if args.last.is_a?(Hash)
70
78
  kwargs = args.pop
71
79
  receiver.public_send(name, *args, **kwargs, &block)
@@ -1,3 +1,3 @@
1
1
  module ImageProcessing
2
- VERSION = "2.2.0"
2
+ VERSION = "2.2.1"
3
3
  end
@@ -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.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić