cookies.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. ####
  2. # Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu>
  3. #
  4. # All Rights Reserved
  5. #
  6. # Permission to use, copy, modify, and distribute this software
  7. # and its documentation for any purpose and without fee is hereby
  8. # granted, provided that the above copyright notice appear in all
  9. # copies and that both that copyright notice and this permission
  10. # notice appear in supporting documentation, and that the name of
  11. # Timothy O'Malley not be used in advertising or publicity
  12. # pertaining to distribution of the software without specific, written
  13. # prior permission.
  14. #
  15. # Timothy O'Malley DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  17. # AND FITNESS, IN NO EVENT SHALL Timothy O'Malley BE LIABLE FOR
  18. # ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  21. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. # PERFORMANCE OF THIS SOFTWARE.
  23. #
  24. ####
  25. #
  26. # Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
  27. # by Timothy O'Malley <timo@alum.mit.edu>
  28. #
  29. # Cookie.py is a Python module for the handling of HTTP
  30. # cookies as a Python dictionary. See RFC 2109 for more
  31. # information on cookies.
  32. #
  33. # The original idea to treat Cookies as a dictionary came from
  34. # Dave Mitchell (davem@magnet.com) in 1995, when he released the
  35. # first version of nscookie.py.
  36. #
  37. ####
  38. r"""
  39. Here's a sample session to show how to use this module.
  40. At the moment, this is the only documentation.
  41. The Basics
  42. ----------
  43. Importing is easy...
  44. >>> from http import cookies
  45. Most of the time you start by creating a cookie.
  46. >>> C = cookies.SimpleCookie()
  47. Once you've created your Cookie, you can add values just as if it were
  48. a dictionary.
  49. >>> C = cookies.SimpleCookie()
  50. >>> C["fig"] = "newton"
  51. >>> C["sugar"] = "wafer"
  52. >>> C.output()
  53. 'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
  54. Notice that the printable representation of a Cookie is the
  55. appropriate format for a Set-Cookie: header. This is the
  56. default behavior. You can change the header and printed
  57. attributes by using the .output() function
  58. >>> C = cookies.SimpleCookie()
  59. >>> C["rocky"] = "road"
  60. >>> C["rocky"]["path"] = "/cookie"
  61. >>> print(C.output(header="Cookie:"))
  62. Cookie: rocky=road; Path=/cookie
  63. >>> print(C.output(attrs=[], header="Cookie:"))
  64. Cookie: rocky=road
  65. The load() method of a Cookie extracts cookies from a string. In a
  66. CGI script, you would use this method to extract the cookies from the
  67. HTTP_COOKIE environment variable.
  68. >>> C = cookies.SimpleCookie()
  69. >>> C.load("chips=ahoy; vienna=finger")
  70. >>> C.output()
  71. 'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
  72. The load() method is darn-tootin smart about identifying cookies
  73. within a string. Escaped quotation marks, nested semicolons, and other
  74. such trickeries do not confuse it.
  75. >>> C = cookies.SimpleCookie()
  76. >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
  77. >>> print(C)
  78. Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
  79. Each element of the Cookie also supports all of the RFC 2109
  80. Cookie attributes. Here's an example which sets the Path
  81. attribute.
  82. >>> C = cookies.SimpleCookie()
  83. >>> C["oreo"] = "doublestuff"
  84. >>> C["oreo"]["path"] = "/"
  85. >>> print(C)
  86. Set-Cookie: oreo=doublestuff; Path=/
  87. Each dictionary element has a 'value' attribute, which gives you
  88. back the value associated with the key.
  89. >>> C = cookies.SimpleCookie()
  90. >>> C["twix"] = "none for you"
  91. >>> C["twix"].value
  92. 'none for you'
  93. The SimpleCookie expects that all values should be standard strings.
  94. Just to be sure, SimpleCookie invokes the str() builtin to convert
  95. the value to a string, when the values are set dictionary-style.
  96. >>> C = cookies.SimpleCookie()
  97. >>> C["number"] = 7
  98. >>> C["string"] = "seven"
  99. >>> C["number"].value
  100. '7'
  101. >>> C["string"].value
  102. 'seven'
  103. >>> C.output()
  104. 'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
  105. Finis.
  106. """
  107. #
  108. # Import our required modules
  109. #
  110. import re
  111. import string
  112. import types
  113. __all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
  114. _nulljoin = ''.join
  115. _semispacejoin = '; '.join
  116. _spacejoin = ' '.join
  117. #
  118. # Define an exception visible to External modules
  119. #
  120. class CookieError(Exception):
  121. pass
  122. # These quoting routines conform to the RFC2109 specification, which in
  123. # turn references the character definitions from RFC2068. They provide
  124. # a two-way quoting algorithm. Any non-text character is translated
  125. # into a 4 character sequence: a forward-slash followed by the
  126. # three-digit octal equivalent of the character. Any '\' or '"' is
  127. # quoted with a preceding '\' slash.
  128. # Because of the way browsers really handle cookies (as opposed to what
  129. # the RFC says) we also encode "," and ";".
  130. #
  131. # These are taken from RFC2068 and RFC2109.
  132. # _LegalChars is the list of chars which don't require "'s
  133. # _Translator hash-table for fast quoting
  134. #
  135. _LegalChars = string.ascii_letters + string.digits + "!#$%&'*+-.^_`|~:"
  136. _UnescapedChars = _LegalChars + ' ()/<=>?@[]{}'
  137. _Translator = {n: '\\%03o' % n
  138. for n in set(range(256)) - set(map(ord, _UnescapedChars))}
  139. _Translator.update({
  140. ord('"'): '\\"',
  141. ord('\\'): '\\\\',
  142. })
  143. _is_legal_key = re.compile('[%s]+' % re.escape(_LegalChars)).fullmatch
  144. def _quote(str):
  145. r"""Quote a string for use in a cookie header.
  146. If the string does not need to be double-quoted, then just return the
  147. string. Otherwise, surround the string in doublequotes and quote
  148. (with a \) special characters.
  149. """
  150. if str is None or _is_legal_key(str):
  151. return str
  152. else:
  153. return '"' + str.translate(_Translator) + '"'
  154. _OctalPatt = re.compile(r"\\[0-3][0-7][0-7]")
  155. _QuotePatt = re.compile(r"[\\].")
  156. def _unquote(str):
  157. # If there aren't any doublequotes,
  158. # then there can't be any special characters. See RFC 2109.
  159. if str is None or len(str) < 2:
  160. return str
  161. if str[0] != '"' or str[-1] != '"':
  162. return str
  163. # We have to assume that we must decode this string.
  164. # Down to work.
  165. # Remove the "s
  166. str = str[1:-1]
  167. # Check for special sequences. Examples:
  168. # \012 --> \n
  169. # \" --> "
  170. #
  171. i = 0
  172. n = len(str)
  173. res = []
  174. while 0 <= i < n:
  175. o_match = _OctalPatt.search(str, i)
  176. q_match = _QuotePatt.search(str, i)
  177. if not o_match and not q_match: # Neither matched
  178. res.append(str[i:])
  179. break
  180. # else:
  181. j = k = -1
  182. if o_match:
  183. j = o_match.start(0)
  184. if q_match:
  185. k = q_match.start(0)
  186. if q_match and (not o_match or k < j): # QuotePatt matched
  187. res.append(str[i:k])
  188. res.append(str[k+1])
  189. i = k + 2
  190. else: # OctalPatt matched
  191. res.append(str[i:j])
  192. res.append(chr(int(str[j+1:j+4], 8)))
  193. i = j + 4
  194. return _nulljoin(res)
  195. # The _getdate() routine is used to set the expiration time in the cookie's HTTP
  196. # header. By default, _getdate() returns the current time in the appropriate
  197. # "expires" format for a Set-Cookie header. The one optional argument is an
  198. # offset from now, in seconds. For example, an offset of -3600 means "one hour
  199. # ago". The offset may be a floating point number.
  200. #
  201. _weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  202. _monthname = [None,
  203. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  204. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  205. def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
  206. from time import gmtime, time
  207. now = time()
  208. year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future)
  209. return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \
  210. (weekdayname[wd], day, monthname[month], year, hh, mm, ss)
  211. class Morsel(dict):
  212. """A class to hold ONE (key, value) pair.
  213. In a cookie, each such pair may have several attributes, so this class is
  214. used to keep the attributes associated with the appropriate key,value pair.
  215. This class also includes a coded_value attribute, which is used to hold
  216. the network representation of the value.
  217. """
  218. # RFC 2109 lists these attributes as reserved:
  219. # path comment domain
  220. # max-age secure version
  221. #
  222. # For historical reasons, these attributes are also reserved:
  223. # expires
  224. #
  225. # This is an extension from Microsoft:
  226. # httponly
  227. #
  228. # This dictionary provides a mapping from the lowercase
  229. # variant on the left to the appropriate traditional
  230. # formatting on the right.
  231. _reserved = {
  232. "expires" : "expires",
  233. "path" : "Path",
  234. "comment" : "Comment",
  235. "domain" : "Domain",
  236. "max-age" : "Max-Age",
  237. "secure" : "Secure",
  238. "httponly" : "HttpOnly",
  239. "version" : "Version",
  240. "samesite" : "SameSite",
  241. }
  242. _flags = {'secure', 'httponly'}
  243. def __init__(self):
  244. # Set defaults
  245. self._key = self._value = self._coded_value = None
  246. # Set default attributes
  247. for key in self._reserved:
  248. dict.__setitem__(self, key, "")
  249. @property
  250. def key(self):
  251. return self._key
  252. @property
  253. def value(self):
  254. return self._value
  255. @property
  256. def coded_value(self):
  257. return self._coded_value
  258. def __setitem__(self, K, V):
  259. K = K.lower()
  260. if not K in self._reserved:
  261. raise CookieError("Invalid attribute %r" % (K,))
  262. dict.__setitem__(self, K, V)
  263. def setdefault(self, key, val=None):
  264. key = key.lower()
  265. if key not in self._reserved:
  266. raise CookieError("Invalid attribute %r" % (key,))
  267. return dict.setdefault(self, key, val)
  268. def __eq__(self, morsel):
  269. if not isinstance(morsel, Morsel):
  270. return NotImplemented
  271. return (dict.__eq__(self, morsel) and
  272. self._value == morsel._value and
  273. self._key == morsel._key and
  274. self._coded_value == morsel._coded_value)
  275. __ne__ = object.__ne__
  276. def copy(self):
  277. morsel = Morsel()
  278. dict.update(morsel, self)
  279. morsel.__dict__.update(self.__dict__)
  280. return morsel
  281. def update(self, values):
  282. data = {}
  283. for key, val in dict(values).items():
  284. key = key.lower()
  285. if key not in self._reserved:
  286. raise CookieError("Invalid attribute %r" % (key,))
  287. data[key] = val
  288. dict.update(self, data)
  289. def isReservedKey(self, K):
  290. return K.lower() in self._reserved
  291. def set(self, key, val, coded_val):
  292. if key.lower() in self._reserved:
  293. raise CookieError('Attempt to set a reserved key %r' % (key,))
  294. if not _is_legal_key(key):
  295. raise CookieError('Illegal key %r' % (key,))
  296. # It's a good key, so save it.
  297. self._key = key
  298. self._value = val
  299. self._coded_value = coded_val
  300. def __getstate__(self):
  301. return {
  302. 'key': self._key,
  303. 'value': self._value,
  304. 'coded_value': self._coded_value,
  305. }
  306. def __setstate__(self, state):
  307. self._key = state['key']
  308. self._value = state['value']
  309. self._coded_value = state['coded_value']
  310. def output(self, attrs=None, header="Set-Cookie:"):
  311. return "%s %s" % (header, self.OutputString(attrs))
  312. __str__ = output
  313. def __repr__(self):
  314. return '<%s: %s>' % (self.__class__.__name__, self.OutputString())
  315. def js_output(self, attrs=None):
  316. # Print javascript
  317. return """
  318. <script type="text/javascript">
  319. <!-- begin hiding
  320. document.cookie = \"%s\";
  321. // end hiding -->
  322. </script>
  323. """ % (self.OutputString(attrs).replace('"', r'\"'))
  324. def OutputString(self, attrs=None):
  325. # Build up our result
  326. #
  327. result = []
  328. append = result.append
  329. # First, the key=value pair
  330. append("%s=%s" % (self.key, self.coded_value))
  331. # Now add any defined attributes
  332. if attrs is None:
  333. attrs = self._reserved
  334. items = sorted(self.items())
  335. for key, value in items:
  336. if value == "":
  337. continue
  338. if key not in attrs:
  339. continue
  340. if key == "expires" and isinstance(value, int):
  341. append("%s=%s" % (self._reserved[key], _getdate(value)))
  342. elif key == "max-age" and isinstance(value, int):
  343. append("%s=%d" % (self._reserved[key], value))
  344. elif key == "comment" and isinstance(value, str):
  345. append("%s=%s" % (self._reserved[key], _quote(value)))
  346. elif key in self._flags:
  347. if value:
  348. append(str(self._reserved[key]))
  349. else:
  350. append("%s=%s" % (self._reserved[key], value))
  351. # Return the result
  352. return _semispacejoin(result)
  353. __class_getitem__ = classmethod(types.GenericAlias)
  354. #
  355. # Pattern for finding cookie
  356. #
  357. # This used to be strict parsing based on the RFC2109 and RFC2068
  358. # specifications. I have since discovered that MSIE 3.0x doesn't
  359. # follow the character rules outlined in those specs. As a
  360. # result, the parsing rules here are less strict.
  361. #
  362. _LegalKeyChars = r"\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\="
  363. _LegalValueChars = _LegalKeyChars + r'\[\]'
  364. _CookiePattern = re.compile(r"""
  365. \s* # Optional whitespace at start of cookie
  366. (?P<key> # Start of group 'key'
  367. [""" + _LegalKeyChars + r"""]+? # Any word of at least one letter
  368. ) # End of group 'key'
  369. ( # Optional group: there may not be a value.
  370. \s*=\s* # Equal Sign
  371. (?P<val> # Start of group 'val'
  372. "(?:[^\\"]|\\.)*" # Any doublequoted string
  373. | # or
  374. \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr
  375. | # or
  376. [""" + _LegalValueChars + r"""]* # Any word or empty string
  377. ) # End of group 'val'
  378. )? # End of optional value group
  379. \s* # Any number of spaces.
  380. (\s+|;|$) # Ending either at space, semicolon, or EOS.
  381. """, re.ASCII | re.VERBOSE) # re.ASCII may be removed if safe.
  382. # At long last, here is the cookie class. Using this class is almost just like
  383. # using a dictionary. See this module's docstring for example usage.
  384. #
  385. class BaseCookie(dict):
  386. """A container class for a set of Morsels."""
  387. def value_decode(self, val):
  388. """real_value, coded_value = value_decode(STRING)
  389. Called prior to setting a cookie's value from the network
  390. representation. The VALUE is the value read from HTTP
  391. header.
  392. Override this function to modify the behavior of cookies.
  393. """
  394. return val, val
  395. def value_encode(self, val):
  396. """real_value, coded_value = value_encode(VALUE)
  397. Called prior to setting a cookie's value from the dictionary
  398. representation. The VALUE is the value being assigned.
  399. Override this function to modify the behavior of cookies.
  400. """
  401. strval = str(val)
  402. return strval, strval
  403. def __init__(self, input=None):
  404. if input:
  405. self.load(input)
  406. def __set(self, key, real_value, coded_value):
  407. """Private method for setting a cookie's value"""
  408. M = self.get(key, Morsel())
  409. M.set(key, real_value, coded_value)
  410. dict.__setitem__(self, key, M)
  411. def __setitem__(self, key, value):
  412. """Dictionary style assignment."""
  413. if isinstance(value, Morsel):
  414. # allow assignment of constructed Morsels (e.g. for pickling)
  415. dict.__setitem__(self, key, value)
  416. else:
  417. rval, cval = self.value_encode(value)
  418. self.__set(key, rval, cval)
  419. def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
  420. """Return a string suitable for HTTP."""
  421. result = []
  422. items = sorted(self.items())
  423. for key, value in items:
  424. result.append(value.output(attrs, header))
  425. return sep.join(result)
  426. __str__ = output
  427. def __repr__(self):
  428. l = []
  429. items = sorted(self.items())
  430. for key, value in items:
  431. l.append('%s=%s' % (key, repr(value.value)))
  432. return '<%s: %s>' % (self.__class__.__name__, _spacejoin(l))
  433. def js_output(self, attrs=None):
  434. """Return a string suitable for JavaScript."""
  435. result = []
  436. items = sorted(self.items())
  437. for key, value in items:
  438. result.append(value.js_output(attrs))
  439. return _nulljoin(result)
  440. def load(self, rawdata):
  441. """Load cookies from a string (presumably HTTP_COOKIE) or
  442. from a dictionary. Loading cookies from a dictionary 'd'
  443. is equivalent to calling:
  444. map(Cookie.__setitem__, d.keys(), d.values())
  445. """
  446. if isinstance(rawdata, str):
  447. self.__parse_string(rawdata)
  448. else:
  449. # self.update() wouldn't call our custom __setitem__
  450. for key, value in rawdata.items():
  451. self[key] = value
  452. return
  453. def __parse_string(self, str, patt=_CookiePattern):
  454. i = 0 # Our starting point
  455. n = len(str) # Length of string
  456. parsed_items = [] # Parsed (type, key, value) triples
  457. morsel_seen = False # A key=value pair was previously encountered
  458. TYPE_ATTRIBUTE = 1
  459. TYPE_KEYVALUE = 2
  460. # We first parse the whole cookie string and reject it if it's
  461. # syntactically invalid (this helps avoid some classes of injection
  462. # attacks).
  463. while 0 <= i < n:
  464. # Start looking for a cookie
  465. match = patt.match(str, i)
  466. if not match:
  467. # No more cookies
  468. break
  469. key, value = match.group("key"), match.group("val")
  470. i = match.end(0)
  471. if key[0] == "$":
  472. if not morsel_seen:
  473. # We ignore attributes which pertain to the cookie
  474. # mechanism as a whole, such as "$Version".
  475. # See RFC 2965. (Does anyone care?)
  476. continue
  477. parsed_items.append((TYPE_ATTRIBUTE, key[1:], value))
  478. elif key.lower() in Morsel._reserved:
  479. if not morsel_seen:
  480. # Invalid cookie string
  481. return
  482. if value is None:
  483. if key.lower() in Morsel._flags:
  484. parsed_items.append((TYPE_ATTRIBUTE, key, True))
  485. else:
  486. # Invalid cookie string
  487. return
  488. else:
  489. parsed_items.append((TYPE_ATTRIBUTE, key, _unquote(value)))
  490. elif value is not None:
  491. parsed_items.append((TYPE_KEYVALUE, key, self.value_decode(value)))
  492. morsel_seen = True
  493. else:
  494. # Invalid cookie string
  495. return
  496. # The cookie string is valid, apply it.
  497. M = None # current morsel
  498. for tp, key, value in parsed_items:
  499. if tp == TYPE_ATTRIBUTE:
  500. assert M is not None
  501. M[key] = value
  502. else:
  503. assert tp == TYPE_KEYVALUE
  504. rval, cval = value
  505. self.__set(key, rval, cval)
  506. M = self[key]
  507. class SimpleCookie(BaseCookie):
  508. """
  509. SimpleCookie supports strings as cookie values. When setting
  510. the value using the dictionary assignment notation, SimpleCookie
  511. calls the builtin str() to convert the value to a string. Values
  512. received from HTTP are kept as strings.
  513. """
  514. def value_decode(self, val):
  515. return _unquote(val), val
  516. def value_encode(self, val):
  517. strval = str(val)
  518. return strval, _quote(strval)