psf / psf/requests

RequestsCookieJar._find_on_duplicates wrongly fails on cookie with empty value

Open
#5,950 3 comments 0 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

Getting the value of a cookie which is set without a value by a website raises a KeyError. I don't know if this is by design. Browsers seem to accept these kind of cookies, even though this behaviour seems like bad practice.

Expected Result

An empty string.

Actual Result

RequestsCookieJar._find_no_duplicates raises a KeyError

Reproduction Steps

In ipython:

import requests
resp = requests.get("https://www.pib-insurance.com/")
resp.cookies.get_dict() # pretty print cookies
resp.cookies["firstname"]

Fix

I think last if in _find_no_duplicates (line 397 of cookies.py) should be if toReturn is not None: to accommodate for cookies without values.

Thanks!

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 in cookies.py at RequestsCookieJar._find_no_duplicates and reproduce the failure with the IPython example from the issue. Verify the empty-value cookie lookup returns an empty string rather than raising KeyError, and check the existing cookie behavior around this method before considering the change complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.