-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Add session cookie handling in cli context #41390
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
fabpot
merged 1 commit into
symfony:5.4
from
alexander-schranz:bugfix/session-cli-availibility
Sep 6, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,16 @@ | |
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Cookie; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
use Symfony\Component\HttpFoundation\Session\SessionUtils; | ||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\Exception\UnexpectedSessionUsageException; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
* Sets the session onto the request on the "kernel.request" event and saves | ||
|
@@ -36,18 +39,24 @@ | |
* | ||
* @internal | ||
*/ | ||
abstract class AbstractSessionListener implements EventSubscriberInterface | ||
abstract class AbstractSessionListener implements EventSubscriberInterface, ResetInterface | ||
{ | ||
public const NO_AUTO_CACHE_CONTROL_HEADER = 'Symfony-Session-NoAutoCacheControl'; | ||
|
||
protected $container; | ||
private $sessionUsageStack = []; | ||
private $debug; | ||
|
||
public function __construct(ContainerInterface $container = null, bool $debug = false) | ||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
private $sessionOptions; | ||
|
||
public function __construct(ContainerInterface $container = null, bool $debug = false, array $sessionOptions = []) | ||
{ | ||
$this->container = $container; | ||
$this->debug = $debug; | ||
$this->sessionOptions = $sessionOptions; | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event) | ||
|
@@ -60,7 +69,22 @@ public function onKernelRequest(RequestEvent $event) | |
if (!$request->hasSession()) { | ||
// This variable prevents calling `$this->getSession()` twice in case the Request (and the below factory) is cloned | ||
$sess = null; | ||
$request->setSessionFactory(function () use (&$sess) { return $sess ?? $sess = $this->getSession(); }); | ||
$request->setSessionFactory(function () use (&$sess, $request) { | ||
if (!$sess) { | ||
$sess = $this->getSession(); | ||
} | ||
|
||
/* | ||
* For supporting sessions in php runtime with runners like roadrunner or swoole the session | ||
* cookie need read from the cookie bag and set on the session storage. | ||
*/ | ||
if ($sess && !$sess->isStarted()) { | ||
$sessionId = $request->cookies->get($sess->getName(), ''); | ||
$sess->setId($sessionId); | ||
} | ||
|
||
return $sess; | ||
}); | ||
} | ||
|
||
$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : null; | ||
|
@@ -109,6 +133,54 @@ public function onKernelResponse(ResponseEvent $event) | |
* it is saved will just restart it. | ||
*/ | ||
$session->save(); | ||
|
||
/* | ||
* For supporting sessions in php runtime with runners like roadrunner or swoole the session | ||
jderusse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* cookie need to be written on the response object and should not be written by PHP itself. | ||
*/ | ||
$sessionName = $session->getName(); | ||
$sessionId = $session->getId(); | ||
$sessionCookiePath = $this->sessionOptions['cookie_path'] ?? '/'; | ||
$sessionCookieDomain = $this->sessionOptions['cookie_domain'] ?? null; | ||
$sessionCookieSecure = $this->sessionOptions['cookie_secure'] ?? false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$sessionCookieHttpOnly = $this->sessionOptions['cookie_httponly'] ?? true; | ||
$sessionCookieSameSite = $this->sessionOptions['cookie_samesite'] ?? Cookie::SAMESITE_LAX; | ||
|
||
SessionUtils::popSessionCookie($sessionName, $sessionCookiePath); | ||
|
||
$request = $event->getRequest(); | ||
$requestSessionCookieId = $request->cookies->get($sessionName); | ||
|
||
if ($requestSessionCookieId && $session->isEmpty()) { | ||
$response->headers->clearCookie( | ||
$sessionName, | ||
$sessionCookiePath, | ||
$sessionCookieDomain, | ||
$sessionCookieSecure, | ||
$sessionCookieHttpOnly, | ||
$sessionCookieSameSite | ||
); | ||
} elseif ($sessionId !== $requestSessionCookieId) { | ||
Nyholm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$expire = 0; | ||
$lifetime = $this->sessionOptions['cookie_lifetime'] ?? null; | ||
if ($lifetime) { | ||
$expire = time() + $lifetime; | ||
} | ||
|
||
$response->headers->setCookie( | ||
Cookie::create( | ||
$sessionName, | ||
$sessionId, | ||
$expire, | ||
$sessionCookiePath, | ||
$sessionCookieDomain, | ||
$sessionCookieSecure, | ||
$sessionCookieHttpOnly, | ||
false, | ||
$sessionCookieSameSite | ||
) | ||
); | ||
} | ||
} | ||
|
||
if ($session instanceof Session ? $session->getUsageIndex() === end($this->sessionUsageStack) : !$session->isStarted()) { | ||
|
@@ -188,6 +260,20 @@ public static function getSubscribedEvents(): array | |
]; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
if (\PHP_SESSION_ACTIVE === session_status()) { | ||
session_abort(); | ||
} | ||
|
||
session_unset(); | ||
$_SESSION = []; | ||
|
||
if (!headers_sent()) { // session id can only be reset when no headers were so we check for headers_sent first | ||
session_id(''); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the session object. | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is because we switched from
TestSessionListener
toSessionListener