Class: Arachni::HTTP::CookieJar

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/arachni/http/cookie_jar.rb

Overview

Basic CookieJar implementation.

Author:

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#available_port, #cookie_encode, #cookies_from_document, #cookies_from_file, #cookies_from_response, #exception_jail, #exclude_path?, #extract_domain, #follow_protocol?, #form_decode, #form_encode, #form_parse_request_body, #forms_from_document, #forms_from_response, #generate_token, #get_path, #html_decode, #html_encode, #include_path?, #links_from_document, #links_from_response, #normalize_url, #page_from_response, #page_from_url, #parse_query, #parse_set_cookie, #parse_url_vars, #path_in_domain?, #path_too_deep?, #port_available?, #rand_port, #redundant_path?, #remove_constants, #seed, #skip_page?, #skip_path?, #skip_resource?, #to_absolute, #uri_decode, #uri_encode, #uri_parse, #uri_parser, #url_sanitize

Constructor Details

#initialize(cookie_jar_file = nil) ⇒ CookieJar

Returns a new instance of CookieJar.

Parameters:

  • cookie_jar_file (String) (defaults to: nil)

    path to a Netscape cookie-jar



56
57
58
59
# File 'lib/arachni/http/cookie_jar.rb', line 56

def initialize( cookie_jar_file = nil )
    @domains = {}
    load( cookie_jar_file ) if cookie_jar_file
end

Class Method Details

.from_file(*args) ⇒ Arachni::HTTP::CookieJar

Same as #initialize.



51
52
53
# File 'lib/arachni/http/cookie_jar.rb', line 51

def self.from_file( *args )
    new.load( *args )
end

Instance Method Details

#<<(cookies) ⇒ CookieJar

Updates the jar with cookie.

Parameters:

Returns:



85
86
87
88
89
90
# File 'lib/arachni/http/cookie_jar.rb', line 85

def <<( cookies )
    [cookies].flatten.compact.each do |cookie|
        ((@domains[cookie.domain] ||= {})[cookie.path] ||= {})[cookie.name] = cookie.dup
    end
    self
end

#any?Bool

Returns true if cookiejar is not empty, false otherwise.

Returns:

  • (Bool)

    true if cookiejar is not empty, false otherwise



176
177
178
# File 'lib/arachni/http/cookie_jar.rb', line 176

def any?
    !empty?
end

#clearObject

Empties the cookiejar



166
167
168
# File 'lib/arachni/http/cookie_jar.rb', line 166

def clear
    @domains.clear
end

#cookies(include_expired = false) ⇒ Array<Cookie>

Returns all cookies

Parameters:

  • include_expired (Bool) (defaults to: false)

    include expired cookies

Returns:



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/arachni/http/cookie_jar.rb', line 153

def cookies( include_expired = false )
    @domains.values.map do |paths|
        paths.values.map do |cookies|
            if !include_expired
                cookies.values.reject{ |c| c.expired? }
            else
                cookies.values
            end
        end
    end.flatten.compact
end

#empty?Bool

Returns true if cookiejar is empty, false otherwise.

Returns:

  • (Bool)

    true if cookiejar is empty, false otherwise



171
172
173
# File 'lib/arachni/http/cookie_jar.rb', line 171

def empty?
    @domains.empty?
end

#for_url(url) ⇒ Array<Cookie>

Gets cookies for a specific url.

Parameters:

Returns:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/arachni/http/cookie_jar.rb', line 126

def for_url( url )
    uri = to_uri( url )
    request_path   = uri.path
    request_domain = uri.host

    return [] if !request_domain || !request_path

    @domains.map do |domain, paths|
        next if !in_domain?( domain, request_domain )

        paths.map do |path, cookies|
            next if !request_path.start_with?( path )

            cookies.values.reject{ |c| c.expired? }
        end
    end.flatten.compact.sort do |lhs, rhs|
        rhs.path.length <=> lhs.path.length
    end
end

#load(cookie_jar_file, url = '') ⇒ CookieJar

Loads cookies from a Netscape cookiejar file

Parameters:

  • cookie_jar_file (String)

    path to a Netscape cookie-jar

  • url (String) (defaults to: '')

    cookie owner

Returns:



69
70
71
72
73
74
75
76
# File 'lib/arachni/http/cookie_jar.rb', line 69

def load( cookie_jar_file, url = '' )
    # make sure that the provided cookie-jar file exists
    if !File.exist?( cookie_jar_file )
        fail Error::CookieJarFileNotFound, "Cookie-jar '#{cookie_jar_file}' doesn't exist."
    end
    update( cookies_from_file( url, cookie_jar_file ) )
    self
end

#update(cookies) ⇒ CookieJar

Updates the jar with cookies.

Parameters:

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/arachni/http/cookie_jar.rb', line 99

def update( cookies )
    [cookies].flatten.compact.each do |c|
        self << case c
                    when String
                        begin
                            Cookie.from_string( ::Arachni::Options.url.to_s, c )
                        rescue
                            Cookie.from_set_cookie( ::Arachni::Options.url.to_s, c )
                        end

                    when Hash
                        Cookie.new( ::Arachni::Options.url.to_s, c ) if c.any?

                    when Cookie
                        c
                end
    end
    self
end