Skip to content

[HttpKernel] Set session cookie only when not empty #44657

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public function onKernelResponse(ResponseEvent $event)
$request = $event->getRequest();
$requestSessionCookieId = $request->cookies->get($sessionName);

if ($requestSessionCookieId && $session->isEmpty()) {
$isSessionEmpty = $session->isEmpty();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need in 6.0 be replaced with the new logic:

if ($requestSessionCookieId && ($session instanceof Session ? $session->isEmpty() : empty($session->all()))) {

if ($requestSessionCookieId && $isSessionEmpty) {
$response->headers->clearCookie(
$sessionName,
$sessionCookiePath,
Expand All @@ -160,7 +161,7 @@ public function onKernelResponse(ResponseEvent $event)
$sessionCookieHttpOnly,
$sessionCookieSameSite
);
} elseif ($sessionId !== $requestSessionCookieId) {
} elseif ($sessionId !== $requestSessionCookieId && !$isSessionEmpty) {
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 28, 2021

Choose a reason for hiding this comment

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

maybe we should even not write the session in the backend when it's empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a value is removed we need to write it to remove that value from the session bag.

$expire = 0;
$lifetime = $this->sessionOptions['cookie_lifetime'] ?? null;
if ($lifetime) {
Expand Down