Skip to content

Commit 4d85b99

Browse files
committed
fix(Session): use query binding for session lock in PostgreHandler
1 parent 54db387 commit 4d85b99

3 files changed

Lines changed: 114 additions & 5 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Release Date: Unreleased
1515
## Bugs Fixed
1616

1717
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
18+
- **Session:** Fixed a bug where `PostgreHandler::lockSession()` and `releaseLock()` interpolated session IDs and IP addresses into SQL queries instead of using query bindings.
1819
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.
1920

2021
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/Database/PostgreHandler.php‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public function gc($max_lifetime): false|int
7070
*/
7171
protected function lockSession(string $sessionID): bool
7272
{
73-
$arg = "hashtext('{$sessionID}')" . ($this->matchIP ? ", hashtext('{$this->ipAddress}')" : '');
74-
if ($this->db->simpleQuery("SELECT pg_advisory_lock({$arg})") !== false) {
75-
$this->lock = $arg;
73+
$sql = 'SELECT pg_advisory_lock(hashtext(?)' . ($this->matchIP ? ', hashtext(?)' : '') . ')';
74+
$binds = $this->matchIP ? [$sessionID, $this->ipAddress] : [$sessionID];
75+
76+
if ($this->db->query($sql, $binds) !== false) {
77+
$this->lock = $sessionID;
7678

7779
return true;
7880
}
@@ -85,11 +87,15 @@ protected function lockSession(string $sessionID): bool
8587
*/
8688
protected function releaseLock(): bool
8789
{
88-
if (! $this->lock) {
90+
if ($this->lock === false) {
8991
return true;
9092
}
9193

92-
if ($this->db->simpleQuery("SELECT pg_advisory_unlock({$this->lock})") !== false) {
94+
$sessionID = is_string($this->lock) ? $this->lock : (string) $this->sessionID;
95+
$sql = 'SELECT pg_advisory_unlock(hashtext(?)' . ($this->matchIP ? ', hashtext(?)' : '') . ')';
96+
$binds = $this->matchIP ? [$sessionID, $this->ipAddress] : [$sessionID];
97+
98+
if ($this->db->query($sql, $binds) !== false) {
9399
$this->lock = false;
94100

95101
return true;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Session\Handlers\Database;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\Mock\MockConnection;
18+
use CodeIgniter\Test\Mock\MockResult;
19+
use Config\Session as SessionConfig;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('Others')]
26+
final class PostgreHandlerMockTest extends CIUnitTestCase
27+
{
28+
private function createHandler(bool $matchIP, object $mockDb): PostgreHandler
29+
{
30+
$config = new SessionConfig();
31+
$config->driver = PostgreHandler::class;
32+
$config->savePath = 'ci_sessions';
33+
$config->matchIP = $matchIP;
34+
35+
$handler = new PostgreHandler($config, "192.168.1.1' OR '1'='1");
36+
$this->setPrivateProperty($handler, 'db', $mockDb);
37+
38+
return $handler;
39+
}
40+
41+
private function createMockDb(): object
42+
{
43+
return new class (['DBDriver' => 'Postgre']) extends MockConnection {
44+
/**
45+
* @var list<array{sql: string, binds: array<array-key, mixed>|null}>
46+
*/
47+
public array $executed = [];
48+
49+
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = ''): MockResult
50+
{
51+
$this->executed[] = [
52+
'sql' => $sql,
53+
'binds' => is_array($binds) ? $binds : null,
54+
];
55+
56+
return new MockResult($this->connID, $this->resultID);
57+
}
58+
};
59+
}
60+
61+
public function testLockAndReleaseSessionWithoutMatchIP(): void
62+
{
63+
$db = $this->createMockDb();
64+
$handler = $this->createHandler(false, $db);
65+
66+
$lockSession = self::getPrivateMethodInvoker($handler, 'lockSession');
67+
$releaseLock = self::getPrivateMethodInvoker($handler, 'releaseLock');
68+
69+
$sessionID = "sess'id; DROP TABLE users; --";
70+
$this->assertTrue($lockSession($sessionID));
71+
$this->assertTrue($releaseLock());
72+
73+
$this->assertCount(2, $db->executed);
74+
75+
$this->assertSame('SELECT pg_advisory_lock(hashtext(?))', $db->executed[0]['sql']);
76+
$this->assertSame([$sessionID], $db->executed[0]['binds']);
77+
78+
$this->assertSame('SELECT pg_advisory_unlock(hashtext(?))', $db->executed[1]['sql']);
79+
$this->assertSame([$sessionID], $db->executed[1]['binds']);
80+
}
81+
82+
public function testLockAndReleaseSessionWithMatchIP(): void
83+
{
84+
$db = $this->createMockDb();
85+
$handler = $this->createHandler(true, $db);
86+
87+
$lockSession = self::getPrivateMethodInvoker($handler, 'lockSession');
88+
$releaseLock = self::getPrivateMethodInvoker($handler, 'releaseLock');
89+
90+
$sessionID = "sess'id; DROP TABLE users; --";
91+
$this->assertTrue($lockSession($sessionID));
92+
$this->assertTrue($releaseLock());
93+
94+
$this->assertCount(2, $db->executed);
95+
96+
$this->assertSame('SELECT pg_advisory_lock(hashtext(?), hashtext(?))', $db->executed[0]['sql']);
97+
$this->assertSame([$sessionID, "192.168.1.1' OR '1'='1"], $db->executed[0]['binds']);
98+
99+
$this->assertSame('SELECT pg_advisory_unlock(hashtext(?), hashtext(?))', $db->executed[1]['sql']);
100+
$this->assertSame([$sessionID, "192.168.1.1' OR '1'='1"], $db->executed[1]['binds']);
101+
}
102+
}

0 commit comments

Comments
 (0)