Add the createId() method of SessionInterface and then use it in SessionMiddleware when requestSessionId is null - #88
klsoft-web wants to merge 2 commits into
Conversation
…ionMiddleware when requestSessionId is null
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #88 +/- ##
=============================================
- Coverage 100.00% 99.47% -0.53%
- Complexity 77 78 +1
=============================================
Files 3 3
Lines 187 192 +5
=============================================
+ Hits 187 191 +4
- Misses 0 1 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
samdark
left a comment
There was a problem hiding this comment.
Changed behavior is not covered with tests.
Overall, I think the fix should be in Session::open() so anonymous requests are not producing new sessions.
| if ($requestSessionId !== null && $this->session->getId() === null) { | ||
| $this->session->setId($requestSessionId); | ||
| if ($this->session->getId() === null) { | ||
| $this->session->setId($requestSessionId ?? $this->session->createId()); |
There was a problem hiding this comment.
That causes creating a new empty session for every anonymous request, which is very wasteful. Also, Set-Cookie is sent on every response, so it's impossible to cache static anonymous pages.
There was a problem hiding this comment.
Also, it will likely cause a fatal error in commitSession() for HTTP-requests (not HTTPS):
- Session ID is always assigned now, so
$currentSessionId === nullis never true. - It will try to build a cookie and if that's 2nd request, the error will be
"cookie_secure" is on but connection is not secure...since secure cookie is default.
There was a problem hiding this comment.
2. It will try to build a cookie and if that's 2nd request, the error will be
"cookie_secure" is on but connection is not secure...since secure cookie is default.
When I set 'options' => 'cookie_secure' => 1, I get an error on the first request. However, I get the same error with the existing code:
if ($requestSessionId !== null && $this->session->getId() === null) {
$this->session->setId($requestSessionId);
}There was a problem hiding this comment.
That causes creating a new empty session for every anonymous request, which is very wasteful. Also, Set-Cookie is sent on every response, so it's impossible to cache static anonymous pages.
I don't understand the point of the comment, since the current version of the yiisoft/session package also sends Set-Cookie with an empty PHPSESSID cookie on every request.
There was a problem hiding this comment.
- Session ID is always assigned now, so
$currentSessionId === nullis never true.
I think it makes sense to replace the createId() call within the Session::open() method.
Replace this code:
if ($this->sessionId !== null) {
session_id($this->sessionId);
}With:
session_id($this->sessionId ?? $this->createId());What do you think?
Yes, the issue occurred in $this->sessionId = session_id(); |
To prevent the same session ID being received for both Swoole and Road Runner, add the
createId()method ofSessionInterfaceand then use it inSessionMiddlewarewhenrequestSessionIdisnull