-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from merefield/improve_safe_ruby
Improve safe ruby
- Loading branch information
Showing
4 changed files
with
301 additions
and
295 deletions.
There are no files selected for viewing
29 changes: 15 additions & 14 deletions
29
lib/discourse_chatbot/safe_ruby/lib/constant_whitelist.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
ALLOWED_CONSTANTS= [ | ||
:Object, :Module, :Class, :BasicObject, :Kernel, :NilClass, :NIL, :Data, :TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding, | ||
:Comparable, :Enumerable, :String, :Symbol, :Exception, :SystemExit, :SignalException, :Interrupt, :StandardError, :TypeError, | ||
:ArgumentError, :IndexError, :KeyError, :RangeError, :ScriptError, :SyntaxError, :LoadError, :NotImplementedError, :NameError, | ||
:NoMethodError, :RuntimeError, :SecurityError, :NoMemoryError, :EncodingError, :SystemCallError, :Errno, :ZeroDivisionError, | ||
:FloatDomainError, :Numeric, :Integer, :Fixnum, :Float, :Bignum, :Array, :Hash, :Struct, :RegexpError, :Regexp, | ||
:MatchData, :Marshal, :Range, :IOError, :EOFError, :IO, :STDIN, :STDOUT, :STDERR, :Time, :Random, | ||
:Signal, :Proc, :LocalJumpError, :SystemStackError, :Method, :UnboundMethod, :Binding, :Math, :Enumerator, | ||
:StopIteration, :RubyVM, :Thread, :TOPLEVEL_BINDING, :ThreadGroup, :Mutex, :ThreadError, :Fiber, :FiberError, :Rational, :Complex, | ||
:RUBY_VERSION, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :RUBY_ENGINE, | ||
:TracePoint, :ARGV, :Gem, :RbConfig, :Config, :CROSS_COMPILING, :Date, :ConditionVariable, :Queue, :SizedQueue, :MonitorMixin, :Monitor, | ||
:Exception2MessageMapper, :IRB, :RubyToken, :RubyLex, :Readline, :RUBYGEMS_ACTIVATION_MONITOR | ||
] | ||
class SafeRuby | ||
ALLOWED_CONSTANTS= [ | ||
:Object, :Module, :Class, :BasicObject, :Kernel, :NilClass, :NIL, :Data, :TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding, | ||
:Comparable, :Enumerable, :String, :Symbol, :Exception, :SystemExit, :SignalException, :Interrupt, :StandardError, :TypeError, | ||
:ArgumentError, :IndexError, :KeyError, :RangeError, :ScriptError, :SyntaxError, :LoadError, :NotImplementedError, :NameError, | ||
:NoMethodError, :RuntimeError, :SecurityError, :NoMemoryError, :EncodingError, :SystemCallError, :Errno, :ZeroDivisionError, | ||
:FloatDomainError, :Numeric, :Integer, :Fixnum, :Float, :Bignum, :Array, :Hash, :Struct, :RegexpError, :Regexp, | ||
:MatchData, :Marshal, :Range, :IOError, :EOFError, :IO, :STDIN, :STDOUT, :STDERR, :Time, :Random, | ||
:Signal, :Proc, :LocalJumpError, :SystemStackError, :Method, :UnboundMethod, :Binding, :Math, :Enumerator, | ||
:StopIteration, :RubyVM, :Thread, :TOPLEVEL_BINDING, :ThreadGroup, :Mutex, :ThreadError, :Fiber, :FiberError, :Rational, :Complex, | ||
:RUBY_VERSION, :RUBY_RELEASE_DATE, :RUBY_PLATFORM, :RUBY_PATCHLEVEL, :RUBY_REVISION, :RUBY_DESCRIPTION, :RUBY_COPYRIGHT, :RUBY_ENGINE, | ||
:TracePoint, :ARGV, :Gem, :RbConfig, :Config, :CROSS_COMPILING, :Date, :ConditionVariable, :Queue, :SizedQueue, :MonitorMixin, :Monitor, | ||
:Exception2MessageMapper, :IRB, :RubyToken, :RubyLex, :Readline, :RUBYGEMS_ACTIVATION_MONITOR | ||
] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
# frozen_string_literal: true | ||
class SafeRuby | ||
MAKE_SAFE_CODE = <<-STRING | ||
def keep_singleton_methods(klass, singleton_methods) | ||
klass = Object.const_get(klass) | ||
singleton_methods = singleton_methods.map(&:to_sym) | ||
undef_methods = (klass.singleton_methods - singleton_methods) | ||
MAKE_SAFE_CODE = <<-STRING | ||
def keep_singleton_methods(klass, singleton_methods) | ||
klass = Object.const_get(klass) | ||
singleton_methods = singleton_methods.map(&:to_sym) | ||
undef_methods = (klass.singleton_methods - singleton_methods) | ||
undef_methods.each do |method| | ||
klass.singleton_class.send(:undef_method, method) | ||
end | ||
undef_methods.each do |method| | ||
klass.singleton_class.send(:undef_method, method) | ||
end | ||
end | ||
def keep_methods(klass, methods) | ||
klass = Object.const_get(klass) | ||
methods = methods.map(&:to_sym) | ||
undef_methods = (klass.methods(false) - methods) | ||
undef_methods.each do |method| | ||
klass.send(:undef_method, method) | ||
end | ||
end | ||
def keep_methods(klass, methods) | ||
klass = Object.const_get(klass) | ||
methods = methods.map(&:to_sym) | ||
undef_methods = (klass.methods(false) - methods) | ||
undef_methods.each do |method| | ||
klass.send(:undef_method, method) | ||
def clean_constants | ||
(Object.constants - #{ALLOWED_CONSTANTS}).each do |const| | ||
Object.send(:remove_const, const) if defined?(const) | ||
end | ||
end | ||
end | ||
def clean_constants | ||
(Object.constants - #{ALLOWED_CONSTANTS}).each do |const| | ||
Object.send(:remove_const, const) if defined?(const) | ||
keep_singleton_methods(:Kernel, #{KERNEL_S_METHODS}) | ||
keep_singleton_methods(:Symbol, #{SYMBOL_S_METHODS}) | ||
keep_singleton_methods(:String, #{STRING_S_METHODS}) | ||
keep_singleton_methods(:IO, #{IO_S_METHODS}) | ||
keep_methods(:Kernel, #{KERNEL_METHODS}) | ||
keep_methods(:NilClass, #{NILCLASS_METHODS}) | ||
keep_methods(:TrueClass, #{TRUECLASS_METHODS}) | ||
keep_methods(:FalseClass, #{FALSECLASS_METHODS}) | ||
keep_methods(:Enumerable, #{ENUMERABLE_METHODS}) | ||
keep_methods(:String, #{STRING_METHODS}) | ||
Kernel.class_eval do | ||
def `(*args) | ||
raise NoMethodError, "` is unavailable" | ||
end | ||
end | ||
keep_singleton_methods(:Kernel, #{KERNEL_S_METHODS}) | ||
keep_singleton_methods(:Symbol, #{SYMBOL_S_METHODS}) | ||
keep_singleton_methods(:String, #{STRING_S_METHODS}) | ||
keep_singleton_methods(:IO, #{IO_S_METHODS}) | ||
keep_methods(:Kernel, #{KERNEL_METHODS}) | ||
keep_methods(:NilClass, #{NILCLASS_METHODS}) | ||
keep_methods(:TrueClass, #{TRUECLASS_METHODS}) | ||
keep_methods(:FalseClass, #{FALSECLASS_METHODS}) | ||
keep_methods(:Enumerable, #{ENUMERABLE_METHODS}) | ||
keep_methods(:String, #{STRING_METHODS}) | ||
Kernel.class_eval do | ||
def `(*args) | ||
raise NoMethodError, "` is unavailable" | ||
end | ||
def system(*args) | ||
raise NoMethodError, "system is unavailable" | ||
end | ||
end | ||
def system(*args) | ||
raise NoMethodError, "system is unavailable" | ||
end | ||
end | ||
clean_constants | ||
clean_constants | ||
STRING | ||
STRING | ||
end |
Oops, something went wrong.