Class: Rubikon::Application::Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/rubikon/application/sandbox.rb

Overview

The application sandbox is a wrapper used to secure internal Rubikon logic from access by user generated application code.

This is mostly to prevent accidental execution or change of Rubikon’s internal code. But it also helps to prevent possible security problems depending on the code used inside the application logic.

See Also:

Since:

  • 0.4.0

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Sandbox

Create a new application sandbox

Parameters:

Raises:

  • (ArgumentError)

Since:

  • 0.4.0



24
25
26
27
# File 'lib/rubikon/application/sandbox.rb', line 24

def initialize(app)
  raise ArgumentError unless app.is_a? Application::Base
  @__app__ = app
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Method calls on the sandbox wrapper will be relayed to the singleton instance. Methods defined in InstanceMethods are protected and will raise a NoMethodError.

Parameters:

  • method_name (Symbol)

    The name of the method being called

  • args (Array)

    Any arguments that are given to the method

  • block (Proc)

    A block that may be given to the method

Raises:

  • (NoMethodError)

    if a method is called that is defined inside InstanceMethods and should therefore be protected

See Also:

Since:

  • 0.4.0



37
38
39
40
41
42
43
44
45
# File 'lib/rubikon/application/sandbox.rb', line 37

def method_missing(name, *args, &block)
  if @__app__.class.instance_methods(false).include?(name.to_s) ||
     !(InstanceMethods.method_defined?(name) ||
     InstanceMethods.private_method_defined?(name))
    @__app__.send(name, *args, &block)
  else
    raise NoMethodError.new("Method `#{name}' is protected by the application sandbox", name)
  end
end

Instance Method Details

#putc(text) ⇒ Object

Relay putc to the instance method

This is used to hide Kernel#putc so that the application’s output IO object is used for printing characters

Parameters:

  • char (String, Numeric)

    The character to write into the output stream

Since:

  • 0.4.0



54
55
56
# File 'lib/rubikon/application/sandbox.rb', line 54

def putc(text)
  @__app__.send(:putc, text)
end

#puts(text = nil) ⇒ Object

Relay puts to the instance method

This is used to hide Kernel#puts so that the application’s output IO object is used for printing text

Parameters:

  • text (String) (defaults to: nil)

    The text to write into the output stream

Since:

  • 0.4.0



64
65
66
# File 'lib/rubikon/application/sandbox.rb', line 64

def puts(text = nil)
  @__app__.send(:puts, text)
end