Skip to content

Commit 14b37e1

Browse files
committed
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
1 parent a142d4c commit 14b37e1

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

DependencyInjection/Configuration.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,16 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
339339
->children()
340340
->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
341341
->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
342-
->scalarNode('name')->end()
342+
->scalarNode('name')
343+
->validate()
344+
->ifTrue(function ($v) {
345+
parse_str($v, $parsed);
346+
347+
return implode('&', array_keys($parsed)) !== (string) $v;
348+
})
349+
->thenInvalid('Session name %s contains illegal character(s)')
350+
->end()
351+
->end()
343352
->scalarNode('cookie_lifetime')->end()
344353
->scalarNode('cookie_path')->end()
345354
->scalarNode('cookie_domain')->end()

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,55 @@ public function testDoNoDuplicateDefaultFormResources()
4141
$this->assertEquals(array('FrameworkBundle:Form'), $config['templating']['form']['resources']);
4242
}
4343

44+
/**
45+
* @dataProvider getTestValidSessionName
46+
*/
47+
public function testValidSessionName($sessionName)
48+
{
49+
$processor = new Processor();
50+
$config = $processor->processConfiguration(
51+
new Configuration(true),
52+
array(array('session' => array('name' => $sessionName)))
53+
);
54+
55+
$this->assertEquals($sessionName, $config['session']['name']);
56+
}
57+
58+
public function getTestValidSessionName()
59+
{
60+
return array(
61+
array(null),
62+
array('PHPSESSID'),
63+
array('a&b'),
64+
array(',_-!@#$%^*(){}:<>/?'),
65+
);
66+
}
67+
68+
/**
69+
* @dataProvider getTestInvalidSessionName
70+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
71+
*/
72+
public function testInvalidSessionName($sessionName)
73+
{
74+
$processor = new Processor();
75+
$processor->processConfiguration(
76+
new Configuration(true),
77+
array(array('session' => array('name' => $sessionName)))
78+
);
79+
}
80+
81+
public function getTestInvalidSessionName()
82+
{
83+
return array(
84+
array('a.b'),
85+
array('a['),
86+
array('a[]'),
87+
array('a[b]'),
88+
array('a=b'),
89+
array('a+b'),
90+
);
91+
}
92+
4493
/**
4594
* @dataProvider getTestValidTrustedProxiesData
4695
*/

0 commit comments

Comments
 (0)