Skip to content

Commit 0e1f1f0

Browse files
tirkarthiorsenthil
authored andcommitted
bpo-35647: Fix path check in cookiejar (python#11436)
* Refactor cookie path check as per RFC 6265 * Add tests for prefix match of path * Add news entry * Fix set_ok_path and refactor tests * Use slice for last letter
1 parent 1aeeaeb commit 0e1f1f0

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

Lib/http/cookiejar.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ def set_ok_path(self, cookie, request):
993993
req_path = request_path(request)
994994
if ((cookie.version > 0 or
995995
(cookie.version == 0 and self.strict_ns_set_path)) and
996-
not req_path.startswith(cookie.path)):
996+
not self.path_return_ok(cookie.path, request)):
997997
_debug(" path attribute %s is not a prefix of request "
998998
"path %s", cookie.path, req_path)
999999
return False
@@ -1200,11 +1200,15 @@ def domain_return_ok(self, domain, request):
12001200
def path_return_ok(self, path, request):
12011201
_debug("- checking cookie path=%s", path)
12021202
req_path = request_path(request)
1203-
if not req_path.startswith(path):
1204-
_debug(" %s does not path-match %s", req_path, path)
1205-
return False
1206-
return True
1203+
pathlen = len(path)
1204+
if req_path == path:
1205+
return True
1206+
elif (req_path.startswith(path) and
1207+
(path.endswith("/") or req_path[pathlen:pathlen+1] == "/")):
1208+
return True
12071209

1210+
_debug(" %s does not path-match %s", req_path, path)
1211+
return False
12081212

12091213
def vals_sorted_by_key(adict):
12101214
keys = sorted(adict.keys())

Lib/test/test_http_cookiejar.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,30 @@ def test_request_path(self):
720720
req = urllib.request.Request("http://www.example.com")
721721
self.assertEqual(request_path(req), "/")
722722

723+
def test_path_prefix_match(self):
724+
pol = DefaultCookiePolicy()
725+
strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
726+
727+
c = CookieJar(pol)
728+
base_url = "http://bar.com"
729+
interact_netscape(c, base_url, 'spam=eggs; Path=/foo')
730+
cookie = c._cookies['bar.com']['/foo']['spam']
731+
732+
for path, ok in [('/foo', True),
733+
('/foo/', True),
734+
('/foo/bar', True),
735+
('/', False),
736+
('/foobad/foo', False)]:
737+
url = f'{base_url}{path}'
738+
req = urllib.request.Request(url)
739+
h = interact_netscape(c, url)
740+
if ok:
741+
self.assertIn('spam=eggs', h, f"cookie not set for {path}")
742+
self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req))
743+
else:
744+
self.assertNotIn('spam=eggs', h, f"cookie set for {path}")
745+
self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
746+
723747
def test_request_port(self):
724748
req = urllib.request.Request("http://www.acme.com:1234/",
725749
headers={"Host": "www.acme.com:4321"})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Don't set cookie for a request when the request path is a prefix match of
2+
the cookie's path attribute but doesn't end with "/". Patch by Karthikeyan
3+
Singaravelan.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy