Skip to content

Commit

Permalink
Merge pull request #95 from merefield/improve_safe_ruby
Browse files Browse the repository at this point in the history
Improve safe ruby
  • Loading branch information
merefield authored Jun 5, 2024
2 parents 835fed6 + 68e2899 commit 9aa7bd0
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 295 deletions.
29 changes: 15 additions & 14 deletions lib/discourse_chatbot/safe_ruby/lib/constant_whitelist.rb
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
83 changes: 42 additions & 41 deletions lib/discourse_chatbot/safe_ruby/lib/make_safe_code.rb
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
Loading

0 comments on commit 9aa7bd0

Please sign in to comment.