Debian Codegraph @

Lib/test/test_http_cookiejar.py

raw
1 """Tests for http/cookiejar.py."""
2
3 import os
4 import stat
5 import sys
6 import re
7 from test import support
8 from test.support import os_helper
9 from test.support import warnings_helper
10 import time
11 import unittest
12 import urllib.request
13
14 from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
15 parse_ns_headers, join_header_words, split_header_words, Cookie,
16 CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar,
17 LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path,
18 reach, is_HDN, domain_match, user_domain_match, request_path,
19 request_port, request_host)
20
21 mswindows = (sys.platform == "win32")
22
23 class DateTimeTests(unittest.TestCase):
24
25 def test_time2isoz(self):
26 base = 1019227000
27 day = 24*3600
28 self.assertEqual(time2isoz(base), "2002-04-19 14:36:40Z")
29 self.assertEqual(time2isoz(base+day), "2002-04-20 14:36:40Z")
30 self.assertEqual(time2isoz(base+2*day), "2002-04-21 14:36:40Z")
31 self.assertEqual(time2isoz(base+3*day), "2002-04-22 14:36:40Z")
32
33 az = time2isoz()
34 bz = time2isoz(500000)
35 for text in (az, bz):
36 self.assertRegex(text, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$",
37 "bad time2isoz format: %s %s" % (az, bz))
38
39 def test_time2netscape(self):
40 base = 1019227000
41 day = 24*3600
42 self.assertEqual(time2netscape(base), "Fri, 19-Apr-2002 14:36:40 GMT")
43 self.assertEqual(time2netscape(base+day),
44 "Sat, 20-Apr-2002 14:36:40 GMT")
45
46 self.assertEqual(time2netscape(base+2*day),
47 "Sun, 21-Apr-2002 14:36:40 GMT")
48
49 self.assertEqual(time2netscape(base+3*day),
50 "Mon, 22-Apr-2002 14:36:40 GMT")
51
52 az = time2netscape()
53 bz = time2netscape(500000)
54 for text in (az, bz):
55 # Format "%s, %02d-%s-%04d %02d:%02d:%02d GMT"
56 self.assertRegex(
57 text,
58 r"[a-zA-Z]{3}, \d{2}-[a-zA-Z]{3}-\d{4} \d{2}:\d{2}:\d{2} GMT$",
59 "bad time2netscape format: %s %s" % (az, bz))
60
61 def test_http2time(self):
62 def parse_date(text):
63 return time.gmtime(http2time(text))[:6]
64
65 self.assertEqual(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0))
66
67 # this test will break around year 2070
68 self.assertEqual(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0))
69
70 # this test will break around year 2048
71 self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0))
72
73 def test_http2time_formats(self):
74 # test http2time for supported dates. Test cases with 2 digit year
75 # will probably break in year 2044.
76 tests = [
77 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format
78 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format
79 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format
80
81 '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday)
82 '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday)
83 '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)
84 '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)
85 '03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz)
86 '02-Feb-1994 24:00', # broken rfc850 (no weekday, no seconds,
87 # no tz) using hour 24 with yesterday date
88
89 '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
90 '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
91 '03 Feb 1994', # proposed new HTTP format (no weekday, no time)
92
93 # A few tests with extra space at various places
94 ' 03 Feb 1994 0:00 ',
95 ' 03-Feb-1994 ',
96 ]
97
98 test_t = 760233600 # assume broken POSIX counting of seconds
99 result = time2isoz(test_t)
100 expected = "1994-02-03 00:00:00Z"
101 self.assertEqual(result, expected,
102 "%s => '%s' (%s)" % (test_t, result, expected))
103
104 for s in tests:
105 self.assertEqual(http2time(s), test_t, s)
106 self.assertEqual(http2time(s.lower()), test_t, s.lower())
107 self.assertEqual(http2time(s.upper()), test_t, s.upper())
108
109 @support.subTests('test', [
110 '',
111 'Garbage',
112 'Mandag 16. September 1996',
113 '01-00-1980',
114 '01-13-1980',
115 '00-01-1980',
116 '32-01-1980',
117 '01-01-1980 25:00:00',
118 '01-01-1980 00:61:00',
119 '01-01-1980 00:00:62',
120 '08-Oct-3697739',
121 '08-01-3697739',
122 '09 Feb 19942632 22:23:32 GMT',
123 'Wed, 09 Feb 1994834 22:23:32 GMT',
124 ])
125 def test_http2time_garbage(self, test):
126 self.assertIsNone(http2time(test))
127
128 def test_http2time_redos_regression_actually_completes(self):
129 # LOOSE_HTTP_DATE_RE was vulnerable to malicious input which caused catastrophic backtracking (REDoS).
130 # If we regress to cubic complexity, this test will take a very long time to succeed.
131 # If fixed, it should complete within a fraction of a second.
132 http2time("01 Jan 1970{}00:00:00 GMT!".format(" " * 10 ** 5))
133 http2time("01 Jan 1970 00:00:00{}GMT!".format(" " * 10 ** 5))
134
135 def test_iso2time(self):
136 def parse_date(text):
137 return time.gmtime(iso2time(text))[:6]
138
139 # ISO 8601 compact format
140 self.assertEqual(parse_date("19940203T141529Z"),
141 (1994, 2, 3, 14, 15, 29))
142
143 # ISO 8601 with time behind UTC
144 self.assertEqual(parse_date("1994-02-03 07:15:29 -0700"),
145 (1994, 2, 3, 14, 15, 29))
146
147 # ISO 8601 with time ahead of UTC
148 self.assertEqual(parse_date("1994-02-03 19:45:29 +0530"),
149 (1994, 2, 3, 14, 15, 29))
150
151 @support.subTests('s', [
152 '1994-02-03 00:00:00 -0000', # ISO 8601 format
153 '1994-02-03 00:00:00 +0000', # ISO 8601 format
154 '1994-02-03 00:00:00', # zone is optional
155 '1994-02-03', # only date
156 '1994-02-03T00:00:00', # Use T as separator
157 '19940203', # only date
158 '1994-02-02 24:00:00', # using hour-24 yesterday date
159 '19940203T000000Z', # ISO 8601 compact format
160
161 # A few tests with extra space at various places
162 ' 1994-02-03 ',
163 ' 1994-02-03T00:00:00 ',
164 ])
165 def test_iso2time_formats(self, s):
166 # test iso2time for supported dates.
167 test_t = 760233600 # assume broken POSIX counting of seconds
168 self.assertEqual(iso2time(s), test_t, s)
169 self.assertEqual(iso2time(s.lower()), test_t, s.lower())
170 self.assertEqual(iso2time(s.upper()), test_t, s.upper())
171
172 @support.subTests('test', [
173 '',
174 'Garbage',
175 'Thursday, 03-Feb-94 00:00:00 GMT',
176 '1980-00-01',
177 '1980-13-01',
178 '1980-01-00',
179 '1980-01-32',
180 '1980-01-01 25:00:00',
181 '1980-01-01 00:61:00',
182 '01-01-1980 00:00:62',
183 '01-01-1980T00:00:62',
184 '19800101T250000Z',
185 ])
186 def test_iso2time_garbage(self, test):
187 self.assertIsNone(iso2time(test))
188
189 def test_iso2time_performance_regression(self):
190 # If ISO_DATE_RE regresses to quadratic complexity, this test will take a very long time to succeed.
191 # If fixed, it should complete within a fraction of a second.
192 iso2time('1994-02-03{}14:15:29 -0100!'.format(' '*10**6))
193 iso2time('1994-02-03 14:15:29{}-0100!'.format(' '*10**6))
194
195
196 class HeaderTests(unittest.TestCase):
197
198 @support.subTests('hdr', [
199 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',
200 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"',
201 ])
202 def test_parse_ns_headers(self, hdr):
203 # quotes should be stripped
204 expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]]
205 self.assertEqual(parse_ns_headers([hdr]), expected)
206
207 @support.subTests('hdr', [
208 'foo=bar; version="1"',
209 'foo=bar; Version="1"',
210 ])
211 def test_parse_ns_headers_version(self, hdr):
212 # quotes should be stripped
213 expected = [[('foo', 'bar'), ('version', '1')]]
214 self.assertEqual(parse_ns_headers([hdr]), expected)
215
216 def test_parse_ns_headers_special_names(self):
217 # names such as 'expires' are not special in first name=value pair
218 # of Set-Cookie: header
219 # Cookie with name 'expires'
220 hdr = 'expires=01 Jan 2040 22:23:32 GMT'
221 expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
222 self.assertEqual(parse_ns_headers([hdr]), expected)
223
224 @support.subTests('src,expected', [
225 ([[("foo", None), ("bar", "baz")]], "foo; bar=baz"),
226 (([]), ""),
227 (([[]]), ""),
228 (([[("a", "_")]]), "a=_"),
229 (([[("a", ";")]]), 'a=";"'),
230 ([[("n", None), ("foo", "foo;_")], [("bar", "foo_bar")]],
231 'n; foo="foo;_", bar=foo_bar'),
232 ([[("n", "m"), ("foo", None)], [("bar", "foo_bar")]],
233 'n=m; foo, bar=foo_bar'),
234 ])
235 def test_join_header_words(self, src, expected):
236 self.assertEqual(join_header_words(src), expected)
237
238 @support.subTests('arg,expect', [
239 ("foo", [[("foo", None)]]),
240 ("foo=bar", [[("foo", "bar")]]),
241 (" foo ", [[("foo", None)]]),
242 (" foo= ", [[("foo", "")]]),
243 (" foo=", [[("foo", "")]]),
244 (" foo= ; ", [[("foo", "")]]),
245 (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
246 ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
247 # doesn't really matter if this next fails, but it works ATM
248 ("foo= bar=baz", [[("foo", "bar=baz")]]),
249 ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
250 ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
251 ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
252 (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
253 [[("foo", None), ("bar", "baz")],
254 [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
255 ])
256 def test_split_header_words(self, arg, expect):
257 try:
258 result = split_header_words([arg])
259 except:
260 import traceback, io
261 f = io.StringIO()
262 traceback.print_exc(None, f)
263 result = "(error -- traceback follows)\n\n%s" % f.getvalue()
264 self.assertEqual(result, expect, """
265 When parsing: '%s'
266 Expected: '%s'
267 Got: '%s'
268 """ % (arg, expect, result))
269
270 @support.subTests('arg,expect', [
271 ("foo", "foo"),
272 ("foo=bar", "foo=bar"),
273 (" foo ", "foo"),
274 ("foo=", 'foo=""'),
275 ("foo=bar bar=baz", "foo=bar; bar=baz"),
276 ("foo=bar;bar=baz", "foo=bar; bar=baz"),
277 ('foo bar baz', "foo; bar; baz"),
278 (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
279 ("föo=bär", 'föo="bär"'),
280 ('foo,,,bar', 'foo, bar'),
281 ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
282 ("foo=\n", 'foo=""'),
283 ('foo="\n"', 'foo="\n"'),
284 ('foo=bar\n', 'foo=bar'),
285 ('foo="bar\n"', 'foo="bar\n"'),
286 ('foo=bar\nbaz', 'foo=bar; baz'),
287 ('foo="bar\nbaz"', 'foo="bar\nbaz"'),
288
289 ('text/html; charset=iso-8859-1',
290 'text/html; charset=iso-8859-1'),
291
292 ('text/html; charset="iso-8859/1"',
293 'text/html; charset="iso-8859/1"'),
294
295 ('foo="bar"; port="80,81"; discard, bar=baz',
296 'foo=bar; port="80,81"; discard, bar=baz'),
297
298 (r'Basic realm="\"foo\\\\bar\""',
299 r'Basic; realm="\"foo\\\\bar\""'),
300
301 ('n; foo="foo;_", bar="foo,_"',
302 'n; foo="foo;_", bar="foo,_"'),
303 ])
304 def test_roundtrip(self, arg, expect):
305 input = split_header_words([arg])
306 res = join_header_words(input)
307 self.assertEqual(res, expect, """
308 When parsing: '%s'
309 Expected: '%s'
310 Got: '%s'
311 Input was: '%s'
312 """ % (arg, expect, res, input))
313
314
315 class FakeResponse:
316 def __init__(self, headers=[], url=None):
317 """
318 headers: list of RFC822-style 'Key: value' strings
319 """
320 import email
321 self._headers = email.message_from_string("\n".join(headers))
322 self._url = url
323 def info(self): return self._headers
324
325 def interact_2965(cookiejar, url, *set_cookie_hdrs):
326 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")
327
328 def interact_netscape(cookiejar, url, *set_cookie_hdrs):
329 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")
330
331 def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
332 """Perform a single request / response cycle, returning Cookie: header."""
333 req = urllib.request.Request(url)
334 cookiejar.add_cookie_header(req)
335 cookie_hdr = req.get_header("Cookie", "")
336 headers = []
337 for hdr in set_cookie_hdrs:
338 headers.append("%s: %s" % (hdr_name, hdr))
339 res = FakeResponse(headers, url)
340 cookiejar.extract_cookies(res, req)
341 return cookie_hdr
342
343
344 class FileCookieJarTests(unittest.TestCase):
345 def test_constructor_with_str(self):
346 filename = os_helper.TESTFN
347 c = LWPCookieJar(filename)
348 self.assertEqual(c.filename, filename)
349
350 def test_constructor_with_path_like(self):
351 filename = os_helper.TESTFN
352 c = LWPCookieJar(os_helper.FakePath(filename))
353 self.assertEqual(c.filename, filename)
354
355 def test_constructor_with_none(self):
356 c = LWPCookieJar(None)
357 self.assertIsNone(c.filename)
358
359 def test_constructor_with_other_types(self):
360 class A:
361 pass
362
363 for type_ in (int, float, A):
364 with self.subTest(filename=type_):
365 with self.assertRaises(TypeError):
366 instance = type_()
367 c = LWPCookieJar(filename=instance)
368
369 def test_lwp_valueless_cookie(self):
370 # cookies with no value should be saved and loaded consistently
371 filename = os_helper.TESTFN
372 c = LWPCookieJar()
373 interact_netscape(c, "http://www.acme.com/", 'boo')
374 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
375 try:
376 c.save(filename, ignore_discard=True)
377 c = LWPCookieJar()
378 c.load(filename, ignore_discard=True)
379 finally:
380 os_helper.unlink(filename)
381 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
382
383 @unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
384 @os_helper.skip_unless_working_chmod
385 def test_lwp_filepermissions(self):
386 # Cookie file should only be readable by the creator
387 filename = os_helper.TESTFN
388 c = LWPCookieJar()
389 interact_netscape(c, "http://www.acme.com/", 'boo')
390 try:
391 c.save(filename, ignore_discard=True)
392 st = os.stat(filename)
393 self.assertEqual(stat.S_IMODE(st.st_mode), 0o600)
394 finally:
395 os_helper.unlink(filename)
396
397 @unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
398 @os_helper.skip_unless_working_chmod
399 def test_mozilla_filepermissions(self):
400 # Cookie file should only be readable by the creator
401 filename = os_helper.TESTFN
402 c = MozillaCookieJar()
403 interact_netscape(c, "http://www.acme.com/", 'boo')
404 try:
405 c.save(filename, ignore_discard=True)
406 st = os.stat(filename)
407 self.assertEqual(stat.S_IMODE(st.st_mode), 0o600)
408 finally:
409 os_helper.unlink(filename)
410
411 @unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes")
412 @os_helper.skip_unless_working_chmod
413 def test_cookie_files_are_truncated(self):
414 filename = os_helper.TESTFN
415 for cookiejar_class in (LWPCookieJar, MozillaCookieJar):
416 c = cookiejar_class(filename)
417
418 req = urllib.request.Request("http://www.acme.com/")
419 headers = ["Set-Cookie: pll_lang=en; Max-Age=31536000; path=/"]
420 res = FakeResponse(headers, "http://www.acme.com/")
421 c.extract_cookies(res, req)
422 self.assertEqual(len(c), 1)
423
424 try:
425 # Save the first version with contents:
426 c.save()
427 # Now, clear cookies and re-save:
428 c.clear()
429 c.save()
430 # Check that file was truncated:
431 c.load()
432 finally:
433 os_helper.unlink(filename)
434
435 self.assertEqual(len(c), 0)
436
437 def test_bad_magic(self):
438 # OSErrors (eg. file doesn't exist) are allowed to propagate
439 filename = os_helper.TESTFN
440 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
441 c = cookiejar_class()
442 try:
443 c.load(filename="for this test to work, a file with this "
444 "filename should not exist")
445 except OSError as exc:
446 # an OSError subclass (likely FileNotFoundError), but not
447 # LoadError
448 self.assertIsNot(exc.__class__, LoadError)
449 else:
450 self.fail("expected OSError for invalid filename")
451 # Invalid contents of cookies file (eg. bad magic string)
452 # causes a LoadError.
453 try:
454 with open(filename, "w") as f:
455 f.write("oops\n")
456 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
457 c = cookiejar_class()
458 self.assertRaises(LoadError, c.load, filename)
459 finally:
460 os_helper.unlink(filename)
461
462 class CookieTests(unittest.TestCase):
463 # XXX
464 # Get rid of string comparisons where not actually testing str / repr.
465 # .clear() etc.
466 # IP addresses like 50 (single number, no dot) and domain-matching
467 # functions (and is_HDN)? See draft RFC 2965 errata.
468 # Strictness switches
469 # is_third_party()
470 # unverifiability / third-party blocking
471 # Netscape cookies work the same as RFC 2965 with regard to port.
472 # Set-Cookie with negative max age.
473 # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber
474 # Set-Cookie cookies.
475 # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.).
476 # Cookies (V1 and V0) with no expiry date should be set to be discarded.
477 # RFC 2965 Quoting:
478 # Should accept unquoted cookie-attribute values? check errata draft.
479 # Which are required on the way in and out?
480 # Should always return quoted cookie-attribute values?
481 # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata).
482 # Path-match on return (same for V0 and V1).
483 # RFC 2965 acceptance and returning rules
484 # Set-Cookie2 without version attribute is rejected.
485
486 # Netscape peculiarities list from Ronald Tschalar.
487 # The first two still need tests, the rest are covered.
488 ## - Quoting: only quotes around the expires value are recognized as such
489 ## (and yes, some folks quote the expires value); quotes around any other
490 ## value are treated as part of the value.
491 ## - White space: white space around names and values is ignored
492 ## - Default path: if no path parameter is given, the path defaults to the
493 ## path in the request-uri up to, but not including, the last '/'. Note
494 ## that this is entirely different from what the spec says.
495 ## - Commas and other delimiters: Netscape just parses until the next ';'.
496 ## This means it will allow commas etc inside values (and yes, both
497 ## commas and equals are commonly appear in the cookie value). This also
498 ## means that if you fold multiple Set-Cookie header fields into one,
499 ## comma-separated list, it'll be a headache to parse (at least my head
500 ## starts hurting every time I think of that code).
501 ## - Expires: You'll get all sorts of date formats in the expires,
502 ## including empty expires attributes ("expires="). Be as flexible as you
503 ## can, and certainly don't expect the weekday to be there; if you can't
504 ## parse it, just ignore it and pretend it's a session cookie.
505 ## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not
506 ## just the 7 special TLD's listed in their spec. And folks rely on
507 ## that...
508
509 @support.subTests('url,domain,ok', [
510 ("http://foo.bar.com/", "blah.com", False),
511 ("http://foo.bar.com/", "rhubarb.blah.com", False),
512 ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
513 ("http://foo.bar.com/", ".foo.bar.com", True),
514 ("http://foo.bar.com/", "foo.bar.com", True),
515 ("http://foo.bar.com/", ".bar.com", True),
516 ("http://foo.bar.com/", "bar.com", True),
517 ("http://foo.bar.com/", "com", True),
518 ("http://foo.com/", "rhubarb.foo.com", False),
519 ("http://foo.com/", ".foo.com", True),
520 ("http://foo.com/", "foo.com", True),
521 ("http://foo.com/", "com", True),
522 ("http://foo/", "rhubarb.foo", False),
523 ("http://foo/", ".foo", True),
524 ("http://foo/", "foo", True),
525 ("http://foo/", "foo.local", True),
526 ("http://foo/", ".local", True),
527 ("http://barfoo.com", ".foo.com", False),
528 ("http://barfoo.com", "foo.com", False),
529 ])
530 def test_domain_return_ok(self, url, domain, ok):
531 # test optimization: .domain_return_ok() should filter out most
532 # domains in the CookieJar before we try to access them (because that
533 # may require disk access -- in particular, with MSIECookieJar)
534 # This is only a rough check for performance reasons, so it's not too
535 # critical as long as it's sufficiently liberal.
536 pol = DefaultCookiePolicy()
537 request = urllib.request.Request(url)
538 r = pol.domain_return_ok(domain, request)
539 if ok: self.assertTrue(r)
540 else: self.assertFalse(r)
541
542 def test_missing_value(self):
543 # missing = sign in Cookie: header is regarded by Mozilla as a missing
544 # name, and by http.cookiejar as a missing value
545 filename = os_helper.TESTFN
546 c = MozillaCookieJar(filename)
547 interact_netscape(c, "http://www.acme.com/", 'eggs')
548 interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
549 cookie = c._cookies["www.acme.com"]["/"]["eggs"]
550 self.assertIsNone(cookie.value)
551 self.assertEqual(cookie.name, "eggs")
552 cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
553 self.assertIsNone(cookie.value)
554 self.assertEqual(cookie.name, '"spam"')
555 self.assertEqual(lwp_cookie_str(cookie), (
556 r'"spam"; path="/foo/"; domain=www.acme.com; '
557 'path_spec; discard; version=0'))
558 old_str = repr(c)
559 c.save(ignore_expires=True, ignore_discard=True)
560 try:
561 c = MozillaCookieJar(filename)
562 c.revert(ignore_expires=True, ignore_discard=True)
563 finally:
564 os_helper.unlink(c.filename)
565 # cookies unchanged apart from lost info re. whether path was specified
566 self.assertEqual(
567 repr(c),
568 re.sub("path_specified=%s" % True, "path_specified=%s" % False,
569 old_str)
570 )
571 self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
572 '"spam"; eggs')
573
574 @support.subTests('rfc2109_as_netscape,rfc2965,version', [
575 # default according to rfc2965 if not explicitly specified
576 (None, False, 0),
577 (None, True, 1),
578 # explicit rfc2109_as_netscape
579 (False, False, None), # version None here means no cookie stored
580 (False, True, 1),
581 (True, False, 0),
582 (True, True, 0),
583 ])
584 def test_rfc2109_handling(self, rfc2109_as_netscape, rfc2965, version):
585 # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
586 # dependent on policy settings
587 policy = DefaultCookiePolicy(
588 rfc2109_as_netscape=rfc2109_as_netscape,
589 rfc2965=rfc2965)
590 c = CookieJar(policy)
591 interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
592 try:
593 cookie = c._cookies["www.example.com"]["/"]["ni"]
594 except KeyError:
595 self.assertIsNone(version) # didn't expect a stored cookie
596 else:
597 self.assertEqual(cookie.version, version)
598 # 2965 cookies are unaffected
599 interact_2965(c, "http://www.example.com/",
600 "foo=bar; Version=1")
601 if rfc2965:
602 cookie2965 = c._cookies["www.example.com"]["/"]["foo"]
603 self.assertEqual(cookie2965.version, 1)
604
605 def test_ns_parser(self):
606 c = CookieJar()
607 interact_netscape(c, "http://www.acme.com/",
608 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
609 interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')
610 interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')
611 interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')
612 interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '
613 'expires="Foo Bar 25 33:22:11 3022"')
614 interact_netscape(c, 'http://www.acme.com/', 'fortytwo=')
615 interact_netscape(c, 'http://www.acme.com/', '=unladenswallow')
616 interact_netscape(c, 'http://www.acme.com/', 'holyhandgrenade')
617
618 cookie = c._cookies[".acme.com"]["/"]["spam"]
619 self.assertEqual(cookie.domain, ".acme.com")
620 self.assertTrue(cookie.domain_specified)
621 self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)
622 self.assertFalse(cookie.port_specified)
623 # case is preserved
624 self.assertTrue(cookie.has_nonstandard_attr("blArgh"))
625 self.assertFalse(cookie.has_nonstandard_attr("blargh"))
626
627 cookie = c._cookies["www.acme.com"]["/"]["ni"]
628 self.assertEqual(cookie.domain, "www.acme.com")
629 self.assertFalse(cookie.domain_specified)
630 self.assertEqual(cookie.port, "80,8080")
631 self.assertTrue(cookie.port_specified)
632
633 cookie = c._cookies["www.acme.com"]["/"]["nini"]
634 self.assertIsNone(cookie.port)
635 self.assertFalse(cookie.port_specified)
636
637 # invalid expires should not cause cookie to be dropped
638 foo = c._cookies["www.acme.com"]["/"]["foo"]
639 spam = c._cookies["www.acme.com"]["/"]["foo"]
640 self.assertIsNone(foo.expires)
641 self.assertIsNone(spam.expires)
642
643 cookie = c._cookies['www.acme.com']['/']['fortytwo']
644 self.assertIsNotNone(cookie.value)
645 self.assertEqual(cookie.value, '')
646
647 # there should be a distinction between a present but empty value
648 # (above) and a value that's entirely missing (below)
649
650 cookie = c._cookies['www.acme.com']['/']['holyhandgrenade']
651 self.assertIsNone(cookie.value)
652
653 def test_ns_parser_special_names(self):
654 # names such as 'expires' are not special in first name=value pair
655 # of Set-Cookie: header
656 c = CookieJar()
657 interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
658 interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
659
660 cookies = c._cookies["www.acme.com"]["/"]
661 self.assertIn('expires', cookies)
662 self.assertIn('version', cookies)
663
664 def test_expires(self):
665 # if expires is in future, keep cookie...
666 c = CookieJar()
667 future = time2netscape(time.time()+3600)
668
669 with warnings_helper.check_no_warnings(self):
670 headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"]
671 req = urllib.request.Request("http://www.coyote.com/")
672 res = FakeResponse(headers, "http://www.coyote.com/")
673 cookies = c.make_cookies(res, req)
674 self.assertEqual(len(cookies), 1)
675 self.assertEqual(time2netscape(cookies[0].expires), future)
676
677 interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
678 future)
679 self.assertEqual(len(c), 1)
680 now = time2netscape(time.time()-1)
681 # ... and if in past or present, discard it
682 interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
683 now)
684 h = interact_netscape(c, "http://www.acme.com/")
685 self.assertEqual(len(c), 1)
686 self.assertIn('spam="bar"', h)
687 self.assertNotIn("foo", h)
688
689 # max-age takes precedence over expires, and zero max-age is request to
690 # delete both new cookie and any old matching cookie
691 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
692 future)
693 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
694 future)
695 self.assertEqual(len(c), 3)
696 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
697 'expires=%s; max-age=0' % future)
698 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
699 'max-age=0; expires=%s' % future)
700 h = interact_netscape(c, "http://www.acme.com/")
701 self.assertEqual(len(c), 1)
702
703 # test expiry at end of session for cookies with no expires attribute
704 interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
705 self.assertEqual(len(c), 2)
706 c.clear_session_cookies()
707 self.assertEqual(len(c), 1)
708 self.assertIn('spam="bar"', h)
709
710 # test if fractional expiry is accepted
711 cookie = Cookie(0, "name", "value",
712 None, False, "www.python.org",
713 True, False, "/",
714 False, False, "1444312383.018307",
715 False, None, None,
716 {})
717 self.assertEqual(cookie.expires, 1444312383)
718
719 # XXX RFC 2965 expiry rules (some apply to V0 too)
720
721 def test_default_path(self):
722 # RFC 2965
723 pol = DefaultCookiePolicy(rfc2965=True)
724
725 c = CookieJar(pol)
726 interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
727 self.assertIn("/", c._cookies["www.acme.com"])
728
729 c = CookieJar(pol)
730 interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')
731 self.assertIn("/", c._cookies["www.acme.com"])
732
733 c = CookieJar(pol)
734 interact_2965(c, "http://www.acme.com/blah/rhubarb",
735 'eggs="bar"; Version="1"')
736 self.assertIn("/blah/", c._cookies["www.acme.com"])
737
738 c = CookieJar(pol)
739 interact_2965(c, "http://www.acme.com/blah/rhubarb/",
740 'eggs="bar"; Version="1"')
741 self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])
742
743 # Netscape
744
745 c = CookieJar()
746 interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
747 self.assertIn("/", c._cookies["www.acme.com"])
748
749 c = CookieJar()
750 interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
751 self.assertIn("/", c._cookies["www.acme.com"])
752
753 c = CookieJar()
754 interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
755 self.assertIn("/blah", c._cookies["www.acme.com"])
756
757 c = CookieJar()
758 interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
759 self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])
760
761 def test_default_path_with_query(self):
762 cj = CookieJar()
763 uri = "http://example.com/?spam/eggs"
764 value = 'eggs="bar"'
765 interact_netscape(cj, uri, value)
766 # Default path does not include query, so is "/", not "/?spam".
767 self.assertIn("/", cj._cookies["example.com"])
768 # Cookie is sent back to the same URI.
769 self.assertEqual(interact_netscape(cj, uri), value)
770
771 @support.subTests('arg,result', [
772 # quoted safe
773 ("/foo%2f/bar", "/foo%2F/bar"),
774 ("/foo%2F/bar", "/foo%2F/bar"),
775 # quoted %
776 ("/foo%%/bar", "/foo%%/bar"),
777 # quoted unsafe
778 ("/fo%19o/bar", "/fo%19o/bar"),
779 ("/fo%7do/bar", "/fo%7Do/bar"),
780 # unquoted safe
781 ("/foo/bar&", "/foo/bar&"),
782 ("/foo//bar", "/foo//bar"),
783 ("\176/foo/bar", "\176/foo/bar"),
784 # unquoted unsafe
785 ("/foo\031/bar", "/foo%19/bar"),
786 ("/\175foo/bar", "/%7Dfoo/bar"),
787 # unicode, latin-1 range
788 ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded
789 # unicode
790 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded
791 ])
792 def test_escape_path(self, arg, result):
793 self.assertEqual(escape_path(arg), result)
794
795 def test_request_path(self):
796 # with parameters
797 req = urllib.request.Request(
798 "http://www.example.com/rheum/rhaponticum;"
799 "foo=bar;sing=song?apples=pears&spam=eggs#ni")
800 self.assertEqual(request_path(req),
801 "/rheum/rhaponticum;foo=bar;sing=song")
802 # without parameters
803 req = urllib.request.Request(
804 "http://www.example.com/rheum/rhaponticum?"
805 "apples=pears&spam=eggs#ni")
806 self.assertEqual(request_path(req), "/rheum/rhaponticum")
807 # missing final slash
808 req = urllib.request.Request("http://www.example.com")
809 self.assertEqual(request_path(req), "/")
810
811 def test_path_prefix_match(self):
812 pol = DefaultCookiePolicy()
813 strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
814
815 c = CookieJar(pol)
816 base_url = "http://bar.com"
817 interact_netscape(c, base_url, 'spam=eggs; Path=/foo')
818 cookie = c._cookies['bar.com']['/foo']['spam']
819
820 for path, ok in [('/foo', True),
821 ('/foo/', True),
822 ('/foo/bar', True),
823 ('/', False),
824 ('/foobad/foo', False)]:
825 url = f'{base_url}{path}'
826 req = urllib.request.Request(url)
827 h = interact_netscape(c, url)
828 if ok:
829 self.assertIn('spam=eggs', h, f"cookie not set for {path}")
830 self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req))
831 else:
832 self.assertNotIn('spam=eggs', h, f"cookie set for {path}")
833 self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
834
835 def test_request_port(self):
836 req = urllib.request.Request("http://www.acme.com:1234/",
837 headers={"Host": "www.acme.com:4321"})
838 self.assertEqual(request_port(req), "1234")
839 req = urllib.request.Request("http://www.acme.com/",
840 headers={"Host": "www.acme.com:4321"})
841 self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)
842
843 def test_request_host(self):
844 # this request is illegal (RFC2616, 14.2.3)
845 req = urllib.request.Request("http://1.1.1.1/",
846 headers={"Host": "www.acme.com:80"})
847 # libwww-perl wants this response, but that seems wrong (RFC 2616,
848 # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
849 #self.assertEqual(request_host(req), "www.acme.com")
850 self.assertEqual(request_host(req), "1.1.1.1")
851 req = urllib.request.Request("http://www.acme.com/",
852 headers={"Host": "irrelevant.com"})
853 self.assertEqual(request_host(req), "www.acme.com")
854 # port shouldn't be in request-host
855 req = urllib.request.Request("http://www.acme.com:2345/resource.html",
856 headers={"Host": "www.acme.com:5432"})
857 self.assertEqual(request_host(req), "www.acme.com")
858
859 def test_is_HDN(self):
860 self.assertTrue(is_HDN("foo.bar.com"))
861 self.assertTrue(is_HDN("1foo2.3bar4.5com"))
862 self.assertFalse(is_HDN("192.168.1.1"))
863 self.assertFalse(is_HDN(""))
864 self.assertFalse(is_HDN("."))
865 self.assertFalse(is_HDN(".foo.bar.com"))
866 self.assertFalse(is_HDN("..foo"))
867 self.assertFalse(is_HDN("foo."))
868
869 def test_reach(self):
870 self.assertEqual(reach("www.acme.com"), ".acme.com")
871 self.assertEqual(reach("acme.com"), "acme.com")
872 self.assertEqual(reach("acme.local"), ".local")
873 self.assertEqual(reach(".local"), ".local")
874 self.assertEqual(reach(".com"), ".com")
875 self.assertEqual(reach("."), ".")
876 self.assertEqual(reach(""), "")
877 self.assertEqual(reach("192.168.0.1"), "192.168.0.1")
878
879 def test_domain_match(self):
880 self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
881 self.assertFalse(domain_match("192.168.1.1", ".168.1.1"))
882 self.assertTrue(domain_match("x.y.com", "x.Y.com"))
883 self.assertTrue(domain_match("x.y.com", ".Y.com"))
884 self.assertFalse(domain_match("x.y.com", "Y.com"))
885 self.assertTrue(domain_match("a.b.c.com", ".c.com"))
886 self.assertFalse(domain_match(".c.com", "a.b.c.com"))
887 self.assertTrue(domain_match("example.local", ".local"))
888 self.assertFalse(domain_match("blah.blah", ""))
889 self.assertFalse(domain_match("", ".rhubarb.rhubarb"))
890 self.assertTrue(domain_match("", ""))
891
892 self.assertTrue(user_domain_match("acme.com", "acme.com"))
893 self.assertFalse(user_domain_match("acme.com", ".acme.com"))
894 self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
895 self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
896 self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
897 self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
898 self.assertFalse(user_domain_match("x.y.com", "Y.com"))
899 self.assertTrue(user_domain_match("y.com", "Y.com"))
900 self.assertFalse(user_domain_match(".y.com", "Y.com"))
901 self.assertTrue(user_domain_match(".y.com", ".Y.com"))
902 self.assertTrue(user_domain_match("x.y.com", ".com"))
903 self.assertFalse(user_domain_match("x.y.com", "com"))
904 self.assertFalse(user_domain_match("x.y.com", "m"))
905 self.assertFalse(user_domain_match("x.y.com", ".m"))
906 self.assertFalse(user_domain_match("x.y.com", ""))
907 self.assertFalse(user_domain_match("x.y.com", "."))
908 self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
909 # not both HDNs, so must string-compare equal to match
910 self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1"))
911 self.assertFalse(user_domain_match("192.168.1.1", "."))
912 # empty string is a special case
913 self.assertFalse(user_domain_match("192.168.1.1", ""))
914
915 def test_wrong_domain(self):
916 # Cookies whose effective request-host name does not domain-match the
917 # domain are rejected.
918
919 # XXX far from complete
920 c = CookieJar()
921 interact_2965(c, "http://www.nasty.com/",
922 'foo=bar; domain=friendly.org; Version="1"')
923 self.assertEqual(len(c), 0)
924
925 def test_strict_domain(self):
926 # Cookies whose domain is a country-code tld like .co.uk should
927 # not be set if CookiePolicy.strict_domain is true.
928 cp = DefaultCookiePolicy(strict_domain=True)
929 cj = CookieJar(policy=cp)
930 interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
931 interact_netscape(cj, "http://example.co.uk/",
932 'okey=dokey; Domain=.example.co.uk')
933 self.assertEqual(len(cj), 2)
934 for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
935 interact_netscape(cj, "http://example.%s/" % pseudo_tld,
936 'spam=eggs; Domain=.co.uk')
937 self.assertEqual(len(cj), 2)
938
939 def test_two_component_domain_ns(self):
940 # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
941 # should all get accepted, as should .acme.com, acme.com and no domain
942 # for 2-component domains like acme.com.
943 c = CookieJar()
944
945 # two-component V0 domain is OK
946 interact_netscape(c, "http://foo.net/", 'ns=bar')
947 self.assertEqual(len(c), 1)
948 self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")
949 self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")
950 # *will* be returned to any other domain (unlike RFC 2965)...
951 self.assertEqual(interact_netscape(c, "http://www.foo.net/"),
952 "ns=bar")
953 # ...unless requested otherwise
954 pol = DefaultCookiePolicy(
955 strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
956 c.set_policy(pol)
957 self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")
958
959 # unlike RFC 2965, even explicit two-component domain is OK,
960 # because .foo.net matches foo.net
961 interact_netscape(c, "http://foo.net/foo/",
962 'spam1=eggs; domain=foo.net')
963 # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
964 interact_netscape(c, "http://foo.net/foo/bar/",
965 'spam2=eggs; domain=.foo.net')
966 self.assertEqual(len(c), 3)
967 self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,
968 "eggs")
969 self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
970 "eggs")
971 self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),
972 "spam2=eggs; spam1=eggs; ns=bar")
973
974 # top-level domain is too general
975 interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
976 self.assertEqual(len(c), 3)
977
978 ## # Netscape protocol doesn't allow non-special top level domains (such
979 ## # as co.uk) in the domain attribute unless there are at least three
980 ## # dots in it.
981 # Oh yes it does! Real implementations don't check this, and real
982 # cookies (of course) rely on that behaviour.
983 interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')
984 ## self.assertEqual(len(c), 2)
985 self.assertEqual(len(c), 4)
986
987 def test_localhost_domain(self):
988 c = CookieJar()
989
990 interact_netscape(c, "http://localhost", "foo=bar; domain=localhost;")
991
992 self.assertEqual(len(c), 1)
993
994 def test_localhost_domain_contents(self):
995 c = CookieJar()
996
997 interact_netscape(c, "http://localhost", "foo=bar; domain=localhost;")
998
999 self.assertEqual(c._cookies[".localhost"]["/"]["foo"].value, "bar")
1000
1001 def test_localhost_domain_contents_2(self):
1002 c = CookieJar()
1003
1004 interact_netscape(c, "http://localhost", "foo=bar;")
1005
1006 self.assertEqual(c._cookies["localhost.local"]["/"]["foo"].value, "bar")
1007
1008 def test_evil_nonlocal_domain(self):
1009 c = CookieJar()
1010
1011 interact_netscape(c, "http://evil.com", "foo=bar; domain=.localhost")
1012
1013 self.assertEqual(len(c), 0)
1014
1015 def test_evil_local_domain(self):
1016 c = CookieJar()
1017
1018 interact_netscape(c, "http://localhost", "foo=bar; domain=.evil.com")
1019
1020 self.assertEqual(len(c), 0)
1021
1022 def test_evil_local_domain_2(self):
1023 c = CookieJar()
1024
1025 interact_netscape(c, "http://localhost", "foo=bar; domain=.someother.local")
1026
1027 self.assertEqual(len(c), 0)
1028
1029 def test_two_component_domain_rfc2965(self):
1030 pol = DefaultCookiePolicy(rfc2965=True)
1031 c = CookieJar(pol)
1032
1033 # two-component V1 domain is OK
1034 interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
1035 self.assertEqual(len(c), 1)
1036 self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")
1037 self.assertEqual(interact_2965(c, "http://foo.net/"),
1038 "$Version=1; foo=bar")
1039 # won't be returned to any other domain (because domain was implied)
1040 self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")
1041
1042 # unless domain is given explicitly, because then it must be
1043 # rewritten to start with a dot: foo.net --> .foo.net, which does
1044 # not domain-match foo.net
1045 interact_2965(c, "http://foo.net/foo",
1046 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
1047 self.assertEqual(len(c), 1)
1048 self.assertEqual(interact_2965(c, "http://foo.net/foo"),
1049 "$Version=1; foo=bar")
1050
1051 # explicit foo.net from three-component domain www.foo.net *does* get
1052 # set, because .foo.net domain-matches .foo.net
1053 interact_2965(c, "http://www.foo.net/foo/",
1054 'spam=eggs; domain=foo.net; Version="1"')
1055 self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,
1056 "eggs")
1057 self.assertEqual(len(c), 2)
1058 self.assertEqual(interact_2965(c, "http://foo.net/foo/"),
1059 "$Version=1; foo=bar")
1060 self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),
1061 '$Version=1; spam=eggs; $Domain="foo.net"')
1062
1063 # top-level domain is too general
1064 interact_2965(c, "http://foo.net/",
1065 'ni="ni"; domain=".net"; Version="1"')
1066 self.assertEqual(len(c), 2)
1067
1068 # RFC 2965 doesn't require blocking this
1069 interact_2965(c, "http://foo.co.uk/",
1070 'nasty=trick; domain=.co.uk; Version="1"')
1071 self.assertEqual(len(c), 3)
1072
1073 def test_domain_allow(self):
1074 c = CookieJar(policy=DefaultCookiePolicy(
1075 blocked_domains=["acme.com"],
1076 allowed_domains=["www.acme.com"]))
1077
1078 req = urllib.request.Request("http://acme.com/")
1079 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
1080 res = FakeResponse(headers, "http://acme.com/")
1081 c.extract_cookies(res, req)
1082 self.assertEqual(len(c), 0)
1083
1084 req = urllib.request.Request("http://www.acme.com/")
1085 res = FakeResponse(headers, "http://www.acme.com/")
1086 c.extract_cookies(res, req)
1087 self.assertEqual(len(c), 1)
1088
1089 req = urllib.request.Request("http://www.coyote.com/")
1090 res = FakeResponse(headers, "http://www.coyote.com/")
1091 c.extract_cookies(res, req)
1092 self.assertEqual(len(c), 1)
1093
1094 # set a cookie with non-allowed domain...
1095 req = urllib.request.Request("http://www.coyote.com/")
1096 res = FakeResponse(headers, "http://www.coyote.com/")
1097 cookies = c.make_cookies(res, req)
1098 c.set_cookie(cookies[0])
1099 self.assertEqual(len(c), 2)
1100 # ... and check is doesn't get returned
1101 c.add_cookie_header(req)
1102 self.assertFalse(req.has_header("Cookie"))
1103
1104 def test_domain_block(self):
1105 pol = DefaultCookiePolicy(
1106 rfc2965=True, blocked_domains=[".acme.com"])
1107 c = CookieJar(policy=pol)
1108 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
1109
1110 req = urllib.request.Request("http://www.acme.com/")
1111 res = FakeResponse(headers, "http://www.acme.com/")
1112 c.extract_cookies(res, req)
1113 self.assertEqual(len(c), 0)
1114
1115 p = pol.set_blocked_domains(["acme.com"])
1116 c.extract_cookies(res, req)
1117 self.assertEqual(len(c), 1)
1118
1119 c.clear()
1120 req = urllib.request.Request("http://www.roadrunner.net/")
1121 res = FakeResponse(headers, "http://www.roadrunner.net/")
1122 c.extract_cookies(res, req)
1123 self.assertEqual(len(c), 1)
1124 req = urllib.request.Request("http://www.roadrunner.net/")
1125 c.add_cookie_header(req)
1126 self.assertTrue(req.has_header("Cookie"))
1127 self.assertTrue(req.has_header("Cookie2"))
1128
1129 c.clear()
1130 pol.set_blocked_domains([".acme.com"])
1131 c.extract_cookies(res, req)
1132 self.assertEqual(len(c), 1)
1133
1134 # set a cookie with blocked domain...
1135 req = urllib.request.Request("http://www.acme.com/")
1136 res = FakeResponse(headers, "http://www.acme.com/")
1137 cookies = c.make_cookies(res, req)
1138 c.set_cookie(cookies[0])
1139 self.assertEqual(len(c), 2)
1140 # ... and check is doesn't get returned
1141 c.add_cookie_header(req)
1142 self.assertFalse(req.has_header("Cookie"))
1143
1144 c.clear()
1145
1146 pol.set_blocked_domains([])
1147 req = urllib.request.Request("http://acme.com/")
1148 res = FakeResponse(headers, "http://acme.com/")
1149 cookies = c.make_cookies(res, req)
1150 c.extract_cookies(res, req)
1151 self.assertEqual(len(c), 1)
1152
1153 req = urllib.request.Request("http://acme.com/")
1154 c.add_cookie_header(req)
1155 self.assertTrue(req.has_header("Cookie"))
1156
1157 req = urllib.request.Request("http://badacme.com/")
1158 c.add_cookie_header(req)
1159 self.assertFalse(pol.return_ok(cookies[0], req))
1160 self.assertFalse(req.has_header("Cookie"))
1161
1162 p = pol.set_blocked_domains(["acme.com"])
1163 req = urllib.request.Request("http://acme.com/")
1164 c.add_cookie_header(req)
1165 self.assertFalse(req.has_header("Cookie"))
1166
1167 req = urllib.request.Request("http://badacme.com/")
1168 c.add_cookie_header(req)
1169 self.assertFalse(req.has_header("Cookie"))
1170
1171 def test_secure(self):
1172 for ns in True, False:
1173 for whitespace in " ", "":
1174 c = CookieJar()
1175 if ns:
1176 pol = DefaultCookiePolicy(rfc2965=False)
1177 int = interact_netscape
1178 vs = ""
1179 else:
1180 pol = DefaultCookiePolicy(rfc2965=True)
1181 int = interact_2965
1182 vs = "; Version=1"
1183 c.set_policy(pol)
1184 url = "http://www.acme.com/"
1185 int(c, url, "foo1=bar%s%s" % (vs, whitespace))
1186 int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
1187 self.assertFalse(
1188 c._cookies["www.acme.com"]["/"]["foo1"].secure,
1189 "non-secure cookie registered secure")
1190 self.assertTrue(
1191 c._cookies["www.acme.com"]["/"]["foo2"].secure,
1192 "secure cookie registered non-secure")
1193
1194 def test_secure_block(self):
1195 pol = DefaultCookiePolicy()
1196 c = CookieJar(policy=pol)
1197
1198 headers = ["Set-Cookie: session=narf; secure; path=/"]
1199 req = urllib.request.Request("https://www.acme.com/")
1200 res = FakeResponse(headers, "https://www.acme.com/")
1201 c.extract_cookies(res, req)
1202 self.assertEqual(len(c), 1)
1203
1204 req = urllib.request.Request("https://www.acme.com/")
1205 c.add_cookie_header(req)
1206 self.assertTrue(req.has_header("Cookie"))
1207
1208 req = urllib.request.Request("http://www.acme.com/")
1209 c.add_cookie_header(req)
1210 self.assertFalse(req.has_header("Cookie"))
1211
1212 # secure websocket protocol
1213 req = urllib.request.Request("wss://www.acme.com/")
1214 c.add_cookie_header(req)
1215 self.assertTrue(req.has_header("Cookie"))
1216
1217 # non-secure websocket protocol
1218 req = urllib.request.Request("ws://www.acme.com/")
1219 c.add_cookie_header(req)
1220 self.assertFalse(req.has_header("Cookie"))
1221
1222 def test_custom_secure_protocols(self):
1223 pol = DefaultCookiePolicy(secure_protocols=["foos"])
1224 c = CookieJar(policy=pol)
1225
1226 headers = ["Set-Cookie: session=narf; secure; path=/"]
1227 req = urllib.request.Request("https://www.acme.com/")
1228 res = FakeResponse(headers, "https://www.acme.com/")
1229 c.extract_cookies(res, req)
1230 self.assertEqual(len(c), 1)
1231
1232 # test https removed from secure protocol list
1233 req = urllib.request.Request("https://www.acme.com/")
1234 c.add_cookie_header(req)
1235 self.assertFalse(req.has_header("Cookie"))
1236
1237 req = urllib.request.Request("http://www.acme.com/")
1238 c.add_cookie_header(req)
1239 self.assertFalse(req.has_header("Cookie"))
1240
1241 req = urllib.request.Request("foos://www.acme.com/")
1242 c.add_cookie_header(req)
1243 self.assertTrue(req.has_header("Cookie"))
1244
1245 req = urllib.request.Request("foo://www.acme.com/")
1246 c.add_cookie_header(req)
1247 self.assertFalse(req.has_header("Cookie"))
1248
1249 def test_quote_cookie_value(self):
1250 c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
1251 interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
1252 h = interact_2965(c, "http://www.acme.com/")
1253 self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')
1254
1255 def test_missing_final_slash(self):
1256 # Missing slash from request URL's abs_path should be assumed present.
1257 url = "http://www.acme.com"
1258 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1259 interact_2965(c, url, "foo=bar; Version=1")
1260 req = urllib.request.Request(url)
1261 self.assertEqual(len(c), 1)
1262 c.add_cookie_header(req)
1263 self.assertTrue(req.has_header("Cookie"))
1264
1265 def test_domain_mirror(self):
1266 pol = DefaultCookiePolicy(rfc2965=True)
1267
1268 c = CookieJar(pol)
1269 url = "http://foo.bar.com/"
1270 interact_2965(c, url, "spam=eggs; Version=1")
1271 h = interact_2965(c, url)
1272 self.assertNotIn("Domain", h,
1273 "absent domain returned with domain present")
1274
1275 c = CookieJar(pol)
1276 url = "http://foo.bar.com/"
1277 interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
1278 h = interact_2965(c, url)
1279 self.assertIn('$Domain=".bar.com"', h, "domain not returned")
1280
1281 c = CookieJar(pol)
1282 url = "http://foo.bar.com/"
1283 # note missing initial dot in Domain
1284 interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
1285 h = interact_2965(c, url)
1286 self.assertIn('$Domain="bar.com"', h, "domain not returned")
1287
1288 def test_path_mirror(self):
1289 pol = DefaultCookiePolicy(rfc2965=True)
1290
1291 c = CookieJar(pol)
1292 url = "http://foo.bar.com/"
1293 interact_2965(c, url, "spam=eggs; Version=1")
1294 h = interact_2965(c, url)
1295 self.assertNotIn("Path", h, "absent path returned with path present")
1296
1297 c = CookieJar(pol)
1298 url = "http://foo.bar.com/"
1299 interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
1300 h = interact_2965(c, url)
1301 self.assertIn('$Path="/"', h, "path not returned")
1302
1303 def test_port_mirror(self):
1304 pol = DefaultCookiePolicy(rfc2965=True)
1305
1306 c = CookieJar(pol)
1307 url = "http://foo.bar.com/"
1308 interact_2965(c, url, "spam=eggs; Version=1")
1309 h = interact_2965(c, url)
1310 self.assertNotIn("Port", h, "absent port returned with port present")
1311
1312 c = CookieJar(pol)
1313 url = "http://foo.bar.com/"
1314 interact_2965(c, url, "spam=eggs; Version=1; Port")
1315 h = interact_2965(c, url)
1316 self.assertRegex(h, r"\$Port([^=]|$)",
1317 "port with no value not returned with no value")
1318
1319 c = CookieJar(pol)
1320 url = "http://foo.bar.com/"
1321 interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
1322 h = interact_2965(c, url)
1323 self.assertIn('$Port="80"', h,
1324 "port with single value not returned with single value")
1325
1326 c = CookieJar(pol)
1327 url = "http://foo.bar.com/"
1328 interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
1329 h = interact_2965(c, url)
1330 self.assertIn('$Port="80,8080"', h,
1331 "port with multiple values not returned with multiple "
1332 "values")
1333
1334 def test_no_return_comment(self):
1335 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1336 url = "http://foo.bar.com/"
1337 interact_2965(c, url, 'spam=eggs; Version=1; '
1338 'Comment="does anybody read these?"; '
1339 'CommentURL="http://foo.bar.net/comment.html"')
1340 h = interact_2965(c, url)
1341 self.assertNotIn("Comment", h,
1342 "Comment or CommentURL cookie-attributes returned to server")
1343
1344 def test_Cookie_iterator(self):
1345 cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
1346 # add some random cookies
1347 interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
1348 'Comment="does anybody read these?"; '
1349 'CommentURL="http://foo.bar.net/comment.html"')
1350 interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
1351 interact_2965(cs, "http://www.acme.com/blah/",
1352 "foo=bar; secure; Version=1")
1353 interact_2965(cs, "http://www.acme.com/blah/",
1354 "foo=bar; path=/; Version=1")
1355 interact_2965(cs, "http://www.sol.no",
1356 r'bang=wallop; version=1; domain=".sol.no"; '
1357 r'port="90,100, 80,8080"; '
1358 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
1359
1360 versions = [1, 0, 1, 1, 1]
1361 names = ["foo", "spam", "foo", "foo", "bang"]
1362 domains = ["blah.spam.org", "www.acme.com", "www.acme.com",
1363 "www.acme.com", ".sol.no"]
1364 paths = ["/", "/blah", "/blah/", "/", "/"]
1365
1366 for i in range(4):
1367 i = 0
1368 for c in cs:
1369 self.assertIsInstance(c, Cookie)
1370 self.assertEqual(c.version, versions[i])
1371 self.assertEqual(c.name, names[i])
1372 self.assertEqual(c.domain, domains[i])
1373 self.assertEqual(c.path, paths[i])
1374 i = i + 1
1375
1376 def test_parse_ns_headers(self):
1377 # missing domain value (invalid cookie)
1378 self.assertEqual(
1379 parse_ns_headers(["foo=bar; path=/; domain"]),
1380 [[("foo", "bar"),
1381 ("path", "/"), ("domain", None), ("version", "0")]]
1382 )
1383 # invalid expires value
1384 self.assertEqual(
1385 parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
1386 [[("foo", "bar"), ("expires", None), ("version", "0")]]
1387 )
1388 # missing cookie value (valid cookie)
1389 self.assertEqual(
1390 parse_ns_headers(["foo"]),
1391 [[("foo", None), ("version", "0")]]
1392 )
1393 # missing cookie values for parsed attributes
1394 self.assertEqual(
1395 parse_ns_headers(['foo=bar; expires']),
1396 [[('foo', 'bar'), ('expires', None), ('version', '0')]])
1397 self.assertEqual(
1398 parse_ns_headers(['foo=bar; version']),
1399 [[('foo', 'bar'), ('version', None)]])
1400 # shouldn't add version if header is empty
1401 self.assertEqual(parse_ns_headers([""]), [])
1402
1403 def test_bad_cookie_header(self):
1404
1405 def cookiejar_from_cookie_headers(headers):
1406 c = CookieJar()
1407 req = urllib.request.Request("http://www.example.com/")
1408 r = FakeResponse(headers, "http://www.example.com/")
1409 c.extract_cookies(r, req)
1410 return c
1411
1412 future = time2netscape(time.time()+3600)
1413
1414 # none of these bad headers should cause an exception to be raised
1415 for headers in [
1416 ["Set-Cookie: "], # actually, nothing wrong with this
1417 ["Set-Cookie2: "], # ditto
1418 # missing domain value
1419 ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
1420 # bad max-age
1421 ["Set-Cookie: b=foo; max-age=oops"],
1422 # bad version
1423 ["Set-Cookie: b=foo; version=spam"],
1424 ["Set-Cookie:; Expires=%s" % future],
1425 ]:
1426 c = cookiejar_from_cookie_headers(headers)
1427 # these bad cookies shouldn't be set
1428 self.assertEqual(len(c), 0)
1429
1430 # cookie with invalid expires is treated as session cookie
1431 headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
1432 c = cookiejar_from_cookie_headers(headers)
1433 cookie = c._cookies["www.example.com"]["/"]["c"]
1434 self.assertIsNone(cookie.expires)
1435
1436
1437 class LWPCookieTests(unittest.TestCase):
1438 # Tests taken from libwww-perl, with a few modifications and additions.
1439
1440 def test_netscape_example_1(self):
1441 #-------------------------------------------------------------------
1442 # First we check that it works for the original example at
1443 # http://www.netscape.com/newsref/std/cookie_spec.html
1444
1445 # Client requests a document, and receives in the response:
1446 #
1447 # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
1448 #
1449 # When client requests a URL in path "/" on this server, it sends:
1450 #
1451 # Cookie: CUSTOMER=WILE_E_COYOTE
1452 #
1453 # Client requests a document, and receives in the response:
1454 #
1455 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1456 #
1457 # When client requests a URL in path "/" on this server, it sends:
1458 #
1459 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1460 #
1461 # Client receives:
1462 #
1463 # Set-Cookie: SHIPPING=FEDEX; path=/fo
1464 #
1465 # When client requests a URL in path "/" on this server, it sends:
1466 #
1467 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1468 #
1469 # When client requests a URL in path "/foo" on this server, it sends:
1470 #
1471 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
1472 #
1473 # The last Cookie is buggy, because both specifications say that the
1474 # most specific cookie must be sent first. SHIPPING=FEDEX is the
1475 # most specific and should thus be first.
1476
1477 year_plus_one = time.localtime()[0] + 1
1478
1479 headers = []
1480
1481 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1482
1483 #req = urllib.request.Request("http://1.1.1.1/",
1484 # headers={"Host": "www.acme.com:80"})
1485 req = urllib.request.Request("http://www.acme.com:80/",
1486 headers={"Host": "www.acme.com:80"})
1487
1488 headers.append(
1489 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
1490 "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
1491 res = FakeResponse(headers, "http://www.acme.com/")
1492 c.extract_cookies(res, req)
1493
1494 req = urllib.request.Request("http://www.acme.com/")
1495 c.add_cookie_header(req)
1496
1497 self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
1498 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1499
1500 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1501 res = FakeResponse(headers, "http://www.acme.com/")
1502 c.extract_cookies(res, req)
1503
1504 req = urllib.request.Request("http://www.acme.com/foo/bar")
1505 c.add_cookie_header(req)
1506
1507 h = req.get_header("Cookie")
1508 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1509 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1510
1511 headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
1512 res = FakeResponse(headers, "http://www.acme.com")
1513 c.extract_cookies(res, req)
1514
1515 req = urllib.request.Request("http://www.acme.com/")
1516 c.add_cookie_header(req)
1517
1518 h = req.get_header("Cookie")
1519 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1520 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1521 self.assertNotIn("SHIPPING=FEDEX", h)
1522
1523 req = urllib.request.Request("http://www.acme.com/foo/")
1524 c.add_cookie_header(req)
1525
1526 h = req.get_header("Cookie")
1527 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1528 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1529 self.assertStartsWith(h, "SHIPPING=FEDEX;")
1530
1531 def test_netscape_example_2(self):
1532 # Second Example transaction sequence:
1533 #
1534 # Assume all mappings from above have been cleared.
1535 #
1536 # Client receives:
1537 #
1538 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1539 #
1540 # When client requests a URL in path "/" on this server, it sends:
1541 #
1542 # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
1543 #
1544 # Client receives:
1545 #
1546 # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
1547 #
1548 # When client requests a URL in path "/ammo" on this server, it sends:
1549 #
1550 # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
1551 #
1552 # NOTE: There are two name/value pairs named "PART_NUMBER" due to
1553 # the inheritance of the "/" mapping in addition to the "/ammo" mapping.
1554
1555 c = CookieJar()
1556 headers = []
1557
1558 req = urllib.request.Request("http://www.acme.com/")
1559 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1560 res = FakeResponse(headers, "http://www.acme.com/")
1561
1562 c.extract_cookies(res, req)
1563
1564 req = urllib.request.Request("http://www.acme.com/")
1565 c.add_cookie_header(req)
1566
1567 self.assertEqual(req.get_header("Cookie"),
1568 "PART_NUMBER=ROCKET_LAUNCHER_0001")
1569
1570 headers.append(
1571 "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
1572 res = FakeResponse(headers, "http://www.acme.com/")
1573 c.extract_cookies(res, req)
1574
1575 req = urllib.request.Request("http://www.acme.com/ammo")
1576 c.add_cookie_header(req)
1577
1578 self.assertRegex(req.get_header("Cookie"),
1579 r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
1580 "PART_NUMBER=ROCKET_LAUNCHER_0001")
1581
1582 def test_ietf_example_1(self):
1583 #-------------------------------------------------------------------
1584 # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
1585 #
1586 # 5. EXAMPLES
1587
1588 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1589
1590 #
1591 # 5.1 Example 1
1592 #
1593 # Most detail of request and response headers has been omitted. Assume
1594 # the user agent has no stored cookies.
1595 #
1596 # 1. User Agent -> Server
1597 #
1598 # POST /acme/login HTTP/1.1
1599 # [form data]
1600 #
1601 # User identifies self via a form.
1602 #
1603 # 2. Server -> User Agent
1604 #
1605 # HTTP/1.1 200 OK
1606 # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
1607 #
1608 # Cookie reflects user's identity.
1609
1610 cookie = interact_2965(
1611 c, 'http://www.acme.com/acme/login',
1612 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
1613 self.assertFalse(cookie)
1614
1615 #
1616 # 3. User Agent -> Server
1617 #
1618 # POST /acme/pickitem HTTP/1.1
1619 # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
1620 # [form data]
1621 #
1622 # User selects an item for ``shopping basket.''
1623 #
1624 # 4. Server -> User Agent
1625 #
1626 # HTTP/1.1 200 OK
1627 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1628 # Path="/acme"
1629 #
1630 # Shopping basket contains an item.
1631
1632 cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
1633 'Part_Number="Rocket_Launcher_0001"; '
1634 'Version="1"; Path="/acme"');
1635 self.assertRegex(cookie,
1636 r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$')
1637
1638 #
1639 # 5. User Agent -> Server
1640 #
1641 # POST /acme/shipping HTTP/1.1
1642 # Cookie: $Version="1";
1643 # Customer="WILE_E_COYOTE"; $Path="/acme";
1644 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1645 # [form data]
1646 #
1647 # User selects shipping method from form.
1648 #
1649 # 6. Server -> User Agent
1650 #
1651 # HTTP/1.1 200 OK
1652 # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
1653 #
1654 # New cookie reflects shipping method.
1655
1656 cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
1657 'Shipping="FedEx"; Version="1"; Path="/acme"')
1658
1659 self.assertRegex(cookie, r'^\$Version="?1"?;')
1660 self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;'
1661 r'\s*\$Path="\/acme"')
1662 self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;'
1663 r'\s*\$Path="\/acme"')
1664
1665 #
1666 # 7. User Agent -> Server
1667 #
1668 # POST /acme/process HTTP/1.1
1669 # Cookie: $Version="1";
1670 # Customer="WILE_E_COYOTE"; $Path="/acme";
1671 # Part_Number="Rocket_Launcher_0001"; $Path="/acme";
1672 # Shipping="FedEx"; $Path="/acme"
1673 # [form data]
1674 #
1675 # User chooses to process order.
1676 #
1677 # 8. Server -> User Agent
1678 #
1679 # HTTP/1.1 200 OK
1680 #
1681 # Transaction is complete.
1682
1683 cookie = interact_2965(c, "http://www.acme.com/acme/process")
1684 self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"')
1685 self.assertIn("WILE_E_COYOTE", cookie)
1686
1687 #
1688 # The user agent makes a series of requests on the origin server, after
1689 # each of which it receives a new cookie. All the cookies have the same
1690 # Path attribute and (default) domain. Because the request URLs all have
1691 # /acme as a prefix, and that matches the Path attribute, each request
1692 # contains all the cookies received so far.
1693
1694 def test_ietf_example_2(self):
1695 # 5.2 Example 2
1696 #
1697 # This example illustrates the effect of the Path attribute. All detail
1698 # of request and response headers has been omitted. Assume the user agent
1699 # has no stored cookies.
1700
1701 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1702
1703 # Imagine the user agent has received, in response to earlier requests,
1704 # the response headers
1705 #
1706 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1707 # Path="/acme"
1708 #
1709 # and
1710 #
1711 # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
1712 # Path="/acme/ammo"
1713
1714 interact_2965(
1715 c, "http://www.acme.com/acme/ammo/specific",
1716 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
1717 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
1718
1719 # A subsequent request by the user agent to the (same) server for URLs of
1720 # the form /acme/ammo/... would include the following request header:
1721 #
1722 # Cookie: $Version="1";
1723 # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
1724 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1725 #
1726 # Note that the NAME=VALUE pair for the cookie with the more specific Path
1727 # attribute, /acme/ammo, comes before the one with the less specific Path
1728 # attribute, /acme. Further note that the same cookie name appears more
1729 # than once.
1730
1731 cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
1732 self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001")
1733
1734 # A subsequent request by the user agent to the (same) server for a URL of
1735 # the form /acme/parts/ would include the following request header:
1736 #
1737 # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1738 #
1739 # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
1740 # the request URL, /acme/parts/, so the cookie does not get forwarded to
1741 # the server.
1742
1743 cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
1744 self.assertIn("Rocket_Launcher_0001", cookie)
1745 self.assertNotIn("Riding_Rocket_0023", cookie)
1746
1747 def test_rejection(self):
1748 # Test rejection of Set-Cookie2 responses based on domain, path, port.
1749 pol = DefaultCookiePolicy(rfc2965=True)
1750
1751 c = LWPCookieJar(policy=pol)
1752
1753 max_age = "max-age=3600"
1754
1755 # illegal domain (no embedded dots)
1756 cookie = interact_2965(c, "http://www.acme.com",
1757 'foo=bar; domain=".com"; version=1')
1758 self.assertFalse(c)
1759
1760 # legal domain
1761 cookie = interact_2965(c, "http://www.acme.com",
1762 'ping=pong; domain="acme.com"; version=1')
1763 self.assertEqual(len(c), 1)
1764
1765 # illegal domain (host prefix "www.a" contains a dot)
1766 cookie = interact_2965(c, "http://www.a.acme.com",
1767 'whiz=bang; domain="acme.com"; version=1')
1768 self.assertEqual(len(c), 1)
1769
1770 # legal domain
1771 cookie = interact_2965(c, "http://www.a.acme.com",
1772 'wow=flutter; domain=".a.acme.com"; version=1')
1773 self.assertEqual(len(c), 2)
1774
1775 # can't partially match an IP-address
1776 cookie = interact_2965(c, "http://125.125.125.125",
1777 'zzzz=ping; domain="125.125.125"; version=1')
1778 self.assertEqual(len(c), 2)
1779
1780 # illegal path (must be prefix of request path)
1781 cookie = interact_2965(c, "http://www.sol.no",
1782 'blah=rhubarb; domain=".sol.no"; path="/foo"; '
1783 'version=1')
1784 self.assertEqual(len(c), 2)
1785
1786 # legal path
1787 cookie = interact_2965(c, "http://www.sol.no/foo/bar",
1788 'bing=bong; domain=".sol.no"; path="/foo"; '
1789 'version=1')
1790 self.assertEqual(len(c), 3)
1791
1792 # illegal port (request-port not in list)
1793 cookie = interact_2965(c, "http://www.sol.no",
1794 'whiz=ffft; domain=".sol.no"; port="90,100"; '
1795 'version=1')
1796 self.assertEqual(len(c), 3)
1797
1798 # legal port
1799 cookie = interact_2965(
1800 c, "http://www.sol.no",
1801 r'bang=wallop; version=1; domain=".sol.no"; '
1802 r'port="90,100, 80,8080"; '
1803 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
1804 self.assertEqual(len(c), 4)
1805
1806 # port attribute without any value (current port)
1807 cookie = interact_2965(c, "http://www.sol.no",
1808 'foo9=bar; version=1; domain=".sol.no"; port; '
1809 'max-age=100;')
1810 self.assertEqual(len(c), 5)
1811
1812 # encoded path
1813 # LWP has this test, but unescaping allowed path characters seems
1814 # like a bad idea, so I think this should fail:
1815 ## cookie = interact_2965(c, "http://www.sol.no/foo/",
1816 ## r'foo8=bar; version=1; path="/%66oo"')
1817 # but this is OK, because '<' is not an allowed HTTP URL path
1818 # character:
1819 cookie = interact_2965(c, "http://www.sol.no/<oo/",
1820 r'foo8=bar; version=1; path="/%3coo"')
1821 self.assertEqual(len(c), 6)
1822
1823 # save and restore
1824 filename = os_helper.TESTFN
1825
1826 try:
1827 c.save(filename, ignore_discard=True)
1828 old = repr(c)
1829
1830 c = LWPCookieJar(policy=pol)
1831 c.load(filename, ignore_discard=True)
1832 finally:
1833 os_helper.unlink(filename)
1834
1835 self.assertEqual(old, repr(c))
1836
1837 def test_url_encoding(self):
1838 # Try some URL encodings of the PATHs.
1839 # (the behaviour here has changed from libwww-perl)
1840 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1841 interact_2965(c, "http://www.acme.com/foo%2f%25/"
1842 "%3c%3c%0Anew%C3%A5/%C3%A5",
1843 "foo = bar; version = 1")
1844
1845 cookie = interact_2965(
1846 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
1847 'bar=baz; path="/foo/"; version=1');
1848 version_re = re.compile(r'^\$version=\"?1\"?', re.I)
1849 self.assertIn("foo=bar", cookie)
1850 self.assertRegex(cookie, version_re)
1851
1852 cookie = interact_2965(
1853 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
1854 self.assertFalse(cookie)
1855
1856 # unicode URL doesn't raise exception
1857 cookie = interact_2965(c, "http://www.acme.com/\xfc")
1858
1859 def test_mozilla(self):
1860 # Save / load Mozilla/Netscape cookie file format.
1861 year_plus_one = time.localtime()[0] + 1
1862
1863 filename = os_helper.TESTFN
1864
1865 c = MozillaCookieJar(filename,
1866 policy=DefaultCookiePolicy(rfc2965=True))
1867 interact_2965(c, "http://www.acme.com/",
1868 "foo1=bar; max-age=100; Version=1")
1869 interact_2965(c, "http://www.acme.com/",
1870 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
1871 interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
1872
1873 expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
1874 interact_netscape(c, "http://www.foo.com/",
1875 "fooa=bar; %s" % expires)
1876 interact_netscape(c, "http://www.foo.com/",
1877 "foob=bar; Domain=.foo.com; %s" % expires)
1878 interact_netscape(c, "http://www.foo.com/",
1879 "fooc=bar; Domain=www.foo.com; %s" % expires)
1880
1881 for cookie in c:
1882 if cookie.name == "foo1":
1883 cookie.set_nonstandard_attr("HTTPOnly", "")
1884
1885 def save_and_restore(cj, ignore_discard):
1886 try:
1887 cj.save(ignore_discard=ignore_discard)
1888 new_c = MozillaCookieJar(filename,
1889 DefaultCookiePolicy(rfc2965=True))
1890 new_c.load(ignore_discard=ignore_discard)
1891 finally:
1892 os_helper.unlink(filename)
1893 return new_c
1894
1895 new_c = save_and_restore(c, True)
1896 self.assertEqual(len(new_c), 6) # none discarded
1897 self.assertIn("name='foo1', value='bar'", repr(new_c))
1898 self.assertIn("rest={'HTTPOnly': ''}", repr(new_c))
1899
1900 new_c = save_and_restore(c, False)
1901 self.assertEqual(len(new_c), 4) # 2 of them discarded on save
1902 self.assertIn("name='foo1', value='bar'", repr(new_c))
1903
1904 def test_netscape_misc(self):
1905 # Some additional Netscape cookies tests.
1906 c = CookieJar()
1907 headers = []
1908 req = urllib.request.Request("http://foo.bar.acme.com/foo")
1909
1910 # Netscape allows a host part that contains dots
1911 headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
1912 res = FakeResponse(headers, "http://www.acme.com/foo")
1913 c.extract_cookies(res, req)
1914
1915 # and that the domain is the same as the host without adding a leading
1916 # dot to the domain. Should not quote even if strange chars are used
1917 # in the cookie value.
1918 headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
1919 res = FakeResponse(headers, "http://www.acme.com/foo")
1920 c.extract_cookies(res, req)
1921
1922 req = urllib.request.Request("http://foo.bar.acme.com/foo")
1923 c.add_cookie_header(req)
1924 self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie"))
1925 self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie"))
1926
1927 def test_intranet_domains_2965(self):
1928 # Test handling of local intranet hostnames without a dot.
1929 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1930 interact_2965(c, "http://example/",
1931 "foo1=bar; PORT; Discard; Version=1;")
1932 cookie = interact_2965(c, "http://example/",
1933 'foo2=bar; domain=".local"; Version=1')
1934 self.assertIn("foo1=bar", cookie)
1935
1936 interact_2965(c, "http://example/", 'foo3=bar; Version=1')
1937 cookie = interact_2965(c, "http://example/")
1938 self.assertIn("foo2=bar", cookie)
1939 self.assertEqual(len(c), 3)
1940
1941 def test_intranet_domains_ns(self):
1942 c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
1943 interact_netscape(c, "http://example/", "foo1=bar")
1944 cookie = interact_netscape(c, "http://example/",
1945 'foo2=bar; domain=.local')
1946 self.assertEqual(len(c), 2)
1947 self.assertIn("foo1=bar", cookie)
1948
1949 cookie = interact_netscape(c, "http://example/")
1950 self.assertIn("foo2=bar", cookie)
1951 self.assertEqual(len(c), 2)
1952
1953 def test_empty_path(self):
1954 # Test for empty path
1955 # Broken web-server ORION/1.3.38 returns to the client response like
1956 #
1957 # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
1958 #
1959 # ie. with Path set to nothing.
1960 # In this case, extract_cookies() must set cookie to / (root)
1961 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1962 headers = []
1963
1964 req = urllib.request.Request("http://www.ants.com/")
1965 headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
1966 res = FakeResponse(headers, "http://www.ants.com/")
1967 c.extract_cookies(res, req)
1968
1969 req = urllib.request.Request("http://www.ants.com/")
1970 c.add_cookie_header(req)
1971
1972 self.assertEqual(req.get_header("Cookie"),
1973 "JSESSIONID=ABCDERANDOM123")
1974 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1975
1976 # missing path in the request URI
1977 req = urllib.request.Request("http://www.ants.com:8080")
1978 c.add_cookie_header(req)
1979
1980 self.assertEqual(req.get_header("Cookie"),
1981 "JSESSIONID=ABCDERANDOM123")
1982 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1983
1984 def test_session_cookies(self):
1985 year_plus_one = time.localtime()[0] + 1
1986
1987 # Check session cookies are deleted properly by
1988 # CookieJar.clear_session_cookies method
1989
1990 req = urllib.request.Request('http://www.perlmeister.com/scripts')
1991 headers = []
1992 headers.append("Set-Cookie: s1=session;Path=/scripts")
1993 headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
1994 "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
1995 year_plus_one)
1996 headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
1997 "02-Feb-%d 23:24:20 GMT" % year_plus_one)
1998 headers.append("Set-Cookie: s2=session;Path=/scripts;"
1999 "Domain=.perlmeister.com")
2000 headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
2001 res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
2002
2003 c = CookieJar()
2004 c.extract_cookies(res, req)
2005 # How many session/permanent cookies do we have?
2006 counter = {"session_after": 0,
2007 "perm_after": 0,
2008 "session_before": 0,
2009 "perm_before": 0}
2010 for cookie in c:
2011 key = "%s_before" % cookie.value
2012 counter[key] = counter[key] + 1
2013 c.clear_session_cookies()
2014 # How many now?
2015 for cookie in c:
2016 key = "%s_after" % cookie.value
2017 counter[key] = counter[key] + 1
2018
2019 # a permanent cookie got lost accidentally
2020 self.assertEqual(counter["perm_after"], counter["perm_before"])
2021 # a session cookie hasn't been cleared
2022 self.assertEqual(counter["session_after"], 0)
2023 # we didn't have session cookies in the first place
2024 self.assertNotEqual(counter["session_before"], 0)
2025
2026
2027 if __name__ == "__main__":
2028 unittest.main()