Skip to content

Commit

Permalink
Fix issue with session cache
Browse files Browse the repository at this point in the history
Fixes #1932.
  • Loading branch information
lukasbestle authored and bastianallgeier committed Jul 29, 2019
1 parent 87f5825 commit 2ec8bb5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Session/Session.php
Expand Up @@ -469,6 +469,9 @@ public function regenerateToken()
} else {
$this->needsRetransmission = true;
}

// update cache of the Sessions instance with the new token
$this->sessions->updateCache($this);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Session/Sessions.php
Expand Up @@ -242,6 +242,18 @@ public function collectGarbage()
$this->store()->collectGarbage();
}

/**
* Updates the instance cache with a newly created
* session or a session with a regenerated token
*
* @internal
* @param Kirby\Session\Session $session Session instance to push to the cache
*/
public function updateCache(Session $session)
{
$this->cache[$session->token()] = $session;
}

/**
* Returns the auth token from the cookie
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Session/SessionTest.php
Expand Up @@ -676,6 +676,10 @@ public function testRenewNotRenewable()
*/
public function testRegenerateToken()
{
$sessionsReflector = new ReflectionClass(Sessions::class);
$cache = $sessionsReflector->getProperty('cache');
$cache->setAccessible(true);

$token = '9999999999.valid.' . $this->store->validKey;
$session = new Session($this->sessions, $token, []);

Expand All @@ -702,6 +706,10 @@ public function testRegenerateToken()

// validate that a cookie has been set
$this->assertEquals($newToken, Cookie::get('kirby_session'));

// validate that the new session is cached in the $sessions object
$this->assertArrayHasKey($newToken, $cache->getValue($this->sessions));
$this->assertEquals($session, $cache->getValue($this->sessions)[$newToken]);
}

/**
Expand All @@ -710,14 +718,23 @@ public function testRegenerateToken()
*/
public function testRegenerateTokenHeaderMode()
{
$sessionsReflector = new ReflectionClass(Sessions::class);
$cache = $sessionsReflector->getProperty('cache');
$cache->setAccessible(true);

$token = '9999999999.valid.' . $this->store->validKey;
$session = new Session($this->sessions, $token, ['mode' => 'header']);

Cookie::remove('kirby_session');
$this->assertFalse($session->needsRetransmission());
$session->regenerateToken();
$this->assertNotEquals($token, $newToken = $session->token());
$this->assertTrue($session->needsRetransmission());
$this->assertNull(Cookie::get('kirby_session'));

// validate that the new session is cached in the $sessions object
$this->assertArrayHasKey($newToken, $cache->getValue($this->sessions));
$this->assertEquals($session, $cache->getValue($this->sessions)[$newToken]);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/Session/SessionsTest.php
Expand Up @@ -275,6 +275,29 @@ public function testCollectGarbage()
$this->assertTrue($this->store->collectedGarbage);
}

/**
* @covers ::updateCache
*/
public function testUpdateCache()
{
$sessionsReflector = new ReflectionClass(Sessions::class);
$cache = $sessionsReflector->getProperty('cache');
$cache->setAccessible(true);

$sessionReflector = new ReflectionClass(Session::class);
$tokenKey = $sessionReflector->getProperty('tokenKey');
$tokenKey->setAccessible(true);

$sessions = new Sessions($this->store, ['mode' => 'header']);
$session = $sessions->get('9999999999.valid.' . $this->store->validKey);
$tokenKey->setValue($session, 'new-key');

$this->assertArrayNotHasKey('9999999999.valid.new-key', $cache->getValue($sessions));
$sessions->updateCache($session);
$this->assertArrayHasKey('9999999999.valid.new-key', $cache->getValue($sessions));
$this->assertEquals($session, $cache->getValue($sessions)['9999999999.valid.new-key']);
}

/**
* @covers ::tokenFromCookie
*/
Expand Down

0 comments on commit 2ec8bb5

Please sign in to comment.