sessions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. from __future__ import annotations
  2. import hashlib
  3. import typing as t
  4. from collections.abc import MutableMapping
  5. from datetime import datetime
  6. from datetime import timezone
  7. from itsdangerous import BadSignature
  8. from itsdangerous import URLSafeTimedSerializer
  9. from werkzeug.datastructures import CallbackDict
  10. from .json.tag import TaggedJSONSerializer
  11. if t.TYPE_CHECKING: # pragma: no cover
  12. from .app import Flask
  13. from .wrappers import Request, Response
  14. class SessionMixin(MutableMapping):
  15. """Expands a basic dictionary with session attributes."""
  16. @property
  17. def permanent(self) -> bool:
  18. """This reflects the ``'_permanent'`` key in the dict."""
  19. return self.get("_permanent", False)
  20. @permanent.setter
  21. def permanent(self, value: bool) -> None:
  22. self["_permanent"] = bool(value)
  23. #: Some implementations can detect whether a session is newly
  24. #: created, but that is not guaranteed. Use with caution. The mixin
  25. # default is hard-coded ``False``.
  26. new = False
  27. #: Some implementations can detect changes to the session and set
  28. #: this when that happens. The mixin default is hard coded to
  29. #: ``True``.
  30. modified = True
  31. #: Some implementations can detect when session data is read or
  32. #: written and set this when that happens. The mixin default is hard
  33. #: coded to ``True``.
  34. accessed = True
  35. class SecureCookieSession(CallbackDict, SessionMixin):
  36. """Base class for sessions based on signed cookies.
  37. This session backend will set the :attr:`modified` and
  38. :attr:`accessed` attributes. It cannot reliably track whether a
  39. session is new (vs. empty), so :attr:`new` remains hard coded to
  40. ``False``.
  41. """
  42. #: When data is changed, this is set to ``True``. Only the session
  43. #: dictionary itself is tracked; if the session contains mutable
  44. #: data (for example a nested dict) then this must be set to
  45. #: ``True`` manually when modifying that data. The session cookie
  46. #: will only be written to the response if this is ``True``.
  47. modified = False
  48. #: When data is read or written, this is set to ``True``. Used by
  49. # :class:`.SecureCookieSessionInterface` to add a ``Vary: Cookie``
  50. #: header, which allows caching proxies to cache different pages for
  51. #: different users.
  52. accessed = False
  53. def __init__(self, initial: t.Any = None) -> None:
  54. def on_update(self) -> None:
  55. self.modified = True
  56. self.accessed = True
  57. super().__init__(initial, on_update)
  58. def __getitem__(self, key: str) -> t.Any:
  59. self.accessed = True
  60. return super().__getitem__(key)
  61. def get(self, key: str, default: t.Any = None) -> t.Any:
  62. self.accessed = True
  63. return super().get(key, default)
  64. def setdefault(self, key: str, default: t.Any = None) -> t.Any:
  65. self.accessed = True
  66. return super().setdefault(key, default)
  67. class NullSession(SecureCookieSession):
  68. """Class used to generate nicer error messages if sessions are not
  69. available. Will still allow read-only access to the empty session
  70. but fail on setting.
  71. """
  72. def _fail(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn:
  73. raise RuntimeError(
  74. "The session is unavailable because no secret "
  75. "key was set. Set the secret_key on the "
  76. "application to something unique and secret."
  77. )
  78. __setitem__ = __delitem__ = clear = pop = popitem = update = setdefault = _fail # type: ignore # noqa: B950
  79. del _fail
  80. class SessionInterface:
  81. """The basic interface you have to implement in order to replace the
  82. default session interface which uses werkzeug's securecookie
  83. implementation. The only methods you have to implement are
  84. :meth:`open_session` and :meth:`save_session`, the others have
  85. useful defaults which you don't need to change.
  86. The session object returned by the :meth:`open_session` method has to
  87. provide a dictionary like interface plus the properties and methods
  88. from the :class:`SessionMixin`. We recommend just subclassing a dict
  89. and adding that mixin::
  90. class Session(dict, SessionMixin):
  91. pass
  92. If :meth:`open_session` returns ``None`` Flask will call into
  93. :meth:`make_null_session` to create a session that acts as replacement
  94. if the session support cannot work because some requirement is not
  95. fulfilled. The default :class:`NullSession` class that is created
  96. will complain that the secret key was not set.
  97. To replace the session interface on an application all you have to do
  98. is to assign :attr:`flask.Flask.session_interface`::
  99. app = Flask(__name__)
  100. app.session_interface = MySessionInterface()
  101. Multiple requests with the same session may be sent and handled
  102. concurrently. When implementing a new session interface, consider
  103. whether reads or writes to the backing store must be synchronized.
  104. There is no guarantee on the order in which the session for each
  105. request is opened or saved, it will occur in the order that requests
  106. begin and end processing.
  107. .. versionadded:: 0.8
  108. """
  109. #: :meth:`make_null_session` will look here for the class that should
  110. #: be created when a null session is requested. Likewise the
  111. #: :meth:`is_null_session` method will perform a typecheck against
  112. #: this type.
  113. null_session_class = NullSession
  114. #: A flag that indicates if the session interface is pickle based.
  115. #: This can be used by Flask extensions to make a decision in regards
  116. #: to how to deal with the session object.
  117. #:
  118. #: .. versionadded:: 0.10
  119. pickle_based = False
  120. def make_null_session(self, app: Flask) -> NullSession:
  121. """Creates a null session which acts as a replacement object if the
  122. real session support could not be loaded due to a configuration
  123. error. This mainly aids the user experience because the job of the
  124. null session is to still support lookup without complaining but
  125. modifications are answered with a helpful error message of what
  126. failed.
  127. This creates an instance of :attr:`null_session_class` by default.
  128. """
  129. return self.null_session_class()
  130. def is_null_session(self, obj: object) -> bool:
  131. """Checks if a given object is a null session. Null sessions are
  132. not asked to be saved.
  133. This checks if the object is an instance of :attr:`null_session_class`
  134. by default.
  135. """
  136. return isinstance(obj, self.null_session_class)
  137. def get_cookie_name(self, app: Flask) -> str:
  138. """The name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``."""
  139. return app.config["SESSION_COOKIE_NAME"]
  140. def get_cookie_domain(self, app: Flask) -> str | None:
  141. """The value of the ``Domain`` parameter on the session cookie. If not set,
  142. browsers will only send the cookie to the exact domain it was set from.
  143. Otherwise, they will send it to any subdomain of the given value as well.
  144. Uses the :data:`SESSION_COOKIE_DOMAIN` config.
  145. .. versionchanged:: 2.3
  146. Not set by default, does not fall back to ``SERVER_NAME``.
  147. """
  148. rv = app.config["SESSION_COOKIE_DOMAIN"]
  149. return rv if rv else None
  150. def get_cookie_path(self, app: Flask) -> str:
  151. """Returns the path for which the cookie should be valid. The
  152. default implementation uses the value from the ``SESSION_COOKIE_PATH``
  153. config var if it's set, and falls back to ``APPLICATION_ROOT`` or
  154. uses ``/`` if it's ``None``.
  155. """
  156. return app.config["SESSION_COOKIE_PATH"] or app.config["APPLICATION_ROOT"]
  157. def get_cookie_httponly(self, app: Flask) -> bool:
  158. """Returns True if the session cookie should be httponly. This
  159. currently just returns the value of the ``SESSION_COOKIE_HTTPONLY``
  160. config var.
  161. """
  162. return app.config["SESSION_COOKIE_HTTPONLY"]
  163. def get_cookie_secure(self, app: Flask) -> bool:
  164. """Returns True if the cookie should be secure. This currently
  165. just returns the value of the ``SESSION_COOKIE_SECURE`` setting.
  166. """
  167. return app.config["SESSION_COOKIE_SECURE"]
  168. def get_cookie_samesite(self, app: Flask) -> str:
  169. """Return ``'Strict'`` or ``'Lax'`` if the cookie should use the
  170. ``SameSite`` attribute. This currently just returns the value of
  171. the :data:`SESSION_COOKIE_SAMESITE` setting.
  172. """
  173. return app.config["SESSION_COOKIE_SAMESITE"]
  174. def get_expiration_time(self, app: Flask, session: SessionMixin) -> datetime | None:
  175. """A helper method that returns an expiration date for the session
  176. or ``None`` if the session is linked to the browser session. The
  177. default implementation returns now + the permanent session
  178. lifetime configured on the application.
  179. """
  180. if session.permanent:
  181. return datetime.now(timezone.utc) + app.permanent_session_lifetime
  182. return None
  183. def should_set_cookie(self, app: Flask, session: SessionMixin) -> bool:
  184. """Used by session backends to determine if a ``Set-Cookie`` header
  185. should be set for this session cookie for this response. If the session
  186. has been modified, the cookie is set. If the session is permanent and
  187. the ``SESSION_REFRESH_EACH_REQUEST`` config is true, the cookie is
  188. always set.
  189. This check is usually skipped if the session was deleted.
  190. .. versionadded:: 0.11
  191. """
  192. return session.modified or (
  193. session.permanent and app.config["SESSION_REFRESH_EACH_REQUEST"]
  194. )
  195. def open_session(self, app: Flask, request: Request) -> SessionMixin | None:
  196. """This is called at the beginning of each request, after
  197. pushing the request context, before matching the URL.
  198. This must return an object which implements a dictionary-like
  199. interface as well as the :class:`SessionMixin` interface.
  200. This will return ``None`` to indicate that loading failed in
  201. some way that is not immediately an error. The request
  202. context will fall back to using :meth:`make_null_session`
  203. in this case.
  204. """
  205. raise NotImplementedError()
  206. def save_session(
  207. self, app: Flask, session: SessionMixin, response: Response
  208. ) -> None:
  209. """This is called at the end of each request, after generating
  210. a response, before removing the request context. It is skipped
  211. if :meth:`is_null_session` returns ``True``.
  212. """
  213. raise NotImplementedError()
  214. session_json_serializer = TaggedJSONSerializer()
  215. class SecureCookieSessionInterface(SessionInterface):
  216. """The default session interface that stores sessions in signed cookies
  217. through the :mod:`itsdangerous` module.
  218. """
  219. #: the salt that should be applied on top of the secret key for the
  220. #: signing of cookie based sessions.
  221. salt = "cookie-session"
  222. #: the hash function to use for the signature. The default is sha1
  223. digest_method = staticmethod(hashlib.sha1)
  224. #: the name of the itsdangerous supported key derivation. The default
  225. #: is hmac.
  226. key_derivation = "hmac"
  227. #: A python serializer for the payload. The default is a compact
  228. #: JSON derived serializer with support for some extra Python types
  229. #: such as datetime objects or tuples.
  230. serializer = session_json_serializer
  231. session_class = SecureCookieSession
  232. def get_signing_serializer(self, app: Flask) -> URLSafeTimedSerializer | None:
  233. if not app.secret_key:
  234. return None
  235. signer_kwargs = dict(
  236. key_derivation=self.key_derivation, digest_method=self.digest_method
  237. )
  238. return URLSafeTimedSerializer(
  239. app.secret_key,
  240. salt=self.salt,
  241. serializer=self.serializer,
  242. signer_kwargs=signer_kwargs,
  243. )
  244. def open_session(self, app: Flask, request: Request) -> SecureCookieSession | None:
  245. s = self.get_signing_serializer(app)
  246. if s is None:
  247. return None
  248. val = request.cookies.get(self.get_cookie_name(app))
  249. if not val:
  250. return self.session_class()
  251. max_age = int(app.permanent_session_lifetime.total_seconds())
  252. try:
  253. data = s.loads(val, max_age=max_age)
  254. return self.session_class(data)
  255. except BadSignature:
  256. return self.session_class()
  257. def save_session(
  258. self, app: Flask, session: SessionMixin, response: Response
  259. ) -> None:
  260. name = self.get_cookie_name(app)
  261. domain = self.get_cookie_domain(app)
  262. path = self.get_cookie_path(app)
  263. secure = self.get_cookie_secure(app)
  264. samesite = self.get_cookie_samesite(app)
  265. httponly = self.get_cookie_httponly(app)
  266. # Add a "Vary: Cookie" header if the session was accessed at all.
  267. if session.accessed:
  268. response.vary.add("Cookie")
  269. # If the session is modified to be empty, remove the cookie.
  270. # If the session is empty, return without setting the cookie.
  271. if not session:
  272. if session.modified:
  273. response.delete_cookie(
  274. name,
  275. domain=domain,
  276. path=path,
  277. secure=secure,
  278. samesite=samesite,
  279. httponly=httponly,
  280. )
  281. response.vary.add("Cookie")
  282. return
  283. if not self.should_set_cookie(app, session):
  284. return
  285. expires = self.get_expiration_time(app, session)
  286. val = self.get_signing_serializer(app).dumps(dict(session)) # type: ignore
  287. response.set_cookie(
  288. name,
  289. val, # type: ignore
  290. expires=expires,
  291. httponly=httponly,
  292. domain=domain,
  293. path=path,
  294. secure=secure,
  295. samesite=samesite,
  296. )
  297. response.vary.add("Cookie")