secure 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -1 +1,12 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'spec/rake/spectask'
4
+
5
+ desc "Run all examples"
6
+ Spec::Rake::SpecTask.new('spec') do |t|
7
+ t.spec_opts << "--colour --format specdoc --loadby mtime --reverse"
8
+ t.spec_opts << "-r spec/spec_helper"
9
+ t.spec_files = FileList['spec/**/*.rb']
10
+ end
11
+
12
+ task :default => :spec
data/lib/secure.rb CHANGED
@@ -1,5 +1,17 @@
1
1
  require "secure/version"
2
+ require "secure/response"
3
+ require "secure/errors"
4
+ require "secure/guard_thread"
5
+ require "secure/runner"
2
6
 
3
7
  module Secure
4
- # Your code goes here...
8
+ class << self
9
+ def run(opts = {})
10
+ Runner.new(opts).run do
11
+ yield
12
+ end
13
+ end
14
+
15
+ alias :ly :run
16
+ end
5
17
  end
@@ -0,0 +1,4 @@
1
+ module Secure
2
+ class TimeoutError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,12 @@
1
+ module Secure
2
+ class GuardThread < Thread
3
+ class << self
4
+ def kill_thread_on_timeout(secs, thread)
5
+ Thread.start(secs, thread) do |s, t|
6
+ t.join(s)
7
+ t.raise(TimeoutError, "This thread has taken more than #{s} seconds")
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module Secure
2
+ class Response
3
+ attr_reader :error, :value
4
+
5
+ def initialize(error, value)
6
+ @error = error
7
+ @value = value
8
+ end
9
+
10
+ def success?
11
+ error.nil?
12
+ end
13
+
14
+ class << self
15
+ def success(value)
16
+ Response.new(nil, value)
17
+ end
18
+
19
+ def error(error)
20
+ Response.new(error, nil)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ module Secure
2
+ class Runner
3
+ def initialize(opts)
4
+ @timeout = opts[:timeout] || 1
5
+ end
6
+
7
+ def guard_threads
8
+ @guard_threads || []
9
+ end
10
+
11
+ def run
12
+ thread = Thread.start do
13
+ $SAFE=3
14
+ Response.success(yield)
15
+ end
16
+
17
+ guard_threads << GuardThread.kill_thread_on_timeout(@timeout, thread)
18
+
19
+ thread.value
20
+ rescue StandardError => e
21
+ Response.error(e)
22
+ ensure
23
+ #guard_threads.each(&:exit!)
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module Secure
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,15 @@
1
+ module Secure
2
+ describe Response do
3
+ it "knows if it is successful" do
4
+ response = Response.success(2)
5
+ response.should be_success
6
+ response.value.should == 2
7
+ end
8
+
9
+ it "knows if it is an error" do
10
+ response = Response.error(SecurityError.new)
11
+ response.should_not be_success
12
+ response.error.should be_a(SecurityError)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ describe Secure do
2
+ it "should execute normal code as expected" do
3
+ response = Secure.ly do
4
+ 4 + 4
5
+ end
6
+ response.should be_success
7
+ response.value.should == 8
8
+ end
9
+
10
+ it "should not allow an eval to be called" do
11
+ response = Secure.ly do
12
+ eval "45"
13
+ end
14
+ response.should_not be_success
15
+ response.error.should be_a(SecurityError)
16
+ end
17
+
18
+ it "should not allow system calls" do
19
+ response = Secure.ly do
20
+ system("echo hi")
21
+ end
22
+ response.should_not be_success
23
+ response.error.should be_a(SecurityError)
24
+ end
25
+
26
+ it "should have safe value set" do
27
+ response = Secure.ly do
28
+ $SAFE
29
+ end
30
+ response.should be_success
31
+ response.value.should == 3
32
+ end
33
+
34
+ it "should not have affected the global safe value" do
35
+ response = Secure.ly {}
36
+ response.should be_success
37
+ $SAFE.should == 0
38
+ end
39
+
40
+ it "should kill infinite loops" do
41
+ response = Secure.ly :timeout => 0.005 do
42
+ while true; end
43
+ end
44
+ response.should_not be_success
45
+ response.error.should be_a(Secure::TimeoutError)
46
+ end
47
+
48
+ it "should kill all threads after running" do
49
+ Secure.ly do
50
+ 10
51
+ end
52
+ Thread.list.should have(1).things
53
+ end
54
+ end
@@ -0,0 +1 @@
1
+ require 'secure'
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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
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-27 00:00:00 +05:30
18
+ date: 2011-08-28 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -33,8 +33,15 @@ files:
33
33
  - Gemfile
34
34
  - Rakefile
35
35
  - lib/secure.rb
36
+ - lib/secure/errors.rb
37
+ - lib/secure/guard_thread.rb
38
+ - lib/secure/response.rb
39
+ - lib/secure/runner.rb
36
40
  - lib/secure/version.rb
37
41
  - secure.gemspec
42
+ - spec/secure/response_spec.rb
43
+ - spec/secure_spec.rb
44
+ - spec/spec_helper.rb
38
45
  has_rdoc: true
39
46
  homepage: ""
40
47
  licenses: []
@@ -69,5 +76,7 @@ rubygems_version: 1.6.2
69
76
  signing_key:
70
77
  specification_version: 3
71
78
  summary: gem to do things securely using ruby $SAFE
72
- test_files: []
73
-
79
+ test_files:
80
+ - spec/secure/response_spec.rb
81
+ - spec/secure_spec.rb
82
+ - spec/spec_helper.rb