Skip to content
Merged
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion WebFiori/Framework/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
71 changes: 71 additions & 0 deletions tests/WebFiori/Framework/Tests/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Loading