safemode 1.3.1 → 2.0.3
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.
- checksums.yaml +5 -5
- data/Gemfile +3 -16
- data/README.markdown +16 -18
- data/Rakefile +5 -36
- data/demo.rb +2 -0
- data/lib/action_view/template_handlers/safe_erb.rb +2 -2
- data/lib/action_view/template_handlers/safe_haml.rb +1 -1
- data/lib/action_view/template_handlers/safemode_handler.rb +5 -5
- data/lib/haml/safemode.rb +11 -11
- data/lib/safemode/blankslate.rb +8 -2
- data/lib/safemode/core_ext.rb +4 -4
- data/lib/safemode/core_jails.rb +21 -24
- data/lib/safemode/exceptions.rb +3 -3
- data/lib/safemode/jail.rb +8 -13
- data/lib/safemode/parser.rb +159 -68
- data/lib/safemode/scope.rb +28 -20
- data/lib/safemode.rb +12 -20
- data/safemode.gemspec +33 -62
- data/test/test_erb_eval.rb +17 -17
- data/test/test_for_loop_jail.rb +27 -0
- data/test/test_helper.rb +45 -22
- data/test/test_jail.rb +29 -4
- data/test/test_parser_node_allowlist.rb +13 -0
- data/test/test_safemode_eval.rb +184 -23
- data/test/test_safemode_parser.rb +145 -8
- metadata +42 -101
- data/VERSION +0 -1
- data/lib/ruby_parser_string_io_patch.diff +0 -194
- data/lib/rubyparser_bug.rb +0 -8
- /data/{LICENCSE → LICENSE} +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b2feca54490235c1a1dc543fbdb04ac9d3f81495b8f1126cc4d7b411c71fd963
|
|
4
|
+
data.tar.gz: 8a2279cf395544183a5947822bf68a1b7cb190caf093544eae667ca79d8423d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b965556669e1de0b4b966cd7d69a8a55397965a6fbad4b559e91736ed400ccaea3e2f4c50f2dcdef739aa8fb4e2891f927c31a42b471b7b3a2b10020eb981b7c
|
|
7
|
+
data.tar.gz: a0ea7e3948e167e017e27f1e56a1d3a3ad6d6701368aebcd04a0599ebd89d6aab1fd6328ba2624983852e55bb5c4e654480a90a8249239968caea8766d1eb568
|
data/Gemfile
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
gem 'ruby2ruby', ">= 2.0.6"
|
|
5
|
-
gem "ruby_parser", ">= 3.2.0"
|
|
3
|
+
source 'http://rubygems.org'
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
# Include everything needed to run rake, tests, features, etc.
|
|
9
|
-
group :development do
|
|
10
|
-
gem "shoulda", ">= 0"
|
|
11
|
-
gem "rdoc", "~> 3.12"
|
|
12
|
-
gem "bundler", "~> 1.0"
|
|
13
|
-
gem "jeweler", ">= 0"
|
|
14
|
-
gem "rcov", :platforms => :ruby_18
|
|
15
|
-
gem "simplecov", :platforms => [:ruby_19, :ruby_20, :ruby_21, :ruby_22, :ruby_23, :ruby_24]
|
|
16
|
-
gem "test-unit", :platforms => [:ruby_19, :ruby_20, :ruby_21, :ruby_22, :ruby_23, :ruby_24]
|
|
17
|
-
gem "rake"
|
|
18
|
-
end
|
|
5
|
+
gemspec
|
data/README.markdown
CHANGED
|
@@ -23,9 +23,11 @@ For manual evaluation of Ruby code and ERB templates see demo.rb
|
|
|
23
23
|
You can use the ActionView template handlers by registering them, e.g., in
|
|
24
24
|
a config/initializer file like this:
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
```ruby
|
|
27
|
+
# in config/intializer/safemode_tempate_handlers.rb
|
|
28
|
+
ActionView::Template.register_template_handler :serb, ActionView::TemplateHandlers::SafeErb
|
|
29
|
+
ActionView::Template.register_template_handler :haml, ActionView::TemplateHandlers::SafeHaml
|
|
30
|
+
```
|
|
29
31
|
|
|
30
32
|
If you register the ERB template handler for the file extension :erb be aware
|
|
31
33
|
that this most probably will break when your application tries to render an
|
|
@@ -36,11 +38,13 @@ You will then have to "whitelist" all method calls to the objects that are
|
|
|
36
38
|
registered as template variables by explicitely allowing access to them. You
|
|
37
39
|
can do that by defining a Safemode::Jail class for your classes, like so:
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
```ruby
|
|
42
|
+
class User
|
|
43
|
+
class Jail < Safemode::Jail
|
|
44
|
+
allow :name
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
```
|
|
44
48
|
|
|
45
49
|
This will allow your template users to access the name method on your User
|
|
46
50
|
objects.
|
|
@@ -52,9 +56,9 @@ class is only accessible when returned by a method or passed into a template.
|
|
|
52
56
|
For more details about the concepts behind Safemode please refer to the
|
|
53
57
|
following blog posts until a more comprehensive writeup is available:
|
|
54
58
|
|
|
55
|
-
* Initial reasoning:
|
|
56
|
-
* Refined concept:
|
|
57
|
-
* ActionView ERB handler:
|
|
59
|
+
* Initial reasoning: http://www.artweb-design.de/2008/2/5/sexy-theme-templating-with-haml-safemode-finally
|
|
60
|
+
* Refined concept: http://www.artweb-design.de/2008/2/17/sending-ruby-to-the-jail-an-attemp-on-a-haml-safemode
|
|
61
|
+
* ActionView ERB handler: http://www.artweb-design.de/2008/4/22/an-erb-safemode-handler-for-actionview
|
|
58
62
|
|
|
59
63
|
### Dependencies
|
|
60
64
|
|
|
@@ -63,15 +67,9 @@ Requires the gems:
|
|
|
63
67
|
* RubyParser
|
|
64
68
|
* Ruby2Ruby
|
|
65
69
|
|
|
66
|
-
As of writing RubyParser alters StringIO and thus breaks usage with Rails.
|
|
67
|
-
See [http://www.zenspider.com/pipermail/parsetree/2008-April/000026.html](http://www.zenspider.com/pipermail/parsetree/2008-April/000026.html)
|
|
68
|
-
|
|
69
|
-
A patch is included that fixes this issue and can be applied to RubyParser.
|
|
70
|
-
See lib/ruby\_parser\_string\_io\_patch.diff
|
|
71
|
-
|
|
72
70
|
### Credits
|
|
73
71
|
|
|
74
|
-
* Sven Fuchs - Maintainer
|
|
72
|
+
* Sven Fuchs - Initial Maintainer
|
|
75
73
|
* Peter Cooper
|
|
76
74
|
* Matthias Viehweger
|
|
77
75
|
* Ohad Levy
|
data/Rakefile
CHANGED
|
@@ -19,27 +19,6 @@ end
|
|
|
19
19
|
end
|
|
20
20
|
require 'rake'
|
|
21
21
|
|
|
22
|
-
require 'jeweler'
|
|
23
|
-
Jeweler::Tasks.new do |gem|
|
|
24
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
|
25
|
-
gem.name = "safemode"
|
|
26
|
-
gem.homepage = "http://github.com/svenfuchs/safemode"
|
|
27
|
-
gem.license = "MIT"
|
|
28
|
-
gem.summary = %Q{A library for safe evaluation of Ruby code based on ParseTree/RubyParser and Ruby2Ruby}
|
|
29
|
-
gem.description = %Q{A library for safe evaluation of Ruby code based on RubyParser and Ruby2Ruby. Provides Rails ActionView template handlers for ERB and Haml.}
|
|
30
|
-
gem.email = "ohadlevy@gmail.com"
|
|
31
|
-
gem.authors = [
|
|
32
|
-
"Sven Fuchs",
|
|
33
|
-
"Peter Cooper",
|
|
34
|
-
"Matthias Viehweger",
|
|
35
|
-
"Kingsley Hendrickse",
|
|
36
|
-
"Ohad Levy",
|
|
37
|
-
"Dmitri Dolguikh",
|
|
38
|
-
]
|
|
39
|
-
# dependencies defined in Gemfile
|
|
40
|
-
end
|
|
41
|
-
Jeweler::RubygemsDotOrgTasks.new
|
|
42
|
-
|
|
43
22
|
require 'rake/testtask'
|
|
44
23
|
Rake::TestTask.new(:test) do |test|
|
|
45
24
|
test.libs << 'lib' << 'test'
|
|
@@ -47,27 +26,17 @@ Rake::TestTask.new(:test) do |test|
|
|
|
47
26
|
test.verbose = true
|
|
48
27
|
end
|
|
49
28
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
Rake::Task[:test].execute
|
|
55
|
-
end
|
|
56
|
-
else
|
|
57
|
-
require 'rcov/rcovtask'
|
|
58
|
-
Rcov::RcovTask.new do |test|
|
|
59
|
-
test.libs << 'test'
|
|
60
|
-
test.pattern = 'test/**/test_*.rb'
|
|
61
|
-
test.verbose = true
|
|
62
|
-
test.rcov_opts << '--exclude "gems/*"'
|
|
63
|
-
end
|
|
29
|
+
desc "Generate coverage report for tests"
|
|
30
|
+
task :coverage do |cov|
|
|
31
|
+
ENV['COVERAGE'] = 'true'
|
|
32
|
+
Rake::Task[:test].execute
|
|
64
33
|
end
|
|
65
34
|
|
|
66
35
|
task :default => :test
|
|
67
36
|
|
|
68
37
|
require 'rdoc/task'
|
|
69
38
|
Rake::RDocTask.new do |rdoc|
|
|
70
|
-
version =
|
|
39
|
+
version = Gem::Specification.find_by_name('safemode').version
|
|
71
40
|
|
|
72
41
|
rdoc.rdoc_dir = 'rdoc'
|
|
73
42
|
rdoc.title = "safemode #{version}"
|
data/demo.rb
CHANGED
|
@@ -20,9 +20,9 @@ module ActionView
|
|
|
20
20
|
|
|
21
21
|
# code = ::ERB.new(src, nil, @view.erb_trim_mode).src
|
|
22
22
|
code = ::ERB.new("<% __in_erb_template=true %>#{src}", nil, erb_trim_mode, '@output_buffer').src
|
|
23
|
-
# Ruby 1.9 prepends an encoding to the source. However this is
|
|
23
|
+
# Ruby 1.9+ prepends an encoding to the source. However this is
|
|
24
24
|
# useless because you can only set an encoding on the first line
|
|
25
|
-
|
|
25
|
+
src.sub(/\A#coding:.*\n/, '') : src
|
|
26
26
|
|
|
27
27
|
code.gsub!('\\','\\\\\\') # backslashes would disappear in compile_template/modul_eval, so we escape them
|
|
28
28
|
|
|
@@ -5,23 +5,23 @@ module ActionView
|
|
|
5
5
|
def valid_assigns(assigns)
|
|
6
6
|
assigns = assigns.reject{|key, value| skip_assigns.include?(key) }
|
|
7
7
|
end
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
def delegate_methods(view)
|
|
10
|
-
[ :render, :params, :flash ] +
|
|
11
|
-
helper_methods(view) +
|
|
10
|
+
[ :render, :params, :flash ] +
|
|
11
|
+
helper_methods(view) +
|
|
12
12
|
ActionController::Routing::Routes.named_routes.helpers
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def helper_methods(view)
|
|
16
16
|
view.class.included_modules.collect {|m| m.instance_methods(false) }.flatten.map(&:to_sym)
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def skip_assigns
|
|
20
20
|
[ "_cookies", "_flash", "_headers", "_params", "_request",
|
|
21
21
|
"_response", "_session", "before_filter_chain_aborted",
|
|
22
22
|
"ignore_missing_templates", "logger", "request_origin",
|
|
23
23
|
"template", "template_class", "url", "variables_added",
|
|
24
|
-
"view_paths" ]
|
|
24
|
+
"view_paths" ]
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
end
|
data/lib/haml/safemode.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require 'haml'
|
|
2
2
|
|
|
3
|
-
module Haml
|
|
3
|
+
module Haml
|
|
4
4
|
class Buffer
|
|
5
5
|
class Jail < Safemode::Jail
|
|
6
6
|
allow :push_script, :push_text, :_hamlout, :open_tag
|
|
@@ -8,33 +8,33 @@ module Haml
|
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
module Haml
|
|
11
|
+
module Haml
|
|
12
12
|
class Engine
|
|
13
|
-
def precompile_for_safemode(filename, ignore_assigns = [], delegate_methods = [])
|
|
13
|
+
def precompile_for_safemode(filename, ignore_assigns = [], delegate_methods = [])
|
|
14
14
|
@precompiled.gsub!('\\','\\\\\\') # backslashes would disappear in compile_template/modul_eval, so we escape them
|
|
15
|
-
|
|
16
|
-
<<-CODE
|
|
15
|
+
|
|
16
|
+
<<-CODE
|
|
17
17
|
buffer = Haml::Buffer.new(#{options_for_buffer.inspect})
|
|
18
18
|
local_assigns = local_assigns.merge :_hamlout => buffer
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
handler = ActionView::TemplateHandlers::SafeHaml
|
|
21
21
|
assigns = handler.valid_assigns(@template.assigns)
|
|
22
22
|
methods = handler.delegate_methods(self)
|
|
23
23
|
code = %Q(#{code});
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
box = Safemode::Box.new(self, methods, #{filename.inspect}, 0)
|
|
26
|
-
box.eval(code, assigns, local_assigns, &lambda{ yield })
|
|
27
|
-
buffer.buffer
|
|
26
|
+
box.eval(code, assigns, local_assigns, &lambda{ yield })
|
|
27
|
+
buffer.buffer
|
|
28
28
|
CODE
|
|
29
29
|
|
|
30
30
|
# preamble = "buffer = Haml::Buffer.new(#{options_for_buffer.inspect})
|
|
31
31
|
# local_assigns = local_assigns.merge :_hamlout => buffer
|
|
32
32
|
# assigns = @template.assigns.reject{|key, value| #{ignore_assigns.inspect}.include?(key) };".gsub("\n", ';')
|
|
33
|
-
#
|
|
33
|
+
#
|
|
34
34
|
# postamble = "box = Safemode::Box.new(self, #{delegate_methods.inspect})
|
|
35
35
|
# box.eval(code, assigns, local_assigns, &lambda{ yield })
|
|
36
36
|
# buffer.buffer".gsub("\n", ';')
|
|
37
|
-
#
|
|
37
|
+
#
|
|
38
38
|
# preamble + "code = %Q(#{@precompiled});" + postamble
|
|
39
39
|
end
|
|
40
40
|
end
|
data/lib/safemode/blankslate.rb
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
module Safemode
|
|
2
2
|
class Blankslate
|
|
3
|
-
@@allow_instance_methods = ['class', 'methods', 'respond_to?', 'respond_to_missing?', 'to_s'
|
|
4
|
-
@@allow_class_methods = ['methods', 'new', 'name', '<', 'ancestors', '==']
|
|
3
|
+
@@allow_instance_methods = ['class', 'methods', 'respond_to?', 'respond_to_missing?', 'to_s']
|
|
4
|
+
@@allow_class_methods = ['singleton_class?', 'methods', 'new', 'name', '<', 'ancestors', '=='] # < needed in Rails Object#subclasses_of
|
|
5
|
+
if defined?(JRUBY_VERSION)
|
|
6
|
+
# JRuby seems to silently fail to remove method_missing
|
|
7
|
+
# (also see https://github.com/jruby/jruby/blob/9.1.7.0/core/src/main/java/org/jruby/RubyModule.java#L1109)
|
|
8
|
+
@@allow_class_methods << 'method_missing'
|
|
9
|
+
(@@allow_class_methods << ['singleton_method_undefined', 'singleton_method_added']).flatten! # needed for JRuby support
|
|
10
|
+
end
|
|
5
11
|
|
|
6
12
|
silently { undef_methods(*instance_methods.map(&:to_s) - @@allow_instance_methods) }
|
|
7
13
|
class << self
|
data/lib/safemode/core_ext.rb
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
module Kernel
|
|
1
|
+
module Kernel
|
|
2
2
|
def silently(&blk)
|
|
3
3
|
old_verbose, $VERBOSE = $VERBOSE, nil
|
|
4
4
|
yield
|
|
5
5
|
$VERBOSE = old_verbose
|
|
6
|
-
end
|
|
6
|
+
end
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
class Module
|
|
9
|
+
class Module
|
|
10
10
|
def undef_methods(*methods)
|
|
11
11
|
methods.each { |name| undef_method(name) }
|
|
12
12
|
end
|
|
@@ -29,7 +29,7 @@ end
|
|
|
29
29
|
# Safemode.jail collect{ |obj| obj.to_jail }
|
|
30
30
|
# end
|
|
31
31
|
# end
|
|
32
|
-
#
|
|
32
|
+
#
|
|
33
33
|
# class Hash
|
|
34
34
|
# def to_jail
|
|
35
35
|
# hash = {}
|
data/lib/safemode/core_jails.rb
CHANGED
|
@@ -17,15 +17,11 @@ module Safemode
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def core_classes
|
|
20
|
-
klasses = [ Array, Float, Hash, Range, String, Symbol, Time, NilClass, FalseClass, TrueClass ]
|
|
20
|
+
klasses = [ Array, Float, Hash, Integer, Range, String, Symbol, Time, NilClass, FalseClass, TrueClass ]
|
|
21
21
|
klasses << Date if defined? Date
|
|
22
22
|
klasses << DateTime if defined? DateTime
|
|
23
|
-
if
|
|
24
|
-
|
|
25
|
-
else
|
|
26
|
-
klasses << Bignum
|
|
27
|
-
klasses << Fixnum
|
|
28
|
-
end
|
|
23
|
+
klasses << Data if defined? Data # Ruby 3.2 addition
|
|
24
|
+
klasses << Set if defined? Set # Ruby 3.2 addition
|
|
29
25
|
klasses
|
|
30
26
|
end
|
|
31
27
|
|
|
@@ -46,31 +42,21 @@ module Safemode
|
|
|
46
42
|
# whitelisted methods for core classes ... kind of arbitrary selection
|
|
47
43
|
@@methods_whitelist = {
|
|
48
44
|
'Array' => %w(any? assoc at blank? collect collect! compact compact!
|
|
49
|
-
concat delete delete_at delete_if each each_index
|
|
50
|
-
fetch fill first flatten flatten! hash include? index
|
|
51
|
-
indexes indices inject insert join last length map map!
|
|
45
|
+
concat delete delete_at delete_if each each_index each_with_index
|
|
46
|
+
empty? fetch fill first flatten flatten! hash include? index
|
|
47
|
+
indexes indices inject insert join last length map map! max min
|
|
52
48
|
nitems pop push present? rassoc reject reject! reverse
|
|
53
49
|
reverse! reverse_each rindex select shift size slice
|
|
54
|
-
slice! sort sort! transpose uniq uniq! unshift
|
|
55
|
-
zip),
|
|
56
|
-
|
|
57
|
-
'Bignum' => %w(abs blank? ceil chr coerce div divmod downto floor hash
|
|
58
|
-
integer? modulo next nonzero? present? quo remainder round
|
|
59
|
-
singleton_method_added size step succ times to_f to_i
|
|
60
|
-
to_int to_s truncate upto zero?),
|
|
61
|
-
|
|
62
|
-
'Fixnum' => %w(abs blank? ceil chr coerce div divmod downto floor id2name
|
|
63
|
-
integer? modulo modulo next nonzero? present? quo remainder
|
|
64
|
-
round singleton_method_added size step succ times to_f to_i
|
|
65
|
-
to_int to_s to_sym truncate upto zero?),
|
|
50
|
+
slice! sort sort! transpose to_sentence uniq uniq! unshift
|
|
51
|
+
values_at zip),
|
|
66
52
|
|
|
67
53
|
'Float' => %w(abs blank? ceil coerce div divmod finite? floor hash
|
|
68
54
|
infinite? integer? modulo nan? nonzero? present? quo
|
|
69
55
|
remainder round singleton_method_added step to_f to_i
|
|
70
56
|
to_int to_s truncate zero?),
|
|
71
57
|
|
|
72
|
-
'Hash' => %w(any? blank? clear delete delete_if each each_key
|
|
73
|
-
|
|
58
|
+
'Hash' => %w(any? blank? clear delete delete_if each each_key each_pair
|
|
59
|
+
each_value each_with_index empty? fetch dig has_key? has_value?
|
|
74
60
|
include? index invert key? keys length member? merge merge!
|
|
75
61
|
present? rec_merge! rehash reject reject! select shift
|
|
76
62
|
size sort store update value? values values_at),
|
|
@@ -116,6 +102,17 @@ module Safemode
|
|
|
116
102
|
'DateTime' => %w(blank? hour min new_offset newof of offset present? sec
|
|
117
103
|
sec_fraction strftime to_datetime_default_s to_json zone),
|
|
118
104
|
|
|
105
|
+
'Data' => %w(deconstruct deconstruct_keys hash members),
|
|
106
|
+
|
|
107
|
+
'Set' => %w(any? blank? clear clone collect count delete delete?
|
|
108
|
+
delete_if difference disjoint? each each_with_index
|
|
109
|
+
each_with_object empty? filter find first flatten
|
|
110
|
+
flatten! hash include? inject intersect? intersection
|
|
111
|
+
join keep_if length map max member? merge min
|
|
112
|
+
present? reject reject! replace select select!
|
|
113
|
+
size sort sort_by subset? superset? union
|
|
114
|
+
uniq zip),
|
|
115
|
+
|
|
119
116
|
'NilClass' => %w(blank? duplicable? present? to_f to_i),
|
|
120
117
|
|
|
121
118
|
'FalseClass' => %w(blank? duplicable? present?),
|
data/lib/safemode/exceptions.rb
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
module Safemode
|
|
2
2
|
class Error < RuntimeError; end
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
class SecurityError < Error
|
|
5
5
|
@@types = { :const => 'constant',
|
|
6
6
|
:xstr => 'shell command',
|
|
7
7
|
:fcall => 'method',
|
|
8
8
|
:vcall => 'method',
|
|
9
9
|
:gvar => 'global variable' }
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
def initialize(type, value = nil)
|
|
12
12
|
type = @@types[type] if @@types.include?(type)
|
|
13
13
|
super "Safemode doesn't allow to access '#{type}'" + (value ? " on #{value}" : '')
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
class NoMethodError < Error
|
|
18
18
|
def initialize(method, jail, source = nil)
|
|
19
19
|
super "undefined method '#{method}' for #{jail}" + (source ? " (#{source})" : '')
|
data/lib/safemode/jail.rb
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
module Safemode
|
|
2
|
-
class Jail < Blankslate
|
|
1
|
+
module Safemode
|
|
2
|
+
class Jail < Blankslate
|
|
3
3
|
def initialize(source = nil)
|
|
4
4
|
@source = source
|
|
5
5
|
end
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
def to_jail
|
|
8
8
|
self
|
|
9
9
|
end
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
def to_s
|
|
12
12
|
@source.to_s
|
|
13
13
|
end
|
|
14
|
-
|
|
15
|
-
def method_missing(method, *args, &block)
|
|
14
|
+
|
|
15
|
+
def method_missing(method, *args, **kwargs, &block)
|
|
16
16
|
if @source.is_a?(Class)
|
|
17
17
|
unless self.class.allowed_class_method?(method)
|
|
18
18
|
raise Safemode::NoMethodError.new(".#{method}", self.class.name, @source.name)
|
|
@@ -22,18 +22,13 @@ module Safemode
|
|
|
22
22
|
raise Safemode::NoMethodError.new("##{method}", self.class.name, @source.class.name)
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
# As every call to an object in the eval'ed string will be jailed by the
|
|
27
27
|
# parser we don't need to "proactively" jail arrays and hashes. Likewise we
|
|
28
28
|
# don't need to jail objects returned from a jail. Doing so would provide
|
|
29
29
|
# "double" protection, but it also would break using a return value in an if
|
|
30
30
|
# statement, passing them to a Rails helper etc.
|
|
31
|
-
@source.send(method, *args, &block)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# needed for compatibility with 1.8.7; remove this method once 1.8.7 support has been dropped
|
|
35
|
-
def respond_to?(method, *)
|
|
36
|
-
respond_to_missing?(method)
|
|
31
|
+
@source.send(method, *args, **kwargs, &block)
|
|
37
32
|
end
|
|
38
33
|
|
|
39
34
|
def respond_to_missing?(method_name, include_private = false)
|