Skip to content
Open
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
2 changes: 1 addition & 1 deletion system/Session/Handlers/Database/PostgreHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function prepareData(string $data): string
* @param int $max_lifetime Sessions that have not updated
* for the last max_lifetime seconds will be removed.
*/
public function gc($max_lifetime): false|int
public function gc(int $max_lifetime): false|int
{
$separator = '\'';
$interval = implode($separator, ['', "{$max_lifetime} second", '']);
Expand Down
2 changes: 1 addition & 1 deletion system/Session/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function destroy($id): bool
*
* @return false|int Returns the number of deleted sessions on success, or false on failure.
*/
public function gc($max_lifetime): false|int
public function gc(int $max_lifetime): false|int
{
return $this->db->table($this->table)->where(
'timestamp <',
Expand Down
16 changes: 16 additions & 0 deletions tests/system/Session/Handlers/Database/AbstractHandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\ReflectionHelper;
use Config\Database as DatabaseConfig;
use stdClass;
use Tests\Support\Database\Seeds\CITestSeeder;
use TypeError;

/**
* @internal
Expand Down Expand Up @@ -122,4 +124,18 @@ public function testGC(): void
$handler = $this->getInstance();
$this->assertSame(1, $handler->gc(3600));
}

public function testGCRejectsNonInt(): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests a PHP feature, not the method itself. Thus, the test is unnecessary.

{
$handler = $this->getInstance();

foreach (['malicious; DROP TABLE sessions; --', null, 1.5, [], new stdClass(), true] as $malicious) {
try {
$handler->gc($malicious); // @phpstan-ignore argument.type
$this->fail('TypeError expected for value: ' . gettype($malicious));
} catch (TypeError) {
$this->addToAssertionCount(1);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use DataProvider instead of a loop.

}
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Bugs Fixed
**********

- **Logger:** Fixed a bug where interpolating a log message with array or non-stringable context values could raise PHP warnings or errors.
- **Session:** Added ``int`` type hint to ``DatabaseHandler::gc()`` and ``PostgreHandler::gc()`` to prevent SQL injection from non-integer ``$max_lifetime`` values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add something like this under the Method Signature Changes section:

- **Session:** The ``$max_lifetime`` parameter of the following ``gc()`` methods
  now has the native ``int`` type, matching ``SessionHandlerInterface``.

You can also list all affected handlers - this will match other entries.


See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
Loading