Skip to content

Commit e260f09

Browse files
tirkarthilarryhastings
authored andcommitted
bpo-35647: Fix path check in cookiejar (#11436) (#12278)
* 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 (cherry picked from commit 0e1f1f0)
1 parent 62d3654 commit e260f09

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Lib/http/cookiejar.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def set_ok_path(self, cookie, request):
990990
req_path = request_path(request)
991991
if ((cookie.version > 0 or
992992
(cookie.version == 0 and self.strict_ns_set_path)) and
993-
not req_path.startswith(cookie.path)):
993+
not self.path_return_ok(cookie.path, request)):
994994
_debug(" path attribute %s is not a prefix of request "
995995
"path %s", cookie.path, req_path)
996996
return False
@@ -1188,11 +1188,15 @@ def domain_return_ok(self, domain, request):
11881188
def path_return_ok(self, path, request):
11891189
_debug("- checking cookie path=%s", path)
11901190
req_path = request_path(request)
1191-
if not req_path.startswith(path):
1192-
_debug(" %s does not path-match %s", req_path, path)
1193-
return False
1194-
return True
1191+
pathlen = len(path)
1192+
if req_path == path:
1193+
return True
1194+
elif (req_path.startswith(path) and
1195+
(path.endswith("/") or req_path[pathlen:pathlen+1] == "/")):
1196+
return True
11951197

1198+
_debug(" %s does not path-match %s", req_path, path)
1199+
return False
11961200

11971201
def vals_sorted_by_key(adict):
11981202
keys = sorted(adict.keys())

Lib/test/test_http_cookiejar.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,32 @@ def test_request_path(self):
668668
req = urllib.request.Request("http://www.example.com")
669669
self.assertEqual(request_path(req), "/")
670670

671+
def test_path_prefix_match(self):
672+
pol = DefaultCookiePolicy()
673+
strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
674+
675+
c = CookieJar(pol)
676+
base_url = "http://bar.com"
677+
interact_netscape(c, base_url, 'spam=eggs; Path=/foo')
678+
cookie = c._cookies['bar.com']['/foo']['spam']
679+
680+
for path, ok in [('/foo', True),
681+
('/foo/', True),
682+
('/foo/bar', True),
683+
('/', False),
684+
('/foobad/foo', False)]:
685+
url = '{0}{1}'.format(base_url, path)
686+
req = urllib.request.Request(url)
687+
h = interact_netscape(c, url)
688+
if ok:
689+
self.assertIn('spam=eggs', h,
690+
"cookie not set for {0}".format(path))
691+
self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req))
692+
else:
693+
self.assertNotIn('spam=eggs', h,
694+
"cookie set for {0}".format(path))
695+
self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
696+
671697
def test_request_port(self):
672698
req = urllib.request.Request("http://www.acme.com:1234/",
673699
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