Repository navigation
Description
Bug report
Bug description:
http.cookiejar.DefaultCookiePolicy applies its domain rules only to cookies whose version is 0 (Netscape) or positive (RFC 2965). A Set-Cookie header with a negative Version attribute matches neither case, so the cookie's Domain is never compared with the host that sent it: the cookie is stored for that domain, replaces an existing cookie with the same domain, path and name, and is returned to that domain.
Any server a program contacts with a shared CookieJar controls the header, and can set or overwrite cookies for an unrelated domain (anything with an embedded dot); no cookies are disclosed to it.
import urllib.request
from http.cookiejar import CookieJar
from email.message import Message
class Response:
def __init__(self, set_cookie):
self.msg = Message()
self.msg["Set-Cookie"] = set_cookie
def info(self):
return self.msg
for version in "", "; Version=-1":
jar = CookieJar()
req = urllib.request.Request("http://www.a.example/")
jar.extract_cookies(
Response("k=v; Domain=b.example; Path=/" + version), req)
req = urllib.request.Request("http://www.b.example/")
jar.add_cookie_header(req)
print(repr(version), "->", req.get_header("Cookie"))Output on main:
'' -> None
'; Version=-1' -> k=v
Expected None in both cases. The code is unchanged since the module was added, so all maintained branches behave the same. This is the set-side counterpart of bpo-35121.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-160300: Reject cookies with a negative version in http.cookiejar #160301
- [3.15] gh-160300: Reject cookies with a negative version in http.cookiejar (GH-160301) #160645
- [3.14] gh-160300: Reject cookies with a negative version in http.cookiejar (GH-160301) #160646
- [3.13] gh-160300: Reject cookies with a negative version in http.cookiejar (GH-160301) #160647
- [3.12] gh-160300: Reject cookies with a negative version in http.cookiejar (GH-160301) #160648
- [3.11] gh-160300: Reject cookies with a negative version in http.cookiejar (GH-160301) #160649
ANT-2026-TA36I7V7
Activity
pythongh-160300: Reject cookies with a negative version in http.cooki…
gh-160300: Reject cookies with a negative version in http.cookiejar (G…
[3.15] gh-160300: Reject cookies with a negative version in http.cook…
[3.14] gh-160300: Reject cookies with a negative version in http.cook…
[3.13] gh-160300: Reject cookies with a negative version in http.cook…
[3.12] gh-160300: Reject cookies with a negative version in http.cook…
[3.11] gh-160300: Reject cookies with a negative version in http.cook…
gpshead commentedon Oct 25, 2026
thanks for the reviews!
[3.11] pythongh-160300: Reject cookies with a negative version in htt…
[3.11] pythongh-160300: Reject cookies with a negative version in htt…