Skip to content

Commit 36f7f00

Browse files
committed
fix: require AS keyword for function/subquery aliases in isIdentifierEscapeExempt to prevent SQL injection bypass
1 parent 17d700d commit 36f7f00

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

system/Database/BaseConnection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ public function escapeIdentifier($item): string
14401440
private function isIdentifierEscapeExempt(string $item): bool
14411441
{
14421442
$item = trim($item);
1443-
1443+
14441444
if ($item === '') {
14451445
return false;
14461446
}
@@ -1458,7 +1458,7 @@ private function isIdentifierEscapeExempt(string $item): bool
14581458
// SQL functions or subqueries (e.g. MAX(id), (SELECT ...)) with an optional alias
14591459
if (str_contains($item, '(')) {
14601460
// Regex matching balanced parentheses (from start to end or with a safe alias)
1461-
if (preg_match('/^(?:[a-zA-Z0-9_.]+\s*)?(?P<parens>\((?:[^()]+|(?&parens))*\))(?:\s+(?:AS\s+)?(?:[a-zA-Z0-9_.]+|"[^"]*"|\'[^\']*\'|`[^`]*`))?$/is', $item)) {
1461+
if (preg_match('/^(?:[a-zA-Z0-9_.]+\s*)?(?P<parens>\((?:[^()]+|(?&parens))*\))(?:\s+AS\s+(?:[a-zA-Z0-9_.]+|"[^"]*"|\'[^\']*\'|`[^`]*`))?$/is', $item)) {
14621462
return true;
14631463
}
14641464
}

tests/system/Database/BaseConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public static function provideProtectIdentifiers(): iterable
445445
true,
446446
true,
447447
'COUNT(id) OR 1=1',
448-
'COUNT(id) OR "1=1"',
448+
'"COUNT(id) OR" "1=1"',
449449
],
450450
'SQLi: Unbalanced parenthesis attack' => [
451451
false,

0 commit comments

Comments
 (0)