test_http_cookiejar.py 82 KB

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