Skip to content

Commit 9e024ee

Browse files
committed
fix: escapeIdentifiers and protectIdentifiers SQL injection bypass
Implemented strict regex validation for SQL function calls and string literals in BaseConnection::isSafeToBypassEscape() to prevent SQL injection vulnerabilities. Updated tests to match the new secure behavior and added explicit SQL injection test cases.
1 parent ac18654 commit 9e024ee

2 files changed

Lines changed: 88 additions & 18 deletions

File tree

‎system/Database/BaseConnection.php‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,9 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro
12811281
// Added exception for single quotes as well, we don't want to alter
12821282
// literal strings.
12831283
if (strcspn($item, "()'") !== strlen($item)) {
1284-
/** @psalm-suppress NoValue I don't know why ERROR. */
1285-
return $item;
1284+
if ($this->isSafeToBypassEscape($item)) {
1285+
return $item;
1286+
}
12861287
}
12871288

12881289
// Do not protect identifiers and do not prefix, no swap prefix, there is nothing to do
@@ -1433,6 +1434,34 @@ public function escapeIdentifier($item): string
14331434
. $this->escapeChar;
14341435
}
14351436

1437+
/**
1438+
* Checks if an identifier with parentheses or quotes is a safe function call or a string literal.
1439+
*/
1440+
private function isSafeToBypassEscape(string $item): bool
1441+
{
1442+
$item = trim($item);
1443+
1444+
// String literals starting and ending with single quotes
1445+
if ($item[0] === "'" && preg_match('/^\'(?:[^\']|\'\')*\'$/s', $item)) {
1446+
return true;
1447+
}
1448+
1449+
// String literals (for PostgreSQL it can be double quotes)
1450+
if ($this->escapeChar !== '"' && $item[0] === '"' && preg_match('/^"(?:[^"]|"")*"$/s', $item)) {
1451+
return true;
1452+
}
1453+
1454+
// SQL functions or subqueries (e.g. MAX(id), (SELECT ...)) with an optional alias
1455+
if (str_contains($item, '(')) {
1456+
// Regex matching balanced parentheses (from start to end or with a safe alias)
1457+
if (preg_match('/^(?:[a-zA-Z0-9_.]+\s*)?(?P<parens>\((?:[^()]+|(?&parens))*\))(?:\s+(?:AS\s+)?(?:[a-zA-Z0-9_.]+|"[^"]*"|\'[^\']*\'|`[^`]*`))?$/is', $item)) {
1458+
return true;
1459+
}
1460+
}
1461+
1462+
return false;
1463+
}
1464+
14361465
/**
14371466
* Returns escaped table name with alias.
14381467
*/
@@ -1467,11 +1496,7 @@ public function escapeIdentifiers($item)
14671496
return $item;
14681497
}
14691498

1470-
// Avoid breaking functions and literal values inside queries
1471-
if (ctype_digit($item)
1472-
|| $item[0] === "'"
1473-
|| ($this->escapeChar !== '"' && $item[0] === '"')
1474-
|| str_contains($item, '(')) {
1499+
if (ctype_digit($item) || $this->isSafeToBypassEscape($item)) {
14751500
return $item;
14761501
}
14771502

‎tests/system/Database/BaseConnectionTest.php‎

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,16 @@ public static function provideProtectIdentifiers(): iterable
367367
'table.column' => [false, true, true, 'users.id', '"test_users"."id"'], // Prefixed because it has segments
368368
'table.column prefix' => [true, true, true, 'users.id', '"test_users"."id"'],
369369
'table.column AS' => [
370-
false, true, true,
370+
false,
371+
true,
372+
true,
371373
'users.id AS user_id',
372374
'"test_users"."id" AS "user_id"', // Prefixed because it has segments
373375
],
374376
'table.column AS prefix' => [
375-
true, true, true,
377+
true,
378+
true,
379+
true,
376380
'users.id AS user_id',
377381
'"test_users"."id" AS "user_id"',
378382
],
@@ -384,42 +388,78 @@ public static function provideProtectIdentifiers(): iterable
384388
'function column' => [false, true, true, 'SUM(id)', 'SUM(id)'],
385389

386390
'function column AS' => [
387-
false, true, true,
391+
false,
392+
true,
393+
true,
388394
'COUNT(payments) AS myAlias',
389395
'COUNT(payments) AS myAlias',
390396
],
391397
'function column AS prefix' => [
392-
true, true, true,
398+
true,
399+
true,
400+
true,
393401
'COUNT(payments) AS myAlias',
394402
'COUNT(payments) AS myAlias',
395403
],
396404

397405
'function quoted table column AS' => [
398-
false, true, true,
406+
false,
407+
true,
408+
true,
399409
'MAX("db"."payments") AS "payments"',
400410
'MAX("db"."payments") AS "payments"',
401411
],
402412

403413
'quoted column operator AS' => [
404-
false, true, true,
414+
false,
415+
true,
416+
true,
405417
'"numericValue1" + "numericValue2" AS "numericResult"',
406418
'"numericValue1"" + ""numericValue2" AS "numericResult"', // Cannot process correctly
407419
],
408420
'quoted column operator AS no-protect' => [
409-
false, false, true,
421+
false,
422+
false,
423+
true,
410424
'"numericValue1" + "numericValue2" AS "numericResult"',
411425
'"numericValue1" + "numericValue2" AS "numericResult"',
412426
],
413427

414428
'sub query' => [
415-
false, true, true,
416-
'(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4) AS amount_paid)',
429+
false,
430+
true,
431+
true,
417432
'(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4) AS amount_paid)',
433+
'(SELECT SUM(payments.test_amount) FROM payments WHERE payments.invoice_id=4) AS "amount_paid)"',
418434
],
419435
'sub query with missing `)` at the end' => [
420-
false, true, true,
421-
'(SELECT MAX(advance_amount) FROM "orders" WHERE "id" > 2',
436+
false,
437+
true,
438+
true,
422439
'(SELECT MAX(advance_amount) FROM "orders" WHERE "id" > 2',
440+
'"(SELECT MAX(advance_amount) FROM ""orders"" WHERE ""id"" >" 2',
441+
],
442+
443+
'SQLi: Trailing operators after valid function' => [
444+
false,
445+
true,
446+
true,
447+
'COUNT(id) OR 1=1',
448+
'COUNT(id) OR "1=1"',
449+
],
450+
'SQLi: Unbalanced parenthesis attack' => [
451+
false,
452+
true,
453+
true,
454+
'id) OR (1=1',
455+
'"id) OR" "(1=1"',
456+
],
457+
'SQLi: String literal bypass attempt' => [
458+
false,
459+
true,
460+
true,
461+
"'admin' OR 1=1",
462+
'"\'admin\' OR" "1=1"',
423463
],
424464
];
425465
}
@@ -446,6 +486,11 @@ public static function provideEscapeIdentifiers(): iterable
446486
// $item, $expected
447487
'simple' => ['test', '"test"'],
448488
'with dots' => ['com.sitedb.web', '"com"."sitedb"."web"'],
489+
490+
// Security Tests: SQL Injection Bypasses in escapeIdentifiers
491+
'SQLi: Trailing operators after valid function' => ['COUNT(id) OR 1=1', '"COUNT(id) OR 1=1"'],
492+
'SQLi: Unbalanced parenthesis attack' => ['id) OR (1=1', '"id) OR (1=1"'],
493+
'SQLi: String literal bypass attempt' => ["'admin' OR 1=1", '"\'admin\' OR 1=1"'],
449494
];
450495
}
451496

0 commit comments

Comments
 (0)