You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import base64
  2. import logging
  3. import string
  4. from datetime import datetime, timedelta
  5. from django.conf import settings
  6. from django.contrib.sessions.exceptions import SuspiciousSession
  7. from django.core.exceptions import SuspiciousOperation
  8. from django.utils import timezone
  9. from django.utils.crypto import (
  10. constant_time_compare, get_random_string, salted_hmac,
  11. )
  12. from django.utils.module_loading import import_string
  13. # session_key should not be case sensitive because some backends can store it
  14. # on case insensitive file systems.
  15. VALID_KEY_CHARS = string.ascii_lowercase + string.digits
  16. class CreateError(Exception):
  17. """
  18. Used internally as a consistent exception type to catch from save (see the
  19. docstring for SessionBase.save() for details).
  20. """
  21. pass
  22. class UpdateError(Exception):
  23. """
  24. Occurs if Django tries to update a session that was deleted.
  25. """
  26. pass
  27. class SessionBase:
  28. """
  29. Base class for all Session classes.
  30. """
  31. TEST_COOKIE_NAME = 'testcookie'
  32. TEST_COOKIE_VALUE = 'worked'
  33. __not_given = object()
  34. def __init__(self, session_key=None):
  35. self._session_key = session_key
  36. self.accessed = False
  37. self.modified = False
  38. self.serializer = import_string(settings.SESSION_SERIALIZER)
  39. def __contains__(self, key):
  40. return key in self._session
  41. def __getitem__(self, key):
  42. return self._session[key]
  43. def __setitem__(self, key, value):
  44. self._session[key] = value
  45. self.modified = True
  46. def __delitem__(self, key):
  47. del self._session[key]
  48. self.modified = True
  49. def get(self, key, default=None):
  50. return self._session.get(key, default)
  51. def pop(self, key, default=__not_given):
  52. self.modified = self.modified or key in self._session
  53. args = () if default is self.__not_given else (default,)
  54. return self._session.pop(key, *args)
  55. def setdefault(self, key, value):
  56. if key in self._session:
  57. return self._session[key]
  58. else:
  59. self.modified = True
  60. self._session[key] = value
  61. return value
  62. def set_test_cookie(self):
  63. self[self.TEST_COOKIE_NAME] = self.TEST_COOKIE_VALUE
  64. def test_cookie_worked(self):
  65. return self.get(self.TEST_COOKIE_NAME) == self.TEST_COOKIE_VALUE
  66. def delete_test_cookie(self):
  67. del self[self.TEST_COOKIE_NAME]
  68. def _hash(self, value):
  69. key_salt = "django.contrib.sessions" + self.__class__.__name__
  70. return salted_hmac(key_salt, value).hexdigest()
  71. def encode(self, session_dict):
  72. "Return the given session dictionary serialized and encoded as a string."
  73. serialized = self.serializer().dumps(session_dict)
  74. hash = self._hash(serialized)
  75. return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')
  76. def decode(self, session_data):
  77. encoded_data = base64.b64decode(session_data.encode('ascii'))
  78. try:
  79. # could produce ValueError if there is no ':'
  80. hash, serialized = encoded_data.split(b':', 1)
  81. expected_hash = self._hash(serialized)
  82. if not constant_time_compare(hash.decode(), expected_hash):
  83. raise SuspiciousSession("Session data corrupted")
  84. else:
  85. return self.serializer().loads(serialized)
  86. except Exception as e:
  87. # ValueError, SuspiciousOperation, unpickling exceptions. If any of
  88. # these happen, just return an empty dictionary (an empty session).
  89. if isinstance(e, SuspiciousOperation):
  90. logger = logging.getLogger('django.security.%s' % e.__class__.__name__)
  91. logger.warning(str(e))
  92. return {}
  93. def update(self, dict_):
  94. self._session.update(dict_)
  95. self.modified = True
  96. def has_key(self, key):
  97. return key in self._session
  98. def keys(self):
  99. return self._session.keys()
  100. def values(self):
  101. return self._session.values()
  102. def items(self):
  103. return self._session.items()
  104. def clear(self):
  105. # To avoid unnecessary persistent storage accesses, we set up the
  106. # internals directly (loading data wastes time, since we are going to
  107. # set it to an empty dict anyway).
  108. self._session_cache = {}
  109. self.accessed = True
  110. self.modified = True
  111. def is_empty(self):
  112. "Return True when there is no session_key and the session is empty."
  113. try:
  114. return not self._session_key and not self._session_cache
  115. except AttributeError:
  116. return True
  117. def _get_new_session_key(self):
  118. "Return session key that isn't being used."
  119. while True:
  120. session_key = get_random_string(32, VALID_KEY_CHARS)
  121. if not self.exists(session_key):
  122. return session_key
  123. def _get_or_create_session_key(self):
  124. if self._session_key is None:
  125. self._session_key = self._get_new_session_key()
  126. return self._session_key
  127. def _validate_session_key(self, key):
  128. """
  129. Key must be truthy and at least 8 characters long. 8 characters is an
  130. arbitrary lower bound for some minimal key security.
  131. """
  132. return key and len(key) >= 8
  133. def _get_session_key(self):
  134. return self.__session_key
  135. def _set_session_key(self, value):
  136. """
  137. Validate session key on assignment. Invalid values will set to None.
  138. """
  139. if self._validate_session_key(value):
  140. self.__session_key = value
  141. else:
  142. self.__session_key = None
  143. session_key = property(_get_session_key)
  144. _session_key = property(_get_session_key, _set_session_key)
  145. def _get_session(self, no_load=False):
  146. """
  147. Lazily load session from storage (unless "no_load" is True, when only
  148. an empty dict is stored) and store it in the current instance.
  149. """
  150. self.accessed = True
  151. try:
  152. return self._session_cache
  153. except AttributeError:
  154. if self.session_key is None or no_load:
  155. self._session_cache = {}
  156. else:
  157. self._session_cache = self.load()
  158. return self._session_cache
  159. _session = property(_get_session)
  160. def get_expiry_age(self, **kwargs):
  161. """Get the number of seconds until the session expires.
  162. Optionally, this function accepts `modification` and `expiry` keyword
  163. arguments specifying the modification and expiry of the session.
  164. """
  165. try:
  166. modification = kwargs['modification']
  167. except KeyError:
  168. modification = timezone.now()
  169. # Make the difference between "expiry=None passed in kwargs" and
  170. # "expiry not passed in kwargs", in order to guarantee not to trigger
  171. # self.load() when expiry is provided.
  172. try:
  173. expiry = kwargs['expiry']
  174. except KeyError:
  175. expiry = self.get('_session_expiry')
  176. if not expiry: # Checks both None and 0 cases
  177. return settings.SESSION_COOKIE_AGE
  178. if not isinstance(expiry, datetime):
  179. return expiry
  180. delta = expiry - modification
  181. return delta.days * 86400 + delta.seconds
  182. def get_expiry_date(self, **kwargs):
  183. """Get session the expiry date (as a datetime object).
  184. Optionally, this function accepts `modification` and `expiry` keyword
  185. arguments specifying the modification and expiry of the session.
  186. """
  187. try:
  188. modification = kwargs['modification']
  189. except KeyError:
  190. modification = timezone.now()
  191. # Same comment as in get_expiry_age
  192. try:
  193. expiry = kwargs['expiry']
  194. except KeyError:
  195. expiry = self.get('_session_expiry')
  196. if isinstance(expiry, datetime):
  197. return expiry
  198. expiry = expiry or settings.SESSION_COOKIE_AGE # Checks both None and 0 cases
  199. return modification + timedelta(seconds=expiry)
  200. def set_expiry(self, value):
  201. """
  202. Set a custom expiration for the session. ``value`` can be an integer,
  203. a Python ``datetime`` or ``timedelta`` object or ``None``.
  204. If ``value`` is an integer, the session will expire after that many
  205. seconds of inactivity. If set to ``0`` then the session will expire on
  206. browser close.
  207. If ``value`` is a ``datetime`` or ``timedelta`` object, the session
  208. will expire at that specific future time.
  209. If ``value`` is ``None``, the session uses the global session expiry
  210. policy.
  211. """
  212. if value is None:
  213. # Remove any custom expiration for this session.
  214. try:
  215. del self['_session_expiry']
  216. except KeyError:
  217. pass
  218. return
  219. if isinstance(value, timedelta):
  220. value = timezone.now() + value
  221. self['_session_expiry'] = value
  222. def get_expire_at_browser_close(self):
  223. """
  224. Return ``True`` if the session is set to expire when the browser
  225. closes, and ``False`` if there's an expiry date. Use
  226. ``get_expiry_date()`` or ``get_expiry_age()`` to find the actual expiry
  227. date/age, if there is one.
  228. """
  229. if self.get('_session_expiry') is None:
  230. return settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
  231. return self.get('_session_expiry') == 0
  232. def flush(self):
  233. """
  234. Remove the current session data from the database and regenerate the
  235. key.
  236. """
  237. self.clear()
  238. self.delete()
  239. self._session_key = None
  240. def cycle_key(self):
  241. """
  242. Create a new session key, while retaining the current session data.
  243. """
  244. data = self._session
  245. key = self.session_key
  246. self.create()
  247. self._session_cache = data
  248. if key:
  249. self.delete(key)
  250. # Methods that child classes must implement.
  251. def exists(self, session_key):
  252. """
  253. Return True if the given session_key already exists.
  254. """
  255. raise NotImplementedError('subclasses of SessionBase must provide an exists() method')
  256. def create(self):
  257. """
  258. Create a new session instance. Guaranteed to create a new object with
  259. a unique key and will have saved the result once (with empty data)
  260. before the method returns.
  261. """
  262. raise NotImplementedError('subclasses of SessionBase must provide a create() method')
  263. def save(self, must_create=False):
  264. """
  265. Save the session data. If 'must_create' is True, create a new session
  266. object (or raise CreateError). Otherwise, only update an existing
  267. object and don't create one (raise UpdateError if needed).
  268. """
  269. raise NotImplementedError('subclasses of SessionBase must provide a save() method')
  270. def delete(self, session_key=None):
  271. """
  272. Delete the session data under this key. If the key is None, use the
  273. current session key value.
  274. """
  275. raise NotImplementedError('subclasses of SessionBase must provide a delete() method')
  276. def load(self):
  277. """
  278. Load the session data and return a dictionary.
  279. """
  280. raise NotImplementedError('subclasses of SessionBase must provide a load() method')
  281. @classmethod
  282. def clear_expired(cls):
  283. """
  284. Remove expired sessions from the session store.
  285. If this operation isn't possible on a given backend, it should raise
  286. NotImplementedError. If it isn't necessary, because the backend has
  287. a built-in expiration mechanism, it should be a no-op.
  288. """
  289. raise NotImplementedError('This backend does not support clear_expired().')