Session ID: extend the regex to match possible hash representations
Improves #306 Relates to #335 & #336 Duplicated by #339 Issues: - PHP regenerates the session ID if it is not compliant - the regex checking the session ID does not cover all cases - different algorithms: md5, sha1, sha256, etc. - bit representations: 4, 5, 6 Fix: - `index.php`: - remove `uniqid()` usage - call `session_regenerate_id()` if an invalid cookie is detected - regex: support all possible characters - '[a-zA-Z,-]{2,128}' - tests: add coverage for all algorithms & bit representations See: - http://php.net/manual/en/session.configuration.php#ini.session.hash-function - https://secure.php.net/manual/en/session.configuration.php#ini.session.hash-bits-per-character - http://php.net/manual/en/function.session-id.php - http://php.net/manual/en/function.session-regenerate-id.php - http://php.net/manual/en/function.hash-algos.php Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
parent
a02257b8ae
commit
68bc21353a
4 changed files with 119 additions and 9 deletions
tests
|
@ -4,12 +4,28 @@
|
|||
*/
|
||||
|
||||
require_once 'application/Utils.php';
|
||||
require_once 'tests/utils/ReferenceSessionIdHashes.php';
|
||||
|
||||
// Initialize reference data before PHPUnit starts a session
|
||||
ReferenceSessionIdHashes::genAllHashes();
|
||||
|
||||
|
||||
/**
|
||||
* Unitary tests for Shaarli utilities
|
||||
*/
|
||||
class UtilsTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
// Session ID hashes
|
||||
protected static $sidHashes = null;
|
||||
|
||||
/**
|
||||
* Assign reference data
|
||||
*/
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::$sidHashes = ReferenceSessionIdHashes::getHashes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a link by its hash
|
||||
*/
|
||||
|
@ -152,11 +168,41 @@ class UtilsTest extends PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* Test is_session_id_valid with a valid ID.
|
||||
* Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
|
||||
*
|
||||
* This tests extensively covers all hash algorithms / bit representations
|
||||
*/
|
||||
public function testIsSessionIdValid()
|
||||
public function testIsAnyHashSessionIdValid()
|
||||
{
|
||||
$this->assertTrue(is_session_id_valid('azertyuiop123456789AZERTYUIOP1aA'));
|
||||
foreach (self::$sidHashes as $algo => $bpcs) {
|
||||
foreach ($bpcs as $bpc => $hash) {
|
||||
$this->assertTrue(is_session_id_valid($hash));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is_session_id_valid with a valid ID - SHA-1 hashes
|
||||
*/
|
||||
public function testIsSha1SessionIdValid()
|
||||
{
|
||||
$this->assertTrue(is_session_id_valid(sha1('shaarli')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is_session_id_valid with a valid ID - SHA-256 hashes
|
||||
*/
|
||||
public function testIsSha256SessionIdValid()
|
||||
{
|
||||
$this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is_session_id_valid with a valid ID - SHA-512 hashes
|
||||
*/
|
||||
public function testIsSha512SessionIdValid()
|
||||
{
|
||||
$this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -166,6 +212,8 @@ class UtilsTest extends PHPUnit_Framework_TestCase
|
|||
{
|
||||
$this->assertFalse(is_session_id_valid(''));
|
||||
$this->assertFalse(is_session_id_valid(array()));
|
||||
$this->assertFalse(is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI='));
|
||||
$this->assertFalse(
|
||||
is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue