From fab87c2696b9d6a26310f1bfc024b018ca5184fe Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Fri, 27 Apr 2018 22:12:22 +0200 Subject: Move LoginManager and SessionManager to the Security namespace Signed-off-by: VirtualTam --- tests/security/SessionManagerTest.php | 149 ++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 tests/security/SessionManagerTest.php (limited to 'tests/security/SessionManagerTest.php') diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php new file mode 100644 index 00000000..e4e1cfbc --- /dev/null +++ b/tests/security/SessionManagerTest.php @@ -0,0 +1,149 @@ +generateToken(); + + $this->assertEquals(1, $session['tokens'][$token]); + $this->assertEquals(40, strlen($token)); + } + + /** + * Check a session token + */ + public function testCheckToken() + { + $token = '4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b'; + $session = [ + 'tokens' => [ + $token => 1, + ], + ]; + $sessionManager = new SessionManager($session, self::$conf); + + // 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)); + } + + /** + * Generate and check a session token + */ + public function testGenerateAndCheckToken() + { + $session = []; + $sessionManager = new SessionManager($session, self::$conf); + + $token = $sessionManager->generateToken(); + + // ensure a token has been generated + $this->assertEquals(1, $session['tokens'][$token]); + $this->assertEquals(40, strlen($token)); + + // 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)); + } + + /** + * Check an invalid session token + */ + public function testCheckInvalidToken() + { + $session = []; + $sessionManager = new SessionManager($session, self::$conf); + + $this->assertFalse($sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b')); + } + + /** + * 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=') + ); + } +} -- cgit v1.2.3 From 51f0128cdba52099c40693379e72f094b42a6f80 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Fri, 27 Apr 2018 23:17:38 +0200 Subject: Refactor session and cookie timeout control Signed-off-by: VirtualTam --- tests/security/SessionManagerTest.php | 181 +++++++++++++++++++++++++++++----- 1 file changed, 159 insertions(+), 22 deletions(-) (limited to 'tests/security/SessionManagerTest.php') diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php index e4e1cfbc..e1c72707 100644 --- a/tests/security/SessionManagerTest.php +++ b/tests/security/SessionManagerTest.php @@ -14,11 +14,17 @@ use \PHPUnit\Framework\TestCase; */ class SessionManagerTest extends TestCase { - // Session ID hashes + /** @var array Session ID hashes */ protected static $sidHashes = null; - // Fake ConfigManager - protected static $conf = null; + /** @var FakeConfigManager ConfigManager substitute for testing */ + protected $conf = null; + + /** @var array $_SESSION array for testing */ + protected $session = []; + + /** @var SessionManager Server-side session management abstraction */ + protected $sessionManager = null; /** * Assign reference data @@ -26,7 +32,20 @@ class SessionManagerTest extends TestCase public static function setUpBeforeClass() { self::$sidHashes = ReferenceSessionIdHashes::getHashes(); - self::$conf = new FakeConfigManager(); + } + + /** + * Initialize or reset test resources + */ + public function setUp() + { + $this->conf = new FakeConfigManager([ + 'credentials.login' => 'johndoe', + 'credentials.salt' => 'salt', + 'security.session_protection_disabled' => false, + ]); + $this->session = []; + $this->sessionManager = new SessionManager($this->session, $this->conf); } /** @@ -34,12 +53,9 @@ class SessionManagerTest extends TestCase */ public function testGenerateToken() { - $session = []; - $sessionManager = new SessionManager($session, self::$conf); - - $token = $sessionManager->generateToken(); + $token = $this->sessionManager->generateToken(); - $this->assertEquals(1, $session['tokens'][$token]); + $this->assertEquals(1, $this->session['tokens'][$token]); $this->assertEquals(40, strlen($token)); } @@ -54,7 +70,7 @@ class SessionManagerTest extends TestCase $token => 1, ], ]; - $sessionManager = new SessionManager($session, self::$conf); + $sessionManager = new SessionManager($session, $this->conf); // check and destroy the token $this->assertTrue($sessionManager->checkToken($token)); @@ -69,21 +85,18 @@ class SessionManagerTest extends TestCase */ public function testGenerateAndCheckToken() { - $session = []; - $sessionManager = new SessionManager($session, self::$conf); - - $token = $sessionManager->generateToken(); + $token = $this->sessionManager->generateToken(); // ensure a token has been generated - $this->assertEquals(1, $session['tokens'][$token]); + $this->assertEquals(1, $this->session['tokens'][$token]); $this->assertEquals(40, strlen($token)); // check and destroy the token - $this->assertTrue($sessionManager->checkToken($token)); - $this->assertFalse(isset($session['tokens'][$token])); + $this->assertTrue($this->sessionManager->checkToken($token)); + $this->assertFalse(isset($this->session['tokens'][$token])); // ensure the token has been destroyed - $this->assertFalse($sessionManager->checkToken($token)); + $this->assertFalse($this->sessionManager->checkToken($token)); } /** @@ -91,10 +104,7 @@ class SessionManagerTest extends TestCase */ public function testCheckInvalidToken() { - $session = []; - $sessionManager = new SessionManager($session, self::$conf); - - $this->assertFalse($sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b')); + $this->assertFalse($this->sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b')); } /** @@ -146,4 +156,131 @@ class SessionManagerTest extends TestCase SessionManager::checkId('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') ); } + + /** + * Store login information after a successful login + */ + public function testStoreLoginInfo() + { + $this->sessionManager->storeLoginInfo('ip_id'); + + $this->assertTrue(isset($this->session['uid'])); + $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 = [ + 'uid' => 'some-uid', + 'ip' => 'ip_id', + 'expires_on' => time() + 1000, + 'username' => 'johndoe', + 'visibility' => 'public', + 'untaggedonly' => false, + ]; + $this->sessionManager->logout(); + + $this->assertFalse(isset($this->session['uid'])); + $this->assertFalse(isset($this->session['ip'])); + $this->assertFalse(isset($this->session['expires_on'])); + $this->assertFalse(isset($this->session['username'])); + $this->assertFalse(isset($this->session['visibility'])); + $this->assertFalse(isset($this->session['untaggedonly'])); + } + + /** + * The session is considered as expired because the UID is missing + */ + public function testHasExpiredNoUid() + { + $this->assertTrue($this->sessionManager->hasSessionExpired()); + } + + /** + * The session is active and expiration time has been reached + */ + public function testHasExpiredTimeElapsed() + { + $this->session['uid'] = 'some-uid'; + $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['uid'] = 'some-uid'; + $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')); + } } -- cgit v1.2.3 From ebf615173824a46de82fa97a165bcfd883db15ce Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 10 May 2018 13:07:51 +0200 Subject: SessionManager: remove unused UID token MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There already are dedicated tokens for: - CSRF protection - user stay-signed-in feature, via cookie This token was most likely intended as a randomly generated, server-side, secret key to be used when generating hashes. See http://sebsauvage.net/wiki/doku.php?id=php:session [FR] Relevant section: Une clé secrète unique aléatoire est générée côté serveur (et jamais envoyée). Elle peut servir pour signer les formulaires (HMAC) ou générer des token de formulaires (protection contre XSRF). Voir $_SESSION['uid']. Translation: A unique, server-side secret key is randomly generated (and never transmitted). It can be used to sign forms (HMAC) or generate form tokens (protection against XSRF). See $_SESSION['uid'] Signed-off-by: VirtualTam --- tests/security/SessionManagerTest.php | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'tests/security/SessionManagerTest.php') diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php index e1c72707..ae10ffa6 100644 --- a/tests/security/SessionManagerTest.php +++ b/tests/security/SessionManagerTest.php @@ -164,7 +164,6 @@ class SessionManagerTest extends TestCase { $this->sessionManager->storeLoginInfo('ip_id'); - $this->assertTrue(isset($this->session['uid'])); $this->assertGreaterThan(time(), $this->session['expires_on']); $this->assertEquals('ip_id', $this->session['ip']); $this->assertEquals('johndoe', $this->session['username']); @@ -209,7 +208,6 @@ class SessionManagerTest extends TestCase public function testLogout() { $this->session = [ - 'uid' => 'some-uid', 'ip' => 'ip_id', 'expires_on' => time() + 1000, 'username' => 'johndoe', @@ -218,7 +216,6 @@ class SessionManagerTest extends TestCase ]; $this->sessionManager->logout(); - $this->assertFalse(isset($this->session['uid'])); $this->assertFalse(isset($this->session['ip'])); $this->assertFalse(isset($this->session['expires_on'])); $this->assertFalse(isset($this->session['username'])); @@ -226,20 +223,11 @@ class SessionManagerTest extends TestCase $this->assertFalse(isset($this->session['untaggedonly'])); } - /** - * The session is considered as expired because the UID is missing - */ - public function testHasExpiredNoUid() - { - $this->assertTrue($this->sessionManager->hasSessionExpired()); - } - /** * The session is active and expiration time has been reached */ public function testHasExpiredTimeElapsed() { - $this->session['uid'] = 'some-uid'; $this->session['expires_on'] = time() - 10; $this->assertTrue($this->sessionManager->hasSessionExpired()); @@ -250,7 +238,6 @@ class SessionManagerTest extends TestCase */ public function testHasNotExpired() { - $this->session['uid'] = 'some-uid'; $this->session['expires_on'] = time() + 1000; $this->assertFalse($this->sessionManager->hasSessionExpired()); -- cgit v1.2.3 From 704637bfebc73ada4b800b35c457e9fe56ad3567 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sun, 6 May 2018 17:12:48 +0200 Subject: Add test coverage for LoginManager methods Signed-off-by: VirtualTam --- tests/security/SessionManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/security/SessionManagerTest.php') diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php index ae10ffa6..9bd868f8 100644 --- a/tests/security/SessionManagerTest.php +++ b/tests/security/SessionManagerTest.php @@ -17,7 +17,7 @@ class SessionManagerTest extends TestCase /** @var array Session ID hashes */ protected static $sidHashes = null; - /** @var FakeConfigManager ConfigManager substitute for testing */ + /** @var \FakeConfigManager ConfigManager substitute for testing */ protected $conf = null; /** @var array $_SESSION array for testing */ -- cgit v1.2.3