RubyLess

DESCRIPTION:

RubyLess is an interpreter for “safe ruby”. The idea is to transform some “unsafe” ruby code into safe, type checked ruby, eventually rewriting some variables or methods.

GOALS:

1. give ruby scripting access to users without any security risk
2. rewrite variable names depending on compilation context
3. never raise runtime errors through compile time type checking and powerful nil handling

This library is based on Ruby2Ruby by Ryan Davis, thanks to him for sharing his work.

SYNOPSIS:

For every class that will be involved in your RubyLess scripts, you need to declare safe methods with the ‘safe_method’ macro if you want to enable methods from this class. You have to specify the return type of the method. If you have some methods that return ‘nil’ instead of the declared output, you need to wrap your final ruby ‘eval’ with a rescue clause.

# signature is made of [method, arg_class, arg_class, ...]
class Node
  include RubyLess
  safe_method [:ancestor?, Node] => Boolean
end

# methods defined in helper

# global methods
include RubyLess
safe_method :prev => {:class => Dummy, :method => 'previous', :nil => true}
safe_method :node => lambda {|h| {:class => h.context[:node_class], :method => h.context[:node]}}
safe_method [:strftime, Time, String] => String
safe_method_for String, [:==, String] => Boolean
safe_method_for String, [:to_s] => String

You can also redefine ‘safe_method_type’ for any class or for the main helper in order to do some more complicated renaming. Note also that you should add ‘:nil => true’ declaration to any method that could return a nil value so that RubyLess can render code that will not break during runtime (adding nil checking in the form of “foo ? foo.name : nil”).

Or you can group all declarations in a single place with ‘safe_method_for’:

RubyLess::SafeClass.safe_method_for Dummy, :prev => {:class => Dummy, :method => 'previous', :nil => true},
                                           :node => lambda {|h| {:class => h.context[:node_class], :method => h.context[:node]}}

You can now parse some ruby code:

RubyLess.translate("!prev.ancestor?(main) && !node.ancestor?(main)", self)
=> "(not previous.ancestor?(@node) and not node.ancestor?(@node))"

RubyLess.translate("id > 45 and (3 > -id or 3+3)", self)
=> "(node.zip>45 and ((3>-node.zip) or (3+3)))"

RubyLess.translate("strftime(now, '%Y')", self)
=> "strftime(Time.now, \"%Y\")"

RubyLess.translate("log_info(spouse, spouse.name)", self)
=> "(node.spouse ? log_info(node.spouse, node.spouse.name) : nil)"

You can look at the tests for an idea of how to declare things. If you have more questions, ask on zena’s mailing list:

zenadmin.org/community

TRICKS:

If you define your own ‘safe_method_type’ resolution methods, or you define a method by providing a symbol (method executed to get type), you can use the special :prepend_args and :append_args elements in the type response. You need to provide a TypedString or an array of TypedString and these elements will be inserted in the final method. This can be used to provide default values:

safe_method_type [:parse_date, String] => :get_parse_date

def get_parse_date(signature)
  {:class => Time, :append_args => RubyLess::TypedString.new('visitor.tz', :class => TZInfo::Timezone)}
end

# parse_date('2003-12-01')
# ==> parse_date('2003-12-01', visitor.tz)

WARNING:

Changing the safe methods during runtime removes the cache for all classes and should therefore be avoided in favor of dynamic “safe_method_type” methods for the objects/classes concerned.

REQUIREMENTS:

  • parse_tree

INSTALL:

sudo gem install rubyless

LICENSE:

(The MIT License)

Copyright © 2009 Gaspard Bucher

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.