如何只允许访问某些特定的 ruby 类?
How to allow access to only some specific ruby classes?
我正在尝试使用 jruby 作为脚本语言(用于编写游戏任务),但我需要防止使用具有潜在危险的 类,例如 Dir
、Process
或 RubyVM
.
我已经找到了一种将单个方法列入黑名单的麻烦方法 类:
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
但我真的希望有一种更简单的方法来执行此任务,或者更好地将应该能够使用的 类 列入白名单。
您可以将特定方法设为私有:
class Dir
private :close, :each ....
private_class_method :glob, :home ...
end
或者你可以取消定义整个 class(对我来说是个坏主意):
Object.send(:remove_const, :Dir)
或删除方法(从 Ruby 核心 class 中删除方法也是个坏主意):
class Dir
remove_method :close, :each ....
end
我正在尝试使用 jruby 作为脚本语言(用于编写游戏任务),但我需要防止使用具有潜在危险的 类,例如 Dir
、Process
或 RubyVM
.
我已经找到了一种将单个方法列入黑名单的麻烦方法 类:
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
但我真的希望有一种更简单的方法来执行此任务,或者更好地将应该能够使用的 类 列入白名单。
您可以将特定方法设为私有:
class Dir
private :close, :each ....
private_class_method :glob, :home ...
end
或者你可以取消定义整个 class(对我来说是个坏主意):
Object.send(:remove_const, :Dir)
或删除方法(从 Ruby 核心 class 中删除方法也是个坏主意):
class Dir
remove_method :close, :each ....
end