2017-10-22 18:44:46 +02:00
|
|
|
<?php
|
2017-11-08 20:24:49 +01:00
|
|
|
|
2020-07-07 10:15:56 +02:00
|
|
|
namespace Shaarli\Security;
|
2017-10-22 18:44:46 +02:00
|
|
|
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2017-10-22 18:44:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test coverage for SessionManager
|
|
|
|
*/
|
|
|
|
class SessionManagerTest extends TestCase
|
|
|
|
{
|
2018-04-27 23:17:38 +02:00
|
|
|
/** @var array Session ID hashes */
|
2017-10-22 19:54:44 +02:00
|
|
|
protected static $sidHashes = null;
|
|
|
|
|
2018-05-06 17:12:48 +02:00
|
|
|
/** @var \FakeConfigManager ConfigManager substitute for testing */
|
2018-04-27 23:17:38 +02:00
|
|
|
protected $conf = null;
|
|
|
|
|
|
|
|
/** @var array $_SESSION array for testing */
|
|
|
|
protected $session = [];
|
|
|
|
|
|
|
|
/** @var SessionManager Server-side session management abstraction */
|
|
|
|
protected $sessionManager = null;
|
2017-11-08 20:24:49 +01:00
|
|
|
|
2017-10-22 19:54:44 +02:00
|
|
|
/**
|
|
|
|
* Assign reference data
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
2017-10-22 19:54:44 +02:00
|
|
|
{
|
2020-07-07 10:15:56 +02:00
|
|
|
self::$sidHashes = \ReferenceSessionIdHashes::getHashes();
|
2018-04-27 23:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize or reset test resources
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2018-04-27 23:17:38 +02:00
|
|
|
{
|
2020-07-07 10:15:56 +02:00
|
|
|
$this->conf = new \FakeConfigManager([
|
2018-04-27 23:17:38 +02:00
|
|
|
'credentials.login' => 'johndoe',
|
|
|
|
'credentials.salt' => 'salt',
|
|
|
|
'security.session_protection_disabled' => false,
|
|
|
|
]);
|
|
|
|
$this->session = [];
|
2020-07-07 10:15:56 +02:00
|
|
|
$this->sessionManager = new SessionManager($this->session, $this->conf, 'session_path');
|
2017-10-22 19:54:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 18:44:46 +02:00
|
|
|
/**
|
|
|
|
* Generate a session token
|
|
|
|
*/
|
|
|
|
public function testGenerateToken()
|
|
|
|
{
|
2018-04-27 23:17:38 +02:00
|
|
|
$token = $this->sessionManager->generateToken();
|
2017-10-22 18:44:46 +02:00
|
|
|
|
2018-04-27 23:17:38 +02:00
|
|
|
$this->assertEquals(1, $this->session['tokens'][$token]);
|
2017-10-22 18:44:46 +02:00
|
|
|
$this->assertEquals(40, strlen($token));
|
|
|
|
}
|
|
|
|
|
2017-10-24 22:01:02 +02:00
|
|
|
/**
|
|
|
|
* Check a session token
|
|
|
|
*/
|
|
|
|
public function testCheckToken()
|
|
|
|
{
|
|
|
|
$token = '4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b';
|
|
|
|
$session = [
|
|
|
|
'tokens' => [
|
|
|
|
$token => 1,
|
|
|
|
],
|
|
|
|
];
|
2020-07-07 10:15:56 +02:00
|
|
|
$sessionManager = new SessionManager($session, $this->conf, 'session_path');
|
2017-10-24 22:01:02 +02:00
|
|
|
|
|
|
|
// check and destroy the token
|
|
|
|
$this->assertTrue($sessionManager->checkToken($token));
|
|
|
|
$this->assertFalse(isset($session['tokens'][$token]));
|
|
|
|
|
|
|
|
// ensure the token has been destroyed
|
|
|
|
$this->assertFalse($sessionManager->checkToken($token));
|
|
|
|
}
|
|
|
|
|
2017-10-22 18:44:46 +02:00
|
|
|
/**
|
|
|
|
* Generate and check a session token
|
|
|
|
*/
|
|
|
|
public function testGenerateAndCheckToken()
|
|
|
|
{
|
2018-04-27 23:17:38 +02:00
|
|
|
$token = $this->sessionManager->generateToken();
|
2017-10-22 18:44:46 +02:00
|
|
|
|
|
|
|
// ensure a token has been generated
|
2018-04-27 23:17:38 +02:00
|
|
|
$this->assertEquals(1, $this->session['tokens'][$token]);
|
2017-10-22 18:44:46 +02:00
|
|
|
$this->assertEquals(40, strlen($token));
|
|
|
|
|
|
|
|
// check and destroy the token
|
2018-04-27 23:17:38 +02:00
|
|
|
$this->assertTrue($this->sessionManager->checkToken($token));
|
|
|
|
$this->assertFalse(isset($this->session['tokens'][$token]));
|
2017-10-22 18:44:46 +02:00
|
|
|
|
|
|
|
// ensure the token has been destroyed
|
2018-04-27 23:17:38 +02:00
|
|
|
$this->assertFalse($this->sessionManager->checkToken($token));
|
2017-10-22 18:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check an invalid session token
|
|
|
|
*/
|
|
|
|
public function testCheckInvalidToken()
|
|
|
|
{
|
2018-04-27 23:17:38 +02:00
|
|
|
$this->assertFalse($this->sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b'));
|
2017-10-22 18:44:46 +02:00
|
|
|
}
|
2017-10-22 19:54:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test SessionManager::checkId with a valid ID - TEST ALL THE HASHES!
|
|
|
|
*
|
|
|
|
* This tests extensively covers all hash algorithms / bit representations
|
|
|
|
*/
|
|
|
|
public function testIsAnyHashSessionIdValid()
|
|
|
|
{
|
|
|
|
foreach (self::$sidHashes as $algo => $bpcs) {
|
|
|
|
foreach ($bpcs as $bpc => $hash) {
|
|
|
|
$this->assertTrue(SessionManager::checkId($hash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkId with a valid ID - SHA-1 hashes
|
|
|
|
*/
|
|
|
|
public function testIsSha1SessionIdValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue(SessionManager::checkId(sha1('shaarli')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkId with a valid ID - SHA-256 hashes
|
|
|
|
*/
|
|
|
|
public function testIsSha256SessionIdValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue(SessionManager::checkId(hash('sha256', 'shaarli')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkId with a valid ID - SHA-512 hashes
|
|
|
|
*/
|
|
|
|
public function testIsSha512SessionIdValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue(SessionManager::checkId(hash('sha512', 'shaarli')));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkId with invalid IDs.
|
|
|
|
*/
|
|
|
|
public function testIsSessionIdInvalid()
|
|
|
|
{
|
|
|
|
$this->assertFalse(SessionManager::checkId(''));
|
|
|
|
$this->assertFalse(SessionManager::checkId([]));
|
|
|
|
$this->assertFalse(
|
|
|
|
SessionManager::checkId('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
|
|
|
|
);
|
|
|
|
}
|
2018-04-27 23:17:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store login information after a successful login
|
|
|
|
*/
|
|
|
|
public function testStoreLoginInfo()
|
|
|
|
{
|
|
|
|
$this->sessionManager->storeLoginInfo('ip_id');
|
|
|
|
|
|
|
|
$this->assertGreaterThan(time(), $this->session['expires_on']);
|
|
|
|
$this->assertEquals('ip_id', $this->session['ip']);
|
|
|
|
$this->assertEquals('johndoe', $this->session['username']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend a server-side session by SessionManager::$SHORT_TIMEOUT
|
|
|
|
*/
|
|
|
|
public function testExtendSession()
|
|
|
|
{
|
|
|
|
$this->sessionManager->extendSession();
|
|
|
|
|
|
|
|
$this->assertGreaterThan(time(), $this->session['expires_on']);
|
|
|
|
$this->assertLessThanOrEqual(
|
|
|
|
time() + SessionManager::$SHORT_TIMEOUT,
|
|
|
|
$this->session['expires_on']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend a server-side session by SessionManager::$LONG_TIMEOUT
|
|
|
|
*/
|
|
|
|
public function testExtendSessionStaySignedIn()
|
|
|
|
{
|
|
|
|
$this->sessionManager->setStaySignedIn(true);
|
|
|
|
$this->sessionManager->extendSession();
|
|
|
|
|
|
|
|
$this->assertGreaterThan(time(), $this->session['expires_on']);
|
|
|
|
$this->assertGreaterThan(
|
|
|
|
time() + SessionManager::$LONG_TIMEOUT - 10,
|
|
|
|
$this->session['expires_on']
|
|
|
|
);
|
|
|
|
$this->assertLessThanOrEqual(
|
|
|
|
time() + SessionManager::$LONG_TIMEOUT,
|
|
|
|
$this->session['expires_on']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset session variables after logging out
|
|
|
|
*/
|
|
|
|
public function testLogout()
|
|
|
|
{
|
|
|
|
$this->session = [
|
|
|
|
'ip' => 'ip_id',
|
|
|
|
'expires_on' => time() + 1000,
|
|
|
|
'username' => 'johndoe',
|
|
|
|
'visibility' => 'public',
|
2020-08-31 13:58:09 +02:00
|
|
|
'untaggedonly' => true,
|
2018-04-27 23:17:38 +02:00
|
|
|
];
|
|
|
|
$this->sessionManager->logout();
|
|
|
|
|
2020-08-31 13:58:09 +02:00
|
|
|
$this->assertArrayNotHasKey('ip', $this->session);
|
|
|
|
$this->assertArrayNotHasKey('expires_on', $this->session);
|
|
|
|
$this->assertArrayNotHasKey('username', $this->session);
|
|
|
|
$this->assertArrayNotHasKey('visibility', $this->session);
|
|
|
|
$this->assertArrayHasKey('untaggedonly', $this->session);
|
|
|
|
$this->assertTrue($this->session['untaggedonly']);
|
2018-04-27 23:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The session is active and expiration time has been reached
|
|
|
|
*/
|
|
|
|
public function testHasExpiredTimeElapsed()
|
|
|
|
{
|
|
|
|
$this->session['expires_on'] = time() - 10;
|
|
|
|
|
|
|
|
$this->assertTrue($this->sessionManager->hasSessionExpired());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The session is active and expiration time has not been reached
|
|
|
|
*/
|
|
|
|
public function testHasNotExpired()
|
|
|
|
{
|
|
|
|
$this->session['expires_on'] = time() + 1000;
|
|
|
|
|
|
|
|
$this->assertFalse($this->sessionManager->hasSessionExpired());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session hijacking protection is disabled, we assume the IP has not changed
|
|
|
|
*/
|
|
|
|
public function testHasClientIpChangedNoSessionProtection()
|
|
|
|
{
|
|
|
|
$this->conf->set('security.session_protection_disabled', true);
|
|
|
|
|
|
|
|
$this->assertFalse($this->sessionManager->hasClientIpChanged(''));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The client IP identifier has not changed
|
|
|
|
*/
|
|
|
|
public function testHasClientIpChangedNope()
|
|
|
|
{
|
|
|
|
$this->session['ip'] = 'ip_id';
|
|
|
|
$this->assertFalse($this->sessionManager->hasClientIpChanged('ip_id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The client IP identifier has changed
|
|
|
|
*/
|
|
|
|
public function testHasClientIpChanged()
|
|
|
|
{
|
|
|
|
$this->session['ip'] = 'ip_id_one';
|
|
|
|
$this->assertTrue($this->sessionManager->hasClientIpChanged('ip_id_two'));
|
|
|
|
}
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creating an entry in the session array
|
|
|
|
*/
|
|
|
|
public function testSetSessionParameterCreate(): void
|
|
|
|
{
|
|
|
|
$this->sessionManager->setSessionParameter('abc', 'def');
|
|
|
|
|
|
|
|
static::assertSame('def', $this->session['abc']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating an entry in the session array
|
|
|
|
*/
|
|
|
|
public function testSetSessionParameterUpdate(): void
|
|
|
|
{
|
|
|
|
$this->session['abc'] = 'ghi';
|
|
|
|
|
|
|
|
$this->sessionManager->setSessionParameter('abc', 'def');
|
|
|
|
|
|
|
|
static::assertSame('def', $this->session['abc']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating an entry in the session array with null value
|
|
|
|
*/
|
|
|
|
public function testSetSessionParameterUpdateNull(): void
|
|
|
|
{
|
|
|
|
$this->session['abc'] = 'ghi';
|
|
|
|
|
|
|
|
$this->sessionManager->setSessionParameter('abc', null);
|
|
|
|
|
|
|
|
static::assertArrayHasKey('abc', $this->session);
|
|
|
|
static::assertNull($this->session['abc']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deleting an existing entry in the session array
|
|
|
|
*/
|
|
|
|
public function testDeleteSessionParameter(): void
|
|
|
|
{
|
|
|
|
$this->session['abc'] = 'def';
|
|
|
|
|
|
|
|
$this->sessionManager->deleteSessionParameter('abc');
|
|
|
|
|
|
|
|
static::assertArrayNotHasKey('abc', $this->session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deleting a non existent entry in the session array
|
|
|
|
*/
|
|
|
|
public function testDeleteSessionParameterNotExisting(): void
|
|
|
|
{
|
|
|
|
$this->sessionManager->deleteSessionParameter('abc');
|
|
|
|
|
|
|
|
static::assertArrayNotHasKey('abc', $this->session);
|
|
|
|
}
|
2017-10-22 18:44:46 +02:00
|
|
|
}
|