2

我正在尝试将 jruby 用作脚本语言(用于编写游戏任务),但我需要防止使用具有潜在危险的类,例如Dir,ProcessRubyVM.

我已经找到了一种将单个方法和类列入黑名单的繁琐方法:

class Dir
    def Dir.[]
    end
    def Dir.chdir
    end
    def Dir.chroot
    end
    def Dir.delete
    end
    def Dir.entries
    end
    def Dir.exist?
    end
    def Dir.exists?
    end
    def Dir.foreach
    end
    def Dir.getwd
    end
    def Dir.glob
    end
    def Dir.home
    end
    def Dir.mkdir
    end
    def Dir.initialitze
    end
    def Dir.open
    end
    def Dir.pwd
    end
    def Dir.rmdir
    end
    def Dir.unlink
    end
    def close
    end
    def each
    end
    def fileno
    end
    def inspect
    end
    def path
    end
    def pos
    end
    def read
    end
    def rewind
    end
    def seek
    end
    def tell
    end
    def to_path
    end
end

但我真的希望有一种更简单的方法来执行这项任务,或者甚至更好地将应该能够使用的类列入白名单。

4

1 回答 1

4

您可以将特定方法设为私有:

class Dir
  private :close, :each ....
  private_class_method :glob, :home ...
end

或者你可以取消定义整个班级(对我来说是个坏主意):

Object.send(:remove_const, :Dir)

或者删除方法(从 Ruby 核心类中删除方法也是一个坏主意):

class Dir
  remove_method :close, :each ....
end
于 2016-04-20T10:22:43.453 回答