diff options
author | Jeremy Evans <code@jeremyevans.net> | 2022-08-10 13:02:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-10 13:02:19 -0700 |
commit | bfa6a8ddc84fffe0aef5a0f91b417167e124dbbf (patch) | |
tree | f84003447830d01379d2cfd34329b55e7f773480 /spec/ruby/core | |
parent | d115a06037e900e1ba29d2293e1d9e4964499ff2 (diff) | |
download | ruby-bfa6a8ddc84fffe0aef5a0f91b417167e124dbbf.tar.gz |
Only allow procs created by Symbol#to_proc to call public methods
Fixes [Bug #18826]
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Diffstat (limited to 'spec/ruby/core')
-rw-r--r-- | spec/ruby/core/kernel/fixtures/warn_core_method.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/symbol/to_proc_spec.rb | 27 |
2 files changed, 28 insertions, 1 deletions
diff --git a/spec/ruby/core/kernel/fixtures/warn_core_method.rb b/spec/ruby/core/kernel/fixtures/warn_core_method.rb index f5dee6b668..fd82562404 100644 --- a/spec/ruby/core/kernel/fixtures/warn_core_method.rb +++ b/spec/ruby/core/kernel/fixtures/warn_core_method.rb @@ -1,6 +1,6 @@ raise 'should be run without RubyGems' if defined?(Gem) -def deprecated(n=1) +public def deprecated(n=1) # puts nil, caller(0), nil warn "use X instead", uplevel: n end diff --git a/spec/ruby/core/symbol/to_proc_spec.rb b/spec/ruby/core/symbol/to_proc_spec.rb index 47f2a939ab..81939e0046 100644 --- a/spec/ruby/core/symbol/to_proc_spec.rb +++ b/spec/ruby/core/symbol/to_proc_spec.rb @@ -46,6 +46,33 @@ describe "Symbol#to_proc" do end end + ruby_version_is "3.2" do + it "only calls public methods" do + body = proc do + public def pub; @a << :pub end + protected def pro; @a << :pro end + private def pri; @a << :pri end + attr_reader :a + end + + @a = [] + singleton_class.class_eval(&body) + tap(&:pub) + proc{tap(&:pro)}.should raise_error(NoMethodError) + proc{tap(&:pri)}.should raise_error(NoMethodError) + @a.should == [:pub] + + @a = [] + c = Class.new(&body) + o = c.new + o.instance_variable_set(:@a, []) + o.tap(&:pub) + proc{tap(&:pro)}.should raise_error(NoMethodError) + proc{o.tap(&:pri)}.should raise_error(NoMethodError) + o.a.should == [:pub] + end + end + it "raises an ArgumentError when calling #call on the Proc without receiver" do -> { :object_id.to_proc.call |