Execute ruby entered in view in controller

HI, I have a requirement where a user could enter ruby scripts in the
view( typically a text area) and the controller should pick this text on submit and execute the script(content of text area) line by line.

How should I do it in rails?

Well you could just use eval, but that sounds like an incredibly bad
idea security wise.

Fred

Frederick Cheung wrote:

+1 to Fred's comment about security - there are some solutions that can mitigate the security problems, but eval'ing code sent from the web is a BAD IDEA. If you have *any* user access control in your system, this can get around it.

There are a couple things you might be interested in:

- _why's Sandbox class. It's mostly a proof of concept, but it might have some ideas. Note that while it can keep some bad things from happening, you'll still need to give the sandboxed code access to the DB (that is why you're evaling Ruby from the web, right?)

- at the very least, some kind of usage of $SAFE, which could protect your environment a little. But then you've got threading problems...

- if you just want a console-like environment, Kawaii (http:// github.com/eviltrout/kawaii) might save you from re-inventing the wheel.

Finally, to answer your actual question, you'd use a rescue clause to catch execution errors. Check your favorite Ruby reference for more details.

--Matt Jones

+1 to Fred's comment about security - there are some solutions that can mitigate the security problems, but eval'ing code sent from the web is a BAD IDEA. If you have *any* user access control in your system, this can get around it.

There are a couple things you might be interested in:

One thing I've been thinking recently is that jruby might be neat for this, assuming you can just lean on Java's security stuff (no idea if you can).

Fred

Frederick Cheung wrote: