Skip to content

Commit 79598a1

Browse files
committed
fix: add create_sid() and validateId() to the session BaseHandler for PHP 8.6
1 parent 0531321 commit 79598a1

3 files changed

Lines changed: 67 additions & 19 deletions

File tree

‎docs/changelogs/v4.8.0.md‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ Release Date: Unreleased
6565

6666
- **CLI:** The `Console::run()` method now accepts an optional `array $tokens` parameter. This allows you to pass an array of command tokens directly to the console runner, which is useful for testing or programmatically running commands. If not provided, it will default to using the global `$argv`.
6767

68-
- **CodeIgniter:** The deprecated parameters in methods have been removed:
69-
- `CodeIgniter\CodeIgniter::handleRequest()` no longer accepts the deprecated `$cacheConfig` and `$returnResponse` parameters.
68+
- **CodeIgniter:** The deprecated parameters in methods have been removed:
69+
- `CodeIgniter\CodeIgniter::handleRequest()` no longer accepts the deprecated `$cacheConfig` and `$returnResponse` parameters.
7070
- `$cacheConfig` is no longer used and is now hard deprecated. A deprecation notice will be triggered if this is passed to the method.
7171
- `$returnResponse` has been removed, since it was already deprecated and unused.
7272
- The updated method signature is now `handleRequest(?RouteCollectionInterface $routes, ?Cache $cacheConfig = null)`.
@@ -75,7 +75,7 @@ Release Date: Unreleased
7575

7676
- **Config:** `CodeIgniter\Config\Services::request()` no longer accepts any parameter.
7777

78-
- **Database:** The following methods have had their signatures updated to remove deprecated parameters:
78+
- **Database:** The following methods have had their signatures updated to remove deprecated parameters:
7979
- `CodeIgniter\Database\Forge::_createTable()` no longer accepts the deprecated `$ifNotExists` parameter. The method signature is now `_createTable(string $table, array $attributes)`.
8080

8181
- **HTTP:** `CodeIgniter\HTTP\OutgoingRequest::__construct()` now requires the `$uri` parameter, which was previously `?URI $uri = null`. Passing `null` only worked when a `Host` header was supplied in the same call, since the constructor otherwise dereferences the URI to set that header. Consequently `OutgoingRequest::getUri()` now returns `URI` instead of `URI|null`, matching `OutgoingRequestInterface`. See [Upgrading from 4.7.x to 4.8.0](../installation/upgrade_480.md) for the migration.
@@ -86,41 +86,41 @@ Release Date: Unreleased
8686

8787
### Property Scope Changes
8888

89-
- **HTTP:** The following properties have changed their scope (visibility):
89+
- **HTTP:** The following properties have changed their scope (visibility):
9090
- `CodeIgniter\HTTP\RequestTrait::$ipAddress` is now private (previously protected).
9191

9292
### Method Scope Changes
9393

9494
- **CLI:** `CodeIgniter\CLI\Commands::getCommandAlternatives()` is now public (previously protected).
9595

96-
- **HTTP:** The following methods have changed their scope (visibility):
96+
- **HTTP:** The following methods have changed their scope (visibility):
9797
- `CodeIgniter\HTTP\URI::setUri()` is now private (previously public).
9898
- `CodeIgniter\HTTP\URI::refreshPath()` is now protected (previously public).
9999
- `CodeIgniter\HTTP\SiteURI::refreshPath()` is now protected (previously public).
100100

101101
### Removed Deprecated Items
102102

103-
- **Autoloader:** Removed the following deprecated methods:
103+
- **Autoloader:** Removed the following deprecated methods:
104104
- `CodeIgniter\Autoloader\Autoloader::sanitizeFileName()`
105105
- `CodeIgniter\Autoloader\Autoloader::discoverComposerNamespaces()`
106106

107107
- **Bootstrap:** The deprecated **system/bootstrap.php** file has been removed.
108108

