Skip to content

Commit 9d64d96

Browse files
committed
fix(event_handler): emit Max-Age=0 for cookies with max_age=0
Cookie.__str__ checked max_age for truthiness, so max_age=0 skipped the Max-Age attribute and produced a session cookie instead of expiring it. Check for None instead so zero is serialized as Max-Age=0.
1 parent 4a307b6 commit 9d64d96

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎aws_lambda_powertools/shared/cookies.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def __str__(self) -> str:
9999
if self.expires:
100100
payload.write(f"; Expires={_format_date(self.expires)}")
101101

102-
if self.max_age:
102+
if self.max_age is not None:
103103
if self.max_age > 0:
104104
payload.write(f"; Max-Age={self.max_age}")
105105
else:

‎tests/unit/test_cookie_class.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ def test_cookie_with_max_age_negative():
7979
assert str(cookie) == "powertools=test; Path=/; Max-Age=0; Secure"
8080

8181

82+
def test_cookie_with_max_age_zero():
83+
# GIVEN a cookie with a zero max age
84+
cookie = Cookie(name="powertools", value="test", path="/", max_age=0)
85+
86+
# WHEN getting the cookie's attributes
87+
# THEN Max-Age must be 0 so the browser deletes the cookie
88+
assert cookie.max_age == 0
89+
assert str(cookie) == "powertools=test; Path=/; Max-Age=0; Secure"
90+
91+
8292
def test_cookie_with_http_only():
8393
# GIVEN a cookie with http_only
8494
cookie = Cookie(name="powertools", value="test", path="/", http_only=True)

0 commit comments

Comments
 (0)