Skip to content

Commit 853f64e

Browse files
committed
fix: add create_sid() and validateId() to the session BaseHandler for PHP 8.6
1 parent d4da78c commit 853f64e

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎docs/changelogs/v4.8.0.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,5 +394,6 @@ Release Date: Unreleased
394394
## Bugs Fixed
395395

396396
- **Config:** Fixed a bug where instantiating an anonymous `BaseConfig` subclass threw a `ValueError` from `getenv()` on PHP 8.6.
397+
- **Session:** Fixed a bug where session handlers failed to load on PHP 8.6 for lack of `create_sid()` and `validateId()`.
397398

398399
See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.

‎system/Session/Handlers/BaseHandler.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ abstract class BaseHandler implements SessionHandlerInterface
114114
*/
115115
protected $ipAddress;
116116

117+
private string $createdSid = '';
118+
117119
public function __construct(SessionConfig $config, string $ipAddress)
118120
{
119121
// Store Session configurations
@@ -132,6 +134,22 @@ public function __construct(SessionConfig $config, string $ipAddress)
132134
$this->ipAddress = $ipAddress;
133135
}
134136

137+
/**
138+
* Generates a session ID of 32 hexadecimal characters, as enforced by `Session::configure()`.
139+
*/
140+
public function create_sid(): string
141+
{
142+
return $this->createdSid = bin2hex(random_bytes(16));
143+
}
144+
145+
/**
146+
* Reports the ID of the last `create_sid()` call as unused and accepts every other ID.
147+
*/
148+
public function validateId(string $id): bool
149+
{
150+
return $id !== $this->createdSid;
151+
}
152+
135153
/**
136154
* Internal method to force removal of a cookie by the client
137155
* when session_destroy() is called.

‎tests/system/Session/Handlers/BaseHandlerTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ public function gc(int $maxLifetime): int
131131
};
132132
}
133133

134+
public function testCreateSidReturnsUniqueThirtyTwoHexCharacters(): void
135+
{
136+
$handler = $this->createHandler();
137+
138+
$first = $handler->create_sid();
139+
$second = $handler->create_sid();
140+
141+
$this->assertMatchesRegularExpression('/\A[0-9a-f]{32}\z/', $first);
142+
$this->assertMatchesRegularExpression('/\A[0-9a-f]{32}\z/', $second);
143+
$this->assertNotSame($first, $second);
144+
}
145+
146+
public function testValidateIdReportsOnlyTheCreatedSidAsUnused(): void
147+
{
148+
$handler = $this->createHandler();
149+
150+
$created = $handler->create_sid();
151+
152+
$this->assertFalse($handler->validateId($created));
153+
$this->assertTrue($handler->validateId(str_repeat('ab', 16)));
154+
$this->assertTrue($handler->validateId('invalid-session-id!'));
155+
156+
$newer = $handler->create_sid();
157+
158+
$this->assertFalse($handler->validateId($newer));
159+
$this->assertTrue($handler->validateId($created));
160+
}
161+
134162
public function testConstructorInitializesCookieSameSiteFromConfig(): void
135163
{
136164
$cookieConfig = new CookieConfig();

0 commit comments

Comments
 (0)