109-
- **Cache:** Removed the following deprecated methods and constant:
109+
- **Cache:** Removed the following deprecated methods and constant:
110110
- `CodeIgniter\Cache\Handlers\BaseHandler::RESERVED_CHARACTERS` (deprecated since v4.1.5)
111111
- `CodeIgniter\Cache\Handlers\FileHandler::writeFile()` (deprecated since v4.6.0)
112112
- `CodeIgniter\Cache\Handlers\FileHandler::deleteFile()` (deprecated since v4.6.0)
113113
- `CodeIgniter\Cache\Handlers\FileHandler::getDirFileInfo()` (deprecated since v4.6.0)
114114
- `CodeIgniter\Cache\Handlers\FileHandler::getFileInfo()` (deprecated since v4.6.0)
115115

116-
- **CLI:** Removed the following deprecated properties and methods:
116+
- **CLI:** Removed the following deprecated properties and methods:
117117
- `CodeIgniter\CLI\BaseCommand::getPad()`
118118
- `CodeIgniter\CLI\CLI::$readline_support`
119119
- `CodeIgniter\CLI\CLI::$wait_msg`
120120
- `CodeIgniter\CLI\CLI::isWindows()`
121121
- `CodeIgniter\CLI\GeneratorTrait::execute()`
122122

123-
- **CodeIgniter:** Removed the following deprecated properties and methods:
123+
- **CodeIgniter:** Removed the following deprecated properties and methods:
124124
- `CodeIgniter\CodeIgniter::$cacheTTL`
125125
- `CodeIgniter\CodeIgniter::$returnResponse`
126126
- `CodeIgniter\CodeIgniter::initializeKint()`
@@ -136,10 +136,10 @@ Release Date: Unreleased
136136
- `CodeIgniter\CodeIgniter::callExit()`
137137
- `CodeIgniter\Test\MockCodeIgniter::callExit()`
138138

139-
- **Config:** Removed the following deprecated property:
139+
- **Config:** Removed the following deprecated property:
140140
- `CodeIgniter\Config\BaseService::$services` (deprecated since v4.5.0)
141141

142-
- **Database:** Removed the following deprecated properties and methods:
142+
- **Database:** Removed the following deprecated properties and methods:
143143
- `CodeIgniter\Database\BaseBuilder::setInsertBatch()`
144144
- `CodeIgniter\Database\BaseBuilder::setUpdateBatch()`
145145
- `CodeIgniter\Database\BaseBuilder::cleanClone()`
@@ -151,7 +151,7 @@ Release Date: Unreleased
151151
- `CodeIgniter\Database\OCI8\Forge::getError()`
152152
- `CodeIgniter\Database\SQLSRV\Forge::$createTableIfStr`
153153

154-
- **Debug:** Removed the following deprecated properties and methods:
154+
- **Debug:** Removed the following deprecated properties and methods:
155155
- `CodeIgniter\Debug\Toolbar\Collectors\BaseCollector::cleanPath()` (deprecated since v4.2.0)
156156
- `CodeIgniter\Debug\Exceptions::$ob_level` (deprecated since v4.4.0)
157157
- `CodeIgniter\Debug\Exceptions::$viewPath` (deprecated since v4.4.0)
@@ -164,25 +164,25 @@ Release Date: Unreleased
164164
- `CodeIgniter\Debug\Exceptions::describeMemory()` (deprecated since v4.4.0)
165165
- `CodeIgniter\Debug\Exceptions::highlightFile()` (deprecated since v4.4.0)
166166

167-
- **Exceptions:** Removed the following static constructors of `FrameworkException` and its child classes:
167+
- **Exceptions:** Removed the following static constructors of `FrameworkException` and its child classes:
168168
- `CodeIgniter\Exceptions\DownloadException::forCannotSetCache()`
169169
- `CodeIgniter\Exceptions\FrameworkException::forMissingExtension()`
170170
- `CodeIgniter\Honeypot\Exceptions\HoneypotException::forNoHiddenValue()`
171171
- `CodeIgniter\HTTP\Exceptions\HTTPException::forInvalidSameSiteSetting()`
172172
- `CodeIgniter\Security\Exceptions\SecurityException::forInvalidSameSite()`
173173
- `CodeIgniter\Session\Exceptions\SessionException::forInvalidSameSiteSetting()`
174174

