Skip to content

Commit 5df1414

Browse files
committed
fix trailing line breaks in Set-Cookie leading to empty Set-Cookie headers
1 parent 79fa9a0 commit 5df1414

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

actionpack/lib/action_controller/response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def convert_language!
231231

232232
def convert_cookies!
233233
set_cookie = headers['Set-Cookie']
234-
cookies = set_cookie.is_a?(String) ? set_cookie.lines.to_a : Array(set_cookie)
234+
cookies = set_cookie.is_a?(String) ? set_cookie.split("\n") : Array(set_cookie)
235235
cookies.compact!
236236
headers['Set-Cookie'] = cookies unless cookies.empty?
237237
end

actionpack/test/controller/cookie_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def set_permanent_signed_cookie
5858
cookies.permanent.signed[:remember_me] = 100
5959
end
6060

61+
def set_cookies_manually
62+
headers['Set-Cookie'] = "foo=bar; path=/\nbar=baz; path=/"
63+
cookies['user-name'] = "david"
64+
end
65+
6166
def rescue_action(e)
6267
raise unless ActionView::MissingTemplate # No templates here, and we don't care about the output
6368
end
@@ -123,7 +128,7 @@ def test_not_setting_cookie_with_secure
123128
def test_multiple_cookies
124129
get :set_multiple_cookies
125130
assert_equal 2, @response.cookies.size
126-
assert_equal "user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT\n", @response.headers["Set-Cookie"][0]
131+
assert_equal "user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT", @response.headers["Set-Cookie"][0]
127132
assert_equal "login=XJ-122; path=/", @response.headers["Set-Cookie"][1]
128133
assert_equal({"login" => "XJ-122", "user_name" => "david"}, @response.cookies)
129134
end
@@ -193,6 +198,11 @@ def test_permanent_signed_cookie
193198
assert_equal 100, @controller.send(:cookies).signed[:remember_me]
194199
end
195200

201+
def test_manual_cookie_header_is_merged
202+
get :set_cookies_manually
203+
assert_equal ["foo=bar; path=/", "bar=baz; path=/", "user-name=david; path=/"], @response.headers["Set-Cookie"]
204+
end
205+
196206
private
197207
def with_environment(enviroment)
198208
old_rails = Object.const_get(:Rails) rescue nil

0 commit comments

Comments
 (0)