Skip to content

Commit

Permalink
Fix array set-cookie headers in rack 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Mar 24, 2011
1 parent c4da6df commit aeffb92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/rack/ssl.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ def hsts_headers


def flag_cookies_as_secure!(headers) def flag_cookies_as_secure!(headers)
if cookies = headers['Set-Cookie'] if cookies = headers['Set-Cookie']
headers['Set-Cookie'] = cookies.split("\n").map { |cookie| # Rack 1.1's set_cookie_header! will sometimes wrap
# Set-Cookie in an array
unless cookies.respond_to?(:to_ary)
cookies = cookies.split("\n")
end

headers['Set-Cookie'] = cookies.map { |cookie|
if cookie !~ /; secure(;|$)/ if cookie !~ /; secure(;|$)/
"#{cookie}; secure" "#{cookie}; secure"
else else
Expand Down
27 changes: 21 additions & 6 deletions test/test_ssl.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,15 +82,30 @@ def test_flag_cookies_as_secure
end end


def test_flag_cookies_as_secure_at_end_of_line def test_flag_cookies_as_secure_at_end_of_line
default_app = lambda { |env| self.app = Rack::SSL.new(lambda { |env|
headers = {'Content-Type' => "text/html", headers = {
'Set-Cookie' => "problem=def; path=/; HttpOnly; secure" } 'Content-Type' => "text/html",
'Set-Cookie' => "problem=def; path=/; HttpOnly; secure"
}
[200, headers, ["OK"]] [200, headers, ["OK"]]
} })
self.app = Rack::SSL.new(default_app)
get "https://example.org/"
assert_equal ["problem=def; path=/; HttpOnly; secure"],
last_response.headers['Set-Cookie'].split("\n")
end

def test_legacy_array_headers
self.app = Rack::SSL.new(lambda { |env|
headers = {
'Content-Type' => "text/html",
'Set-Cookie' => ["id=1; path=/", "token=abc; path=/; HttpOnly"]
}
[200, headers, ["OK"]]
})


get "https://example.org/" get "https://example.org/"
assert_equal ["problem=def; path=/; HttpOnly; secure" ], assert_equal ["id=1; path=/; secure", "token=abc; path=/; HttpOnly; secure"],
last_response.headers['Set-Cookie'].split("\n") last_response.headers['Set-Cookie'].split("\n")
end end


Expand Down

0 comments on commit aeffb92

Please sign in to comment.