The ELC Community Blog
A knowledge exchange on Ruby on Rails and Agile Development
Sandboxing in ruby
by stevend on October 22, 2007
A few weeks ago, I decided to make a rails-based game. I wanted to bring the strength of ruby's metaprogramming into the game world, so I investigated sandboxing user uploaded code blocks. The only ruby sandbox was written by Why the Lucky Stiff, and you can find complete details on it here:
The sandbox is an amazing hack on ruby's lookup tables to essentially allow a completely separate execution context with its completely own set of classes. The interesting part is how it interfaces with the outside world (the "Jungle"):
- Classes can be copied in from the Jungle using
, and exist in both places with separate definitions. The sandbox automatically does this with simple essentially classes like String, Object, Hash, Array, etc.1 Sandbox.import
- Classes can be proxied in from the Jungle using
. In this case, a proxy class is defined in the sandbox with exactly the same name as the outside class, but with only a two methods: const_missing & method_missing. When a method is called on the proxy, the sandbox is disabled and the actual method executes outside the sandbox. The result of the method is Marshalled into the sandbox, and it is enabled again.1 Sandbox.ref
- Objects can be copied into the sandbox using
as long as they're defined there. This is accomplished by marshalling1 Sandbox.set
- Objects can be returned from the sandbox at the end of a
call. This is accomplished by marshalling1 Sandbox.eval
Now you're ready to start writing your own applications using the sandbox. It's a pain to install in ruby 1.8.6, because it requires a small patch, but Why says that it works without patch in ruby 1.9.
Timeline
- Sliding Session Timeout plugin
- Readability Tips
- Leopard ImageMagick
- Transcoding with On2 and Ruby
- Safely exposing your app to a ruby Sandbox
- Sandboxing in ruby
- Its about the team
- RSpec Tutorial
- Amazon S3: Simple(?) Storage Service
- Ready for hockey season?
- A room hung with pictures is a room hung with thoughts
Comments