Code Monkey home page Code Monkey logo

safe_ruby's Introduction

Safe Ruby

Safe Ruby provides a way to run untrusted ruby code outside of the current process in a safe environment. Creating this environment is largery based on jruby sandbox, whitelisting the methods one can use on potentially dangerous classes. Constants are also whitelisted, eliminating some core ruby functionality such as spawning another process.

Getting Started

Run gem install safe_ruby in your terminal, then require 'safe_ruby' in your app and you're ready to go.

Examples

Evaluating ruby code

  SafeRuby.eval('[1,2,3].map{ |n| n + 1 }')    #=> [2, 3, 4]
  
  SafeRuby.eval('system("rm *")')              #=> system is unavailable
  
  SafeRuby.eval('Kernel.abort')                #=> undefined method `abort' for Kernel:Module

Default timeout for evaluating is 5 seconds, but this can be specified.

  SafeRuby.eval('loop{}')                      #<ChildProcess::TimeoutError: process still alive after 5 seconds>
  
  SafeRuby.eval('loop{}', timeout: 1)          #<ChildProcess::TimeoutError: process still alive after 1 seconds>

This library was built for a codeacademy-style tutoring app, so checking answers is built into the SafeRuby class

  SafeRuby.check('[1,2,3].map{ |n| n + 1 }', '[2,3,4]')  #=> true

In this example, the second argument(expected answer) can also be untrusted, it will be run safely.

safe_ruby's People

Contributors

dbc-apprentice avatar theycallmeswift avatar ukutaht avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

safe_ruby's Issues

Execute code with required files

Hi there,
I'm trying execute code which contain minitest's test with row

require 'minitest/autorun'

but I'm get error

'eval': uninitialized constant Bundler::RubygemsIntegration::File (NameError)

Change (add :Bunder) ALLOWED_CONSTANTS constant not resolves problem.
How can I make resolve this issue?

Transfer request !

You said here the you woud be happy to transfer the ownership of this module.

I have proposed here to take the ownership.

This issue is just a trig try ;)

I would need you to add me as owner in rubygems.org:

$>gem owner safe_ruby --add [email protected]

ruby 3.3.0 'incompatible marshal file format` errors

Four of the tests fail in ruby 3.3.0 (although they work in 3.2.2) with an incompatible marshal file format error, even after updating gem dependencies:

Finished in 5.32 seconds (files took 0.04708 seconds to load)
25 examples, 4 failures

Failed examples:

rspec ./spec/safe_ruby_spec.rb:5 # SafeRuby#eval allows basic operations
rspec ./spec/safe_ruby_spec.rb:10 # SafeRuby#eval returns correct object
rspec ./spec/safe_ruby_spec.rb:46 # SafeRuby#eval options timeout defaults to a 5 second timeout
rspec ./spec/safe_ruby_spec.rb:53 # SafeRuby#eval options timeout allows custom timeout

Here is the first one:

  1) SafeRuby#eval allows basic operations
     Failure/Error: raise data

     RuntimeError:
       /var/folders/cy/7pf5w_mj4fg3n6hzjxjs01mr0000gn/T/saferuby20240215-86452-xokox4:17:in `undef_method': undefined method `new' for class `String' (NameError)

           klass.send(:undef_method, method)
                ^^^^^
       Did you mean?  next
       	from /var/folders/cy/7pf5w_mj4fg3n6hzjxjs01mr0000gn/T/saferuby20240215-86452-xokox4:17:in `block in keep_methods'
       	from /var/folders/cy/7pf5w_mj4fg3n6hzjxjs01mr0000gn/T/saferuby20240215-86452-xokox4:16:in `each'
       	from /var/folders/cy/7pf5w_mj4fg3n6hzjxjs01mr0000gn/T/saferuby20240215-86452-xokox4:16:in `keep_methods'
       	from /var/folders/cy/7pf5w_mj4fg3n6hzjxjs01mr0000gn/T/saferuby20240215-86452-xokox4:37:in `<main>'
     # ./lib/safe_ruby/runner.rb:45:in `rescue in eval'
     # ./lib/safe_ruby/runner.rb:41:in `eval'
     # ./lib/safe_ruby/runner.rb:20:in `eval'
     # ./spec/safe_ruby_spec.rb:6:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # TypeError:
     #   incompatible marshal file format (can't be read)
     #   	format version 4.8 required; 47.118 given
     #   ./lib/safe_ruby/runner.rb:42:in `eval'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.