Skip to content

Commit 4da87dc

Browse files
committed
fix(Database): escape table, column, and sequence names in Postgre Connection::insertID()
1 parent 54db387 commit 4da87dc

3 files changed

Lines changed: 105 additions & 2 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Release Date: Unreleased
1414

1515
## Bugs Fixed
1616

17+
- **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping.
1718
- **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.
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

‎system/Database/Postgre/Connection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public function insertID()
489489
$sql = 'SELECT LASTVAL() AS ins_id';
490490
} elseif ($table !== null) {
491491
if ($column !== null && $v >= '8.0') {
492-
$sql = "SELECT pg_get_serial_sequence('{$table}', '{$column}') AS seq";
492+
$sql = 'SELECT pg_get_serial_sequence(' . $this->escape($table) . ', ' . $this->escape($column) . ') AS seq';
493493
$query = $this->query($sql);
494494
$query = $query->getRow();
495495
$seq = $query->seq;
@@ -498,7 +498,7 @@ public function insertID()
498498
$seq = $table;
499499
}
500500

501-
$sql = "SELECT CURRVAL('{$seq}') AS ins_id";
501+
$sql = 'SELECT CURRVAL(' . $this->escape($seq) . ') AS ins_id';
502502
} else {
503503
return pg_last_oid($this->resultID);
504504
}
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\Database\Postgre;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use PHPUnit\Framework\Attributes\Group;
18+
use stdClass;
19+
20+
/**
21+
* Mock pg_version in the Postgre namespace to test insertID without live DB connection.
22+
*
23+
* @param mixed $connection
24+
*
25+
* @return array{server: string}
26+
*/
27+
function pg_version($connection = null): array
28+
{
29+
return ['server' => '15.0'];
30+
}
31+
32+
/**
33+
* Mock pg_escape_literal in the Postgre namespace for testing without live DB connection.
34+
*
35+
* @param mixed $connection
36+
*/
37+
function pg_escape_literal($connection, string $string): string
38+
{
39+
return "'" . str_replace("'", "''", $string) . "'";
40+
}
41+
42+
/**
43+
* @internal
44+
*/
45+
#[Group('Others')]
46+
final class InsertIDTest extends CIUnitTestCase
47+
{
48+
public function testInsertIDQuotesTableColumnAndSequenceNames(): void
49+
{
50+
$connection = new class () extends Connection {
51+
/**
52+
* @var list<string>
53+
*/
54+
public array $queries = [];
55+
56+
public function __construct()
57+
{
58+
$this->DBDriver = 'Postgre';
59+
$this->connID = false;
60+
}
61+
62+
public function initialize(): void
63+
{
64+
}
65+
66+
/**
67+
* @param array<array-key, mixed>|string|null $binds
68+
*/
69+
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = ''): Result
70+
{
71+
$this->queries[] = $sql;
72+
73+
$row = new stdClass();
74+
$row->seq = "user's_id_seq";
75+
$row->ins_id = 123;
76+
77+
$result = new class ($this->connID, $this->resultID) extends Result {
78+
public function setRowData(stdClass $row): void
79+
{
80+
$this->resultObject = [$row];
81+
}
82+
};
83+
$result->setRowData($row);
84+
85+
return $result;
86+
}
87+
};
88+
89+
$id = $connection->insertID("user's", "id'col");
90+
91+
$this->assertSame(123, $id);
92+
$this->assertCount(2, $connection->queries);
93+
$this->assertSame(
94+
"SELECT pg_get_serial_sequence('user''s', 'id''col') AS seq",
95+
$connection->queries[0],
96+
);
97+
$this->assertSame(
98+
"SELECT CURRVAL('user''s_id_seq') AS ins_id",
99+
$connection->queries[1],
100+
);
101+
}
102+
}

0 commit comments

Comments
 (0)