Cookies that include a port flag set to 443 aren't stored if they came from an https location without the 443 port in the URI

Aperta
#6,110 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
networking

Direzione di ricerca

Inizia con il riproduttore in tests/test_cookies.py ed esegui i test dei cookie parametrizzati per confermare il fallimento per https://localhost con port=443. Traccia il modo in cui la Session trasferisce i cookie della risposta nel proprio cookie jar, quindi fai passare il caso che fallisce senza introdurre regressioni nelle altre combinazioni di URL e porta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When performing a request to an https location that doesn't include a port explicitly (for example: https://localhost) and that location returns a cookie that includes a port flag set to 443, that cookie isn't stored into the cookie jar. However, this kind of cookie is stored into the cookie jar, when the port is set explicitly in the https location (for example: https://localhost:443).

Expected Result

A cookie that includes a port flag set to 443 should be stored into the cookie jar when the request is performed to an https location that doesn't include a port.

Reproduction Steps

I hope you don't mind that I've moved the Actual Result below this section. I did it because I've written a test-suite to reproduce the issue and the result of running that test-suite has been pasted in the Actual Result section.

import http
import pytest
import requests

from io import BytesIO
from urllib3.response import HTTPHeaderDict, HTTPResponse


class OriginalResponseShim:
    def __init__(self, headers):
        self.msg = headers

    def isclosed(self):
        return True

    def close(self):
        return


def make_http_response(
    method,
    url,
    status=requests.codes.ok,
    headers=None
):
    http_headers = HTTPHeaderDict()
    if headers is not None:
        http_headers.extend(headers)

    def _make_http_response(adapter, request, *args, **kwargs):
        http_response = HTTPResponse(
            status=status,
            reason=http.client.responses.get(status, None),
            body=BytesIO(b''),
            headers=http_headers,
            original_response=OriginalResponseShim(http_headers),
            preload_content=False,
        )

        return adapter.build_response(request, http_response)

    return _make_http_response


class TestCookies:
    @pytest.mark.parametrize('url', [
        'http://localhost',
        'http://localhost:80',
        'http://localhost:8080',
        'https://localhost',
        'https://localhost:443',
        'https://localhost:8080',
    ])
    def test_cookie_without_port(self, mocker, url):
        mocker.patch(
            'requests.adapters.HTTPAdapter.send',
            new=make_http_response(
                'GET',
                url,
                headers={
                    'set-cookie': 'cookie=value',
                },
            )
        )

        session = requests.Session()
        resp = session.request('GET', url)

        assert resp.status_code == requests.codes.ok
        assert 'set-cookie' in resp.headers
        assert 'cookie' in session.cookies

        cookie = next(iter(session.cookies))
        assert cookie.name == 'cookie'
        assert cookie.value == 'value'
        assert cookie.port is None

    @pytest.mark.parametrize('url, port', [
        ('http://localhost', 80),
        ('http://localhost:80', 80),
        ('http://localhost:443', 443),
        ('http://localhost:8080', 8080),
        ('https://localhost', 443),
        ('https://localhost:80', 80),
        ('https://localhost:443', 443),
        ('https://localhost:8080', 8080),
    ])
    def test_cookie_with_port(self, mocker, url, port):
        mocker.patch(
            'requests.adapters.HTTPAdapter.send',
            new=make_http_response(
                'GET',
                url,
                headers={
                    'set-cookie': f'cookie=value; port={port}',
                },
            )
        )

        session = requests.Session()
        resp = session.request('GET', url)

        assert resp.status_code == requests.codes.ok
        assert 'set-cookie' in resp.headers
        assert 'cookie' in session.cookies

        cookie = next(iter(session.cookies))
        assert cookie.name == 'cookie'
        assert cookie.value == 'value'
        assert cookie.port == f'{port}'

Actual Result

platform darwin -- Python 3.10.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /Users/fran/code/requests, configfile: pytest.ini
plugins: httpbin-1.0.0, mock-2.0.0, cov-3.0.0
collected 14 items

tests/test_cookies.py ..........F...                                               [100%]

======================================== FAILURES ========================================
________________ TestCookies.test_cookie_with_port[https://localhost-443] ________________

self = <tests.test_cookies.TestCookies object at 0x10e309ff0>
mocker = <pytest_mock.plugin.MockFixture object at 0x10e30a290>, url = 'https://localhost'
port = 443

    @pytest.mark.parametrize('url, port', [
        ('http://localhost', 80),
        ('http://localhost:80', 80),
        ('http://localhost:443', 443),
        ('http://localhost:8080', 8080),
        ('https://localhost', 443),
        ('https://localhost:80', 80),
        ('https://localhost:443', 443),
        ('https://localhost:8080', 8080),
    ])
    def test_cookie_with_port(self, mocker, url, port):
        mocker.patch(
            'requests.adapters.HTTPAdapter.send',
            new=make_http_response(
                'GET',
                url,
                headers={
                    'set-cookie': f'cookie=value; port={port}',
                },
            )
        )

        session = requests.Session()
        resp = session.request('GET', url)

        assert resp.status_code == requests.codes.ok
        assert 'set-cookie' in resp.headers
>       assert 'cookie' in session.cookies
E       AssertionError: assert 'cookie' in <RequestsCookieJar[]>
E        +  where <RequestsCookieJar[]> = <requests.sessions.Session object at 0x10e309b10>.cookies

tests/test_cookies.py:105: AssertionError

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "2.0.12"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.3"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.10.2"
  },
  "platform": {
    "release": "21.4.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.27.1"
  },
  "system_ssl": {
    "version": "101010ef"
  },
  "urllib3": {
    "version": "1.26.9"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
Lingua principale
Python
Stelle
54.7k
Fork
10.8k
Merge medio
18m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di psf/requests

Tutte le issue di psf/requests

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.