Skip to content

Commit 6e0a6f8

Browse files
committed
fix(Database): escape table identifiers in SQLite3 foreign_key_list PRAGMA
1 parent c431aa1 commit 6e0a6f8

3 files changed

Lines changed: 94 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
@@ -17,6 +17,7 @@ Release Date: Unreleased
1717
## Bugs Fixed
1818

1919
- **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers.
20+
- **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.
2021
- **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.
2122
- **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.
2223
- **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/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 ? '' : $this->escapeIdentifiers($schema) . '.')
370+
. 'foreign_key_list(' . $this->escapeIdentifiers($tableName) . ')';
371+
372+
$query = $this->query('PRAGMA ' . $pragma)->getResult();
367373
$indexes = [];
368374

369375
foreach ($query as $row) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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('DROP TABLE IF EXISTS `test-parent`');
46+
$this->db->query('DROP TABLE IF EXISTS `test-child`');
47+
48+
$this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)');
49+
$this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))');
50+
51+
$foreignKeys = $this->db->getForeignKeyData('test-child');
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+
$this->db->query('DROP TABLE IF EXISTS `test-child`');
61+
$this->db->query('DROP TABLE IF EXISTS `test-parent`');
62+
}
63+
64+
public function testGetForeignKeyDataWithAttachedDatabase(): void
65+
{
66+
$this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`');
67+
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`');
68+
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`');
69+
70+
$this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)');
71+
$this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))');
72+
73+
$foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child');
74+
75+
$this->assertCount(1, $foreignKeys);
76+
$firstKey = reset($foreignKeys);
77+
$this->assertSame('test_schema.attached-child', $firstKey->table_name);
78+
$this->assertSame(['p_id'], $firstKey->column_name);
79+
$this->assertSame('attached-parent', $firstKey->foreign_table_name);
80+
$this->assertSame(['id'], $firstKey->foreign_column_name);
81+
82+
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`');
83+
$this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`');
84+
$this->db->query('DETACH DATABASE `test_schema`');
85+
}
86+
}

0 commit comments

Comments
 (0)