safe_eval 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mike Harris
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = safe_eval
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Mike Harris. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "safe_eval"
8
+ gem.summary = %Q{safe_eval}
9
+ gem.description = %Q{safe_eval}
10
+ gem.email = "mharris717@gmail.com"
11
+ gem.homepage = "http://github.com/mharris717/safe_eval"
12
+ gem.authors = ["Mike Harris"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "safe_eval #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
@@ -0,0 +1,91 @@
1
+ require 'timeout'
2
+
3
+ DEFAULT_SAFE_LEVEL = 0 unless defined?(DEFAULT_SAFE_LEVEL)
4
+ def eval_safe_level
5
+ $eval_safe_level || DEFAULT_SAFE_LEVEL
6
+ end
7
+
8
+ class SafeEval
9
+ def self.with_level(level)
10
+ old = $eval_safe_level
11
+ $eval_safe_level = level
12
+ yield
13
+ ensure
14
+ $eval_safe_level = old
15
+ end
16
+ end
17
+
18
+ def safe_eval_fork(str,ops)
19
+ fork do
20
+ new_str = "$SAFE=#{eval_safe_level}; #{str}"
21
+ $res = res = eval(new_str)
22
+ puts "RES: #{res}"
23
+ end
24
+ Process.wait
25
+ end
26
+
27
+ def safe_eval_thread(str,ops={})
28
+ res = nil
29
+ level = ops[:safe] || eval_safe_level
30
+ Thread.new do
31
+ new_str = "$SAFE=#{level}; #{str}"
32
+ res = eval(new_str)
33
+ #puts "RES: #{res}"
34
+ end.join
35
+ res
36
+ end
37
+
38
+ class EvalError < SecurityError
39
+ end
40
+
41
+ def safe_eval(str,ops={})
42
+ if true
43
+ res = nil
44
+ seconds = ops[:timeout] || 5
45
+ Timeout::timeout(seconds) { res = safe_eval_thread(str,ops) }
46
+ #puts "Thread return: #{res}"
47
+ res
48
+ else
49
+ safe_eval_fork(str,ops)
50
+ end
51
+ rescue(SecurityError) => exp
52
+ exp = exp.exception("Eval code \"#{str}\"\n#{exp.message}")
53
+ raise exp
54
+ end
55
+
56
+ def bt
57
+ raise 'foo'
58
+ rescue => exp
59
+ return exp.backtrace.join("\n")
60
+ end
61
+
62
+ class Object
63
+ attr_accessor :stored_exception
64
+ def safe_instance_eval(str,ops={})
65
+ res = nil
66
+ level = ops[:safe] || eval_safe_level
67
+ debug_log "going to eval #{str} at level #{level} against #{inspect}"
68
+ Thread.new do
69
+ $SAFE = level if level > $SAFE
70
+ #new_str = "begin; #{str}; rescue(Exception) => e; raise e.exception(bt+e.message); end"
71
+ res = instance_eval(str)
72
+ end.join
73
+ res
74
+ rescue(SecurityError) => exp
75
+ exp = exp.exception("Eval code \"#{str}\"\n#{exp.message}")
76
+ raise exp
77
+ end
78
+ end
79
+
80
+ class Object
81
+ def app_instance_eval(str,ops={})
82
+ TT.log("app_instance_eval #{str}") do
83
+ #if str =~ /children\(:picks\).parent\(:players\)/
84
+ # 4
85
+ #else
86
+ safe_instance_eval(unescape(str),ops)
87
+ #end
88
+ #instance_eval(unescape(str))
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Foo
4
+ attr_accessor :bar
5
+ include FromHash
6
+ end
7
+
8
+ describe "SafeEval" do
9
+ fattr(:foo) { Foo.new(:bar => 1) }
10
+ it 'smoke' do
11
+ (2+2).should == 4
12
+ end
13
+ # it 'shouldnt let shellout' do
14
+ # SafeEval.with_level(4) { `rm -r -f /code/dfgdthryjtyhjtrr` }
15
+ # end
16
+ it 'safe shellout' do
17
+ str = "`rm -r -f /code/dfgdthryjtyhjtrr`"
18
+ lambda { 7.safe_instance_eval(str,:safe => 4) }.should raise_error(SecurityError)
19
+ end
20
+ it 'var change at 2' do
21
+ foo.safe_instance_eval('self.bar = 2')
22
+ foo.bar.should == 2
23
+ end
24
+ it 'var change at 4' do
25
+ lambda { foo.safe_instance_eval('self.bar = 2', :safe => 4) }.should raise_error(SecurityError)
26
+ foo.bar.should == 1
27
+ end
28
+ it 'with level' do
29
+ SafeEval.with_level(4) do
30
+ lambda { foo.safe_instance_eval('self.bar = 2') }.should raise_error(SecurityError)
31
+ foo.bar.should == 1
32
+ end
33
+ foo.safe_instance_eval('self.bar = 2')
34
+ foo.bar.should == 2
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'safe_eval'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'mharris_ext'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
11
+
12
+ def debug_log(*args)
13
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_eval
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Mike Harris
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-01 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: safe_eval
35
+ email: mharris717@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - lib/safe_eval.rb
50
+ - spec/safe_eval_spec.rb
51
+ - spec/spec.opts
52
+ - spec/spec_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/mharris717/safe_eval
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: safe_eval
83
+ test_files:
84
+ - spec/safe_eval_spec.rb
85
+ - spec/spec_helper.rb