Skip to content

Commit 2e62d77

Browse files
committed
Add new method for fetching parsed cookies from an HTTP response
This fixed rapid7#9332.
1 parent fe4c701 commit 2e62d77

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/rex/proto/http/response.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: binary -*-
2+
require 'cgi'
23
require 'uri'
34
require 'rex/proto/http'
45
require 'nokogiri'
@@ -84,6 +85,18 @@ def get_cookies
8485
return cookies.strip
8586
end
8687

88+
#
89+
# Gets cookies from the Set-Cookie header in a parsed format
90+
#
91+
def get_cookies_parsed
92+
if (self.headers.include?('Set-Cookie'))
93+
ret = CGI::Cookie::parse(self.headers['Set-Cookie'])
94+
else
95+
ret = {}
96+
end
97+
ret
98+
end
99+
87100

88101
# Returns a parsed HTML document.
89102
# Instead of using regexes to parse the HTML body, you should use this and use the Nokogiri API.

spec/lib/rex/proto/http/response_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@
133133
HEREDOC
134134
end
135135

136+
let (:get_cookies_spaces_and_missing_semicolon) do
137+
<<-HEREDOC.gsub(/^ {6}/, '')
138+
HTTP/1.1 200 OK
139+
Set-Cookie: k1=v1; k2=v2;k3=v3
140+
141+
HEREDOC
142+
end
143+
136144
let (:meta_name) do
137145
'META_NAME'
138146
end
@@ -176,7 +184,7 @@
176184
<genre>Computer</genre>
177185
<price>44.95</price>
178186
<publish_date>2000-10-01</publish_date>
179-
<description>An in-depth look at creating applications
187+
<description>An in-depth look at creating applications
180188
with XML.</description>
181189
</book>
182190
</catalog>
@@ -396,6 +404,22 @@ def cookie_sanity_check(meth)
396404
expect(cookies_array).to include(*expected_cookies)
397405
end
398406

407+
it 'parses cookies with inconsistent spacing and a missing trailing semicolons' do
408+
resp = described_class.new()
409+
resp.parse(self.send :get_cookies_spaces_and_missing_semicolon)
410+
cookies = resp.get_cookies_parsed
411+
names = cookies.keys.sort
412+
values = []
413+
cookies.each do |_, parsed|
414+
parsed.value.each do |value|
415+
values << value
416+
end
417+
end
418+
values.sort!
419+
expect(names).to eq(%w(k1 k2 k3))
420+
expect(values).to eq(%w(v1 v2 v3))
421+
end
422+
399423
end
400424

401425
end

0 commit comments

Comments
 (0)