Skip to content
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
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Release Date: Unreleased
## Bugs Fixed

- **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.
- **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping.
- **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.
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
- **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.
Expand Down
13 changes: 10 additions & 3 deletions system/Database/OCI8/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,15 @@ protected function _dropTable(string $table, bool $ifExists, bool $cascade)
*/
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
{
return "SELECT constraint_name FROM all_constraints WHERE table_name = '"
. trim($table, '"') . "' AND index_name = '"
. trim($constraintName, '"') . "'";
return sprintf(
<<<'SQL'
SELECT constraint_name
FROM all_constraints
WHERE table_name = %s
AND index_name = %s
SQL,
$this->db->escape(trim($table, '"')),
$this->db->escape(trim($constraintName, '"')),
);
}
}
21 changes: 14 additions & 7 deletions system/Database/Postgre/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,21 @@ protected function _dropTable(string $table, bool $ifExists, bool $cascade): str
*/
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
{
return "SELECT con.conname
FROM pg_catalog.pg_constraint con
return sprintf(
<<<'SQL'
SELECT con.conname
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel
ON rel.oid = con.conrelid
ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp
ON nsp.oid = connamespace
WHERE nsp.nspname = '{$this->db->schema}'
AND rel.relname = '" . trim($table, '"') . "'
AND con.conname = '" . trim($constraintName, '"') . "'";
ON nsp.oid = connamespace
WHERE nsp.nspname = %s
AND rel.relname = %s
AND con.conname = %s
SQL,
$this->db->escape($this->db->schema),
$this->db->escape(trim($table, '"')),
$this->db->escape(trim($constraintName, '"')),
);
}
}
13 changes: 10 additions & 3 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,15 @@ protected function _dropTable(string $table, bool $ifExists, bool $cascade): str
*/
protected function _dropKeyAsConstraint(string $table, string $constraintName): string
{
return "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME= '" . trim($table, '"') . "'
AND CONSTRAINT_NAME = '" . trim($constraintName, '"') . "'";
return sprintf(
<<<'SQL'
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = %s
AND CONSTRAINT_NAME = %s
SQL,
$this->db->escape(trim($table, '"')),
$this->db->escape(trim($constraintName, '"')),
);
}
}
109 changes: 109 additions & 0 deletions tests/system/Database/Forge/DropKeyAsConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Database\Forge;

use CodeIgniter\Database\OCI8\Connection as OCI8Connection;
use CodeIgniter\Database\OCI8\Forge as OCI8Forge;
use CodeIgniter\Database\Postgre\Connection as PostgreConnection;
use CodeIgniter\Database\Postgre\Forge as PostgreForge;
use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection;
use CodeIgniter\Database\SQLSRV\Forge as SQLSRVForge;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('Others')]
final class DropKeyAsConstraintTest extends CIUnitTestCase
{
protected $db;

protected function setUp(): void
{
parent::setUp();

$this->db = new MockConnection([]);
}

public function testPostgreDropKeyAsConstraintEscaping(): void
{
$this->setPrivateProperty($this->db, 'schema', "my'schema");

/** @var PostgreConnection $db */
$db = $this->db;

$forge = new PostgreForge($db);
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');

$sql = $method("user's", "fk'id");

$expected = <<<'SQL'
SELECT con.conname
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel
ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp
ON nsp.oid = connamespace
WHERE nsp.nspname = 'my''schema'
AND rel.relname = 'user''s'
AND con.conname = 'fk''id'
SQL;

$this->assertSame($expected, $sql);
}

public function testOCI8DropKeyAsConstraintEscaping(): void
{
/** @var OCI8Connection $db */
$db = $this->db;

$forge = new OCI8Forge($db);
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');

$sql = $method("user's", "fk'id");

$expected = <<<'SQL'
SELECT constraint_name
FROM all_constraints
WHERE table_name = 'user''s'
AND index_name = 'fk''id'
SQL;

$this->assertSame($expected, $sql);
}

public function testSQLSRVDropKeyAsConstraintEscaping(): void
{
$this->setPrivateProperty($this->db, 'schema', 'dbo');

/** @var SQLSRVConnection $db */
$db = $this->db;

$forge = new SQLSRVForge($db);
$method = $this->getPrivateMethodInvoker($forge, '_dropKeyAsConstraint');

$sql = $method("user's", "fk'id");

$expected = <<<'SQL'
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = 'user''s'
AND CONSTRAINT_NAME = 'fk''id'
SQL;

$this->assertSame($expected, $sql);
}
}
Loading