summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2018-09-16 11:06:05 -0500
committerMichael Merickel <michael@merickel.org>2018-09-16 11:24:41 -0500
commitc3188340e841633924e8ab7a055c1df0dffed9c1 (patch)
tree34180298f1f3c7ce0a6343e3e9fc1e374df6e967
parent8cc4bf732f636abc06a2fbb72b2bd9ab0a680eb2 (diff)
downloadpyramid-c3188340e841633924e8ab7a055c1df0dffed9c1.tar.gz
pyramid-c3188340e841633924e8ab7a055c1df0dffed9c1.tar.bz2
pyramid-c3188340e841633924e8ab7a055c1df0dffed9c1.zip
deprecate pickleable sessions, recommend json
-rw-r--r--CHANGES.rst11
-rw-r--r--docs/api/session.rst2
-rw-r--r--docs/narr/sessions.rst72
-rw-r--r--pyramid/interfaces.py8
-rw-r--r--pyramid/session.py30
5 files changed, 101 insertions, 22 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index d0dbbe5c0..92e1e4313 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -57,6 +57,11 @@ Features
- Add support for Python 3.7. Add testing on Python 3.8 with allowed failures.
See https://github.com/Pylons/pyramid/pull/3333
+- Added ``pyramid.session.JSONSerializer``. See "Upcoming Changes to ISession
+ in Pyramid 2.0" in the "Sessions" chapter of the documentation for more
+ information about this feature.
+ See https://github.com/Pylons/pyramid/pull/3353
+
Bug Fixes
---------
@@ -79,6 +84,12 @@ Bug Fixes
Deprecations
------------
+- The ``pyramid.intefaces.ISession`` interface will move to require
+ json-serializable objects in Pyramid 2.0. See
+ "Upcoming Changes to ISession in Pyramid 2.0" in the "Sessions" chapter
+ of the documentation for more information about this change.
+ See https://github.com/Pylons/pyramid/pull/3353
+
Backward Incompatibilities
--------------------------
diff --git a/docs/api/session.rst b/docs/api/session.rst
index 53bae7c52..e0d2db726 100644
--- a/docs/api/session.rst
+++ b/docs/api/session.rst
@@ -17,3 +17,5 @@
.. autoclass:: PickleSerializer
+ .. autoclass:: JSONSerializer
+
diff --git a/docs/narr/sessions.rst b/docs/narr/sessions.rst
index 2d80b1a63..17e8291a0 100644
--- a/docs/narr/sessions.rst
+++ b/docs/narr/sessions.rst
@@ -59,25 +59,59 @@ using the :meth:`pyramid.config.Configurator.set_session_factory` method.
By default the :func:`~pyramid.session.SignedCookieSessionFactory`
implementation contains the following security concerns:
- - Session data is *unencrypted*. You should not use it when you keep
- sensitive information in the session object, as the information can be
- easily read by both users of your application and third parties who have
- access to your users' network traffic.
-
- - If you use this sessioning implementation, and you inadvertently create a
- cross-site scripting vulnerability in your application, because the
- session data is stored unencrypted in a cookie, it will also be easier for
- evildoers to obtain the current user's cross-site scripting token.
-
- - The default serialization method, while replaceable with something like
- JSON, is implemented using pickle which can lead to remote code execution
- if your secret key is compromised.
-
- In short, use a different session factory implementation (preferably one
- which keeps session data on the server) for anything but the most basic of
- applications where "session security doesn't matter", you are sure your
- application has no cross-site scripting vulnerabilities, and you are confident
- your secret key will not be exposed.
+ - Session data is *unencrypted* (but it is signed / authenticated).
+
+ This means an attacker cannot change the session data, but they can view it.
+ You should not use it when you keep sensitive information in the session object, as the information can be easily read by both users of your application and third parties who have access to your users' network traffic.
+
+ At the very least, use TLS and set ``secure=True`` to avoid arbitrary users on the network from viewing the session contents.
+
+ - If you use this sessioning implementation, and you inadvertently create a cross-site scripting vulnerability in your application, because the session data is stored unencrypted in a cookie, it will also be easier for evildoers to obtain the current user's cross-site scripting token.
+
+ Set ``httponly=True`` to mitigate this vulnerability by hiding the cookie from client-side JavaScript.
+
+ - The default serialization method, while replaceable with something like JSON, is implemented using pickle which can lead to remote code execution if your secret key is compromised.
+
+ To mitigate this, set ``serializer=pyramid.session.JSONSerializer()`` to use :class:`pyramid.session.JSONSerializer`. This option will be the default in :app:`Pyramid` 2.0.
+ See :ref:`pickle_session_deprecation` for more information about this change.
+
+ In short, use a different session factory implementation (preferably one which keeps session data on the server) for anything but the most basic of applications where "session security doesn't matter", you are sure your application has no cross-site scripting vulnerabilities, and you are confident your secret key will not be exposed.
+
+.. _pickle_session_deprecation:
+
+Upcoming Changes to ISession in Pyramid 2.0
+-------------------------------------------
+
+In :app:`Pyramid` 2.0 the :class:`pyramid.interfaces.ISession` interface will be changing to require that session implementations only need to support json-serializable data types.
+This is a stricter contract than the current requirement that all objects be pickleable and it is being done for security purposes.
+This is a backward-incompatible change.
+Currently, if a client-side session implementation is compromised, it leaves the application vulnerable to remote code execution attacks using specially-crafted sessions that execute code when deserialized.
+
+For users with compatibility concerns, it's possible to craft a serializer that can handle both formats until you are satisfied that clients have had time to reasonably upgrade.
+Remember that sessions should be short-lived and thus the number of clients affected should be small (no longer than an auth token, at a maximum). An example serializer:
+
+.. code-block:: python
+ :linenos:
+
+ from pyramid.session import JSONSerializer
+ from pyramid.session import PickleSerializer
+
+ class JSONSerializerWithPickleFallback(object):
+ def __init__(self):
+ self.json = JSONSerializer()
+ self.pickle = PickleSerializer()
+
+ def dumps(self, value):
+ # maybe catch serialization errors here and keep using pickle
+ # while finding spots in your app that are not storing
+ # json-serializable objects, falling back to pickle
+ return self.json.dumps(value)
+
+ def loads(self, value):
+ try:
+ return self.json.loads(value)
+ except ValueError:
+ return self.pickle.loads(value)
.. index::
single: session object
diff --git a/pyramid/interfaces.py b/pyramid/interfaces.py
index bedfb60b3..551ab701e 100644
--- a/pyramid/interfaces.py
+++ b/pyramid/interfaces.py
@@ -960,6 +960,14 @@ class ISession(IDict):
Keys and values of a session must be pickleable.
+ .. warning::
+
+ In :app:`Pyramid` 2.0 the session will only be required to support
+ types that can be serialized using JSON. It's recommended to switch any
+ session implementations to support only JSON and to only store primitive
+ types in sessions. See :ref:`pickle_session_deprecation` for more
+ information about why this change is being made.
+
.. versionchanged:: 1.9
Sessions are no longer required to implement ``get_csrf_token`` and
diff --git a/pyramid/session.py b/pyramid/session.py
index 97039a404..3caf4181a 100644
--- a/pyramid/session.py
+++ b/pyramid/session.py
@@ -4,11 +4,15 @@ import hashlib
import hmac
import os
import time
+import warnings
from zope.deprecation import deprecated
from zope.interface import implementer
-from webob.cookies import SignedSerializer
+from webob.cookies import (
+ JSONSerializer,
+ SignedSerializer,
+)
from pyramid.compat import (
pickle,
@@ -131,6 +135,10 @@ class PickleSerializer(object):
"""Accept a Python object and return bytes."""
return pickle.dumps(appstruct, self.protocol)
+
+JSONSerializer = JSONSerializer # api
+
+
def BaseCookieSessionFactory(
serializer,
cookie_name='session',
@@ -145,8 +153,6 @@ def BaseCookieSessionFactory(
set_on_exception=True,
):
"""
- .. versionadded:: 1.5
-
Configure a :term:`session factory` which will provide cookie-based
sessions. The return value of this function is a :term:`session factory`,
which may be provided as the ``session_factory`` argument of a
@@ -508,6 +514,7 @@ deprecated(
'so existing user session data will be destroyed if you switch to it.'
)
+
def SignedCookieSessionFactory(
secret,
cookie_name='session',
@@ -618,14 +625,31 @@ def SignedCookieSessionFactory(
should be raised for malformed inputs. If a serializer is not passed,
the :class:`pyramid.session.PickleSerializer` serializer will be used.
+ .. warning::
+
+ In :app:`Pyramid` 2.0 the default ``serializer`` option will change to
+ use :class:`pyramid.session.JSONSerializer`. See
+ :ref:`pickle_session_deprecation` for more information about why this
+ change is being made.
+
.. versionadded: 1.5a3
.. versionchanged: 1.10
Added the ``samesite`` option and made the default ``Lax``.
+
"""
if serializer is None:
serializer = PickleSerializer()
+ warnings.warn(
+ 'The default pickle serializer is deprecated as of Pyramid 1.9 '
+ 'and it will be changed to use pyramid.session.JSONSerializer in '
+ 'version 2.0. Explicitly set the serializer to avoid future '
+ 'incompatibilities. See "Upcoming Changes to ISession in '
+ 'Pyramid 2.0" for more information about this change.',
+ DeprecationWarning,
+ stacklevel=1,
+ )
signed_serializer = SignedSerializer(
secret,