8000 [DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop) by dunglas · Pull Request #24673 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop) #24673

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

Closed
wants to merge 2 commits into from

Conversation

dunglas
Copy link
Member
@dunglas dunglas commented Oct 24, 2017
Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

If an environment variable is used to build a service name (like in this snippet), an infinite loop occurs.

It's common to build dynamic service names (in a compiler pass), if the dynamic part comes from a parameter, this bug can occurs.

@stof
Copy link
Member
stof commented Oct 24, 2017

Why the hell would getRemovedIds use getEnv ? Env placeholders are not resolved in service ids. Parameters are not supported there. If we have a case supporting env placeholders in some cases in references, we should identify in which case (I strongly suspect this can lead to other weird bugs).

$this->set($id, new ServiceLocator(array()));
// We don't call set() to prevent an infinite loop:
// set() calls getRemovedIds(), getRemovedIds() calls getEnv() in some cases
$this->services[$id] = new ServiceLocator(array());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assigning directly is not good, as it bypasses a bunch of validation and processing (you don't normalize the id for instance)

Copy link
Member Author
@dunglas dunglas Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's a very special case. The id is known and is already normalized and validated. We may add a third parameter to set to prevent the circular env resolution, but it looks overkill for this special case.

@dunglas
Copy link
Member Author
dunglas commented Oct 24, 2017

Why the hell would getRemovedIds use getEnv ?

Here is an extract of the removed-ids.php file generated for the service definition I've pointed in the initial comment:

return array(
    // ...
    'api_platform.http_cache.purger.varnish' => true,
    'api_platform.http_cache.purger.varnish_client' => true,
    'api_platform.http_cache.purger.varnish_client.'.$this->getEnv('string:VARNISH_URL') => true,
    // ...
);

It looks legit to me or the error generation cannot work properly:

            if (isset($this->getRemovedIds()[$id])) {
                throw new ServiceNotFoundException($id, null, null, array(), sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $id));
            }

@nicolas-grekas
Copy link
Member

@stof is right: service ids cannot be dynamic, this is unsupported and should throw instead.

@dunglas
Copy link
Member Author
dunglas commented Oct 24, 2017

Ok got it, I'll change the patch to throw

@dunglas
Copy link
Member Author
dunglas commented Oct 24, 2017

Status: Needs Review

public function testDynamicServiceName()
{
$container = new ContainerBuilder();
$env = ($container->getParameterBag()->get('env(BAR)'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra brackets around

@chalasr
Copy link
Member
chalasr commented Oct 24, 2017

Broken tests are unrelated, rebasing will make this green. Fabbot complains though

@nicolas-grekas
Copy link
Member

Do we need to do the check for aliases also?

@dunglas dunglas force-pushed the prevent-infinite-loop branch from 59a400e to e4218a9 Compare October 24, 2017 19:40
@dunglas dunglas changed the title [DI] Prevent an ininite loop when using env vars in service names [DI] Prevent an infinite loop when using env vars in service names Oct 24, 2017
@dunglas
Copy link
Member Author
dunglas commented Oct 24, 2017

@nicolas-grekas done

@nicolas-grekas
Copy link
Member

Also the commit+PR title should be updated

@symfony symfony deleted a comment from nicolas-grekas Oct 25, 2017
@dunglas dunglas force-pushed the prevent-infinite-loop branch from 37bd70b to 51e16bb Compare October 25, 2017 06:14
@dunglas dunglas changed the base branch from 3.4 to 2.3 October 25, 2017 06:14
@dunglas dunglas changed the base branch from 2.3 to 3.3 October 25, 2017 06:14
@dunglas dunglas changed the title [DI] Prevent an infinite loop when using env vars in service names [DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop) Oct 25, 2017
@dunglas
Copy link
Member Author
dunglas commented Oct 25, 2017

Errors not related

@dunglas dunglas force-pushed the prevent-infinite-loop branch from b456b2b to 949c6da Compare October 25, 2017 07:20
@dunglas dunglas force-pushed the prevent-infinite-loop branch from 949c6da to 0034cc1 Compare October 25, 2017 07:25
@fabpot
Copy link
Member
fabpot commented Oct 26, 2017

Thank you @dunglas.

fabpot added a commit that referenced this pull request Oct 26, 2017
…c values (prevent an infinite loop) (dunglas)

This PR was squashed before being merged into the 3.3 branch (closes #24673).

Discussion
----------

[DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop)

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

If an environment variable is used to build a service name (like in [this snippet](https://github.com/api-platform/core/blame/4b3d1abfe595d507c9064ef86fd8ef6881b7e5b5/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php#L471)), an infinite loop occurs.

It's common to build dynamic service names (in a compiler pass), if the dynamic part comes from a parameter, this bug can occurs.

Commits
-------

14e3085 [DI] Throw when a service name or an alias contains dynamic values (prevent an infinite loop)
@fabpot fabpot closed this Oct 26, 2017
This was referenced Oct 30, 2017
@fabpot fabpot mentioned this pull request Nov 10, 2017
@dunglas dunglas deleted the prevent-infinite-loop branch November 23, 2017 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0