secure 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/secure.rb CHANGED
@@ -2,6 +2,8 @@ require "secure/version"
2
2
  require "secure/response"
3
3
  require "secure/errors"
4
4
  require "secure/guard_thread"
5
+ require "secure/parent_process"
6
+ require "secure/child_process"
5
7
  require "secure/runner"
6
8
 
7
9
  module Secure
@@ -0,0 +1,31 @@
1
+ module Secure
2
+ class ChildProcess
3
+ def initialize(opts, read_file, write_file)
4
+ read_file.close
5
+ @pipe = write_file
6
+ @timeout = opts[:timeout] || 1
7
+ end
8
+
9
+ def guard_threads
10
+ @guard_threads || []
11
+ end
12
+
13
+ def safely_run_block
14
+ thread = Thread.start do
15
+ $SAFE=3
16
+ yield
17
+ end
18
+
19
+ guard_threads << GuardThread.kill_thread_on_timeout(@timeout, thread)
20
+
21
+ Response.success(thread.value)
22
+ rescue Exception => e
23
+ Response.error(e)
24
+ end
25
+
26
+ def execute
27
+ ret = safely_run_block { yield }
28
+ @pipe.write(Marshal.dump(ret))
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module Secure
2
+ class ParentProcess
3
+ def initialize(read_file, write_file)
4
+ @pipe = read_file
5
+ write_file.close
6
+ end
7
+
8
+ def execute
9
+ Marshal.load(@pipe.read)
10
+ end
11
+ end
12
+ end
data/lib/secure/runner.rb CHANGED
@@ -1,26 +1,25 @@
1
1
  module Secure
2
2
  class Runner
3
3
  def initialize(opts)
4
- @timeout = opts[:timeout] || 1
5
- end
6
-
7
- def guard_threads
8
- @guard_threads || []
4
+ @opts = opts
9
5
  end
10
6
 
11
7
  def run
12
- thread = Thread.start do
13
- $SAFE=3
14
- Response.success(yield)
15
- end
8
+ read_file, write_file = IO.pipe
16
9
 
17
- guard_threads << GuardThread.kill_thread_on_timeout(@timeout, thread)
10
+ child = fork do
11
+ begin
12
+ ChildProcess.new(@opts, read_file, write_file).execute { yield }
13
+ ensure
14
+ exit!
15
+ end
16
+ end
18
17
 
19
- thread.value
20
- rescue SecurityError, TimeoutError => e
21
- Response.error(e)
18
+ Process.wait(child)
19
+ ParentProcess.new(read_file, write_file).execute
22
20
  ensure
23
- #guard_threads.each(&:exit!)
21
+ read_file.close unless read_file.closed?
22
+ write_file.close unless write_file.closed?
24
23
  end
25
24
  end
26
25
  end
@@ -1,3 +1,3 @@
1
1
  module Secure
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secure
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 5
10
- version: 0.0.5
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tejas Dinkar
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-29 00:00:00 +05:30
18
+ date: 2011-08-30 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -33,8 +33,10 @@ files:
33
33
  - Gemfile
34
34
  - Rakefile
35
35
  - lib/secure.rb
36
+ - lib/secure/child_process.rb
36
37
  - lib/secure/errors.rb
37
38
  - lib/secure/guard_thread.rb
39
+ - lib/secure/parent_process.rb
38
40
  - lib/secure/response.rb
39
41
  - lib/secure/runner.rb
40
42
  - lib/secure/version.rb