Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting cookies from localhost #5977

Closed
kraktus opened this issue Nov 13, 2021 · 5 comments
Closed

Not getting cookies from localhost #5977

kraktus opened this issue Nov 13, 2021 · 5 comments

Comments

@kraktus
Copy link

kraktus commented Nov 13, 2021

Hello,

This is a follow up on #4643, which was closed without a fix. The issue is that requests does not get cookies from localhost. I could reproduce on 2.26.0.

I'm reposting the POC from #4643 (comment), which was added after the issue was closed.

import pytest
import threading
import requests

from tests.testserver.server import Server, consume_socket_content

def test_cookie_set_for_localhost():

    def response_handler(sock):
        consume_socket_content(sock, timeout=0.5)
        host, port = sock.getsockname()
        sock.send(
            b'HTTP/1.1 200 OK\r\n'
            b'Content-Length: 0\r\n'
            b'Set-Cookie: foo=bar;   Max-Age=86400; Domain=localhost; Secure; HttpOnly\r\n\r\n'
            # b'Set-Cookie: foo=bar; Max-Age=86400; Domain=localhost:' + str(port).encode('ascii') + b'; Secure; HttpOnly\r\n\r\n' # does not help
        )

    close_server = threading.Event()
    server = Server(response_handler, wait_to_close_event=close_server)

    with server as (host, port):
        url = 'http://{0}:{1}/'.format(host, port)
        r = requests.get(url)

        assert r.status_code == 200

        print(host, port)

        # Verify we received our expected cookie
        assert r.cookies
        assert r.cookies.get('foo') == 'bar'

        close_server.set()

It is unclear how #5388 could get them.

@keddad
Copy link

keddad commented Dec 14, 2021

I don't think this is actually request's bug. It looks like Python's CookieJar.extract_cookies doesn't parse them properly.

@keddad
Copy link

keddad commented Dec 14, 2021

@kraktus
Copy link
Author

kraktus commented Dec 14, 2021

Thanks for looking into the issue.

I doubt it’s a feature or the SoF answer is largely inaccurate since latest version of Chrome/FireFox andSafari stored the cookies on the website I had tested but requests did not.

@keddad
Copy link

keddad commented Dec 14, 2021

I've opened a bug report and wrote a possible solution: https://bugs.python.org/issue46075
However, since this is, definitely not a requests bug, this issue should probably be closed

@kraktus
Copy link
Author

kraktus commented Dec 14, 2021

Thank you 👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants