Skip to content

Commit f2f15f5

Browse files
committed
[SecurityBundle] Added csrf_token_generator and csrf_token_id as new
names for csrf_provider and intention options
1 parent b74a887 commit f2f15f5

File tree

5 files changed

+89
-8
lines changed

5 files changed

+89
-8
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CHANGELOG
55
-----
66

77
* Added 'host' option to firewall configuration
8+
* Added 'csrf_token_generator' and 'csrf_token_id' options to firewall logout
9+
listener configuration to supercede/alias 'csrf_provider' and 'intention'
10+
respectively
811
* Moved 'security.secure_random' service configuration to FrameworkBundle
912

1013
2.3.0

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,43 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
212212
->arrayNode('logout')
213213
->treatTrueLike(array())
214214
->canBeUnset()
215+
->beforeNormalization()
216+
->ifTrue(function($v) { return isset($v['csrf_provider']) && isset($v['csrf_token_generator']); })
217+
->thenInvalid("You should define a value for only one of 'csrf_provider' and 'csrf_token_generator' on a security firewall. Use 'csrf_token_generator' as this replaces 'csrf_provider'.")
218+
->end()
219+
->beforeNormalization()
220+
->ifTrue(function($v) { return isset($v['intention']) && isset($v['csrf_token_id']); })
221+
->thenInvalid("You should define a value for only one of 'intention' and 'csrf_token_id' on a security firewall. Use 'csrf_token_id' as this replaces 'intention'.")
222+
->end()
223+
->beforeNormalization()
224+
->ifTrue(function($v) { return isset($v['csrf_provider']); })
225+
->then(function($v) {
226+
$v['csrf_token_generator'] = $v['csrf_provider'];
227+
228+
return $v;
229+
})
230+
->end()
231+
->beforeNormalization()
232+
->ifTrue(function($v) { return isset($v['intention']); })
233+
->then(function($v) {
234+
$v['csrf_token_id'] = $v['intention'];
235+
236+
return $v;
237+
})
238+
->end()
239+
->beforeNormalization()
240+
->always()
241+
->then(function ($v) {
242+
unset($v['csrf_provider']);
243+
unset($v['intention']);
244+
245+
return $v;
246+
})
247+
->end()
215248
->children()
216249
->scalarNode('csrf_parameter')->defaultValue('_csrf_token')->end()
217-
->scalarNode('csrf_provider')->cannotBeEmpty()->end()
218-
->scalarNode('intention')->defaultValue('logout')->end()
250+
->scalarNode('csrf_token_generator')->cannotBeEmpty()->end()
251+
->scalarNode('csrf_token_id')->defaultValue('logout')->end()
219252
->scalarNode('path')->defaultValue('/logout')->end()
220253
->scalarNode('target')->defaultValue('/')->end()
221254
->scalarNode('success_handler')->end()

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/SimpleFormFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function createListener($container, $id, $config, $userProvider)
6565
$listenerId = parent::createListener($container, $id, $config, $userProvider);
6666
$listener = $container->getDefinition($listenerId);
6767

68-
if (!isset($config['csrf_provider'])) {
68+
if (!isset($config['csrf_token_generator'])) {
6969
$listener->addArgument(null);
7070
}
7171

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
291291
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('security.logout_listener'));
292292
$listener->replaceArgument(3, array(
293293
'csrf_parameter' => $firewall['logout']['csrf_parameter'],
294-
'intention' => $firewall['logout']['intention'],
294+
'intention' => $firewall['logout']['csrf_token_id'],
295295
'logout_path' => $firewall['logout']['path'],
296296
));
297297
$listeners[] = new Reference($listenerId);
@@ -307,8 +307,8 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
307307
$listener->replaceArgument(2, new Reference($logoutSuccessHandlerId));
308308

309309
// add CSRF provider
310-
if (isset($firewall['logout']['csrf_provider'])) {
311-
$listener->addArgument(new Reference($firewall['logout']['csrf_provider']));
310+
if (isset($firewall['logout']['csrf_token_generator'])) {
311+
$listener->addArgument(new Reference($firewall['logout']['csrf_token_generator']));
312312
}
313313

314314
// add session logout handler
@@ -336,9 +336,9 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
336336
->addMethodCall('registerListener', array(
337337
$id,
338338
$firewall['logout']['path'],
339-
$firewall['logout']['intention'],
339+
$firewall['logout']['csrf_token_id'],
340340
$firewall['logout']['csrf_parameter'],
341-
isset($firewall['logout']['csrf_provider']) ? new Reference($firewall['logout']['csrf_provider']) : null,
341+
isset($firewall['logout']['csrf_token_generator']) ? new Reference($firewall['logout']['csrf_token_generator']) : null,
342342
))
343343
;
344344
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/MainConfigurationTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,49 @@ public function testManyConfigForProvider()
6767
$configuration = new MainConfiguration(array(), array());
6868
$config = $processor->processConfiguration($configuration, array($config));
6969
}
70+
71+
public function testCsrfAliases()
72+
{
73+
$config = array(
74+
'firewalls' => array(
75+
'stub' => array(
76+
'logout' => array(
77+
'csrf_provider' => 'a_token_generator',
78+
'intention' => 'a_token_id',
79+
),
80+
),
81+
),
82+
);
83+
$config = array_merge(static::$minimalConfig, $config);
84+
85+
$processor = new Processor();
86+
$configuration = new MainConfiguration(array(), array());
87+
$processedConfig = $processor->processConfiguration($configuration, array($config));
88+
$this->assertTrue(isset($processedConfig['firewalls']['stub']['logout']['csrf_token_generator']));
89+
$this->assertEquals('a_token_generator', $processedConfig['firewalls']['stub']['logout']['csrf_token_generator']);
90+
$this->assertTrue(isset($processedConfig['firewalls']['stub']['logout']['csrf_token_id']));
91+
$this->assertEquals('a_token_id', $processedConfig['firewalls']['stub']['logout']['csrf_token_id']);
92+
}
93+
94+
/**
95+
* @expectedException InvalidArgumentException
96+
*/
97+
public function testCsrfOriginalAndAliasValueCausesException()
98+
{
99+
$config = array(
100+
'firewalls' => array(
101+
'stub' => array(
102+
'logout' => array(
103+
'csrf_token_id' => 'a_token_id',
104+
'intention' => 'old_name',
105+
),
106+
),
107+
),
108+
);
109+
$config = array_merge(static::$minimalConfig, $config);
110+
111+
$processor = new Processor();
112+
$configuration = new MainConfiguration(array(), array());
113+
$processedConfig = $processor->processConfiguration($configuration, array($config));
114+
}
70115
}

0 commit comments

Comments
 (0)