`http.cookies.SimpleCookie.load()` fails to consistently handle malformed cookies
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
There are several issues with http.cookies.SimpleCookie.load() that deviate from current browser behavior:
- Malformed cookies are not processed at all
Consider the cookie a=b;c=d\x09d;e=f. The e value contains \x09, which is not allowed per RFC 6265, Section 4.1.1.
When this is sent to a browser (Chrome 130), the browser processes all valid cookies and filters out invalid ones:
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: a=b;
Set-Cookie: c=d d;
Set-Cookie: e=f
Resulting behavior:
> document.cookie
< 'a=b; e=f'
However, http.cookies.SimpleCookie.load() ignores the entire cookie string:
>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C.load("a=b;c=d\x09d;e=f")
>>> C.output()
''
- Malformed cookies are inconsistently processed
Consider the cookie a=b;c={"d":"e"};f=g. The c value is invalid per RFC 6265, Section 4.1.1.
Browsers process this cookie without an issue:
HTTP/1.1 200 OK
Content-Type: text/html
Set-Cookie: a=b;
Set-Cookie: c={"d":"e"};
Set-Cookie: f=g
Resulting behavior:
> document.cookie
< 'a=b; c={"d":"e"}; f=g'
However, http.cookies.SimpleCookie.load() processes only the valid portion before the malformed cookie and stops entirely:
>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C.load('a=b; c={"d":"e"}; f=g')
>>> C.output()
'Set-Cookie: a=b'
It seems we should ensure consistent handling by (a) processing all valid cookies and discarding only invalid ones, or
(b) rejecting the entire cookie string if any invalid cookie is present.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 http.cookies.SimpleCookie.load() 入口开始,重现 issue 中两个格式错误 cookie 的示例。确定预期行为是处理有效 cookie 并丢弃无效 cookie,还是拒绝整个字符串;当该行为得到一致实现并由测试覆盖时,issue 即完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- networking
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 35/100