@@ -1468,6 +1468,85 @@ library, but you can adapt it to any other library that you may be using::
1468
1468
}
1469
1469
}
1470
1470
1471
+ Another possibility to encrypt session data is to decorate the
1472
+ ``session.marshaller `` service, which points out to
1473
+ :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ MarshallingSessionHandler `.
1474
+ You can decorate this handler with a marshaller that uses encryption,
1475
+ like the :class: `Symfony\\ Component\\ Cache\\ Marshaller\\ SodiumMarshaller `.
1476
+
1477
+ First, you need to generate a secure key and add it to your :doc: `secret
1478
+ store </configuration/secrets>` as ``SESSION_DECRYPTION_FILE ``:
1479
+
1480
+ .. code-block :: terminal
1481
+
1482
+ $ php -r 'echo base64_encode(sodium_crypto_box_keypair());'
1483
+
1484
+ Then, register the ``SodiumMarshaller `` service using this key:
1485
+
1486
+ .. configuration-block ::
1487
+
1488
+ .. code-block :: yaml
1489
+
1490
+ # config/services.yaml
1491
+ services :
1492
+
1493
+ # ...
1494
+ Symfony\Component\Cache\Marshaller\SodiumMarshaller :
1495
+ decorates : ' session.marshaller'
1496
+ arguments :
1497
+ - ['%env(file:resolve:SESSION_DECRYPTION_FILE)%']
1498
+ - ' @Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner'
1499
+
1500
+ .. code-block :: xml
1501
+
1502
+ <!-- config/services.xml -->
1503
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1504
+ <container xmlns =" http://symfony.com/schema/dic/services"
1505
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1506
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1507
+ https://symfony.com/schema/dic/services/services-1.0.xsd"
1508
+ >
1509
+ <services >
1510
+ <service id =" Symfony\Component\Cache\Marshaller\SodiumMarshaller" decorates =" session.marshaller" >
1511
+ <argument type =" collection" >
1512
+ <argument >env(file:resolve:SESSION_DECRYPTION_FILE)</argument >
1513
+ </argument >
1514
+ <argument type =" service" id =" Symfony\Component\Cache\Marshaller\SodiumMarshaller.inner" />
1515
+ </service >
1516
+ </services >
1517
+ </container >
1518
+
1519
+ .. code-block :: php
1520
+
1521
+ // config/services.php
1522
+ use Symfony\Component\Cache\Marshaller\SodiumMarshaller;
1523
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1524
+ // ...
1525
+
1526
+ return function(ContainerConfigurator $container) {
1527
+ $services = $container->services();
1528
+
1529
+ // ...
1530
+
1531
+ $services->set(SodiumMarshaller::class)
1532
+ ->decorate('session.marshaller')
1533
+ ->args([
1534
+ [env('file:resolve:SESSION_DECRYPTION_FILE')],
1535
+ service(SodiumMarshaller::class.'.inner'),
1536
+ ]);
1537
+ };
1538
+
1539
+ .. caution ::
1540
+
1541
+ This will encrypt the values of the cache items, but not the cache keys. Be
1542
+ careful not to leak sensitive data in the keys.
1543
+
1544
+ .. versionadded :: 5.1
1545
+
1546
+ The :class: `Symfony\\ Component\\ Cache\\ Marshaller\\ SodiumMarshaller `
1547
+ and :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ MarshallingSessionHandler `
1548
+ classes were introduced in Symfony 5.1.
1549
+
1471
1550
Read-only Guest Sessions
1472
1551
~~~~~~~~~~~~~~~~~~~~~~~~
1473
1552
0 commit comments