8000 Remove randomness from dumped containers · symfony/symfony@23cb152 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23cb152

Browse files
Remove randomness from dumped containers
1 parent a483d37 commit 23cb152

File tree

7 files changed

+46
-17
lines changed

7 files changed

+46
-17
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,9 +1626,21 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
16261626

16271627
private function registerCacheConfiguration(array $config, ContainerBuilder $container)
16281628
{
1629-
$version = substr(str_replace('/', '-', base64_encode(hash('sha256', uniqid(mt_rand(), true), true))), 0, 22);
1630-
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $version);
1631-
$container->getDefinition('cache.adapter.system')->replaceArgument(2, $version);
1629+
// service('cache.version_identifier') === hash('crc32', filemtime(__FILE__).__CLASS__)
1630+
$version = (new Definition(\ReflectionClass::class))->addArgument(new Reference('service_container'));
1631+
$version = (new Definition())->setFactory(array($version, 'getFileName'));
1632+
$version = (new Definition())->setFactory('implode')->addArgument(array(
1633+
(new Definition())->setFactory('filemtime')->addArgument($version),
1634+
(new Definition())->setFactory('get_class')->addArgument(new Reference('service_container')),
1635+
));
1636+
$container->register('cache.version_identifier', 'string')
1637+
->setPublic(false)
1638+
->setFactory('hash')
1639+
->setArguments(array('crc32', $version))
1640+
;
1641+
1642+
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, new Reference('cache.version_identifier'));
1643+
$container->getDefinition('cache.adapter.system')->replaceArgument(2, new Reference('cache.version_identifier'));
16321644
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);
16331645

16341646
if (isset($config['prefix_seed'])) {
@@ -1664,7 +1676,7 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
16641676

16651677
if (!$container->getParameter('kernel.debug')) {
16661678
$propertyAccessDefinition->setFactory(array(PropertyAccessor::class, 'createCache'));
1667-
$propertyAccessDefinition->setArguments(array(null, null, $version, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
1679+
$propertyAccessDefinition->setArguments(array(null, null, new Reference('cache.version_identifier'), new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
16681680
$propertyAccessDefinition->addTag('cache.pool', array('clearer' => 'cache.system_clearer'));
16691681
$propertyAccessDefinition->addTag('monolog.logger', array('channel' => 'cache'));
16701682
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
289289
->arrayNode('anonymous')
290290
->canBeUnset()
291291
->children()
292-
->scalarNode('secret')->defaultValue(uniqid('', true))->end()
292+
->scalarNode('secret')->defaultNull()->end()
293293
->end()
294294
->end()
295295
->arrayNode('switch_user')

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/InMemoryFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1515
use Symfony\Component\DependencyInjection\ChildDefinition;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Reference;
1819

1920
/**
@@ -27,13 +28,14 @@ class InMemoryFactory implements UserProviderFactoryInterface
2728
public function create(ContainerBuilder $container, $id, $config)
2829
{
2930
$definition = $container->setDefinition($id, new ChildDefinition('security.user.provider.in_memory'));
31+
$defaultPassword = (new Definition())->setFactory('uniqid')->setArguments(array('', true));
3032

3133
foreach ($config['users'] as $username => $user) {
3234
$userId = $id.'_'.$username;
3335

3436
$container
3537
->setDefinition($userId, new ChildDefinition('security.user.provider.in_memory.user'))
36-
->setArguments(array($username, (string) $user['password'], $user['roles']))
38+
->setArguments(array($username, null !== $user['password'] ? (string) $user['password'] : $defaultPassword, $user['roles']))
3739
;
3840

3941
$definition->addMethodCall('createUser', array(new Reference($userId)));
@@ -55,7 +57,7 @@ public function addConfiguration(NodeDefinition $node)
5557
->normalizeKeys(false)
5658
->prototype('array')
5759
->children()
58-
->scalarNode('password')->defaultValue(uniqid('', true))->end()
60+
->scalarNode('password')->defaultNull()->end()
5961
->arrayNode('roles')
6062
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
6163
->prototype('scalar')->end()

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
2020
use Symfony\Component\DependencyInjection\ChildDefinition;
2121
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
22+
use Symfony\Component\DependencyInjection\Definition;
2223
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2324
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2425
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -529,18 +530,27 @@ private function createAuthenticationListeners($container, $id, $firewall, &$aut
529530

530531
// Anonymous
531532
if (isset($firewall['anonymous'])) {
533+
if (null === $secret = $firewall['anonymous']['secret']) {
534+
// $secret = hash('crc32', '%kernel.secret%'.__CLASS__)
535+
$secret = (new Definition())->setFactory('implode')->addArgument(array(
536+
$container->hasParameter('kernel.secret') ? '%kernel.secret%' : '',
537+
(new Definition())->setFactory('get_class')->addArgument(new Reference('service_container')),
538+
));
539+
$secret = (new Definition())->setFactory('hash')->setArguments(array('crc32', $secret));
540+
}
541+
532542
$listenerId = 'security.authentication.listener.anonymous.'.$id;
533543
$container
534544
->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.anonymous'))
535-
->replaceArgument(1, $firewall['anonymous']['secret'])
545+
->replaceArgument(1, $secret)
536546
;
537547

538548
$listeners[] = new Reference($listenerId);
539549

540550
$providerId = 'security.authentication.provider.anonymous.'.$id;
541551
$container
542552
->setDefinition($providerId, new ChildDefinition('security.authentication.provider.anonymous'))
543-
->replaceArgument(0, $firewall['anonymous']['secret'])
553+
->replaceArgument(0, $secret)
544554
;
545555

546556
$authenticationProviders[] = $providerId;

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^5.5.9|>=7.0.8",
2020
"symfony/config": "~3.2|~4.0",
21-
"symfony/twig-bridge": "^3.4.3|~4.0",
21+
"symfony/twig-bridge": "^3.4.3|^4.0.3",
2222
"symfony/http-foundation": "~2.8|~3.0|~4.0",
2323
"symfony/http-kernel": "^3.3|~4.0",
2424
"twig/twig": "~1.34|~2.4"

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,10 @@ private function addNewInstance(Definition $definition, $return, $instantiation,
892892
}
893893

894894
if (0 === strpos($class, 'new ')) {
895-
return $return.sprintf("(%s)->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
895+
return $return.sprintf("(%s)->%s(%s);\n", $class, $callable[1], $arguments ? implode(', ', $arguments) : '');
896896
}
897897

898-
return $return.sprintf("\\call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
898+
return $return.sprintf("\\call_user_func(array(%s, '%s')%s);\n", $class, $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
899899
}
900900

901901
return $return.sprintf("%s(%s);\n", $this->dumpLiteralClass($this->dumpValue($callable)), $arguments ? implode(', ', $arguments) : '');
@@ -1717,16 +1717,21 @@ private function dumpValue($value, $interpolate = true)
17171717
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $factory[1] ?: 'n/a'));
17181718
}
17191719

1720+
$class = $this->dumpValue($factory[0]);
17201721
if (is_string($factory[0])) {
1721-
return sprintf('%s::%s(%s)', $this->dumpLiteralClass($this->dumpValue($factory[0])), $factory[1], implode(', ', $arguments));
1722+
return sprintf('%s::%s(%s)', $this->dumpLiteralClass($class), $factory[1], implode(', ', $arguments));
17221723
}
17231724

17241725
if ($factory[0] instanceof Definition) {
1725-
return sprintf("\\call_user_func(array(%s, '%s')%s)", $this->dumpValue($factory[0]), $factory[1], count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
1726+
if (0 === strpos($class, 'new ')) {
1727+
return sprintf('(%s)->%s(% F438 s)', $class, $factory[1], implode(', ', $arguments));
1728+
}
1729+
1730+
return sprintf("\\call_user_func(array(%s, '%s')%s)", $class, $factory[1], count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
17261731
}
17271732

17281733
if ($factory[0] instanceof Reference) {
1729-
return sprintf('%s->%s(%s)', $this->dumpValue($factory[0]), $factory[1], implode(', ', $arguments));
1734+
return sprintf('%s->%s(%s)', $class, $factory[1], implode(', ', $arguments));
17301735
}
17311736
}
17321737

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ protected function getTestServiceSubscriberService()
8383
*/
8484
protected function getFooServiceService()
8585
{
86-
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber(\call_user_func(array(new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function () {
86+
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function () {
8787
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v = null) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] : $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition()) && false ?: '_'});
8888
}, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => function () {
8989
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber $v) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] : $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber()) && false ?: '_'});
9090
}, 'bar' => function () {
9191
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] : $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber()) && false ?: '_'});
9292
}, 'baz' => function () {
9393
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v = null) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] : $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition()) && false ?: '_'});
94-
})), 'withContext'), 'foo_service', $this));
94+
})))->withContext('foo_service', $this));
9595
}
9696

9797
/**

0 commit comments

Comments
 (0)
0