This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: BaseCookie.__parse_string() doesn't work with expires= between cookies
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: paulie4
Priority: normal Keywords:

Created on 2020-11-03 19:17 by paulie4, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg380294 - (view) Author: Paulie Pena (paulie4) Date: 2020-11-03 19:17
Since `requests` creates a comma-separated list for any duplicated headers, this causes a problem when `expires=...` is between two `Set-Cookie` header values. `BaseCookie.__parse_string()` in https://github.com/python/cpython/blob/master/Lib/http/cookies.py, in that case, will just give up, since it thinks it was given an invalid cookie. The fix is to replace the comma at the end of each trailing `expires=...` with a semicolon. Inside `BaseCookie.__parse_string()`, before the `while` loop, all that should be needed is to add this:
```
str = re.sub('(=\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT),', r'\1;', str)
```
msg380297 - (view) Author: Paulie Pena (paulie4) Date: 2020-11-03 19:33
whoops, the first string should also be a raw string, i.e. `r'...'`
msg391247 - (view) Author: Paulie Pena (paulie4) Date: 2021-04-16 18:32
Here's a simple example. This is correctly parsed via a `SimpleCookie().load()` call:
'key1=val1, key2=val2'

but this fails to parse:
'key1=val1; expires=Wed, 21 Oct 2015 07:28:00 GMT, key2=val2'

My suggested fix:
str = re.sub(r'(=\w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT),', r'\1;', str)

will convert the comma separator to a semicolon, so this will correctly parse:
'key1=val1; expires=Wed, 21 Oct 2015 07:28:00 GMT; key2=val2'
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86422
2021-04-16 18:32:59paulie4setmessages: + msg391247
2020-11-03 19:33:44paulie4setmessages: + msg380297
2020-11-03 19:17:21paulie4create