Disallow illegal characters like "." in session.name

PHP saves cookie with correct name, but upon deserialization to
$_COOKIE, it replaces some characters, e.g. "." becomes "_".

This is probably also reason why \SessionHandler is not able to find
a session.

https://harrybailey.com/2009/04/dots-arent-allowed-in-php-cookie-names/
https://bugs.php.net/bug.php?id=75883
This commit is contained in:
Gabriel Ostrolucký 2018-05-12 21:17:30 +02:00
parent 00c61da3c6
commit 16ebb43bd4
2 changed files with 59 additions and 1 deletions

View File

@ -339,7 +339,16 @@ class Configuration implements ConfigurationInterface
->children()
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
->scalarNode('name')->end()
->scalarNode('name')
->validate()
->ifTrue(function ($v) {
parse_str($v, $parsed);
return implode('&', array_keys($parsed)) !== (string) $v;
})
->thenInvalid('Session name %s contains illegal character(s)')
->end()
->end()
->scalarNode('cookie_lifetime')->end()
->scalarNode('cookie_path')->end()
->scalarNode('cookie_domain')->end()

View File

@ -41,6 +41,55 @@ class ConfigurationTest extends TestCase
$this->assertEquals(array('FrameworkBundle:Form'), $config['templating']['form']['resources']);
}
/**
* @dataProvider getTestValidSessionName
*/
public function testValidSessionName($sessionName)
{
$processor = new Processor();
$config = $processor->processConfiguration(
new Configuration(true),
array(array('session' => array('name' => $sessionName)))
);
$this->assertEquals($sessionName, $config['session']['name']);
}
public function getTestValidSessionName()
{
return array(
array(null),
array('PHPSESSID'),
array('a&b'),
array(',_-!@#$%^*(){}:<>/?'),
);
}
/**
* @dataProvider getTestInvalidSessionName
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testInvalidSessionName($sessionName)
{
$processor = new Processor();
$processor->processConfiguration(
new Configuration(true),
array(array('session' => array('name' => $sessionName)))
);
}
public function getTestInvalidSessionName()
{
return array(
array('a.b'),
array('a['),
array('a[]'),
array('a[b]'),
array('a=b'),
array('a+b'),
);
}
/**
* @dataProvider getTestValidTrustedProxiesData
*/