Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b51b17d

Browse files
Merge branch '4.1'
* 4.1: [DI] Remove default env type check on validate [FrameworkBundle][TwigBridge] Fix BC break from strong dependency on CSRF token storage
2 parents c81f88f + 582165f commit b51b17d

File tree

7 files changed

+72
-63
lines changed

7 files changed

+72
-63
lines changed

src/Symfony/Bridge/Twig/Extension/CsrfExtension.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,22 @@
1111

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14-
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
1514
use Twig\Extension\AbstractExtension;
1615
use Twig\TwigFunction;
1716

1817
/**
1918
* @author Christian Flothmann <[email protected]>
19+
* @author Titouan Galopin <[email protected]>
2020
*/
2121
class CsrfExtension extends AbstractExtension
2222
{
23-
private $csrfTokenManager;
24-
25-
public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
26-
{
27-
$this->csrfTokenManager = $csrfTokenManager;
28-
}
29-
3023
/**
3124
* {@inheritdoc}
3225
*/
3326
public function getFunctions(): array
3427
{
3528
return array(
36-
new TwigFunction('csrf_token', array($this, 'getCsrfToken')),
29+
new TwigFunction('csrf_token', array(CsrfRuntime::class, 'getCsrfToken')),
3730
);
3831
}
39-
40-
public function getCsrfToken(string $tokenId): string
41-
{
42-
return $this->csrfTokenManager->getToken($tokenId)->getValue();
43-
}
4432
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
15+
16+
/**
17+
* @author Christian Flothmann <[email protected]>
18+
* @author Titouan Galopin <[email protected]>
19+
*/
20+
class CsrfRuntime
21+
{
22+
private $csrfTokenManager;
23+
24+
public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
25+
{
26+
$this->csrfTokenManager = $csrfTokenManager;
27+
}
28+
29+
public function getCsrfToken(string $tokenId): string
30+
{
31+
return $this->csrfTokenManager->getToken($tokenId)->getValue();
32+
}
33+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/security_csrf.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
</service>
2323
<service id="Symfony\Component\Security\Csrf\CsrfTokenManagerInterface" alias="security.csrf.token_manager" />
2424

25+
<service id="twig.runtime.security_csrf" class="Symfony\Bridge\Twig\Extension\CsrfRuntime">
26+
<tag name="twig.runtime" />
27+
<argument type="service" id="security.csrf.token_manager" />
28+
</service>
29+
2530
<service id="twig.extension.security_csrf" class="Symfony\Bridge\Twig\Extension\CsrfExtension">
2631
<tag name="twig.extension" />
27-
<argument type="service" id="security.csrf.token_manager" />
2832
</service>
2933
</services>
3034
</container>

src/Symfony/Component/Config/Definition/BaseNode.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -507,35 +507,21 @@ private static function resolvePlaceholderValue($value)
507507
return $value;
508508
}
509509

510-
private static function getType($value): string
511-
{
512-
switch ($type = \gettype($value)) {
513-
case 'boolean':
514-
return 'bool';
515-
case 'double':
516-
return 'float';
517-
case 'integer':
518-
return 'int';
519-
}
520-
521-
return $type;
522-
}
523-
524510
private function doValidateType($value): void
525511
{
526-
if (null === $this->handlingPlaceholder || null === $value) {
527-
$this->validateType($value);
528-
529-
return;
530-
}
531-
532-
if (!$this->allowPlaceholders()) {
512+
if (null !== $this->handlingPlaceholder && !$this->allowPlaceholders()) {
533513
$e = new InvalidTypeException(sprintf('A dynamic value is not compatible with a "%s" node type at path "%s".', get_class($this), $this->getPath()));
534514
$e->setPath($this->getPath());
535515

536516
throw $e;
537517
}
538518

519+
if (null === $this->handlingPlaceholder || null === $value) {
520+
$this->validateType($value);
521+
522+
return;
523+
}
524+
539525
$knownTypes = array_keys(self::$placeholders[$this->handlingPlaceholder]);
540526
$validTypes = $this->getValidPlaceholderTypes();
541527

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Config\Definition\BaseNode;
1515
use Symfony\Component\Config\Definition\Processor;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17-
use Symfony\Component\DependencyInjection\Exception\LogicException;
1817
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
1918
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
2019
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@@ -42,21 +41,20 @@ public function process(ContainerBuilder $container)
4241
return;
4342
}
4443

45-
$defaultBag = new ParameterBag($container->getParameterBag()->all());
44+
$defaultBag = new ParameterBag($resolvingBag->all());
4645
$envTypes = $resolvingBag->getProvidedTypes();
4746
try {
4847
foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) {
49-
$prefix = (false === $i = strpos($env, ':')) ? 'string' : substr($env, 0, $i);
50-
$types = $envTypes[$prefix] ?? array('string');
51-
$default = ($hasEnv = (false === $i && $defaultBag->has("env($env)"))) ? $defaultBag->get("env($env)") : null;
52-
53-
if (null !== $default && !in_array($type = self::getType($default), $types, true)) {
54-
throw new LogicException(sprintf('Invalid type for env parameter "env(%s)". Expected "%s", but got "%s".', $env, implode('", "', $types), $type));
55-
}
56-
5748
$values = array();
58-
foreach ($types as $type) {
59-
$values[$type] = $hasEnv ? $default : self::$typeFixtures[$type] ?? null;
49+
if (false === $i = strpos($env, ':')) {
50+
$default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : null;
51+
$defaultType = null !== $default ? self::getType($default) : 'string';
52+
$values[$defaultType] = $default;
53+
} else {
54+
$prefix = substr($env, 0, $i);
55+
foreach ($envTypes[$prefix] ?? array('string') as $type) {
56+
$values[$type] = self::$typeFixtures[$type] ?? null;
57+
}
6058
}
6159
foreach ($placeholders as $placeholder) {
6260
BaseNode::setPlaceholder($placeholder, $values);

src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@
2222

2323
class ValidateEnvPlaceholdersPassTest extends TestCase
2424
{
25-
/**
26-
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
27-
* @expectedExceptionMessage Invalid type for env parameter "env(FOO)". Expected "string", but got "bool".
28-
*/
29-
public function testDefaultEnvIsValidatedByType()
25+
public function testEnvsAreValidatedInConfig()
3026
{
3127
$container = new ContainerBuilder();
32-
$container->setParameter('env(FOO)', true);
33-
$container->registerExtension(new EnvExtension());
34-
$container->prependExtensionConfig('env_extension', array(
35-
'scalar_node' => '%env(FOO)%',
28+
$container->setParameter('env(NULLED)', null);
29+
$container->setParameter('env(FLOATISH)', 3.2);
30+
$container->registerExtension($ext = new EnvExtension());
31+
$container->prependExtensionConfig('env_extension', $expected = array(
32+
'scalar_node' => '%env(NULLED)%',
33+
'scalar_node_not_empty' => '%env(FLOATISH)%',
34+
'int_node' => '%env(int:FOO)%',
35+
'float_node' => '%env(float:BAR)%',
3636
));
3737

3838
$this->doProcess($container);
39+
40+
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
3941
}
4042

41-
public function testEnvsAreValidatedInConfig()
43+
public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
4244
{
4345
$container = new ContainerBuilder();
44-
$container->setParameter('env(NULLED)', null);
46+
$container->setParameter('env(FLOATISH)', 3.2);
4547
$container->registerExtension($ext = new EnvExtension());
4648
$container->prependExtensionConfig('env_extension', $expected = array(
47-
'scalar_node' => '%env(NULLED)%',
48-
'int_node' => '%env(int:FOO)%',
49-
'float_node' => '%env(float:BAR)%',
49+
'float_node' => '%env(FLOATISH)%',
5050
));
5151

5252
$this->doProcess($container);

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3333
},
3434
"conflict": {
35-
"symfony/config": "<4.1",
35+
"symfony/config": "<4.1.1",
3636
"symfony/finder": "<3.4",
3737
"symfony/proxy-manager-bridge": "<3.4",
3838
"symfony/yaml": "<3.4"

0 commit comments

Comments
 (0)