Skip to content

Commit

Permalink
deprecate pickleable sessions, recommend json
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Sep 16, 2018
1 parent 8cc4bf7 commit c4f30bd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 22 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand All @@ -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
--------------------------

Expand Down
2 changes: 2 additions & 0 deletions docs/api/session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@

.. autoclass:: PickleSerializer

.. autoclass:: JSONSerializer

72 changes: 53 additions & 19 deletions docs/narr/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 all objects stored in the session must be json-serializable.
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
Expand Down
8 changes: 8 additions & 0 deletions pyramid/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,14 @@ class ISession(IDict):
Keys and values of a session must be pickleable.
.. warn::
In 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
Expand Down
30 changes: 27 additions & 3 deletions pyramid/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
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,
Expand Down Expand Up @@ -131,6 +135,10 @@ def dumps(self, appstruct):
"""Accept a Python object and return bytes."""
return pickle.dumps(appstruct, self.protocol)


JSONSerializer = JSONSerializer # api


def BaseCookieSessionFactory(
serializer,
cookie_name='session',
Expand All @@ -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
Expand Down Expand Up @@ -508,6 +514,7 @@ def dumps(self, appstruct):
'so existing user session data will be destroyed if you switch to it.'
)


def SignedCookieSessionFactory(
secret,
cookie_name='session',
Expand Down Expand Up @@ -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.
.. warn::
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,
Expand Down

0 comments on commit c4f30bd

Please sign in to comment.