Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Improve safe ruby #95

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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