Class: Arachni::Browser::Javascript::Proxy::Stub

Inherits:
BasicObject
Defined in:
lib/arachni/browser/javascript/proxy/stub.rb

Overview

Note:

Extends BasicObject because we don't want any baggage to avoid method clashes with the Javascript-side objects.

Prepares JS calls for the given object based on property type.

Author:

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ Stub

Returns a new instance of Stub.

Parameters:



22
23
24
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 22

def initialize( proxy )
    @proxy = proxy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingString

Returns JS code to call the given function or retrieve the given property. (Type detection is performed by Arachni::Browser::Javascript::Proxy#function?.).

Parameters:

  • name (#to_sym)

    Function/property name.

  • arguments (Array)

    Arguments to pass to the JS function.

Returns:



64
65
66
67
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 64

def write( name, *arguments )
    @proxy.function?( name ) ?
        function( name, *arguments ) : property( name )
end

Instance Method Details

#classObject



66
67
68
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 66

def class
    Stub
end

#function(name, *arguments) ⇒ String

Returns JS code to call the given function.

Parameters:

  • name (#to_sym)

    Function name.

  • arguments (Array)

    Arguments to pass to the JS function.

Returns:

  • (String)

    JS code to call the given function.



33
34
35
36
37
38
39
40
41
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 33

def function( name, *arguments )
    arguments = arguments.map { |arg| arg.to_json }.join( ', ' )

    if name.to_s.end_with?( '=' )
        "#{property( name )}#{arguments if !arguments.empty?}"
    else
        "#{property( name )}(#{arguments if !arguments.empty?})"
    end
end

#property(name) ⇒ String

Returns JS code to retrieve the given property.

Parameters:

  • name (#to_sym)

    Function name.

Returns:

  • (String)

    JS code to retrieve the given property.



48
49
50
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 48

def property( name )
    "#{@proxy.js_object}.#{name}"
end

#respond_to?(property) ⇒ Bool

Returns true if self of the JS object responds to property, false otherwise.

Parameters:

  • property (Symbol)

Returns:

  • (Bool)

    true if self of the JS object responds to property, false otherwise.



80
81
82
83
84
85
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 80

def respond_to?( property )
    property = property.to_s
    property = property[0...-1] if property.end_with? '='

    @proxy.javascript.run( "return ('#{property}' in #{@proxy.js_object})" )
end

#to_sString

Returns:



71
72
73
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 71

def to_s
    "<#{self.class}##{object_id} #{@proxy.js_object}>"
end

#write(name, *arguments) ⇒ String Also known as: method_missing

Returns JS code to call the given function or retrieve the given property. (Type detection is performed by Arachni::Browser::Javascript::Proxy#function?.).

Parameters:

  • name (#to_sym)

    Function/property name.

  • arguments (Array)

    Arguments to pass to the JS function.

Returns:



60
61
62
63
# File 'lib/arachni/browser/javascript/proxy/stub.rb', line 60

def write( name, *arguments )
    @proxy.function?( name ) ?
        function( name, *arguments ) : property( name )
end