Skip to content

Commit fea98a8

Browse files
committed
bug symfony#31620 [FrameworkBundle] Inform the user when save_path will be ignored (gnat42)
This PR was squashed before being merged into the 3.4 branch (closes symfony#31620). Discussion ---------- [FrameworkBundle] Inform the user when save_path will be ignored | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no / maybe?? | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#31611 | License | MIT When a project is created, framework.yaml or config.yml has handler_id set to ~. This uses the native php SessionHandler object which is instantiated with the save_path setting from php.ini or php-fpm.d/www.conf. If you set a save_path, it is silently ignored. When using mod_php for apache or php-fpm running as apache/nginx this is typically not a big deal (except your session files are stored someplace other than you actually wanted). However if using php-fpm and running as a non-standard user for the distro, it will fail silently. Sessions won't be saved because the setting has no effect. This throws a warning in those cases to inform the user. _It could be a BC because it changes the default configuration however fixes a 'long standing bug' if you will. Not sure what you want to do about that part._ Commits ------- a090129 [FrameworkBundle] Inform the user when save_path will be ignored
2 parents bd498f2 + a090129 commit fea98a8

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
470470
$rootNode
471471
->children()
472472
->arrayNode('session')
473+
->validate()
474+
->ifTrue(function ($v) {
475+
return empty($v['handler_id']) && !empty($v['save_path']);
476+
})
477+
->thenInvalid('Session save path is ignored without a handler service')
478+
->end()
473479
->info('session configuration')
474480
->canBeEnabled()
475481
->children()
@@ -498,7 +504,7 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
498504
->defaultTrue()
499505
->setDeprecated('The "%path%.%node%" option is enabled by default and deprecated since Symfony 3.4. It will be always enabled in 4.0.')
500506
->end()
501-
->scalarNode('save_path')->defaultValue('%kernel.cache_dir%/sessions')->end()
507+
->scalarNode('save_path')->end()
502508
->integerNode('metadata_update_threshold')
503509
->defaultValue('0')
504510
->info('seconds to wait between 2 session metadata updates')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+9
Original file line numberDiff line numberDiff line change
@@ -877,13 +877,22 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
877877

878878
// session handler (the internal callback registered with PHP session management)
879879
if (null === $config['handler_id']) {
880+
// If the user set a save_path without using a non-default \SessionHandler, it will silently be ignored
881+
if (isset($config['save_path'])) {
882+
throw new LogicException('Session save path is ignored without a handler service');
883+
}
884+
880885
// Set the handler class to be null
881886
$container->getDefinition('session.storage.native')->replaceArgument(1, null);
882887
$container->getDefinition('session.storage.php_bridge')->replaceArgument(0, null);
883888
} else {
884889
$container->setAlias('session.handler', $config['handler_id'])->setPrivate(true);
885890
}
886891

892+
if (!isset($config['save_path'])) {
893+
$config['save_path'] = ini_get('session.save_path');
894+
}
895+
887896
$container->setParameter('session.save_path', $config['save_path']);
888897

889898
if (\PHP_VERSION_ID < 70000) {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ protected static function getBundleDefaultConfig()
378378
'handler_id' => 'session.handler.native_file',
379379
'cookie_httponly' => true,
380380
'gc_probability' => 1,
381-
'save_path' => '%kernel.cache_dir%/sessions',
382381
'metadata_update_threshold' => '0',
383382
'use_strict_mode' => true,
384383
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'session' => [
5+
'handler_id' => null,
6+
'save_path' => '/some/path',
7+
],
8+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:session handler-id="null" save-path="/some/path"/>
11+
</framework:config>
12+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
session:
3+
handler_id: null
4+
save_path: /some/path

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2424
use Symfony\Component\Cache\Adapter\ProxyAdapter;
2525
use Symfony\Component\Cache\Adapter\RedisAdapter;
26+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2627
use Symfony\Component\DependencyInjection\ChildDefinition;
2728
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2829
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -473,6 +474,12 @@ public function testNullSessionHandler()
473474
$this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0));
474475
}
475476

477+
public function testNullSessionHandlerWithSavePath()
478+
{
479+
$this->expectException(InvalidConfigurationException::class);
480+
$this->createContainerFromFile('session_savepath');
481+
}
482+
476483
public function testRequest()
477484
{
478485
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
 (0)