diff --git a/docs/changelogs/v4.8.0.md b/docs/changelogs/v4.8.0.md index e397ab146b2f..0c13807ca322 100644 --- a/docs/changelogs/v4.8.0.md +++ b/docs/changelogs/v4.8.0.md @@ -396,6 +396,7 @@ Release Date: Unreleased - **Config:** Fixed a bug where instantiating an anonymous `BaseConfig` subclass threw a `ValueError` from `getenv()` on PHP 8.6. - **Debug:** Fixed a bug where the `spl_object_hash()` deprecation raised by `mikey179/vfsstream` and `kint-php/kint` on PHP 8.6 was thrown as an exception. It is now logged as a deprecation. +- **Session:** Fixed a bug where session handlers failed to load on PHP 8.6 for lack of `create_sid()` and `validateId()`. - **Testing:** Fixed a bug where `ReflectionHelper::getPrivateMethodInvoker()` passed an object when invoking a static method, which is deprecated in PHP 8.6. See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed. diff --git a/system/Session/Handlers/BaseHandler.php b/system/Session/Handlers/BaseHandler.php index b518edde16f3..5c66af5d2513 100644 --- a/system/Session/Handlers/BaseHandler.php +++ b/system/Session/Handlers/BaseHandler.php @@ -114,6 +114,8 @@ abstract class BaseHandler implements SessionHandlerInterface */ protected $ipAddress; + private string $createdSid = ''; + public function __construct(SessionConfig $config, string $ipAddress) { // Store Session configurations @@ -132,6 +134,22 @@ public function __construct(SessionConfig $config, string $ipAddress) $this->ipAddress = $ipAddress; } + /** + * Generates a session ID of 32 hexadecimal characters, as enforced by `Session::configure()`. + */ + public function create_sid(): string + { + return $this->createdSid = bin2hex(random_bytes(16)); + } + + /** + * Reports the ID of the last `create_sid()` call as unused and accepts every other ID. + */ + public function validateId(string $id): bool + { + return $id !== $this->createdSid; + } + /** * Internal method to force removal of a cookie by the client * when session_destroy() is called. diff --git a/tests/system/Session/Handlers/BaseHandlerTest.php b/tests/system/Session/Handlers/BaseHandlerTest.php index 05a838dd9ece..9429b3941983 100644 --- a/tests/system/Session/Handlers/BaseHandlerTest.php +++ b/tests/system/Session/Handlers/BaseHandlerTest.php @@ -131,6 +131,34 @@ public function gc(int $maxLifetime): int }; } + public function testCreateSidReturnsUniqueThirtyTwoHexCharacters(): void + { + $handler = $this->createHandler(); + + $first = $handler->create_sid(); + $second = $handler->create_sid(); + + $this->assertMatchesRegularExpression('/\A[0-9a-f]{32}\z/', $first); + $this->assertMatchesRegularExpression('/\A[0-9a-f]{32}\z/', $second); + $this->assertNotSame($first, $second); + } + + public function testValidateIdReportsOnlyTheCreatedSidAsUnused(): void + { + $handler = $this->createHandler(); + + $created = $handler->create_sid(); + + $this->assertFalse($handler->validateId($created)); + $this->assertTrue($handler->validateId(str_repeat('ab', 16))); + $this->assertTrue($handler->validateId('invalid-session-id!')); + + $newer = $handler->create_sid(); + + $this->assertFalse($handler->validateId($newer)); + $this->assertTrue($handler->validateId($created)); + } + public function testConstructorInitializesCookieSameSiteFromConfig(): void { $cookieConfig = new CookieConfig();