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

Remove incorrect handling of escaped quotes in cookie values #6891

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ Patches and Suggestions
- Dave Shawley <[email protected]>
- James Clarke (`@jam <https://github.com/jam>`_)
- Kevin Burke <[email protected]>
- Flavio Curella
- David Pursehouse <[email protected]> (`@dpursehouse <https://github.com/dpursehouse>`_)
- Jon Parise (`@jparise <https://github.com/jparise>`_)
- Alexander Karpinsky (`@homm86 <https://twitter.com/homm86>`_)
Expand Down Expand Up @@ -193,3 +192,4 @@ Patches and Suggestions
- Sylvain Marié (`@smarie <https://github.com/smarie>`_)
- Hod Bin Noon (`@hodbn <https://github.com/hodbn>`_)
- Mike Fiedler (`@miketheman <https://github.com/miketheman>`_)
- Linkai Zheng (`@Konano <https://github.com/Konano>`_)
9 changes: 0 additions & 9 deletions src/requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,6 @@ def __delitem__(self, name):
"""
remove_cookie_by_name(self, name)

def set_cookie(self, cookie, *args, **kwargs):
if (
hasattr(cookie.value, "startswith")
and cookie.value.startswith('"')
and cookie.value.endswith('"')
):
cookie.value = cookie.value.replace('\\"', "")
return super().set_cookie(cookie, *args, **kwargs)

def update(self, other):
"""Updates this jar with cookies from another CookieJar or dict-like"""
if isinstance(other, cookielib.CookieJar):
Expand Down
19 changes: 17 additions & 2 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,23 @@ def test_cookie_removed_on_expire(self, httpbin):

def test_cookie_quote_wrapped(self, httpbin):
s = requests.session()
s.get(httpbin('cookies/set?foo="bar:baz"'))
assert s.cookies["foo"] == '"bar:baz"'
r = s.get(httpbin('cookies/set?foo="bar:baz"'))
assert s.cookies["foo"] == '"\\"bar:baz\\""'
assert r.request.headers["Cookie"] == 'foo="\\"bar:baz\\""'
assert r.json()["cookies"]["foo"] == '"bar:baz"'

def test_cookie_with_json_value(self, httpbin):
s = requests.session()
r = s.get(httpbin('cookies/set?foo={"bar":"baz"}'))
assert s.cookies["foo"] == '"{\\"bar\\":\\"baz\\"}"'
assert r.request.headers["Cookie"] == 'foo="{\\"bar\\":\\"baz\\"}"'
assert r.json()["cookies"]["foo"] == '{"bar":"baz"}'

def test_param_cookies_with_json_value(self, httpbin):
s = requests.session()
r = s.get(httpbin("cookies"), cookies={"foo": '"{\\"bar\\":\\"baz\\"}"'})
assert r.request.headers["Cookie"] == 'foo="{\\"bar\\":\\"baz\\"}"'
assert r.json()["cookies"]["foo"] == '{"bar":"baz"}'

def test_cookie_persists_via_api(self, httpbin):
s = requests.session()
Expand Down