Bug: Cookie Expires labels a non-UTC aware datetime as GMT without converting it

未关闭 适合新手
#8,671 3 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@LemonCondor 已经在做这个了。

开始于 2026年10月29日。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
86/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
aws, python
领域
backend

调研方向

从 aws_lambda_powertools/shared/cookies.py 中的 Cookie._format_date 以及 tests/unit/test_cookie_class.py 中提到的回归测试开始。运行该测试,以确认当前格式化后的 header 会保留时区偏移。完成标准是:带时区的 datetime 以 UTC 输出,naive datetime 保持当前行为,并且回归测试通过。

由索引模型根据 Issue 内容生成。

描述

bug event_handlers
Expected Behaviour

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.

Code snippet
from datetime import datetime, timedelta, timezone
from aws_lambda_powertools.shared.cookies import Cookie

cookie = Cookie("a", "b", expires=datetime(2026, 1, 1, 12, tzinfo=timezone(timedelta(hours=2))))
print(str(cookie))  # a=b; Secure; Expires=Thu, 01 Jan 2026 12:00:00 GMT
Possible Solution

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.

Steps to Reproduce

Run the snippet above on develop (5785377).

Powertools for AWS Lambda (Python) version

latest (3.37.0, develop)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs
N/A
主要语言
Python
星标
3.3k
派生
511
平均合并
3 天 8 小时
30 天内合并 PR
92

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aws-powertools/powertools-lambda-python 的其他 Issue

查看 aws-powertools/powertools-lambda-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。