Skip to content

Commit ee15aa2

Browse files
tirkarthiserhiy-storchaka
authored andcommitted
[2.7] bpo-35647: Fix path check in cookiejar. (GH-11436) (GH-13427)
1 parent 979daae commit ee15aa2

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

‎Lib/cookielib.py‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ def set_ok_path(self, cookie, request):
984984
req_path = request_path(request)
985985
if ((cookie.version > 0 or
986986
(cookie.version == 0 and self.strict_ns_set_path)) and
987-
not req_path.startswith(cookie.path)):
987+
not self.path_return_ok(cookie.path, request)):
988988
_debug(" path attribute %s is not a prefix of request "
989989
"path %s", cookie.path, req_path)
990990
return False
@@ -1191,11 +1191,15 @@ def domain_return_ok(self, domain, request):
11911191
def path_return_ok(self, path, request):
11921192
_debug("- checking cookie path=%s", path)
11931193
req_path = request_path(request)
1194-
if not req_path.startswith(path):
1195-
_debug(" %s does not path-match %s", req_path, path)
1196-
return False
1197-
return True
1194+
pathlen = len(path)
1195+
if req_path == path:
1196+
return True
1197+
elif (req_path.startswith(path) and
1198+
(path.endswith("/") or req_path[pathlen:pathlen+1] == "/")):
1199+
return True
11981200

1201+
_debug(" %s does not path-match %s", req_path, path)
1202+
return False
11991203

12001204
def vals_sorted_by_key(adict):
12011205
keys = adict.keys()

‎Lib/test/test_cookielib.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,35 @@ def test_request_path(self):
649649
req = Request("http://www.example.com")
650650
self.assertEqual(request_path(req), "/")
651651

652+
def test_path_prefix_match(self):
653+
from cookielib import CookieJar, DefaultCookiePolicy
654+
from urllib2 import Request
655+
656+
pol = DefaultCookiePolicy()
657+
strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
658+
659+
c = CookieJar(pol)
660+
base_url = "http://bar.com"
661+
interact_netscape(c, base_url, 'spam=eggs; Path=/foo')
662+
cookie = c._cookies['bar.com']['/foo']['spam']
663+
664+
for path, ok in [('/foo', True),
665+
('/foo/', True),
666+
('/foo/bar', True),
667+
('/', False),
668+
('/foobad/foo', False)]:
669+
url = '{0}{1}'.format(base_url, path)
670+
req = Request(url)
671+
h = interact_netscape(c, url)
672+
if ok:
673+
self.assertIn('spam=eggs', h,
674+
"cookie not set for {0}".format(path))
675+
self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req))
676+
else:
677+
self.assertNotIn('spam=eggs', h,
678+
"cookie set for {0}".format(path))
679+
self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
680+
652681
def test_request_port(self):
653682
from urllib2 import Request
654683
from cookielib import request_port, DEFAULT_HTTP_PORT
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)