175-
- **Files:** Removed the following deprecated method:
175+
- **Files:** Removed the following deprecated method:
176176
- `CodeIgniter\Files\File::getSizeByUnit()` (deprecated since v4.6.0)
177177

178-
- **Filters:** Removed the following deprecated properties and methods:
178+
- **Filters:** Removed the following deprecated properties and methods:
179179
- `CodeIgniter\Filters\Filters::$arguments` (deprecated since v4.6.0)
180180
- `CodeIgniter\Filters\Filters::$argumentsClass` (deprecated since v4.6.0)
181181
- `CodeIgniter\Filters\Filters::getArguments()` (deprecated since v4.6.0)
182182
- `CodeIgniter\Filters\Filters::discoverFilters()` and the auto-discovery of **Config/Filters.php** files (deprecated since v4.4.2)
183183
- `CodeIgniter\Filters\Filters::$modules` and the `$modules` constructor parameter, which were only used by the auto-discovery
184184

185-
- **HTTP:** Removed the following deprecated properties and methods:
185+
- **HTTP:** Removed the following deprecated properties and methods:
186186
- `CodeIgniter\HTTP\Message::getHeaders()` (deprecated since v4.0.5)
187187
- `CodeIgniter\HTTP\Message::getHeader()` (deprecated since v4.0.5)
188188
- `CodeIgniter\HTTP\RequestTrait::getEnv()` (deprecated since v4.4.4)
@@ -195,7 +195,7 @@ Release Date: Unreleased
195195
- `CodeIgniter\HTTP\SiteURI::setBaseURL()` (deprecated since v4.4.0)
196196
- `CodeIgniter\HTTP\SiteURI::setURI()` (deprecated since v4.4.0)
197197

198-
- **Security:** Removed the following deprecated properties and methods:
198+
- **Security:** Removed the following deprecated properties and methods:
199199
- `CodeIgniter\Security\SecurityInterface::sanitizeFilename()` (deprecated since v4.6.2)
200200
- `CodeIgniter\Security\Security::sanitizeFilename()` (deprecated since v4.6.2)
201201
- `CodeIgniter\Security\Security::$csrfProtection` (deprecated since v4.4.0)
@@ -356,15 +356,15 @@ Release Date: Unreleased
356356

357357
## Message Changes
358358

359-
- Added new language keys:
359+
- Added new language keys:
360360
- `Cache.unsupportedLockStore` (`CacheException::forUnsupportedLockStore()`)
361361
- `CLI.commandAlias` and `CLI.helpAliases` (command alias rendering in `list` and `help`)
362362
- `CLI.altCommandNone`, `CLI.altCommandRun`, and `CLI.altCommandSelect` (correcting a mistyped command)
363363
- `CLI.generator.invalidCommandType` (`make:command` rejecting an invalid `--type`)
364364
- `CLI.generator.invalidReturnType` (`make:model` rejecting an invalid `--return`)
365365
- `Commands.invalidCommandAlias`, `Commands.commandAliasSameAsName`, `Commands.duplicateCommandAlias`, `Commands.aliasClashesWithCommandName`, and `Commands.aliasClashesWithAlias` (command alias validation)
366366

367-
- Removed deprecated language keys tied to removed exception constructors:
367+
- Removed deprecated language keys tied to removed exception constructors:
368368
- `Core.missingExtension` (`FrameworkException::forMissingExtension()`)
369369
- `HTTP.cannotSetCache` (`DownloadException::forCannotSetCache()`)
370370
- `HTTP.disallowedAction`
@@ -393,4 +393,6 @@ Release Date: Unreleased
393393

394394
## Bugs Fixed
395395

396+
- **Session:** Fixed a bug where session handlers failed to load on PHP 8.6 for lack of `create_sid()` and `validateId()`.
397+
396398
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)