Skip to content

Instantly share code, notes, and snippets.

@mlen
Created February 9, 2011 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlen/819105 to your computer and use it in GitHub Desktop.
Save mlen/819105 to your computer and use it in GitHub Desktop.
Calling privilidged methods from sandbox ($SAFE = 4)
#!/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