Skip to content

Commit 5565bca

Browse files
committed
fix(Database): escape table, constraint, and schema in Forge::_dropKeyAsConstraint()
1 parent 59e5eea commit 5565bca

5 files changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,15 @@ 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 sprintf(
286+
<<<'SQL'
287+
SELECT constraint_name
288+
FROM all_constraints
289+
WHERE table_name = %s
290+
AND index_name = %s
291+
SQL,
292+
$this->db->escape(trim($table, '"')),
293+
$this->db->escape(trim($constraintName, '"')),
294+
);
288295
}
289296
}

‎system/Database/Postgre/Forge.php‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,21 @@ 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
203-
FROM pg_catalog.pg_constraint con
202+
return sprintf(
203+
<<<'SQL'
204+
SELECT con.conname
205+
FROM pg_catalog.pg_constraint con
204206
INNER JOIN pg_catalog.pg_class rel
205-
ON rel.oid = con.conrelid
207+
ON rel.oid = con.conrelid
206208
INNER JOIN pg_catalog.pg_namespace nsp
207-
ON nsp.oid = connamespace
208-
WHERE nsp.nspname = '{$this->db->schema}'
209-
AND rel.relname = '" . trim($table, '"') . "'
210-
AND con.conname = '" . trim($constraintName, '"') . "'";
209+
ON nsp.oid = connamespace
210+
WHERE nsp.nspname = %s
211+
AND rel.relname = %s
212+
AND con.conname = %s
213+
SQL,
214+
$this->db->escape($this->db->schema),
215+
$this->db->escape(trim($table, '"')),
216+
$this->db->escape(trim($constraintName, '"')),
217+
);
211218
}
212219
}

‎system/Database/SQLSRV/Forge.php‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,15 @@ 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 sprintf(
454+
<<<'SQL'
455+
SELECT CONSTRAINT_NAME
456+
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
457+
WHERE TABLE_NAME = %s
458+
AND CONSTRAINT_NAME = %s
459+
SQL,
460+
$this->db->escape(trim($table, '"')),
461+
$this->db->escape(trim($constraintName, '"')),
462+
);
456463
}
457464
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
$expected = <<<'SQL'
54+
SELECT con.conname
55+
FROM pg_catalog.pg_constraint con
56+
INNER JOIN pg_catalog.pg_class rel
57+
ON rel.oid = con.conrelid
58+
INNER JOIN pg_catalog.pg_namespace nsp
59+
ON nsp.oid = connamespace
60+
WHERE nsp.nspname = 'my''schema'
61+
AND rel.relname = 'user''s'
62+
AND con.conname = 'fk''id'
63+
SQL;
64+
65+
$this->assertSame($expected, $sql);
66+
}
67+
68+
public function testOCI8DropKeyAsConstraintEscaping(): void
69+
{
70+
/** @var OCI8Connection $db */
71+
$db = $this->db;
72+
73+
$forge = new OCI8Forge($db);
74+
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');
75+
76+
$sql = $method("user's", "fk'id");
77+
78+
$expected = <<<'SQL'
79+
SELECT constraint_name
80+
FROM all_constraints
81+
WHERE table_name = 'user''s'
82+
AND index_name = 'fk''id'
83+
SQL;
84+
85+
$this->assertSame($expected, $sql);
86+
}
87+
88+
public function testSQLSRVDropKeyAsConstraintEscaping(): void
89+
{
90+
$this->setPrivateProperty($this->db, 'schema', 'dbo');
91+
92+
/** @var SQLSRVConnection $db */
93+
$db = $this->db;
94+
95+
$forge = new SQLSRVForge($db);
96+
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');
97+
98+
$sql = $method("user's", "fk'id");
99+
100+
$expected = <<<'SQL'
101+
SELECT CONSTRAINT_NAME
102+
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
103+
WHERE TABLE_NAME = 'user''s'
104+
AND CONSTRAINT_NAME = 'fk''id'
105+
SQL;
106+
107+
$this->assertSame($expected, $sql);
108+
}
109+
}

0 commit comments

Comments
 (0)