Skip to content

Commit 08ba553

Browse files
committed
fix(Database): escape table, constraint, and schema in Forge::_dropKeyAsConstraint()
1 parent 3ad5144 commit 08ba553

5 files changed

Lines changed: 98 additions & 10 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`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema 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/OCI8/Forge.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ protected function _dropTable(string $table, bool $ifExists, bool $cascade)
282282
*/
283283
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
284284
{
285-
return "SELECT constraint_name FROM all_constraints WHERE table_name = '"
286-
. trim($table, '"') . "' AND index_name = '"
287-
. trim($constraintName, '"') . "'";
285+
return 'SELECT constraint_name FROM all_constraints WHERE table_name = '
286+
. $this->db->escape(trim($table, '"')) . ' AND index_name = '
287+
. $this->db->escape(trim($constraintName, '"'));
288288
}
289289
}

‎system/Database/Postgre/Forge.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ protected function _dropTable(string $table, bool $ifExists, bool $cascade): str
199199
*/
200200
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
201201
{
202-
return "SELECT con.conname
202+
return 'SELECT con.conname
203203
FROM pg_catalog.pg_constraint con
204204
INNER JOIN pg_catalog.pg_class rel
205205
ON rel.oid = con.conrelid
206206
INNER JOIN pg_catalog.pg_namespace nsp
207207
ON nsp.oid = connamespace
208-
WHERE nsp.nspname = '{$this->db->schema}'
209-
AND rel.relname = '" . trim($table, '"') . "'
210-
AND con.conname = '" . trim($constraintName, '"') . "'";
208+
WHERE nsp.nspname = ' . $this->db->escape($this->db->schema) . '
209+
AND rel.relname = ' . $this->db->escape(trim($table, '"')) . '
210+
AND con.conname = ' . $this->db->escape(trim($constraintName, '"'));
211211
}
212212
}

‎system/Database/SQLSRV/Forge.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,8 @@ protected function _dropTable(string $table, bool $ifExists, bool $cascade): str
450450
*/
451451
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
452452
{
453-
return "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
454-
WHERE TABLE_NAME= '" . trim($table, '"') . "'
455-
AND CONSTRAINT_NAME = '" . trim($constraintName, '"') . "'";
453+
return 'SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
454+
WHERE TABLE_NAME = ' . $this->db->escape(trim($table, '"')) . '
455+
AND CONSTRAINT_NAME = ' . $this->db->escape(trim($constraintName, '"'));
456456
}
457457
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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\Forge;
15+
16+
use CodeIgniter\Database\OCI8\Connection as OCI8Connection;
17+
use CodeIgniter\Database\OCI8\Forge as OCI8Forge;
18+
use CodeIgniter\Database\Postgre\Connection as PostgreConnection;
19+
use CodeIgniter\Database\Postgre\Forge as PostgreForge;
20+
use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection;
21+
use CodeIgniter\Database\SQLSRV\Forge as SQLSRVForge;
22+
use CodeIgniter\Test\CIUnitTestCase;
23+
use CodeIgniter\Test\Mock\MockConnection;
24+
use PHPUnit\Framework\Attributes\Group;
25+
26+
/**
27+
* @internal
28+
*/
29+
#[Group('Others')]
30+
final class DropKeyAsConstraintTest extends CIUnitTestCase
31+
{
32+
protected $db;
33+
34+
protected function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
$this->db = new MockConnection([]);
39+
}
40+
41+
public function testPostgreDropKeyAsConstraintEscaping(): void
42+
{
43+
$this->setPrivateProperty($this->db, 'schema', "my'schema");
44+
45+
/** @var PostgreConnection $db */
46+
$db = $this->db;
47+
48+
$forge = new PostgreForge($db);
49+
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');
50+
51+
$sql = $method("user's", "fk'id");
52+
53+
$this->assertStringContainsString("nsp.nspname = 'my''schema'", $sql);
54+
$this->assertStringContainsString("rel.relname = 'user''s'", $sql);
55+
$this->assertStringContainsString("con.conname = 'fk''id'", $sql);
56+
}
57+
58+
public function testOCI8DropKeyAsConstraintEscaping(): void
59+
{
60+
/** @var OCI8Connection $db */
61+
$db = $this->db;
62+
63+
$forge = new OCI8Forge($db);
64+
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');
65+
66+
$sql = $method("user's", "fk'id");
67+
68+
$this->assertStringContainsString("table_name = 'user''s'", $sql);
69+
$this->assertStringContainsString("index_name = 'fk''id'", $sql);
70+
}
71+
72+
public function testSQLSRVDropKeyAsConstraintEscaping(): void
73+
{
74+
$this->setPrivateProperty($this->db, 'schema', 'dbo');
75+
76+
/** @var SQLSRVConnection $db */
77+
$db = $this->db;
78+
79+
$forge = new SQLSRVForge($db);
80+
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');
81+
82+
$sql = $method("user's", "fk'id");
83+
84+
$this->assertStringContainsString("TABLE_NAME = 'user''s'", $sql);
85+
$this->assertStringContainsString("CONSTRAINT_NAME = 'fk''id'", $sql);
86+
}
87+
}

0 commit comments

Comments
 (0)