Class: Moxy::SandboxEval
- Inherits:
-
Object
- Object
- Moxy::SandboxEval
- Defined in:
- lib/moxy/sandbox_eval.rb
Instance Method Summary collapse
- #evaluate(unsafe_code) ⇒ Object
-
#initialize(verbose = false) ⇒ SandboxEval
constructor
A new instance of SandboxEval.
- #setup_code ⇒ Object
Constructor Details
#initialize(verbose = false) ⇒ SandboxEval
Returns a new instance of SandboxEval.
5 6 7 |
# File 'lib/moxy/sandbox_eval.rb', line 5 def initialize(verbose=false) @verbose = verbose end |
Instance Method Details
#evaluate(unsafe_code) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/moxy/sandbox_eval.rb', line 12 def evaluate(unsafe_code) # run something begin FakeFS.activate! cmd = <<-EOF #{setup_code} FakeFS::FileSystem.clear $SAFE = 3 $stdout = StringIO.new begin #{unsafe_code} end EOF puts "evaling:\n-------->>\n#{cmd}\n-------->>" if @verbose stdout_id = $stdout.to_i $stdout = StringIO.new result = Thread.new { eval cmd, TOPLEVEL_BINDING }.value [:ok, result] rescue SecurityError => e return [:illegal, e] rescue Exception => e return [:error, e] ensure $stdout = IO.new(stdout_id) FakeFS.deactivate! end end |
#setup_code ⇒ Object
8 9 10 |
# File 'lib/moxy/sandbox_eval.rb', line 8 def setup_code "include WebMock::API" end |