Class: Ronin::Support::Network::HTTP::SetCookie

Inherits:
Cookie
  • Object
show all
Defined in:
lib/ronin/support/network/http/set_cookie.rb

Overview

Parses and generates Set-Cookie header values.

Since:

  • 1.0.0

Constant Summary collapse

SAME_SITE =

Mapping of SameSite values to Symbols.

Since:

  • 1.0.0

{
  'None'   => :none,
  'Strict' => :strict,
  'Lax'    => :lax
}

Instance Attribute Summary collapse

Attributes inherited from Cookie

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cookie

#[], #[]=, #each, #empty?, escape, #has_param?, #initialize_copy, #merge, #merge!, #to_h, unescape

Methods included from Enumerable

#map_hash

Constructor Details

#initialize(params, expires: nil, max_age: nil, path: nil, domain: nil, http_only: nil, secure: nil, same_site: nil) ⇒ SetCookie

Initializes the Set-Cookie object.

Parameters:

  • params (Hash{String => String})
  • expires (Time, nil) (defaults to: nil)

    The parsed Expires value.

  • max_age (Integer, nil) (defaults to: nil)

    The parsed Max-Age value.

  • path (String, nil) (defaults to: nil)

    The parsed Path value.

  • domain (String, nil) (defaults to: nil)

    The parsed Domain value.

  • http_only (true, nil) (defaults to: nil)

    Indicates the HttpOnly flag is enabled.

  • secure (true, nil) (defaults to: nil)

    Indicates the Secure flag is enabled.

  • same_site (:strict, :lax, :none, nil) (defaults to: nil)

    The parsed SameSite value.

Since:

  • 1.0.0



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ronin/support/network/http/set_cookie.rb', line 97

def initialize(params, expires:   nil,
                       max_age:   nil,
                       path:      nil,
                       domain:    nil,
                       http_only: nil,
                       secure:    nil,
                       same_site: nil)
  super(params)

  @expires   = expires
  @max_age   = max_age
  @path      = path
  @domain    = domain
  @http_only = http_only
  @secure    = secure
  @same_site = same_site
end

Instance Attribute Details

#domainString? (readonly)

The Domain cookie attribute.

Returns:

Since:

  • 1.0.0



49
50
51
# File 'lib/ronin/support/network/http/set_cookie.rb', line 49

def domain
  @domain
end

#expiresTime? (readonly)

The Expires cookie attribute.

Returns:

  • (Time, nil)

Since:

  • 1.0.0



44
45
46
# File 'lib/ronin/support/network/http/set_cookie.rb', line 44

def expires
  @expires
end

#http_onlytrue? (readonly)

The HttpOnly flag.

Returns:

  • (true, nil)

Since:

  • 1.0.0



64
65
66
# File 'lib/ronin/support/network/http/set_cookie.rb', line 64

def http_only
  @http_only
end

#max_ageInteger? (readonly)

The Max-Age cookie attribute.

Returns:

Since:

  • 1.0.0



39
40
41
# File 'lib/ronin/support/network/http/set_cookie.rb', line 39

def max_age
  @max_age
end

#pathString? (readonly)

The Path cookie attribute.

Returns:

Since:

  • 1.0.0



54
55
56
# File 'lib/ronin/support/network/http/set_cookie.rb', line 54

def path
  @path
end

#same_site:strict, ... (readonly)

The SameSite cookie attribute.

Returns:

  • (:strict, :lax, :none)

Since:

  • 1.0.0



59
60
61
# File 'lib/ronin/support/network/http/set_cookie.rb', line 59

def same_site
  @same_site
end

#securetrue? (readonly)

The Secure flag.

Returns:

  • (true, nil)

Since:

  • 1.0.0



69
70
71
# File 'lib/ronin/support/network/http/set_cookie.rb', line 69

def secure
  @secure
end

Class Method Details

.parse(string) ⇒ Cookie

Parses a Set-Cookie string.

Parameters:

  • string (String)

    The raw Set-Cookie string.

Returns:

  • (Cookie)

    The parsed cookie.

Raises:

  • (ArgumentError)

    The string contained an unknown SameSite value or flag.

Since:

  • 1.0.0



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ronin/support/network/http/set_cookie.rb', line 134

def self.parse(string)
  kwargs = {}
  params = {}

  string.split(/;\s+/) do |field|
    if field.include?('=')
      key, value = field.split('=',2)

      case key
      when 'Max-Age' then kwargs[:max_age] = value.to_i
      when 'Expires' then kwargs[:expires] = Time.parse(value)
      when 'Path'    then kwargs[:path]    = value
      when 'Domain'  then kwargs[:domain]  = value
      when 'SameSite'
        kwargs[:same_site] = SAME_SITE.fetch(value) do
          raise(ArgumentError,"unrecognized SameSite value: #{value.inspect}")
        end
      else
        params[unescape(key)] = unescape(value)
      end
    else
      case field
      when 'HttpOnly' then kwargs[:http_only] = true
      when 'Secure'   then kwargs[:secure]    = true
      else
        raise(ArgumentError,"unrecognized Cookie flag: #{field.inspect}")
      end
    end
  end

  return new(params,**kwargs)
end

Instance Method Details

#http_only?Boolean

Determines if the HttpOnly flag is set.

Returns:

  • (Boolean)

Since:

  • 1.0.0



172
173
174
# File 'lib/ronin/support/network/http/set_cookie.rb', line 172

def http_only?
  @http_only == true
end

#secure?Boolean

Determines if the Secure flag is set.

Returns:

  • (Boolean)

Since:

  • 1.0.0



181
182
183
# File 'lib/ronin/support/network/http/set_cookie.rb', line 181

def secure?
  @secure == true
end

#to_sString

Converts the cookie back into a Set-Cookie value.

Returns:

  • (String)

    The formatted cookie.

Since:

  • 1.0.0



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ronin/support/network/http/set_cookie.rb', line 191

def to_s
  string = super()
  string << "; Max-Age=#{@max_age}"          if @max_age
  string << "; Expires=#{@expires.httpdate}" if @expires
  string << "; Path=#{@path}"                if @path
  string << "; Domain=#{@domain}"            if @domain
  string << "; SameSite=#{@same_site.to_s.capitalize}" if @same_site

  if    @secure    then string << '; Secure'
  elsif @http_only then string << '; HttpOnly'
  end

  string
end