Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 18 additions & 0 deletions system/Session/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ abstract class BaseHandler implements SessionHandlerInterface
*/
protected $ipAddress;

private string $createdSid = '';

public function __construct(SessionConfig $config, string $ipAddress)
{
// Store Session configurations
Expand All @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions tests/system/Session/Handlers/BaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading