Ruby: creating a sandboxed eval?

烂漫一生 提交于 2019-11-28 06:24:02
Pablo Fernandez

You might want to check the 'taint' method and related stuff. This is a good reference:

http://ruby-doc.com/docs/ProgrammingRuby/html/taint.html

Despite that, I can't advise you enough against storing code and evaluating it, it's a security risk that should be avoided and most times there's a simpler way of solving your problems.

If you need to evaluate complex rules and predicates I'd recommend a rule engine to create a nice DSL. Haven't used one in ruby but this one looks good to me:

http://treetop.rubyforge.org/index.html

Cheers

Emirikol

you can do that with a sandboxing gem, https://github.com/tario/shikashi, which allows you to whitelist methods.
credit to https://stackoverflow.com/a/8704768/188355

Assuming you're on at least ruby 1.8, you can run a proc at a different safe level.

def my_unsafe_function
  # possible unsafe stuff
end

proc {
  $SAFE = 4  # change level only inside this proc
  my_unsafe_function
}.call

However, you should rethink whether you really need to store ruby code in the DB. Are users of the app going to be modifying this stored code, and why? If they aren't, why not put the code in the app's files instead? I don't know your setup, but it should be possible to move the logic out of the DB.

If you want to remove some methods from your object, you can check this:
remove_method or undef_method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!