Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

メタプログラミングRuby | 魔術 | サンドボックス

内容

信頼出来ないコードを安全な環境で実行する。
特定の処理をセーフレベルを指定したproc内で実行することで
任意のセキュリティレベルで実行し、安全を確保することが出来ます。

サンプル

# encoding: utf-8
require "pry"

def level2_sandbox(&code)
  proc {
    $SAFE = 2
    yield
  }.call
end

begin
  level2_sandbox {File.delete("hoge.txt")}
rescue Exception => ex
  puts ex
end

出力

Insecure operation `delete' at level 2