Class: Arachni::HTTP::CookieJar

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

Overview

Basic CookieJar implementation.

Author:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#cookie_encode, #cookies_from_document, #cookies_from_file, #cookies_from_response, #exception_jail, #exclude_path?, #extract_domain, #form_decode, #form_encode, #form_parse_request_body, #forms_from_document, #forms_from_response, #get_path, #hash_keys_to_str, #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?, #remove_constants, #seed, #skip_path?, #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



38
39
40
41
# File 'lib/arachni/http/cookie_jar.rb', line 38

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.



33
34
35
# File 'lib/arachni/http/cookie_jar.rb', line 33

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

Instance Method Details

#<<(cookie) ⇒ CookieJar

Updates the jar with cookie.

Parameters:

Returns:



68
69
70
71
# File 'lib/arachni/http/cookie_jar.rb', line 68

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

#any?Bool

Returns true if cookiejar is not empty, false otherwise.

Returns:

  • (Bool)

    true if cookiejar is not empty, false otherwise



142
143
144
# File 'lib/arachni/http/cookie_jar.rb', line 142

def any?
    !empty?
end

#clearObject

Empties the cookiejar



132
133
134
# File 'lib/arachni/http/cookie_jar.rb', line 132

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:



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/arachni/http/cookie_jar.rb', line 119

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



137
138
139
# File 'lib/arachni/http/cookie_jar.rb', line 137

def empty?
    @domains.empty?
end

#for_url(url) ⇒ Array<Cookie>

Gets cookies for a specific url.

Parameters:

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/arachni/http/cookie_jar.rb', line 92

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:



51
52
53
54
55
56
57
58
59
# File 'lib/arachni/http/cookie_jar.rb', line 51

def load( cookie_jar_file, url = '' )
    # make sure that the provided cookie-jar file exists
    if !File.exist?( cookie_jar_file )
        fail( Exceptions::NoCookieJar,
               '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:



80
81
82
83
# File 'lib/arachni/http/cookie_jar.rb', line 80

def update( cookies )
    [cookies].flatten.compact.each { |c| self << c }
    self
end