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

Skip to content

[DI] ContainerBuilder can throw when resolving unescaped parameters #59594

Open
@MatTheCat

Description

@MatTheCat

Symfony version(s) affected

≥ 3.3.7

Description

When ContainerBuilder::compile is called with $resolveEnvPlaceholders, it will end up creating a new ParameterBag from the old one:

if ($resolveEnvPlaceholders) {
$this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
}

With true as its second parameter, resolveEnvPlaceholders will start by calling the bag’s resolveValue method:

if (true === $format ??= '%%env(%s)%%') {
$value = $bag->resolveValue($value);
}

Problem is, at this point the parameter bag already has been resolved by the ResolveParameterPlaceHoldersPass, which means parameters’ value have been unescaped:

$parameters[$key] = $this->unescapeValue($value);

Since unescaped parameters can appear as if they contain a placeholder, those will trigger a ParameterNotFoundException.

How to reproduce

$builder = new ContainerBuilder();
$builder->setParameter('foo', '%%bar%%');
$builder->compile(true);

Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException: You have requested a non-existent parameter "bar".

Possible Solution

I guess an easy fix would be to ignore ParameterNotFoundExceptions when creating the new ParameterBag? It feels like addressing a symptom rather than a cause though.

Additional Context

Spotted while investigating #59028.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions