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
Response cookies are not ingested properly in 0.14.0 #1151
Hi. After last updatet when loading cookies from a file, it gives an error
http/cookiejar.py", line 1947, in _really_load
domain_specified = domain.startswith(".")
AttributeError: 'NoneType' object has no attribute 'startswith'
Doesn't seem to come from httpcore (tested against 0.9.1 with a bit of patching for the API changes…), and the git blame on Cookies shows no code changes, so… 🤷♂️
Ah-ha! Found it :-) When extracting cookies we're calling into response.headers.items(), which used to return all headers, but now returns de-duplicated headers (via #1089).
Yet there seems to be a problem... Here's what response.headers.items() contains:
This is not really the problem that's causing this bug though, but rather that we're still calling .multi_items() when processing response cookies, instead of calling into response.headers.multi_items() in order to properly get all separate Set-Cookie headers:
(Still not sure if concatenating headers into comma-separated lists shouldn't include some conditions on what header is being flattened. Eg for Set-Cookie we probably want to only keep the latest value, instead of concatenating all values which results in an invalid Set-Cookie value syntax.)
Activity
lovelydinosaur commentedon Aug 9, 2020
Heya, first things...
djigit0s commentedon Aug 9, 2020
1. Traceback, script runned on version 0.14.0
(manual demand for payment) djigit0s@R:/mnt/d/prj/manual demand for payment$ python test.py
(manual demand for payment) djigit0s@R:/mnt/d/prj/manual demand for payment$ python test.py
/usr/lib/python3.7/http/cookiejar.py:1966: UserWarning: http.cookiejar bug!
Traceback (most recent call last):
File "/usr/lib/python3.7/http/cookiejar.py", line 1947, in _really_load
domain_specified = domain.startswith(".")
AttributeError: 'NoneType' object has no attribute 'startswith'
_warn_unhandled_exception()
Traceback (most recent call last):
File "/usr/lib/python3.7/http/cookiejar.py", line 1947, in _really_load
domain_specified = domain.startswith(".")
AttributeError: 'NoneType' object has no attribute 'startswith'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 7, in
h.cookies.jar.load()
File "/usr/lib/python3.7/http/cookiejar.py", line 1794, in load
self._really_load(f, filename, ignore_discard, ignore_expires)
File "/usr/lib/python3.7/http/cookiejar.py", line 1968, in _really_load
(filename, line))
http.cookiejar.LoadError: invalid Set-Cookie3 format file 'test': '1P_JAR="2020-08-09-09"; path="/"; domain=".google.ru"; path_spec; domain_dot; expires="2020-09-08 09:44:05Z"; HttpOnly=None; Secure, NID="204=------------------------------yl_8tbSzcfb2Y6lmG7j8kLKLigynEVHW8PqDowWASEG_tCeKBBwx_qpTfUIt6klL4UoSaX6uVK32R9mIcXDK5kN2DRIgu07yYVJB3-CSq5SEoA"; version=0'
(manual demand for payment) djigit0s@R:/mnt/d/prj/manual demand for payment$
2. Checked on version 0.13.0 and 0.13.3, script work.
pay attention in the first message I indicated the results of saving cookies they are different, as if deleted some cookies
florimondmanca commentedon Aug 9, 2020
Yeah, something's weird.
I ran your script against
0.14.0(master), and0.13.0, printing the contentsdict(h.cookies)after the first run of the script.Looks like in 0.14.0 we're conflating the two cookies into one upon receiving the response...
0.14.0:0.13.0:florimondmanca commentedon Aug 9, 2020
Doesn't seem to come from
httpcore(tested against 0.9.1 with a bit of patching for the API changes…), and the git blame onCookiesshows no code changes, so… 🤷♂️florimondmanca commentedon Aug 9, 2020
Ah-ha! Found it :-) When extracting cookies we're calling into
response.headers.items(), which used to return all headers, but now returns de-duplicated headers (via #1089).Yet there seems to be a problem... Here's what
response.headers.items()contains:Notice how this is…?
1P_JAR=2020-08-09-18; expires=Tue, 08-Sep-2020 18:33:35 GMT; path=/; domain=.google.com; Secure","NID=204=KWdXOuypc86YvRfBSiWoW1dEXfSl_5qI7sxZY4umlk4J35yNTeNEkw15MRaujK6uYCwkrtjihTTXZPp285z_xDOUzrdHt4dj0Z5C0VOpbvdLwRdHatHAzQs77TsaiWY78a3qU9r7KP_RbSLvLl2hlhnWFR2Hp5nWKPsAcOhQgSg; expires=Mon, 08-Feb-2021 18:33:35 GMT; path=/; domain=.google.com; HttpOnly"The values from the two
Set-Cookieare indeed conflated into a singleSet-Cookie: <value_1>, <value_2>, as per this line…httpx/httpx/_models.py
Line 367 in 0e73be8
This is not really the problem that's causing this bug though, but rather that we're still calling
.multi_items()when processing response cookies, instead of calling intoresponse.headers.multi_items()in order to properly get all separateSet-Cookieheaders:httpx/httpx/_models.py
Line 1199 in 0e73be8
(Still not sure if concatenating headers into comma-separated lists shouldn't include some conditions on what header is being flattened. Eg for
Set-Cookiewe probably want to only keep the latest value, instead of concatenating all values which results in an invalidSet-Cookievalue syntax.)[-]Save/Load cookies from file[/-][+]Response cookies are not ingested properly in 0.14.0[/+]florimondmanca commentedon Aug 9, 2020
So to be clear, the fix is to change this line to use
.multi_items():httpx/httpx/_models.py
Line 1199 in 0e73be8
@djigit0s Does this sound legit to you? If you're up for throwing a PR together for this feel free to give it a go!
lovelydinosaur commentedon Aug 11, 2020
Now resolved in 0.14.1.
Thanks for raising this!