Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MatTheCat opened this issue Jan 23, 2025 · 0 comments
Open

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

MatTheCat opened this issue Jan 23, 2025 · 0 comments

Comments

@MatTheCat
Copy link
Contributor

MatTheCat commented Jan 23, 2025

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.

@MatTheCat MatTheCat added the Bug label Jan 23, 2025
@MatTheCat MatTheCat changed the title [DI] ContainerBuilder can resolve some parameters twice, resulting in ParameterNotFoundException [DI] ContainerBuilder can try resolving unescaped parameters Jan 23, 2025
@MatTheCat MatTheCat changed the title [DI] ContainerBuilder can try resolving unescaped parameters [DI] ContainerBuilder can throw when resolving unescaped parameters Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants