From 971c6627877d3955e76fb9c9c38dc0f33e25e144 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Sun, 27 Sep 2026 17:47:19 +0300 Subject: [PATCH] fix(session): migrate all per-key state on reGenerateID() reGenerateID() only changed the session cookie value and left every per-key row (application keys, _meta, _user) orphaned under the old ID. In the session-fixation login flow (reGenerateID() then setUser() with no close()), the new ID ended up with only _user; on the next request start() saw no _meta, treated the new ID as brand-new, and lost the user and all session data. reGenerateID() now migrates the full session state to the new ID: snapshot all rows under the old ID (decrypting with the old ID; _meta is a plain array copied as-is), switch the cookie to the new ID, re-write every key re-encrypted under the new ID (encryption is keyed by session ID), then destroy the old ID's rows. Adds a regression test covering the user and multiple custom keys surviving reGenerateID() without an intervening close(). --- WebFiori/Framework/Session/Session.php | 40 ++++++++++- .../Framework/Tests/Session/SessionTest.php | 71 +++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/WebFiori/Framework/Session/Session.php b/WebFiori/Framework/Session/Session.php index a3f2d1b47..9c98b9aae 100644 --- a/WebFiori/Framework/Session/Session.php +++ b/WebFiori/Framework/Session/Session.php @@ -622,13 +622,51 @@ public function refresh(): void { /** * Re-create session ID. * + * All existing per-key state (application keys, the reserved '_meta' and + * '_user' keys) is migrated from the old session ID to the new one, so a + * session-fixation ID regeneration during a request never orphans data. + * Values are re-encrypted under the new ID when SESSION_KEY is defined, + * because the encryption key is derived from the session ID. + * * @return string The new ID of the session. * */ public function reGenerateID() : string { + $oldId = $this->getId(); + + // Snapshot everything stored under the old ID, decrypting values with + // the OLD id (decryptValue()/getId() still returns the old id here). + // '_meta' is stored as a plain array (not encrypted), so copy it as-is. + $storage = SessionsManager::getStorage(); + $migrated = []; + + foreach ($storage->readAll($oldId) as $key => $entry) { + if ($key === '_meta') { + $migrated[$key] = $entry['value']; + } else { + $migrated[$key] = $this->decryptValue($entry['value']); + } + } + + // Switch to the new ID (cookie value). From here getId() is the new ID. $this->getCookie()->setValue($this->generateSessionID($this->getName())); + $newId = $this->getCookie()->getValue(); - return $this->getCookie()->getValue(); + // Re-write every key under the new ID, re-encrypting with the new ID. + foreach ($migrated as $key => $value) { + if ($key === '_meta') { + $storage->write($newId, '_meta', $value, null, ConflictStrategy::LAST_WRITE_WINS); + } else { + $storage->write($newId, $key, $this->encryptValue($value), null, ConflictStrategy::LAST_WRITE_WINS); + } + } + + // Drop the old session's rows so no orphaned/duplicate state remains. + if ($oldId !== $newId) { + $storage->destroy($oldId); + } + + return $newId; } /** * Removes the value of a session variable. diff --git a/tests/WebFiori/Framework/Tests/Session/SessionTest.php b/tests/WebFiori/Framework/Tests/Session/SessionTest.php index a66f8c80d..212f286de 100644 --- a/tests/WebFiori/Framework/Tests/Session/SessionTest.php +++ b/tests/WebFiori/Framework/Tests/Session/SessionTest.php @@ -401,6 +401,77 @@ public function testUserIsPersistedAcrossResume() { $this->assertEquals('Jane D.', $s2->getUser()->getDisplayName()); } + /** + * @test + * + * Regression test for issue #422. + * + * reGenerateID() only changes the session cookie value; it does not + * migrate the existing per-key rows (application keys, '_meta', '_user') + * to the new session ID. The session-fixation login flow regenerates the + * ID and then attaches the user WITHOUT closing the session before the + * request ends (the start-session middleware's afterSend() only calls + * validateStorage()): + * + * $session->set('csrf', 'abc'); // app data under the OLD id + * $session->reGenerateID(); // new id (cookie only) + * $session->setUser($user); // writes _user under the NEW id + * + * As a result, the new ID ends up with only '_user' (written eagerly by + * setUser()) while '_meta', the user's companion state and all previously + * stored application keys remain orphaned under the OLD id. On the next + * request start() reads '_meta' for the new id, finds null, treats it as a + * brand-new session and loses everything. + * + * The whole session state must be carried across a reGenerateID(). This + * test asserts the DESIRED behavior, so it FAILS against current code until + * #422 is fixed. + */ + public function testSessionStateSurvivesRegenerateIdWithoutClose() { + // Request 1: anonymous session with some application data, then login. + $s1 = new Session(['name' => 'regen-test']); + $s1->start(); // writes _meta under the old id + $s1->set('csrf', 'abc123'); // custom app key (old id) + $s1->set('cart', ['apple', 'orange']); // another custom key (old id) + $s1->set('lang_pref', 'ar'); // another custom key (old id) + + $s1->reGenerateID(); // new id (cookie value only) + $newId = $s1->getId(); + + $user = new User('jane.doe', 'secret', 'jane@example.com'); + $user->setID(7); + $s1->setUser($user); // writes _user under the NEW id + // NOTE: intentionally NO close() before the "request" ends. + + // Request 2: resume the NEW id (as the next request would). + $s2 = new Session(['name' => 'regen-test', 'session-id' => $newId]); + $s2->start(); + + // The user must survive. + $this->assertNotNull( + $s2->getUser(), + 'Issue #422: user set after reGenerateID() must survive resume.' + ); + $this->assertEquals(7, $s2->getUser()->getId()); + $this->assertEquals('jane.doe', $s2->getUser()->getUserName()); + + // ALL custom application keys must survive the ID regeneration too. + $this->assertSame('abc123', $s2->get('csrf'), + 'Issue #422: custom key "csrf" must survive reGenerateID().'); + $this->assertSame(['apple', 'orange'], $s2->get('cart'), + 'Issue #422: custom key "cart" must survive reGenerateID().'); + $this->assertSame('ar', $s2->get('lang_pref'), + 'Issue #422: custom key "lang_pref" must survive reGenerateID().'); + + // And they must be readable via getVars() (which excludes reserved keys). + $vars = $s2->getVars(); + $this->assertArrayHasKey('csrf', $vars); + $this->assertArrayHasKey('cart', $vars); + $this->assertArrayHasKey('lang_pref', $vars); + $this->assertArrayNotHasKey('_user', $vars); + $this->assertArrayNotHasKey('_meta', $vars); + } + /** * @test *