Skip to content
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

[FrameworkBundle] start session on flashbag injection #36063

Merged

Conversation

warslett
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #33084
License MIT

This PR addresses an issue whereby if the FlashBag is injected into the application using the default service configuration, we cannot rely that the session has been started. This behaviour is in contradiction to the docs:

Sessions are automatically started whenever you read, write or even check for the existence of data in the session.

This is because symfony ensures the session has been started on calls to getFlashBag() which is normally how the flashbag will be accessed but this is not called if you inject the FlashBag directly into the container.

I have addressed this issue by changing the way the Flashbag service is built so that it uses Session as a factory service and getFlashBag as a factory method. This means that anywhere in symfony where FlashBag is injected can now rely on the fact the session is started.

I have also added a new functional test to verify this behaviour.

<service id="session.flash_bag" class="Symfony\Component\HttpFoundation\Session\Flash\FlashBag" />
<service id="session.flash_bag" class="Symfony\Component\HttpFoundation\Session\Flash\FlashBag">
<factory service="session" method="getFlashBag"/>
</service>
<service id="Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface" alias="session.flash_bag" />

<service id="session.attribute_bag" class="Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag" />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas should we also configure session.attribute_bag in the same way for consistency?

Copy link
Member

Choose a reason for hiding this comment

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

yes that'd make sense

Copy link
Member

Choose a reason for hiding this comment

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

Then on master, we could deprecate these two services.

Copy link
Member

Choose a reason for hiding this comment

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

@warslett up to send a PR to deprecate these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas here: #36273 I get build failures but doesn't seem related. Are the builds on master wonky right now?

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Mar 13, 2020
@nicolas-grekas nicolas-grekas changed the title 33084 start session on flashbag injection [FrameworkBundle] start session on flashbag injection Mar 13, 2020
@fabpot fabpot force-pushed the 33084-start-session-on-flashbag-injection branch from 8b5cbfd to e8b4d35 Compare March 16, 2020 06:35
@fabpot
Copy link
Member

fabpot commented Mar 16, 2020

Thank you @warslett.

@fabpot fabpot merged commit 78b11a5 into symfony:3.4 Mar 16, 2020
@fabpot
Copy link
Member

fabpot commented Mar 16, 2020

@warslett Can you prepare a PR on master to deprecate the two services as suggested by Nicolas (I've merged 3.4 up to master)?

@warslett
Copy link
Contributor Author

@fabpot sure I can do that this evening. I'll use the message: "The "%service_id%" service is deprecated since Symfony 5.1"

@warslett warslett deleted the 33084-start-session-on-flashbag-injection branch March 16, 2020 11:06
This was referenced Mar 27, 2020
@ekosogin
Copy link

After this this bug fix new bug was created if session with namespaces was configured

session:
    class: Symfony\Component\HttpFoundation\Session\Session
    arguments:
        - '@session.storage'
        - '@Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag'
        - '@session.flash_bag'
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag: ~

Circular reference detected for service "session", path: "session -> session.flash_bag -> session".

@warslett
Copy link
Contributor Author

@ekosogin you don't actually need the @session.flash_bag parameter on there. You should be able to do this:

session:
    class: Symfony\Component\HttpFoundation\Session\Session
    arguments:
        - '@session.storage'
        - '@Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag'
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag: ~

@warslett
Copy link
Contributor Author

@fabpot I have a branch with the deprecation on master but haven't created a PR yet (sorry virus got in the way). Do we need to consider @ekosogin's problem first? It is simple enough to remove the session.flash_bag argument from his session definition but if lots of people using symfony have overridden the default session service definition and injected either of these two soon-to-be deprecated services then they might be experiencing the same problem.

@ekosogin
Copy link

The problem with circular reference still present with this configuration

session:
    class: Symfony\Component\HttpFoundation\Session\Session
    arguments:
        - '@session.storage'
        - '@Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag'
Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag: ~

@fabpot fabpot mentioned this pull request Mar 30, 2020
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 30, 2020

FYI, discussions on closed PRs are like oral talks: we'll all forget about them.

@jmalinens
Copy link

jmalinens commented Mar 30, 2020

we have the same issue.

This is the 3rd time in a year where in minor symfony version upgrade breaks something for us in sessions :/

this won't work for us anymore in 4.4.6:

    # Symfony services
    session.flash_bag:
        class: OurCustom\FlashBag

    session.attribute_bag:
        class: Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag
        calls:
            - [setName, ["login"]]
        arguments: ['login', '.']

@jmalinens
Copy link

@nicolas-grekas looks like the ticket is about session flash bag and that' is ok to remove it from Session service definition but

https://github.com/symfony/symfony/pull/36063/files#diff-1777be3d93e71a9295d7fed8ed262c3eL16

but session.attribute_bag is also removed in this PR.

We rely on NamespacedAttributeBag so we store symfony session in non-root namespace.

With this change on: https://github.com/symfony/symfony/blob/v4.4.6/src/Symfony/Component/Security/Http/Firewall/ContextListener.php#L119

symfony ignores our namesapce (login) and because of that can not find symfony session data

@nicolas-grekas
Copy link
Member

Fixed in #36261
Please abstain from commenting on closed PRs next time, except to tell about the new issue you created to report any regression.

fabpot added a commit that referenced this pull request Mar 30, 2020
…hen circular refs are detected (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] revert to legacy wiring of the session when circular refs are detected

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36063
| License       | MIT
| Doc PR        | -

As introduced and reported in the linked PR.

Commits
-------

35644cf [FrameworkBundle] revert to legacy wiring of the session when circular refs are detected
fabpot added a commit that referenced this pull request Apr 1, 2020
…services (William Arslett)

This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[FrameworkBundle] Deprecate flashbag and attributebag services

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | Related to [#10557](#10557)
| Related to PR | #36063
| License       | MIT

FlashBag and AttributeBag are data objects and so should not be available via the service container. The preferred method for accessing these objects is via
`$session->getFlashBag()` or `$session->getAttributeBag()`

Commits
-------

f9b52fe [FrameworkBundle] Deprecate flashbag and attributebag services
nicolas-grekas added a commit that referenced this pull request Apr 21, 2020
…tion (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] Fix session.attribute_bag service definition

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #36465
| License       | MIT
| Doc PR        | -

It looks like in #36063, the fact the the `getAttributeBag` method is private was forgotten. It cannot be used as the factory method. I guess we can make it public. Should it maybe marked `@internal`?

Commits
-------

76072c6 [FrameworkBundle] Fix session.attribute_bag service definition
vitek-rostislav added a commit to shopsys/shopsys that referenced this pull request Jul 26, 2022
- FlashBagProvider is not wired in contructor anymore because it had side effects (session was started when the container was built)
	- see symfony/symfony#36063
vitek-rostislav added a commit to shopsys/shopsys that referenced this pull request Aug 17, 2022
- FlashBagProvider is not wired in contructor anymore because it had side effects (session was started when the container was built)
	- see symfony/symfony#36063
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.

None yet

6 participants