Safebox.eval added. It is like Kernel.eval, but in a box and with <rescue Object>.

This commit is contained in:
Denis Knauf 2010-03-31 19:00:23 +02:00
parent 01aa356e70
commit 13af6a5a6a
3 changed files with 38 additions and 2 deletions

19
bin/box2.rb Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/ruby
require 'safebox'
_ = _e = nil
$stdout.print "(0)$ "
db = {}
db.taint
STDIN.each.each_with_index do |line, i|
ret = Safebox.run line, Class.new( Safebox::Box), db, _, _e
if :value == ret.first
_ = ret.last
$stdout.puts "=> #{ret.last.inspect}"
else
_e = ret.last
$stdout.puts ret.last.inspect, ret.last.backtrace[0..-4].map( &"\t%s".method( :%)), "\tSafebox:1:in `run'"
end
$stdout.print "(#{i+1})$ "
end

View file

@ -3,7 +3,7 @@ require 'safebox/safebox'
class Safebox::Box class Safebox::Box
attr_reader :_, :db attr_reader :_, :db
def initialize db, _ = nil, _e def initialize db, _ = nil, _e = nil
@_, @db, @_e = _, db, _e @_, @db, @_e = _, db, _e
end end

View file

@ -8,7 +8,7 @@ module Safebox
$SAFE = 4 $SAFE = 4
this = box.new *paras this = box.new *paras
begin begin
[:value, this.instance_eval( exe, "Safebox")] [:value, String === exe ? this.instance_eval( exe, "Safebox") : this.instance_eval( &exe)]
rescue Object rescue Object
[:exception, $!] [:exception, $!]
end end
@ -23,5 +23,22 @@ module Safebox
end end
end end
alias new_class create_class alias new_class create_class
def on_exception exc
$stdout.puts "#{exc} (#{exc.class})\n\t#{exc.backtrace.join"\n\t"}"
rescue Object
on_exception $!
end
def eval *paras, &exe
ret = self.run( *paras, &exe)
case ret.first
when :exception # Really unsecure. Somebody can create an own exception with own #to_s, #class or #backtrace.
on_exception ret.last
nil
when :value then ret.last
end
end
public :eval
end end
end end