requests icon indicating copy to clipboard operation
requests copied to clipboard

[Bug] RequestsCookieJar — multiple_domains() returns incorrect results for repeated and distinct cookie domains

Open Vayuz-M opened this issue 2 weeks ago • 2 comments

Summary

RequestsCookieJar.multiple_domains() returns incorrect boolean results. It returns True when cookies share the same domain and False when cookies belong to different domains, which is the opposite of its documented behavior.

Expected Result

  • A cookie jar containing cookies from the same domain should return False.
  • A cookie jar containing cookies from multiple distinct domains should return True.

Actual Result

The results are reversed:

  • Same domain: True
  • Different domains: False

Reproduction Steps

Run the following Python code:

from requests.cookies import RequestsCookieJar

same = RequestsCookieJar()
same.set("a", "1", domain="example.com")
same.set("b", "2", domain="example.com")

different = RequestsCookieJar()
different.set("a", "1", domain="example.com")
different.set("b", "2", domain="other.com")

print("Same domain:", same.multiple_domains())
print("Different domains:", different.multiple_domains())
print("Listed domains:", different.list_domains())

Actual output:

Same domain: True
Different domains: False
Listed domains: ['example.com', 'other.com']

Expected output:

Same domain: False
Different domains: True
Listed domains: ['example.com', 'other.com']

System Information

Output of python -m requests.help:

{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "3.5.2"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.20"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.12.0"
  },
  "platform": {
    "release": "11",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.34.2"
  },
  "system_ssl": {
    "version": "300000b0"
  },
  "urllib3": {
    "version": "2.8.0"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}

Evidence

The issue was reproduced locally using Requests 2.34.2 installed from the cloned repository.

In src/requests/cookies.py, the multiple_domains() method currently contains this condition:

if cookie.domain is not None and cookie.domain in domains:
    return True

This checks whether a domain has already been encountered. Consequently, it detects repeated domains rather than multiple distinct domains.

A historical test in commit 96cd8e9c checked the single-domain case, but did not cover the multiple-distinct-domains scenario. A search of the current repository also found no test specifically covering this method's multiple-domain behavior.

Impact / Scope

Applications relying on RequestsCookieJar.multiple_domains() to determine whether a cookie jar contains cookies from multiple domains may receive incorrect results.

This can lead to incorrect domain-related decisions in code that uses this utility method.

Suspected Cause / Fix

The domain membership condition appears to be reversed.

The implementation should detect when a new, distinct domain is encountered rather than when an existing domain is encountered. The intended handling of cookies without a domain should also be preserved.

Duplicate Check

Searched existing issues in the psf/requests repository for:

  • multiple_domains
  • multiple_domains()
  • Multiple cookie domains
  • Incorrect cookie-domain detection

No existing issue describing this exact incorrect boolean behavior was identified.

Vayuz-M avatar Nov 15 '26 09:11 Vayuz-M

I have a fix ready for this on my fork — branch fix-7649-multiple-domains: https://github.com/psf/requests/compare/main...themythofsisyphusjayant:fix-7649-multiple-domains — but the repo only lets collaborators open pull requests, so I can't open the PR myself. Could a maintainer open a PR from that branch, or lift the restriction? Happy to address any review feedback.

themythofsisyphusjayant avatar Nov 16 '26 02:11 themythofsisyphusjayant

I reproduced this and have a fix ready (collect the distinct non-None domains and return True only when there is more than one, plus a regression test). I will open a PR shortly.

zhengdaodeguL avatar Nov 16 '26 20:11 zhengdaodeguL