Skip to content

Commit 0b34a37

Browse files
committed
fix: add int type hint to gc() to prevent SQL injection
- Add int type hint to parameter in DatabaseHandler::gc() and PostgreHandler::gc() to match PHP SessionHandlerInterface - Change return type to int|false to match the interface - Add test for TypeError on non-int input (SQLi protection) - Add edge case tests for gc(0) and gc(-1) Fixes potential SQL injection via unescaped in INTERVAL clause with = false.
1 parent b635434 commit 0b34a37

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎system/Session/Handlers/Database/PostgreHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function prepareData(string $data): string
5959
* @param int $max_lifetime Sessions that have not updated
6060
* for the last max_lifetime seconds will be removed.
6161
*/
62-
public function gc($max_lifetime): false|int
62+
public function gc(int $max_lifetime): int|false
6363
{
6464
$separator = '\'';
6565
$interval = implode($separator, ['', "{$max_lifetime} second", '']);

‎system/Session/Handlers/DatabaseHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function destroy($id): bool
276276
*
277277
* @return false|int Returns the number of deleted sessions on success, or false on failure.
278278
*/
279-
public function gc($max_lifetime): false|int
279+
public function gc(int $max_lifetime): int|false
280280
{
281281
return $this->db->table($this->table)->where(
282282
'timestamp <',

‎tests/system/Session/Handlers/Database/AbstractHandlerTestCase.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,24 @@ public function testGC(): void
122122
$handler = $this->getInstance();
123123
$this->assertSame(1, $handler->gc(3600));
124124
}
125+
126+
public function testGCRejectsNonInt(): void
127+
{
128+
$handler = $this->getInstance();
129+
130+
$this->expectException(\TypeError::class);
131+
$handler->gc('malicious; DROP TABLE sessions; --');
132+
}
133+
134+
public function testGCWithZero(): void
135+
{
136+
$handler = $this->getInstance();
137+
$this->assertSame(1, $handler->gc(0));
138+
}
139+
140+
public function testGCWithNegativeValue(): void
141+
{
142+
$handler = $this->getInstance();
143+
$this->assertSame(1, $handler->gc(-1));
144+
}
125145
}

0 commit comments

Comments
 (0)