You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Cookie Expires labels a non-UTC aware datetime as GMT without converting it #8671
A Cookie whose expires is a timezone-aware datetime should send that instant in GMT, as RFC 6265 requires for Expires. datetime(2026, 1, 1, 12, tzinfo=timezone(timedelta(hours=2))) is 10:00 UTC, so the header should read Expires=Thu, 01 Jan 2026 10:00:00 GMT.
Current Behaviour
Cookie._format_date formats the datetime with strftime and a hard-coded GMT suffix, without converting it to UTC first. The local wall-clock time is labelled GMT, so the cookie expires earlier or later by the UTC offset: Expires=Thu, 01 Jan 2026 12:00:00 GMT.
Convert an aware datetime to UTC before formatting (expires.astimezone(timezone.utc)); naive datetimes keep today's behaviour. I have this fix with a regression test in tests/unit/test_cookie_class.py (fails on develop, passes with the change) and can open a PR once this is triaged.
Thanks @LemonCondor, confirmed. _format_date stamps GMT on whatever wall-clock time it gets, so an aware datetime outside UTC is off by its offset. Converting aware values with astimezone(timezone.utc) and leaving naive ones alone is the right fix. It's yours, open the PR with the test. This goes into 3.40.0.
One more thing in _format_date, in case it fits the same fix: %a and %b follow the process LC_TIME, so with a German locale the header reads Expires=Fr, 30 Okt 2026 07:28:00 GMT, which browsers cannot parse. email.utils.format_datetime(value, usegmt=True) after the UTC conversion gives the English names regardless of locale. Same root cause as #8706 for feature flags.
Activity
SumiRann1 commentedon Oct 29, 2026
Hii @LemonCondor, I want to work on this!
leandrodamascena commentedon Oct 29, 2026
Thanks @LemonCondor, confirmed.
_format_datestampsGMTon whatever wall-clock time it gets, so an aware datetime outside UTC is off by its offset. Converting aware values withastimezone(timezone.utc)and leaving naive ones alone is the right fix. It's yours, open the PR with the test. This goes into 3.40.0.anishmehta24 commentedon Oct 30, 2026
One more thing in
_format_date, in case it fits the same fix:%aand%bfollow the processLC_TIME, so with a German locale the header readsExpires=Fr, 30 Okt 2026 07:28:00 GMT, which browsers cannot parse.email.utils.format_datetime(value, usegmt=True)after the UTC conversion gives the English names regardless of locale. Same root cause as #8706 for feature flags.