Skip to content

Commit 3c8db0e

Browse files
committed
fix(Database): escape table, column, and sequence names in Postgre Connection::insertID()
1 parent 59e5eea commit 3c8db0e

3 files changed

Lines changed: 67 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
@@ -16,6 +16,7 @@ Release Date: Unreleased
1616

1717
## Bugs Fixed
1818

19+
- **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping.
1920
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
2021
- **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.
2122
- **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.

‎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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Live\Postgre;
15+
16+
use CodeIgniter\Database\Postgre\Connection;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use Config\Database;
19+
use PHPUnit\Framework\Attributes\Group;
20+
21+
/**
22+
* @internal
23+
*/
24+
#[Group('DatabaseLive')]
25+
final class InsertIDTest extends CIUnitTestCase
26+
{
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->db = Database::connect($this->DBGroup);
32+
33+
if ($this->db->DBDriver !== 'Postgre') {
34+
$this->markTestSkipped('This test is only for Postgre.');
35+
}
36+
}
37+
38+
public function testInsertIDWithApostropheInColumnName(): void
39+
{
40+
$tableName = 'temp_insertid_test';
41+
$columnName = "id'col";
42+
43+
$this->db->query(
44+
'CREATE TEMPORARY TABLE ' . $this->db->escapeIdentifiers($tableName) . ' ('
45+
. $this->db->escapeIdentifiers($columnName) . ' SERIAL PRIMARY KEY, "name" VARCHAR(255))',
46+
);
47+
48+
try {
49+
$this->db->query(
50+
'INSERT INTO ' . $this->db->escapeIdentifiers($tableName) . ' ("name") VALUES (?)',
51+
['Test Name'],
52+
);
53+
54+
/** @var Connection $db */
55+
$db = $this->db;
56+
57+
$id = $db->insertID($tableName, $columnName);
58+
59+
$this->assertSame(1, $id);
60+
} finally {
61+
$this->db->query('DROP TABLE IF EXISTS ' . $this->db->escapeIdentifiers($tableName));
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)