How to run untrusted Ruby code inside a safe sandbox?

$SAFE is not enough; you need to be at least at the level of Why’s freaky sandbox. However, I don’t know if that sandbox code is actively maintained or if he/they ever solved the holes such as infinite loops, etc.

Unsafe generally means hostile. If you can relax from hostile to, say, ‘naive’, and depending upon the requirements of your app, you might get away with sandboxing in Ruby. It’s not really a first-class scenario in the language design.

Even with that, though, you probably don’t need to go to the machine level of separation. I’d feel pretty safe using a sandbox in a separately spawned process, with your app functioning as a process manager to kill off any that manage to hang/flame. Now, that is a few orders of magnitude more work than your simple block above.

But remember and keep repeating, “SAFE can’t deal with hostile”.

Leave a Comment