Skip to content

[HttpFoundation] Added MarshallingSessionHandler #18794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,85 @@ library, but you can adapt it to any other library that you may be using::
}
}

Another possibility to encrypt session data is to decorate the
``session.marshaller`` service, which points out to
:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\MarshallingSessionHandler`.
You can decorate this handler with a marshaller that uses encryption,
like the :class:`Symfony\\Component\\Cache\\Marshaller\\SodiumMarshaller`.

First, you need to generate a secure key and add it to your :doc:`secret
store </configuration/secrets>` as ``SESSION_DECRYPTION_FILE``:

.. code-block:: terminal

$ php -r 'echo base64_encode(sodium_crypto_box_keypair());'

Then, register the ``SodiumMarshaller`` service using this key:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:

# ...
Symfony\Component\Cache\Marshaller\SodiumMarshaller:
decorates: 'session.marshaller'
arguments:
- ['%env(file:resolve:SESSION_DECRYPTION_FILE)%']
- '@Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="Symfony\Component\Cache\Marshaller\SodiumMarshaller" decorates="session.marshaller">
<argument type="collection">
<argument>env(file:resolve:SESSION_DECRYPTION_FILE)</argument>
</argument>
<argument type="service" id="Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner"/>
</service>
</services>
</container>

.. code-block:: php

// config/services.php
use Symfony\Component\Cache\Marshaller\SodiumMarshaller;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
// ...

return function(ContainerConfigurator $container) {
$services = $container->services();

// ...

$services->set(SodiumMarshaller::class)
->decorate('session.marshaller')
->args([
[env('file:resolve:SESSION_DECRYPTION_FILE')],
service(SodiumMarshaller::class.'.inner'),
]);
};

.. caution::

This will encrypt the values of the cache items, but not the cache keys. Be
careful not to leak sensitive data in the keys.

.. versionadded:: 5.1

The :class:`Symfony\\Component\\Cache\\Marshaller\\SodiumMarshaller`
and :class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\MarshallingSessionHandler`
classes were introduced in Symfony 5.1.

Read-only Guest Sessions
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down