Skip to content

Bug: Cookie writes a timezone-aware expires as GMT without converting it #8889

Not planned

Description

@omkarbandikatte

Expected Behaviour

The Expires attribute of a Cookie is the instant given in expires, written in GMT. Two datetime values that are the same instant produce the same Expires attribute, whatever timezone they are expressed in.

Current Behaviour

Cookie.__str__ formats expires with strftime("%a, %d %b %Y %H:%M:%S GMT") (_format_date in aws_lambda_powertools/shared/cookies.py). The GMT suffix is a literal and the datetime is never converted, so a timezone-aware datetime that is not in UTC has its local wall-clock time labelled as GMT.

For 12:00 UTC expressed as 07:00 at UTC-5, the cookie says 07:00:00 GMT, so the browser drops it five hours early. With a positive offset it lives longer than intended.

s=v; Expires=Thu, 01 Jan 2026 12:00:00 GMT; Secure
s=v; Expires=Thu, 01 Jan 2026 07:00:00 GMT; Secure

Both lines come from the same instant.

Code snippet

from datetime import datetime, timedelta, timezone

from aws_lambda_powertools.shared.cookies import Cookie

utc = datetime(2026, 1, 1, 12, 0, tzinfo=timezone.utc)
local = utc.astimezone(timezone(timedelta(hours=-5)))

assert utc == local  # the same instant

print(Cookie("s", "v", expires=utc))
print(Cookie("s", "v", expires=local))

Possible Solution

In _format_date, convert an aware datetime to UTC before formatting:

if timestamp.utcoffset() is not None:
    timestamp = timestamp.astimezone(timezone.utc)

A naive datetime has no offset to convert from and would keep being written as is, so nothing changes for it or for a datetime already in UTC.

I have this change ready with two tests in tests/unit/test_cookie_class.py (an aware UTC datetime, and an aware non-UTC datetime that fails on develop and passes with the change). I'm happy to open the PR once this is triaged.

Steps to Reproduce

  1. Install aws-lambda-powertools.
  2. Run the snippet above.
  3. The two printed cookies have different Expires values, although both datetimes are the same instant. The second should also read Thu, 01 Jan 2026 12:00:00 GMT.

Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Run locally against develop (be21802) on Python 3.13, macOS, not inside a Lambda function. An AI coding assistant was used to find and analyse this.

Powertools for AWS Lambda (Python) version

3.41.0 (develop at be21802)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

No response

Activity

  1. boring-cyborg commented on Nov 23, 2026

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  2. powertools-for-aws-oss-automation commented on Nov 23, 2026

    @powertools-for-aws-oss-automation

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out our GitHub Discussions: Join the conversation

  3. leandrodamascena commented on Nov 25, 2026

    @leandrodamascena
    Contributor

    Thanks @omkarbandikatte, this is the same bug as #8671: _format_date stamped GMT on the wall-clock time of an aware datetime. The fix landed in #8876 and goes out in 3.42.0: aware datetimes are converted to UTC first, naive ones still mean UTC. Closing as a duplicate.

  4. powertools-for-aws-oss-automation commented on Nov 25, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions