Cookie having a Domain cookie-attribute with empty string fails to be added to the cookie jar

Open
#6,245 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
api

Research direction

Start with the supplied TestMissingSessionCookieIssue reproduction and the session.cookies.extract_cookies entry point. Run both test methods and compare the no-domain and empty-domain cases. Done means a cookie with a domain=; attribute is added to the cookie jar, matching the expected result.

Written by the indexing model from the issue text.

Description

Using requests to access an API hosted on an application server we noticed that cookies were not added to the cookie jar in the session.
It seems that after an upgrade to the application server framework it adds a domain=; cookie-attribute to the cookies in the response.

Expected Result

The cookie to be added to the jar

Actual Result

The cookie is dropped.

Reproduction Steps

import unittest
from typing import Tuple
from unittest.mock import MagicMock

import requests
from hamcrest import assert_that, has_length


class TestMissingSessionCookieIssue(unittest.TestCase):
    @staticmethod
    def __make_mocks(cookie_header_value: str) -> Tuple[MagicMock, MagicMock]:
        message = MagicMock()
        message.get_all.side_effect = [[], [cookie_header_value]]

        response = MagicMock()
        response.info.return_value = message

        request = MagicMock()
        request.get_full_url.return_value = "http://localhost:1234/page"
        request.origin_req_host = "localhost"

        return request, response

    def test_cookie_with_no_domain(self) -> None:
        request, response = self.__make_mocks("SESSION_ID=12345678; HttpOnly; Path=/page; SameSite=Strict")
        session = requests.session()

        session.cookies.extract_cookies(response, request)

        assert_that(session.cookies, has_length(1))

    def test_cookie_with_empty_domain(self) -> None:
        request, response = self.__make_mocks("SESSION_ID=12345678; domain=; path=/page; samesite=strict; httponly")
        session = requests.session()

        session.cookies.extract_cookies(response, request)

        assert_that(session.cookies, has_length(1))

Requirements:

  • PyHamcrest==2.0.2
  • requests==2.28.1
  • Python 3.9

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "2.0.12"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.5"
  },
  "platform": {
    "release": "5.14.0-1052-oem",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.28.1"
  },
  "system_ssl": {
    "version": "1010106f"
  },
  "urllib3": {
    "version": "1.26.12"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
Dominant language
Python
Stars
54.7k
Forks
10.8k
Avg merge
18m
Merged PRs (30d)
1

Getting set up

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.

More from psf/requests

All issues in psf/requests

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.