Skip to content

Commit

Permalink
Discard nil max_age in put_resp_cookie/4 (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvic authored Apr 29, 2024
1 parent 7309258 commit 9bfa83b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/plug/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,8 @@ defmodule Plug.Conn do
end

defp max_age(opts) do
[keys: Plug.Keys, max_age: Keyword.get(opts, :max_age, 86400)]
max_age = Keyword.get(opts, :max_age) || 86400
[keys: Plug.Keys, max_age: max_age]
end

defp maybe_secure_cookie(cookie, :https), do: Map.put_new(cookie, :secure, true)
Expand Down
11 changes: 11 additions & 0 deletions test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,17 @@ defmodule Plug.ConnTest do
refute Map.has_key?(fetch_cookies(new_conn, signed: "foo").cookies, "foo")
end

test "put_resp_cookie/4 with sign: true and max_age: nil" do
conn =
secret_conn()
|> fetch_cookies()
|> put_resp_cookie("foo", {:signed, :cookie}, sign: true, max_age: nil)
|> send_resp(200, "OK")

new_conn = secret_conn() |> recycle_cookies(conn) |> fetch_cookies(signed: ~w(foo))
assert Map.get(new_conn.cookies, "foo") == {:signed, :cookie}
end

test "put_resp_cookie/4 with encrypt: true and max_age: 0" do
conn =
secret_conn()
Expand Down

0 comments on commit 9bfa83b

Please sign in to comment.