Skip to content

Commit 947a7fe

Browse files
authored
fix(Database): escape table string in SQLSRV Builder::_upsertBatch() Object_ID (#10734)
1 parent f0734b5 commit 947a7fe

3 files changed

Lines changed: 47 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 `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters.
2021
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
2122
- **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.
2223
- **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/SQLSRV/Builder.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
685685
$constraints = $this->QBOptions['constraints'] ?? [];
686686

687687
$tableIdentity = $this->QBOptions['tableIdentity'] ?? '';
688-
$sql = "SELECT name from syscolumns where id = Object_ID('" . $table . "') and colstat = 1";
688+
$sql = 'SELECT name from syscolumns where id = Object_ID(' . $this->db->escape($table) . ') and colstat = 1';
689689
if (($query = $this->db->query($sql)) === false) {
690690
throw new DatabaseException('Failed to get table identity');
691691
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Builder;
15+
16+
use CodeIgniter\Database\SQLSRV\Builder;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\Mock\MockConnection;
19+
use PHPUnit\Framework\Attributes\Group;
20+
use Throwable;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('Others')]
26+
final class SQLSRVBuilderTest extends CIUnitTestCase
27+
{
28+
public function testUpsertBatchEscapesTableNameInObjectID(): void
29+
{
30+
$db = new MockConnection(['DBDriver' => 'SQLSRV']);
31+
$builder = new Builder("user's", $db);
32+
33+
$method = self::getPrivateMethodInvoker($builder, '_upsertBatch');
34+
35+
try {
36+
$method("user's", ['id', 'name'], [['1', 'Alice']]);
37+
} catch (Throwable) {
38+
}
39+
40+
$this->assertSame(
41+
"SELECT name from syscolumns where id = Object_ID('user''s') and colstat = 1",
42+
(string) $db->getLastQuery(),
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)