From a12951eb1e406ec9df9a556d4eace40d03b297d3 Mon Sep 17 00:00:00 2001 From: Matt Valentine-House Date: Thu, 19 Nov 2026 10:48:55 +0000 Subject: [PATCH] Reject non-Proc callbacks in Packer#register_type_internal the Packer invokes callbacks with rb_proc_call_with_block which doesn't do any type checking that the proc is actually a proc, so it's possible for an oject that's not a proc to reach rb_vm_invoke_proc and be treated as an rb_proc_t. This commit adds a type check and raise to register_type_internal. --- ext/msgpack/packer_class.c | 4 ++++ spec/packer_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/ext/msgpack/packer_class.c b/ext/msgpack/packer_class.c index b3654a36..a1612209 100644 --- a/ext/msgpack/packer_class.c +++ b/ext/msgpack/packer_class.c @@ -365,6 +365,10 @@ static VALUE Packer_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type); } + if (!rb_obj_is_proc(proc)) { + rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Proc)", rb_obj_class(proc)); + } + msgpack_packer_ext_registry_put(self, &pk->ext_registry, ext_module, ext_type, 0, proc); if (ext_module == rb_cSymbol) { diff --git a/spec/packer_spec.rb b/spec/packer_spec.rb index 09268acc..d833e90b 100644 --- a/spec/packer_spec.rb +++ b/spec/packer_spec.rb @@ -361,6 +361,16 @@ def self.from_msgpack_ext(data) packer.register_type(0x02, ValueTwo, &:to_msgpack_ext) end + it 'rejects a callback that is not a Proc' do + packer = MessagePack::Packer.new + not_a_proc = Object.new + def not_a_proc.to_proc; "not a proc"; end + + expect { packer.register_type(0x01, ValueOne, not_a_proc) }.to raise_error(TypeError, /expected Proc/) + expect { packer.register_type_internal(0x01, ValueOne, "not a proc") }.to raise_error(TypeError, /expected Proc/) + expect(packer.registered_types).to be_empty + end + it 'returns a Hash which contains map of Class and type' do packer = MessagePack::Packer.new packer.register_type(0x01, ValueOne, :to_msgpack_ext)