Created
February 9, 2011 19:37
-
-
Save mlen/819105 to your computer and use it in GitHub Desktop.
Calling privilidged methods from sandbox ($SAFE = 4)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Sandbox | |
@@call = -> this, method, *args do | |
this.method(method).call(*args) | |
end | |
def self.method_added name | |
super | |
return if name == :initialize || @adding | |
@adding = true | |
alias_method (name.to_s + '_orig'), name | |
remove_method name | |
define_method(name, ->(*args) do | |
@@call[self, name.to_s + '_orig', *args] | |
end) | |
@adding = false | |
end | |
def initialize | |
Thread.abort_on_exception = true | |
Thread.new do | |
$SAFE = 4 | |
eval "test" | |
end.join | |
rescue StandardError, SecurityError | |
puts $! | |
end | |
def test | |
puts "hello" | |
end | |
end | |
Sandbox.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment