8000 Upgrading from Symfony 4.x to Symfony 5.x logouts all users · Issue #44676 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Upgrading from Symfony 4.x to Symfony 5.x logouts all users #44676

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
ajgarlag opened this issue Dec 16, 2021 · 7 comments
Closed

Upgrading from Symfony 4.x to Symfony 5.x logouts all users #44676

ajgarlag opened this issue Dec 16, 2021 · 7 comments

Comments

@ajgarlag
Copy link
Contributor

Symfony version(s) affected

5.x

Description

The Symfony\Component\Security\Core\Role\Role and Symfony\Component\Security\Core\Role\SwitchUserRole classes were deprecated in Symfony 4.3 and removed in Symfony 5.0.

But if you are using PHP sessions to keep users logged in and you are using any authentication token that extends the given AbstractToken, the authenticated token is being serialized with references to old role classes in your Symfony 4 app.

When you upgrade your app to Symfony 5, and the ContextListener tries to unserialize the token, the old role classes no longer exist, causing an exception. The exception is caught by Symfony and your users will only see their sessions closed without any reason.

How to reproduce

Execute:

git clone git@github.com:ajgarlag/sf4-to-sf5-bug.git
cd sf4-to-sf5-bug
git checkout origin/4.x
composer install
symfony serve -d

Now visit http://localhost:8000/profile and fill the login form with:

  • Username: user
  • Password: foobar

You will be logged in. Keep this browser window open.

Execute:

git checkout origin/5.x
composer install

Refresh the browser window. You will be logged out. If you explore the profile from redirect response you will see:

Screenshot_20211216_193000

Possible Solution

I'm not sure how to fix it in Symfony code, so I've created a little package that will provide the missing classes: ajgl/sf4-to-sf5-role-unserialization

To see it in action try the following reproducer:

Execute:

git clone git@github.com:ajgarlag/sf4-to-sf5-bug.git
cd sf4-to-sf5-bug
git checkout origin/4.x
composer install
symfony serve -d

Now visit http://localhost:8000/profile and fill the login form with:

  • Username: user
  • Password: foobar

You will be logged in. Keep this browser window open.

Execute:

git checkout origin/main
composer install

Refresh the browser window. You will be logged in, and your the serialized token stored in session has been successfully upgraded to be Symfony 5 compatible.

Finally you can safely remove my package:

composer remove ajgl/sf4-to-sf5-role-unserialization

If you refresh the browser window, you will be logged in.

Additional Context

No response

@derrabus
Copy link
Member

That would indeed be an unfortunate blocker for an upgrade. I haven't investigated yet, but maybe we can change the serialization on 4.4 to not include the role class? Would you be able to work on a PR?

@wouterj
Copy link
Member
wouterj commented Dec 16, 2021

I'm not sure if we should fix this in Symfony to be honest. As this is about the update from 4.4 to 5.x, people have been facing this for 2 years already. To my knowledge, this is the first report. Which indicates to me that it's not considered important for many users of Symfony.

Even more, @ajgarlag already provided a working solution. So the issue can be fixed without changing anything in the code of an LTS version, which is due to stability reasons always better.

@gmponos
Copy link
Contributor
gmponos commented Dec 16, 2021

To my knowledge, this is the first report.

Just an FYI that I also have faced this issue upgrading from one version to another.. can't remember which.. I think it was on a minor 5.x upgrade. But as you said it was not of a big importance and I haven't reported it.. I remember that it had to do something with changing the serialization changes.. not sure...

@chalasr
Copy link
Member
chalasr commented Dec 16, 2021

people have been facing this for 2 years already.

For people using only LTS versions, it's rather 1 month.

Changing how the token is serialized on 4.4 is likely to break someone else's code though, I agree we should not fix this.
Thank you for the report and the public workaround.

@derrabus
Copy link
Member

I recall that we've fixed similar problems in the past. And imho, apps should be able to upgrade from 4.4 to 5.4 without all active sessions being terminated. Maybe my idea was not the best one, but if we can do something to ease the upgrade here, I think we should.

@ajgarlag
Copy link
Contributor Author

we can change the serialization on 4.4 to not include the role class? Would you be able to work on a PR?

That was my first option: avoid roles serialization in AbstractToken and create new instances of Role when the token is unserialized, but I think it will break any app using custom role classes extending Role

For people using only LTS versions, it's rather 1 month.

This is my case.

I see two options here:

  1. Fix it in Symfony code: we can introduce back the Role and SwitchUserRole classes in SF 5.3, with any method throwing LogicException just to allow the token unserialization, as I did in my package.
  2. Do not fix it and document the problem: we can add a note in UPGRADE docs and upgrade_major page of symfony-docs, so any app that needs to keep sessions active can use my package to avoid the problem.

@jmalinens
Copy link

Thanks for fixing it. I didn't report it but I am sure many high traffic websites with long sessions had this issue. We always check sessions between symfony and PHP upgrades. For me I just copy&pasted Role class from Symfony4 to our codebase. We have 20+ different symfony apps who share the same session and we upgrade and deploy each app separately and session need to work between them

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

7 participants
0