@@ -1584,6 +1584,85 @@ library, but you can adapt it to any other library that you may be using::
1584
1584
}
1585
1585
}
1586
1586
1587
+ Another possibility to encrypt session data is to decorate the
1588
+ ``session.marshaller `` service, which points out to
1589
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ MarshallingSessionHandler `.
1590
+ You can decorate this handler with a marshaller that uses encryption,
1591
+ like the :class: `Symfony\\ Component\\ Cache\\ Marshaller\\ SodiumMarshaller `.
1592
+
1593
+ First, you need to generate a secure key and add it to your :doc: `secret
1594
+ store </configuration/secrets>` as ``SESSION_DECRYPTION_FILE ``:
1595
+
1596
+ .. code-block :: terminal
1597
+
1598
+ $ php -r 'echo base64_encode(sodium_crypto_box_keypair());'
1599
+
1600
+ Then, register the ``SodiumMarshaller `` service using this key:
1601
+
1602
+ .. configuration-block ::
1603
+
1604
+ .. code-block :: yaml
1605
+
1606
+ # config/services.yaml
1607
+ services :
1608
+
1609
+ # ...
1610
+ Symfony\Component\Cache\Marshaller\SodiumMarshaller :
1611
+ decorates : ' session.marshaller'
1612
+ arguments :
1613
+ - ['%env(file:resolve:SESSION_DECRYPTION_FILE)%']
1614
+ - ' @Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'
1615
+
1616
+ .. code-block :: xml
1617
+
1618
+ <!-- config/services.xml -->
1619
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1620
+ <container xmlns =" http://symfony.com/schema/dic/services"
1621
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1622
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1623
+ https://symfony.com/schema/dic/services/services-1.0.xsd"
1624
+ >
1625
+ <services >
1626
+ <service id =" Symfony\Component\Cache\Marshaller\SodiumMarshaller" decorates =" session.marshaller" >
1627
+ <argument type =" collection" >
1628
+ <argument >env(file:resolve:SESSION_DECRYPTION_FILE)</argument >
1629
+ </argument >
1630
+ <argument type =" service" id =" Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner" />
1631
+ </service >
1632
+ </services >
1633
+ </container >
1634
+
1635
+ .. code-block :: php
1636
+
1637
+ // config/services.php
1638
+ use Symfony\Component\Cache\Marshaller\SodiumMarshaller;
1639
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1640
+ // ...
1641
+
1642
+ return function(ContainerConfigurator $container) {
1643
+ $services = $container->services();
1644
+
1645
+ // ...
1646
+
1647
+ $services->set(SodiumMarshaller::class)
1648
+ ->decorate('session.marshaller')
1649
+ ->args([
1650
+ [env('file:resolve:SESSION_DECRYPTION_FILE')],
1651
+ service(SodiumMarshaller::class.'.inner'),
1652
+ ]);
1653
+ };
1654
+
1655
+ .. caution ::
1656
+
1657
+ This will encrypt the values of the cache items, but not the cache keys. Be
1658
+ careful not to leak sensitive data in the keys.
1659
+
1660
+ .. versionadded :: 5.1
1661
+
1662
+ The :class: `Symfony\\ Component\\ Cache\\ Marshaller\\ SodiumMarshaller `
1663
+ and :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ MarshallingSessionHandler `
1664
+ classes were introduced in Symfony 5.1.
1665
+
1587
1666
Read-only Guest Sessions
1588
1667
~~~~~~~~~~~~~~~~~~~~~~~~
1589
1668
0 commit comments