Safebox.eval added. It is like Kernel.eval, but in a box and with <rescue Object>.
This commit is contained in:
parent
01aa356e70
commit
13af6a5a6a
19
bin/box2.rb
Executable file
19
bin/box2.rb
Executable 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
|
|
@ -3,7 +3,7 @@ require 'safebox/safebox'
|
|||
class Safebox::Box
|
||||
attr_reader :_, :db
|
||||
|
||||
def initialize db, _ = nil, _e
|
||||
def initialize db, _ = nil, _e = nil
|
||||
@_, @db, @_e = _, db, _e
|
||||
end
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ module Safebox
|
|||
$SAFE = 4
|
||||
this = box.new *paras
|
||||
begin
|
||||
[:value, this.instance_eval( exe, "Safebox")]
|
||||
[:value, String === exe ? this.instance_eval( exe, "Safebox") : this.instance_eval( &exe)]
|
||||
rescue Object
|
||||
[:exception, $!]
|
||||
end
|
||||
|
@ -23,5 +23,22 @@ module Safebox
|
|||
end
|
||||
end
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue