psf / psf/requests

RequestsCookieJar appends ".local" to cookie from localhost

Open
#5,388 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

When I make a request to localhost (on my development machine), Requests changes the domain from "localhost" to "localhost.local" when saving the cookie. Thus, I am unable to access the cookie if I specify the domain parameter in the get method.

My app requires me to get and set cookies on the session object as a persistence mechanism; I need to persist this cookie over more than one session in the python app.

Expected Result

I would expect the domain in RequestCookieJar to always match the domain I used to make the request.

Actual Result

If I make a request to localhost, instead I get the cookie saved to the domain "localhost.local". (Note that I cannot actually make requests to "localhost.local", they fail.) This puts me in a position where I must make requests to "localhost" but use "localhost.local" to get or set the cookie.

Reproduction Steps

>>> import requests
>>> ses = requests.Session()
>>> ses.get('http://localhost/')
<Response [200]>
>>> ses.cookies
<RequestsCookieJar[Cookie(version=0, name='PHPSESSID', value='ui1hr334bmcr1r85jlu8k3rq65', port=None, port_specified=False, domain='localhost.local', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>
>>> ses.cookies.get('PHPSESSID')
'ui1hr334bmcr1r85jlu8k3rq65'
>>> ses.cookies.get('PHPSESSID', domain='localhost')
>>> ses.cookies.get('PHPSESSID', domain='localhost.local')
'ui1hr334bmcr1r85jlu8k3rq65'

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.8"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.7.3"
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.22.0"
  },
  "system_ssl": {
    "version": "101000af"
  },
  "urllib3": {
    "version": "1.25.3"
  },
  "using_pyopenssl": false
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the localhost request and inspecting how the session's RequestsCookieJar stores cookie domains. Trace the cookie handling path responsible for changing localhost to localhost.local. Done means cookies from localhost retain the request domain and remain accessible through domain='localhost'.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.