mruby_sandbox 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4153c1ee7cc6e9c720dc076ac9c5f36b4af7bd18
4
- data.tar.gz: 31afc8cb58cdc4abd03d83b1cf72207c8e65c789
3
+ metadata.gz: 5a2f7112f407c42a5d6991e90114f42df58f06c0
4
+ data.tar.gz: 81862829d3b0771964526afedf4302c185aed23e
5
5
  SHA512:
6
- metadata.gz: 79148c19851f533cc0acd46408dae7f86905376cfabcfecfcfd0a2372101c8641109f945d75d42eaec06e000f387db272de17589804ab084afe9b5db26b4289a
7
- data.tar.gz: 1d3d6d9ce89fa5e97a97b5bc1859b1c2cc1127dce92b5672f93d9c2eb637962722f9f80abdb25c88683836566b12f1ffb63ee250da59af076a0dc6efa066acf7
6
+ metadata.gz: 8f55f88f1588de6dfbc388ab7c4eabeb4576241e13b556dd2fc7fb55f2f6e492ee1f989b295b800ea6fed627f5887e414c3c2af2c711ee698c9caf423dc340eb
7
+ data.tar.gz: a940f8ed2d48b70671d6df40aa329264f2d16a0bfe278052f5bf16b769b9722c0b16d645b7bb254705fc31fd631568daeb837faf8ae055ec63dbdeedfbb1fd47
data/Gemfile.lock CHANGED
@@ -1,15 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mruby_sandbox (0.4.0)
5
- pipe_rpc (~> 0.3)
4
+ mruby_sandbox (0.5.0)
5
+ pipe_rpc (~> 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  diff-lcs (1.2.5)
11
11
  json (1.8.3)
12
- pipe_rpc (0.3.2)
12
+ pipe_rpc (1.0.0)
13
13
  json
14
14
  rspec (3.4.0)
15
15
  rspec-core (~> 3.4.0)
data/lib/mruby_sandbox.rb CHANGED
@@ -1,75 +1,66 @@
1
1
  require 'pipe_rpc'
2
- require 'forwardable'
3
2
  require 'logger'
3
+
4
4
  require_relative 'mruby_sandbox/version'
5
5
  require_relative 'mruby_sandbox/server'
6
6
 
7
- class MrubySandbox
8
- extend Forwardable
9
-
10
- class << self
11
- attr_writer :logger
7
+ module MrubySandbox
8
+ class MrubySandbox < PipeRpc::Gateway
9
+ class << self
10
+ attr_writer :logger
12
11
 
13
- def logger
14
- @logger ||= Logger.new(STDOUT)
12
+ def logger
13
+ @logger ||= Logger.new(STDOUT)
14
+ end
15
15
  end
16
- end
17
16
 
18
- def initialize
19
- input, w = IO.pipe
20
- r, output = IO.pipe
21
- @pid = spawn(executable, in: r, out: w)
22
- r.close; w.close
17
+ def initialize
18
+ input, w = IO.pipe
19
+ r, output = IO.pipe
20
+ @pid = spawn(executable, in: r, out: w)
21
+ r.close; w.close
23
22
 
24
- self.class.logger.debug "Sandbox(#{__id__}) created with process #{@pid}"
23
+ self.class.logger.debug "Sandbox(#{__id__}) created with process #{@pid}"
25
24
 
26
- @hub = PipeRpc::Hub.new(input: input, output: output)
27
- @data = {}
25
+ super(input: input, output: output)
28
26
 
29
- rescue Errno::ENOENT => e
30
- STDERR.puts "The mruby_sandbox executable is missing. Run `build_mruby_sandbox` first."
31
- fail e
32
- end
27
+ rescue Errno::ENOENT => e
28
+ STDERR.puts "The mruby_sandbox executable is missing. Run `build_mruby_sandbox` first."
29
+ fail e
30
+ end
33
31
 
34
- attr_reader :data
32
+ def client
33
+ clients[:default]
34
+ end
35
35
 
36
- def client
37
- client_for :default
38
- end
36
+ def eval(*args)
37
+ client.eval(*args)
38
+ end
39
39
 
40
- delegate [:clear, :eval] => :client
41
- delegate [:add_server, :rmv_server, :client_for, :channel, :handle_message, :loop_iteration=,
42
- :on_sent, :on_received, :on_incoming_request] => :@hub
43
- alias_method :export, :add_server
40
+ def start_logging
41
+ on_sent do |message|
42
+ self.class.logger.debug "Sandbox(#{__id__}) sent: #{message}"
43
+ end
44
44
 
45
- def start_logging
46
- @hub.logger = proc do |message|
47
- self.class.logger.debug "Sandbox(#{__id__}) #{message}"
45
+ on_received do |message|
46
+ self.class.logger.debug "Sandbox(#{__id__}) received: #{message}"
47
+ end
48
48
  end
49
- end
50
49
 
51
- def reflect_logger_server=(server)
52
- client.debug_mode(!!server)
53
- if server
54
- add_server(reflect_logger: server)
55
- else
56
- rmv_server(:reflect_logger)
50
+ def close
51
+ return unless @pid
52
+ super
53
+ Process.kill 9, @pid
54
+ Process.wait @pid
55
+ self.class.logger.debug "Sandbox(#{__id__}) teared down and process #{@pid} killed"
56
+ @pid = nil
57
57
  end
58
- end
59
58
 
60
- def tear_down
61
- return unless @pid
62
- @hub.cancel
63
- Process.kill 9, @pid
64
- Process.wait @pid
65
- self.class.logger.debug "Sandbox(#{__id__}) teared down and process #{@pid} killed"
66
- @pid = nil
67
- end
68
-
69
- private
59
+ private
70
60
 
71
- def executable
72
- current_dir = File.expand_path(File.dirname(__FILE__))
73
- File.join(current_dir, '../bin/mruby_sandbox')
61
+ def executable
62
+ current_dir = File.expand_path(File.dirname(__FILE__))
63
+ File.join(current_dir, '../bin/mruby_sandbox')
64
+ end
74
65
  end
75
66
  end
@@ -1,3 +1,3 @@
1
- class MrubySandbox
2
- VERSION = "0.4.0"
1
+ module MrubySandbox
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,77 +1,43 @@
1
- class Sandbox < BasicObject
2
- def initialize(main)
3
- @main = main
4
- @main.sandbox = self
1
+ class Sandbox < PipeRpc::Gateway
2
+ def initialize
5
3
  input = IO.new(0, 'r') #STDIN
6
4
  output = IO.new(1, 'w') #STDOUT
7
- @hub = PipeRpc::Hub.new(input: input, output: output)
8
- add_server(default: Controller.new(self))
9
- ::Kernel.loop { iteration }
5
+ super(input: input, output: output)
10
6
  end
11
7
 
12
- def eval(code, file = '', lineno = 0)
13
- @main.eval(code, file, lineno)
14
- end
8
+ def set_up(main)
9
+ servers.add(default: main)
15
10
 
16
- def add_server(args = {})
17
- @hub.add_server(args)
18
- end
11
+ sandbox = self
19
12
 
20
- def client_for(server_name)
21
- @hub.client_for(server_name)
22
- end
13
+ main.define_singleton_method :add_server do |*args|
14
+ sandbox.servers.add(*args)
15
+ end
23
16
 
24
- def debug_mode(debug = true)
25
- @hub.logger = debug ? :reflect : false
26
- end
17
+ main.define_singleton_method :client_for do |server|
18
+ sandbox.clients[server]
19
+ end
27
20
 
28
- private
29
-
30
- def iteration
31
- @hub.handle_message # blocks every iteration
32
- rescue ::Exception => e
33
- # reflect ALL rescueable errors back to the managing process
34
- backtrace = e.backtrace
35
- @hub.send_response PipeRpc::ErrorResponse.new(error: { code: -32603, data: { message: e.message, backtrace: backtrace } })
36
- ::Kernel.raise e
37
- end
38
- end
39
-
40
- class Sandbox::Controller
41
- def initialize(sandbox)
42
- @sandbox = sandbox
43
- end
44
-
45
- def eval(code, file = '', lineno = 0)
46
- @sandbox.eval(code, file, lineno)
47
- end
48
-
49
- def debug_mode(debug = true)
50
- @sandbox.debug_mode(debug)
21
+ loop do
22
+ handle_message # blocks every iteration
23
+ end
51
24
  end
52
25
  end
53
26
 
54
27
  # Interface for untrusted code to communicate with the outside
55
28
  class << self
56
- attr_writer :sandbox
57
-
58
29
  def eval(code, file = '', lineno = 0)
59
30
  instance_eval(code, file, lineno)
60
31
  end
61
32
 
62
- def export(args = {})
63
- @sandbox.add_server(args)
64
- end
65
-
66
- def client_for(server = :default)
67
- @sandbox.client_for(server)
33
+ def client
34
+ clients[:default]
68
35
  end
69
- alias_method :client, :client_for
70
36
  end
71
37
 
72
38
  # Remove constants from global namespace so untrusted code cannot mess around with it.
73
- Sandbox::GC = Object.remove_const(:GC)
74
- Sandbox::ObjectSpace = Object.remove_const(:ObjectSpace)
39
+ Object.remove_const(:GC)
40
+ Object.remove_const(:ObjectSpace)
75
41
  Sandbox::IO = Object.remove_const(:IO)
76
42
  Sandbox::PipeRpc = Object.remove_const(:PipeRpc)
77
- Object.remove_const(:Sandbox).new(self)
43
+ Object.remove_const(:Sandbox).new.set_up(self)
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = ["build_mruby_sandbox"]
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_runtime_dependency "pipe_rpc", "~> 0.3"
24
+ spec.add_runtime_dependency "pipe_rpc", "~> 1.0"
25
25
  spec.add_development_dependency "bundler", "~> 1.8"
26
26
  spec.add_development_dependency "rspec", "~> 3.4"
27
27
  spec.add_development_dependency "rspec-its"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mruby_sandbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Aue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-04 00:00:00.000000000 Z
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pipe_rpc
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.3'
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.3'
26
+ version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement