Skip to content

Commit ce10508

Browse files
committed
minor symfony#23766 Consistently use 7 chars of sha256 for hash-based id generation (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- Consistently use 7 chars of sha256 for hash-based id generation | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This prevents generating over long service ids, and for filesystem-related changes, makes the Windows 258 chars limit farther. Commits ------- bc22cdd Consistently use 7 chars of sha256 for hash-based id generation
2 parents 6c1a5e1 + bc22cdd commit ce10508

File tree

25 files changed

+60
-40
lines changed

25 files changed

+60
-40
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD
387387
$seed = '_'.$container->getParameter('kernel.root_dir');
388388
}
389389
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment').'.'.$container->getParameter('kernel.debug');
390-
$hash = hash('sha256', $seed);
391-
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.$hash;
390+
$namespace = 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);
392391

393392
$cacheDriver['namespace'] = $namespace;
394393
}

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require-dev": {
2424
"symfony/stopwatch": "~2.8|~3.0|~4.0",
25-
"symfony/dependency-injection": "~3.3|~4.0",
25+
"symfony/dependency-injection": "~3.4|~4.0",
2626
"symfony/form": "^3.2.5|~4.0",
2727
"symfony/http-kernel": "~2.8|~3.0|~4.0",
2828
"symfony/property-access": "~2.8|~3.0|~4.0",
@@ -38,7 +38,7 @@
3838
},
3939
"conflict": {
4040
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
41-
"symfony/dependency-injection": "<3.3"
41+
"symfony/dependency-injection": "<3.4"
4242
},
4343
"suggest": {
4444
"symfony/form": "",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static function getServiceProvider(ContainerBuilder $container, $name)
111111
if ($usedEnvs || preg_match('#^[a-z]++://#', $name)) {
112112
$dsn = $name;
113113

114-
if (!$container->hasDefinition($name = md5($dsn))) {
114+
if (!$container->hasDefinition($name = 'cache_connection.'.ContainerBuilder::hash($dsn))) {
115115
$definition = new Definition(AbstractAdapter::class);
116116
$definition->setPublic(false);
117117
$definition->setFactory(array(AbstractAdapter::class, 'createConnection'));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ public function testCacheDefaultRedisProvider()
937937
$container = $this->createContainerFromFile('cache');
938938

939939
$redisUrl = 'redis://localhost';
940-
$providerId = md5($redisUrl);
940+
$providerId = 'cache_connection.'.ContainerBuilder::hash($redisUrl);
941941

942942
$this->assertTrue($container->hasDefinition($providerId));
943943

@@ -951,7 +951,7 @@ public function testCacheDefaultRedisProviderWithEnvVar()
951951
$container = $this->createContainerFromFile('cache_env_var');
952952

953953
$redisUrl = 'redis://paas.com';
954-
$providerId = md5($redisUrl);
954+
$providerId = 'cache_connection.'.ContainerBuilder::hash($redisUrl);
955955

956956
$this->assertTrue($container->hasDefinition($providerId));
957957

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-xml": "*",
2121
"symfony/cache": "~3.4|~4.0",
2222
"symfony/class-loader": "~3.2",
23-
"symfony/dependency-injection": "~3.3|~4.0",
23+
"symfony/dependency-injection": "~3.4|~4.0",
2424
"symfony/config": "~3.3|~4.0",
2525
"symfony/event-dispatcher": "^3.3.1|~4.0",
2626
"symfony/http-foundation": "~3.3|~4.0",

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ private function createSwitchUserListener($container, $id, $config, $defaultProv
666666

667667
private function createExpression($container, $expression)
668668
{
669-
if (isset($this->expressions[$id = 'security.expression.'.sha1($expression)])) {
669+
if (isset($this->expressions[$id = 'security.expression.'.ContainerBuilder::hash($expression)])) {
670670
return $this->expressions[$id];
671671
}
672672

@@ -686,8 +686,7 @@ private function createRequestMatcher($container, $path = null, $host = null, $m
686686
$methods = array_map('strtoupper', (array) $methods);
687687
}
688688

689-
$serialized = serialize(array($path, $host, $methods, $ip, $attributes));
690-
$id = 'security.request_matcher.'.md5($serialized).sha1($serialized);
689+
$id = 'security.request_matcher.'.ContainerBuilder::hash(array($path, $host, $methods, $ip, $attributes));
691690

692691
if (isset($this->requestMatchers[$id])) {
693692
return $this->requestMatchers[$id];

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testFirewalls()
8484
array(
8585
'simple',
8686
'security.user_checker',
87-
'security.request_matcher.707b20193d4cb9f2718114abcbebb32af48f948484fc166a03482f49bf14f25e271f72c7',
87+
'security.request_matcher.6tndozi',
8888
false,
8989
),
9090
array(
@@ -117,7 +117,7 @@ public function testFirewalls()
117117
array(
118118
'host',
119119
'security.user_checker',
120-
'security.request_matcher.dda8b565689ad8509623ee68fb2c639cd81cd4cb339d60edbaf7d67d30e6aa09bd8c63c3',
120+
'security.request_matcher.and0kk1',
121121
true,
122122
false,
123123
'security.user.provider.concrete.default',

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^5.5.9|>=7.0.8",
2020
"ext-xml": "*",
2121
"symfony/security": "~3.4|~4.0",
22-
"symfony/dependency-injection": "~3.3|~4.0",
22+
"symfony/dependency-injection": "~3.4|~4.0",
2323
"symfony/http-kernel": "~3.3|~4.0",
2424
"symfony/polyfill-php70": "~1.0"
2525
},
@@ -29,7 +29,7 @@
2929
"symfony/console": "~3.2|~4.0",
3030
"symfony/css-selector": "~2.8|~3.0|~4.0",
3131
"symfony/dom-crawler": "~2.8|~3.0|~4.0",
32-
"symfony/event-dispatcher": "~3.3|~4.0",
32+
"symfony/event-dispatcher": "^3.3.1|~4.0",
3333
"symfony/form": "^2.8.18|^3.2.5|~4.0",
3434
"symfony/framework-bundle": "^3.2.8|~4.0",
3535
"symfony/http-foundation": "~2.8|~3.0|~4.0",
@@ -47,7 +47,7 @@
4747
},
4848
"conflict": {
4949
"symfony/var-dumper": "<3.3",
50-
"symfony/event-dispatcher": "<3.3"
50+
"symfony/event-dispatcher": "<3.3.1"
5151
},
5252
"suggest": {
5353
"symfony/security-acl": "For using the ACL functionality of this bundle"

src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function processValue($value, $isRoot = false)
5757
if ($public = $value->isPublic()) {
5858
$value->setPublic(false);
5959
}
60-
$id = 'service_locator.'.md5(serialize($value));
60+
$id = 'service_locator.'.ContainerBuilder::hash($value);
6161

6262
if ($isRoot) {
6363
if ($id !== $this->currentId) {
@@ -90,7 +90,7 @@ public static function register(ContainerBuilder $container, array $refMap)
9090
->setPublic(false)
9191
->addTag('container.service_locator');
9292

93-
if (!$container->has($id = 'service_locator.'.md5(serialize($locator)))) {
93+
if (!$container->has($id = 'service_locator.'.ContainerBuilder::hash($locator))) {
9494
$container->setDefinition($id, $locator);
9595
}
9696

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,20 @@ public static function getServiceConditionals($value)
13911391
return $services;
13921392
}
13931393

1394+
/**
1395+
* Computes a reasonably unique hash of a value.
1396+
*
1397+
* @param mixed $value A serializable value
1398+
*
1399+
* @return string
1400+
*/
1401+
public static function hash($value)
1402+
{
1403+
$hash = substr(base64_encode(hash('sha256', serialize($value), true)), 0, 7);
1404+
1405+
return str_replace(array('/', '+'), array('.', '_'), strtolower($hash));
1406+
}
1407+
13941408
/**
13951409
* Retrieves the currently set proxy instantiator or instantiates one.
13961410
*

0 commit comments

Comments
 (0)