-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation][HttpKernel] Configure session.cookie_secure
earlier
#40231
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
Conversation
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this PR works in your case because your php.ini file has cookie_secure=on
and this PR prevent overriding the .ini value with "auto"
.
But what if your .ini is set to on
and you call the page with http
does it removes the cookie_secure
flag? Or what if the .ini is set to off
and the page is called with https
is it re-added?
I believe we should disable the .ini setting when value is auto
then the SessionListener will re-enabled it if the request is secured.
Are you sure that your Code/EventListener has a lower priority than the SessionListener
and you are not reading the session before the flag is correctly sets?
Note: In Symfony 5.3, injecting the session in your service will be deprecated and you'll have to use the RequestStack to get a session. So this will not be an issue anymore, as you won't be able to manipulate the session before the SessionListener initialize the storage and inject the session in the request.
But 'auto' is not a valid value for this ini setting. Yes, setting
You are correct that this PR missed the scenario when the page is accessed through http, not https. The code only sets cookie_secure to if ($this->container->has('session_storage')
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
) {
$storage->setOptions(['cookie_secure' => $masterRequest->isSecure()]);
}
Personally I don't have preference on whether the setting is set in one place, or is first disabled then enabled if the traffic in https. But again, I think the value passed to ini_set shouldn't be 'auto'.
I need to confess that my experience with Symfony internals isn't deep enough for me to guarantee that people will be immune from similar situations. They may still able to register an event listener which accesses the session before SessionListener is called, causing it to fail to properly set the |
no it is not. And that's precisely why this PR will fix the issue by making sure that the |
0016095
to
519eb1c
Compare
@stof is right. I forgot to update my mind when writing the reply. The PR has been updated. In this PR,
Two related notes:
|
session.cookie_secure
earlier
519eb1c
to
e82918c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(implem updated a bit)
session.cookie_secure
earliersession.cookie_secure
earlier
Thank you @tamcy. |
this is fine to me. On future versions where accessing the session as a service is not possible anymore, you won't be able to access the session before it is initialized in the Request by SessionListener (which is also why it runs at priority 100) |
@nicolas-grekas Thank you. May I know if it is intentional to use my first pull request, rather than the second one? I ask this because besides the difference in approach, the second PR includes a fix in the if ($this->container->has('session_storage')
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
&& $masterRequest->isSecure()
) {
$storage->setOptions(['cookie_secure' => true]);
} In this code, if the request is not https, the storage option will not be set. Consequently, the default PHP value will be used. If the default ini value is set to On (forcing secure cookie), the cookie will not work in an auto environment. Which is why the the code in the second PR, the code is changed to: if ($this->container->has('session_storage')
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
) {
$storage->setOptions(['cookie_secure' => $masterRequest->isSecure()]);
} Please advise what could be done. Shall I create another PR for this? Thank you! |
Yes, I reverted that part because I thought it was not desired: the goal here is to upgrade to ssl when possible. Never to downgrade to no-ssl, which is what the change you're mentioning would do to me. |
I see, thank you, this makes sense. Do we need a warning to users though? As this kind of downgrade does happen before the patch (the value is always set to "auto" at first, which effectively turns off the secure cookie flag). |
This PR does what @stof had suggested in #40221, allow me to quote him directly: