fix(http): sync context sid/session after sessionRegenerateId()#1135
Merged
walkor merged 1 commit intowalkor:masterfrom Mar 20, 2026
Merged
Conversation
Update $this->context['sid'] and $this->context['session'] when rotating the session id so session() and sessionId() stay consistent with the new Session instance and Set-Cookie within the same request. Made-with: Cursor
Owner
|
已经合并,说明清晰、逻辑完整,非常棒的PR |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
问题
调用
sessionRegenerateId()时,虽然已通过setSidCookie()写入新的会话 ID,并构造了带迁移数据的Session($newSid),但$this->context['sid']与$this->context['session']未更新。由于:
session()使用$this->context['session'] ??= new Session($this->sessionId()),会长期缓存首次创建的Session实例;sessionId()在context['sid']已存在时直接返回缓存值;导致同一次请求内再次调用
session()/sessionId()仍指向旧的会话 ID 与Session对象,与响应头中的新 Cookie 不一致,可能造成读写错会话或表现为「读不到 session 数据」。修改
在
setSidCookie()之后补充:$this->context['sid'] = $newSid;$this->context['session'] = $session;使内部缓存与新生成的会话及 Cookie 保持一致。