Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SQLite3\Table::copyData() does not escape column names #6055

Merged
merged 4 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Events\Events;
use stdClass;
use Throwable;

/**
Expand Down Expand Up @@ -974,6 +975,7 @@ public function getConnectDuration(int $decimals = 6): string
* @param bool $fieldExists Supplied $item contains a column name?
*
* @return array|string
* @phpstan-return ($item is array ? array : string)
*/
public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $protectIdentifiers = null, bool $fieldExists = true)
{
Expand Down Expand Up @@ -1444,7 +1446,7 @@ public function fieldExists(string $fieldName, string $tableName): bool
/**
* Returns an object with field data
*
* @return array
* @return stdClass[]
*/
public function getFieldData(string $table)
{
Expand Down
18 changes: 14 additions & 4 deletions system/Database/SQLite3/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Table
* All of the fields this table represents.
*
* @var array
* @phpstan-var array<string, array<string, bool|int|string|null>>
*/
protected $fields = [];

Expand Down Expand Up @@ -276,10 +277,18 @@ protected function copyData()
$exFields[] = $name;
}

$exFields = implode(', ', $exFields);
$newFields = implode(', ', $newFields);

$this->db->query("INSERT INTO {$this->prefixedTableName}({$newFields}) SELECT {$exFields} FROM {$this->db->DBPrefix}temp_{$this->tableName}");
$exFields = implode(
', ',
array_map(fn ($item) => $this->db->protectIdentifiers($item), $exFields)
);
$newFields = implode(
', ',
array_map(fn ($item) => $this->db->protectIdentifiers($item), $newFields)
);

$this->db->query(
"INSERT INTO {$this->prefixedTableName}({$newFields}) SELECT {$exFields} FROM {$this->db->DBPrefix}temp_{$this->tableName}"
);
}

/**
Expand All @@ -289,6 +298,7 @@ protected function copyData()
* @param array|bool $fields
*
* @return mixed
* @phpstan-return ($fields is array ? array : mixed)
*/
protected function formatFields($fields)
{
Expand Down
10 changes: 8 additions & 2 deletions tests/system/Database/Live/SQLite/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function setUp(): void
$config = [
'DBDriver' => 'SQLite3',
'database' => 'database.db',
'DBDebug' => true,
];

$this->db = db_connect($config);
Expand Down Expand Up @@ -88,7 +89,7 @@ public function testFromTableFillsDetails()

$fields = $this->getPrivateProperty($this->table, 'fields');

$this->assertCount(4, $fields);
$this->assertCount(5, $fields);
$this->assertArrayHasKey('id', $fields);
$this->assertNull($fields['id']['default']);
$this->assertTrue($fields['id']['null']);
Expand Down Expand Up @@ -219,7 +220,7 @@ public function testProcessCopiesOldData()
->dropColumn('name')
->run();

$this->dontSeeInDatabase('foo', ['name' => 'George Clinton']);
$this->assertFalse($this->db->fieldExists('name', 'foo'));
$this->seeInDatabase('foo', ['email' => 'funkalicious@example.com']);
}

Expand Down Expand Up @@ -264,6 +265,11 @@ protected function createTable(string $tableName = 'foo')
'constraint' => 11,
'unsigned' => true,
],
'group' => [
'type' => 'varchar',
'constraint' => 255,
'null' => true,
],
]);
$this->forge->addPrimaryKey('id');
$this->forge->addKey('name');
Expand Down