Class: Sandbox::Safe

Inherits:
Full
  • Object
show all
Defined in:
lib/sandbox/safe.rb

Instance Method Summary collapse

Instance Method Details

#activate!Object



7
8
9
10
11
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
# File 'lib/sandbox/safe.rb', line 7

def activate!
  activate_fakefs

  keep_singleton_methods(:Kernel, KERNEL_S_METHODS)
  keep_singleton_methods(:Symbol, SYMBOL_S_METHODS)
  keep_singleton_methods(:String, STRING_S_METHODS)
  keep_singleton_methods(:IO, IO_S_METHODS)

  keep_methods(:Kernel, KERNEL_METHODS)
  keep_methods(:NilClass, NILCLASS_METHODS)
  keep_methods(:Symbol, SYMBOL_METHODS)
  keep_methods(:TrueClass, TRUECLASS_METHODS)
  keep_methods(:FalseClass, FALSECLASS_METHODS)
  keep_methods(:Enumerable, ENUMERABLE_METHODS)
  keep_methods(:String, STRING_METHODS)

  # FIXME: Blacklisting Object methods is not a scalable solution.
  # Whitelisting using #keep_methods is safer.
  remove_method(:Object, :java_import)

  Kernel.class_eval do
    def `(*args)
      raise NoMethodError, "` is unavailable"
    end

    def system(*args)
      raise NoMethodError, "system is unavailable"
    end
  end
end

#activate_fakefsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sandbox/safe.rb', line 38

def activate_fakefs
  require "fileutils"

  # unfortunately, the authors of FakeFS used `extend self` in FileUtils, instead of `module_function`.
  # I fixed it for them
  (FakeFS::FileUtils.methods - Module.methods - Kernel.methods).each do |module_method_name|
    FakeFS::FileUtils.send(:module_function, module_method_name)
  end

  import  FakeFS
  ref     FakeFS::Dir
  ref     FakeFS::File
  ref     FakeFS::FileTest
  import  FakeFS::FileUtils #import FileUtils because it is a module

  # this is basically what FakeFS.activate! does, but we want to do it in the sandbox
  # so we have to live with this:
  eval "    Object.class_eval do\n      remove_const(:Dir)\n      remove_const(:File)\n      remove_const(:FileTest)\n      remove_const(:FileUtils)\n\n      const_set(:Dir,       FakeFS::Dir)\n      const_set(:File,      FakeFS::File)\n      const_set(:FileUtils, FakeFS::FileUtils)\n      const_set(:FileTest,  FakeFS::FileTest)\n    end\n\n    [Dir, File, FileUtils, FileTest].each do |fake_class|\n      fake_class.class_eval do\n        def self.class_eval\n          raise NoMethodError, \"class_eval is unavailable\"\n        end\n        def self.instance_eval\n          raise NoMethodError, \"instance_eval is unavailable\"\n        end\n      end\n    end\n  RUBY\n\n  FakeFS::FileSystem.clear\nend\n"

#eval(code, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/sandbox/safe.rb', line 83

def eval(code, options={})
  if seconds = options[:timeout]
    sandbox_timeout(code, seconds) do
      super code
    end
  else
    super code
  end
end