Skip to content

Commit 9601f2d

Browse files
authored
fix: escape table identifiers in SQLite3 foreign_key_list PRAGMA (#10732)
* fix(Database): escape table identifiers in SQLite3 foreign_key_list PRAGMA * fix: properly escape table name and schema in SQLite3 getForeignKeyData
1 parent 6f92d1f commit 9601f2d

3 files changed

Lines changed: 164 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Release Date: Unreleased
2020
- **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping.
2121
- **Database:** Fixed a bug where `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters.
2222
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
23+
- **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas.
2324
- **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters.
2425
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
2526
- **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.

‎system/Database/SQLite3/Connection.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,13 @@ protected function _foreignKeyData(string $table): array
363363
return [];
364364
}
365365

366-
$query = $this->query("PRAGMA foreign_key_list({$table})")->getResult();
366+
$parts = explode('.', $table);
367+
$tableName = array_pop($parts);
368+
$schema = array_pop($parts);
369+
$pragma = ($schema === null || $schema === '' ? '' : $this->escapeIdentifier($schema) . '.')
370+
. 'foreign_key_list(' . $this->escape($tableName) . ')';
371+
372+
$query = $this->query('PRAGMA ' . $pragma)->getResult();
367373
$indexes = [];
368374

369375
foreach ($query as $row) {
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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\SQLite3;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use Config\Database;
18+
use PHPUnit\Framework\Attributes\Group;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[Group('DatabaseLive')]
24+
final class GetForeignKeyDataTest extends CIUnitTestCase
25+
{
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
$this->db = Database::connect($this->DBGroup);
31+
if ($this->db->DBDriver !== 'SQLite3') {
32+
$this->markTestSkipped('This test is only for SQLite3.');
33+
}
34+
35+
$config = [
36+
'DBDriver' => 'SQLite3',
37+
'database' => 'database.db',
38+
'DBDebug' => true,
39+
];
40+
$this->db = db_connect($config, false);
41+
}
42+
43+
public function testGetForeignKeyDataWithSpecialTableName(): void
44+
{
45+
$this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)');
46+
$this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))');
47+
48+
$foreignKeys = $this->db->getForeignKeyData('test-child');
49+
50+
$this->db->query('DROP TABLE IF EXISTS `test-child`');
51+
$this->db->query('DROP TABLE IF EXISTS `test-parent`');
52+
53+
$this->assertCount(1, $foreignKeys);
54+
$firstKey = reset($foreignKeys);
55+
$this->assertSame('test-child', $firstKey->table_name);
56+
$this->assertSame(['parent_id'], $firstKey->column_name);
57+
$this->assertSame('test-parent', $firstKey->foreign_table_name);
58+
$this->assertSame(['id'], $firstKey->foreign_column_name);
59+
}
60+
61+
public function testGetForeignKeyDataWithAttachedDatabase(): void
62+
{
63+
$this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`');
64+
65+
$this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)');
66+
$this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))');
67+
68+
$foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child');
69+
70+
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`');
71+
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`');
72+
$this->db->query('DETACH DATABASE `test_schema`');
73+
74+
$this->assertCount(1, $foreignKeys);
75+
$firstKey = reset($foreignKeys);
76+
$this->assertSame('test_schema.attached-child', $firstKey->table_name);
77+
$this->assertSame(['p_id'], $firstKey->column_name);
78+
$this->assertSame('attached-parent', $firstKey->foreign_table_name);
79+
$this->assertSame(['id'], $firstKey->foreign_column_name);
80+
}
81+
82+
public function testGetForeignKeyDataWithParenthesesInTableName(): void
83+
{
84+
$this->db->query('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY)');
85+
$this->db->query('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id))');
86+
87+
$foreignKeys = $this->db->getForeignKeyData('child(v2)');
88+
89+
$this->db->query('DROP TABLE IF EXISTS "child(v2)"');
90+
$this->db->query('DROP TABLE IF EXISTS "parent(v1)"');
91+
92+
$this->assertCount(1, $foreignKeys);
93+
$firstKey = reset($foreignKeys);
94+
$this->assertSame('child(v2)', $firstKey->table_name);
95+
$this->assertSame(['parent_id'], $firstKey->column_name);
96+
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
97+
$this->assertSame(['id'], $firstKey->foreign_column_name);
98+
}
99+
100+
public function testGetForeignKeyDataWithQuotesAndSpacesInTableName(): void
101+
{
102+
$this->db->query('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY)');
103+
$this->db->query('CREATE TABLE "child\'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id))');
104+
105+
$foreignKeys = $this->db->getForeignKeyData("child'quote with space");
106+
107+
$this->db->query('DROP TABLE IF EXISTS "child\'quote with space"');
108+
$this->db->query('DROP TABLE IF EXISTS "parent table"');
109+
110+
$this->assertCount(1, $foreignKeys);
111+
$firstKey = reset($foreignKeys);
112+
$this->assertSame("child'quote with space", $firstKey->table_name);
113+
$this->assertSame(['p_id'], $firstKey->column_name);
114+
$this->assertSame('parent table', $firstKey->foreign_table_name);
115+
$this->assertSame(['id'], $firstKey->foreign_column_name);
116+
}
117+
118+
public function testGetForeignKeyDataWithNumericAndReservedWordTableName(): void
119+
{
120+
$this->db->query('CREATE TABLE "order" (id INTEGER PRIMARY KEY)');
121+
$this->db->query('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id))');
122+
123+
$foreignKeys = $this->db->getForeignKeyData('12345');
124+
125+
$this->db->query('DROP TABLE IF EXISTS "12345"');
126+
$this->db->query('DROP TABLE IF EXISTS "order"');
127+
128+
$this->assertCount(1, $foreignKeys);
129+
$firstKey = reset($foreignKeys);
130+
$this->assertSame('12345', $firstKey->table_name);
131+
$this->assertSame(['order_id'], $firstKey->column_name);
132+
$this->assertSame('order', $firstKey->foreign_table_name);
133+
$this->assertSame(['id'], $firstKey->foreign_column_name);
134+
}
135+
136+
public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers(): void
137+
{
138+
$this->db->query('ATTACH DATABASE ":memory:" AS "schema-db(1)"');
139+
140+
$this->db->query('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY)');
141+
$this->db->query('CREATE TABLE "schema-db(1)"."child(v2)" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent(v1)"(id))');
142+
143+
$foreignKeys = $this->db->getForeignKeyData('schema-db(1).child(v2)');
144+
145+
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"');
146+
$this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"');
147+
$this->db->query('DETACH DATABASE "schema-db(1)"');
148+
149+
$this->assertCount(1, $foreignKeys);
150+
$firstKey = reset($foreignKeys);
151+
$this->assertSame('schema-db(1).child(v2)', $firstKey->table_name);
152+
$this->assertSame(['p_id'], $firstKey->column_name);
153+
$this->assertSame('parent(v1)', $firstKey->foreign_table_name);
154+
$this->assertSame(['id'], $firstKey->foreign_column_name);
155+
}
156+
}

0 commit comments

Comments
 (0)