Skip to content

[HttpKernel] Fix compatibility with php bridge and already started php sessions #44634

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

Merged
merged 1 commit into from
Jan 26, 2022

Conversation

alexander-schranz
Copy link
Contributor

@alexander-schranz alexander-schranz commented Dec 15, 2021

Q A
Branch? 5.4 for bug fixes
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #44553, Fix #44616
License MIT
Doc PR symfony/symfony-docs#...

Fix compatibility to PHPBridge with new session handling.

TODO:

  • Test

@carsonbot
Copy link

Hey!

I think @mtarld has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@alexander-schranz alexander-schranz changed the title Update AbstractSessionListener.php Fix compatbility to php bridge for sessions Dec 16, 2021
@devnix
Copy link

devnix commented Dec 23, 2021

Hi! I think the following lines would have to be deleted. It just does not make sense to remove the cookie if the session bags are empty, because that does not mean the session is empty (and used by other application): https://github.com/symfony/symfony/blob/v5.4.0/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php#L154-L162

@alexander-schranz
Copy link
Contributor Author

@devnix this lines can not be removed as they handles the correct handling of a logout when running swoole and roadrunner. I'm currently thinking about check if the PHP bridge is activated and then go with the old behaviour and let PHP handle the session cookie then again. Means that swoole and roadrunner could not be used with that applications correctly but I think that this application don't even use the symfony/runtime so I think that would be fine.

@devnix
Copy link

devnix commented Dec 23, 2021

@alexander-schranz alright, that's what I feared (I didn't make directly a PR because I suspected I was going to miss a fact like that).

Speaking from ignorance: maybe the listener could call a method defined by SessionStorageInterface, so each session storage would know how they have to handle by implementing that method?

@alexander-schranz
Copy link
Contributor Author

alexander-schranz commented Dec 23, 2021

@devnix the isEmpty is currently a way to know if invalidate was called:

public function invalidate(int $lifetime = null): bool

As example the logout listener does it by default:
$event->getRequest()->getSession()->invalidate();

So what is missing is to know if a session is invalid or not at current state. But I did avoided to extend the SessionInterface with new methods which are not required currently and so did go with isEmpty. Personaly I think a session which is empty can savely be removed. So I would maybe we should adjust the isEmpty in the PHPBridge to check if there are not other values in the $_SESSION variable beside the empty symfony storage variable. Or find another way to check for invalid session or as said above let PHP handle the session cookie when the php bridge is used.

@carsonbot carsonbot changed the title Fix compatbility to php bridge for sessions [HttpKernel] Fix compatbility to php bridge for sessions Dec 28, 2021
@alexander-schranz
Copy link
Contributor Author

@devnix found that I can check if the session was even changed using the session usage index. Can you check this change with your application?

@alexander-schranz alexander-schranz force-pushed the patch-10 branch 2 times, most recently from f120407 to 8c10721 Compare December 29, 2021 12:23
@alexander-schranz alexander-schranz marked this pull request as ready for review December 29, 2021 12:43
@devnix
Copy link

devnix commented Dec 29, 2021

I will be able to check it next monday!

I have a wild guess: if a session is started by another application, and later a Symfony controller is called without touching the session, will not be deleted because the usage index would be zero?

This can even happen with coexisting, separated web applications, not only with legacy applications integrating Symfony

@alexander-schranz alexander-schranz force-pushed the patch-10 branch 3 times, most recently from b0d6278 to 502f6f1 Compare December 29, 2021 15:42
@alexander-schranz
Copy link
Contributor Author

@devnix Thank you for the feedback. Make sense. I tried to create test cases for it and check now also for $_SESSION to avoid these problems with other native sessions variables written before.

/cc @jderusse

@devnix
Copy link

devnix commented Dec 29, 2021

I will try the patch ASAP, as I don't have access to the repository right now. Thank you so much, @alexander-schranz

@chalasr chalasr requested a review from jderusse January 13, 2022 11:03
@johannes85
Copy link
Contributor

I know, I'm late to the conversation but I think it would be a good idea to give the person, implementing the session handling a way to control the behavior of creating/deleting the session cookie (as @devnix suggested).

In my case we have a centralized login which creates a session (in Redis) and writes the cookie.
Our applications (around 20) are using this already created session.

Now we want to migrate those applications away from the current framework to Symfony, so I implemented a custom authenticator for this session but also wanted to reuse this session in a custom SessionStorage.

This worked up until Symfony 5.4 which now kills the externally generated session cookie.
So, a way for my SessionStorage to tell Symfony: "don't mess with the session cookie" would be nice.

My solution now is to have two sessions. One created by our login application and one created by Symfony, using one Redis connection (singleton via DI).
This also doesn't work as intended because of #44616 but is fixed with #44657

@alexander-schranz alexander-schranz changed the title [HttpKernel] Fix compatbility to php bridge for sessions [HttpKernel] Fix compatbility to php native and already started php sessions Jan 20, 2022
@alexander-schranz alexander-schranz changed the title [HttpKernel] Fix compatbility to php native and already started php sessions [HttpKernel] Fix compatbility to php bridge and already started php sessions Jan 20, 2022
@alexander-schranz
Copy link
Contributor Author

@johannes85 as discussed in #44634 (comment) there would be better options. But are not possible in a bugfix as a bugfix is not allowed to extend the public API.

@nicolas-grekas nicolas-grekas changed the title [HttpKernel] Fix compatbility to php bridge and already started php sessions [HttpKernel] Fix compatibility with php bridge and already started php sessions Jan 26, 2022
@nicolas-grekas
Copy link
Member

Thank you @alexander-schranz.

@nicolas-grekas nicolas-grekas merged commit 2ab5f29 into symfony:5.4 Jan 26, 2022
@alexander-schranz alexander-schranz deleted the patch-10 branch January 26, 2022 15:29
@alexander-schranz
Copy link
Contributor Author

alexander-schranz commented Jan 26, 2022

Thx all for helping, testing and providing feedback!

@nicolas-grekas
Copy link
Member

@alexander-schranz could you please have a look at branch 6.0?
I feel like my merge is wrong there 😬 (tests of HttpKernel are failing).

@alexander-schranz
Copy link
Contributor Author

@nicolas-grekas see #45192

nicolas-grekas added a commit that referenced this pull request Jan 27, 2022
…er-schranz)

This PR was submitted for the 6.0 branch but it was squashed and merged into the 5.4 branch instead.

Discussion
----------

[HttpKernel] Fix session test cases for symfony

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

Related to: #44634

Fix HttpKernel test cases as session are created differently in symfony 6.0.

Commits
-------

b047a2d [HttpKernel] Fix session test cases for symfony
This was referenced Jan 28, 2022
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