-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[11.x] Auto-secure cookies #52422
[11.x] Auto-secure cookies #52422
Conversation
@valorin referencing to our discussion laravel/laravel#6437, I think the required framework modification is that easy and brings a great improvement in terms of "secure by default". Adding the |
Oh wow, that's all it needs? Nice! 😁 Would it be possible to add a test or two to cover the behaviour? It feels a bit like we're passing responsibility over to Symfony with a magic value, and if their behaviour ever changes, Laravel will just inherit it without notice. Also, updating the config file to explain |
I was surprised as well. 😄 Yes, I went back and forth with tests as it feels a bit redundant to the Symfony tests. But it's probably wise to cover it in the framework as well. Let me add some tests. |
Nice work @fabricecw, those tests are great. Should let us know if anything breaks. 👍 |
@@ -224,7 +224,7 @@ protected function addCookieToResponse(Response $response, Session $session) | |||
$this->getCookieExpirationDate(), | |||
$config['path'], | |||
$config['domain'], | |||
$config['secure'] ?? false, | |||
$config['secure'], |
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.
Wouldn't it be better if it has a default value:
$config['secure'] ?? null,
Because if the config key currently does not exist, it would break existing applications. (I think it is present in almost all applications, but the default value was probably there for a reason)
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.
Okay, I just thought about it again, the ?? false
was there, because of the default null
value for the secure
key. So should be fine to be left out completely.
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.
Yes. Back in the days, Symfony Cookie
didn't support null
for secure. null
was introduced for this "auto-secure" behaviour.
Hi @fabricecw, I have a question regarding an issue we're facing. Our app uses both HTTP and HTTPS, and some pages are repeatedly prompting users to log in. Could this commit be responsible for the issue? |
If both HTTP and HTTPS requests access the session, yes. The cookie inherits the You have to disable secure cookies as long as you send HTTP and HTTPS requests. Either you do it in the |
Just as an FYI for others: I encountered a similar issue in one of my projects. I was using GuzzleHttp to run custom race condition tests against my app where during the test execution login attempts are made with test user(s). When my app is configured in production mode, HTTPS is required, and all HTTP requests are automatically redirected. All my test calls for both dev and prod configs were initially made against the same static base URL
My solution was to use a proper https://*:443 base URL for tests when running the app in production mode. If I understand you correctly the root cause for my issue was the same as what you're describing above: i.e. that certain cookies are now no longer shared between HTTP and HTTPS calls. |
The client will not include any |
This PR auto-secures cookies as implemented in symfony/symfony#28447.
VerifyCsrfToken
middleware already auto-secures cookies since it doesn't pass a fallback valuefalse
to the Cookie'ssecure
attribute.By passing
null
, thesecureDefault
value from the Cookie takes over.In the
prepare
method ofIlluminate\Http\Response
(Symfony\Component\HttpFoundation\Response
) , the cookies inherit the "secure" attribute from the request:This behavior would be a good balance between making applications "safe by default" and not breaking existing ones. It further provides a solution to the discussion in laravel/laravel#6437