From fd49d5691b2220e6c0d3d4e99ef586b1ed9a9637 Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Fri, 20 Nov 2026 16:28:48 -0300 Subject: [PATCH 1/5] Refuse where operators that are not SQL operators Compiler::condition() concatenated the operator string into the statement, so where($column, $_GET['op'], $value) with "= 1 OR 1=1 OR id =" was SQL injection. String operators are now checked against a list of known operators (comparison, LIKE/ILIKE, IN, BETWEEN, IS [NOT] [DISTINCT FROM], REGEXP, SIMILAR TO, Postgres and bitwise operators); others throw CompilerException. A Fragment still passes any operator explicitly. --- src/Database/Driver/Compiler.php | 32 +++++++++++ .../Unit/Driver/OperatorAllowlistTest.php | 56 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tests/Database/Unit/Driver/OperatorAllowlistTest.php diff --git a/src/Database/Driver/Compiler.php b/src/Database/Driver/Compiler.php index b851f83..53806c3 100644 --- a/src/Database/Driver/Compiler.php +++ b/src/Database/Driver/Compiler.php @@ -701,6 +701,8 @@ protected function condition(QueryParameters $params, Quoter $q, array $context) $operator = $this->fragment($params, $q, $operator); } elseif (!\is_string($operator)) { throw new CompilerException('Invalid operator type, string or fragment is expected'); + } else { + $operator = self::assertOperator($operator); } if ($value instanceof FragmentInterface) { @@ -742,6 +744,36 @@ protected function condition(QueryParameters $params, Quoter $q, array $context) return $operator . ' ' . $placeholder; } + /** + * Operators a string may name; anything else (a value smuggled into the operator position) + * is refused. Use an Expression for an operator outside this list. + */ + private const OPERATORS = [ + '=' => true, '==' => true, '!=' => true, '<>' => true, '<' => true, '>' => true, '<=' => true, + '>=' => true, '<=>' => true, + 'LIKE' => true, 'NOT LIKE' => true, 'ILIKE' => true, 'NOT ILIKE' => true, + 'IN' => true, 'NOT IN' => true, 'BETWEEN' => true, 'NOT BETWEEN' => true, + 'IS' => true, 'IS NOT' => true, 'IS DISTINCT FROM' => true, 'IS NOT DISTINCT FROM' => true, + 'REGEXP' => true, 'NOT REGEXP' => true, 'RLIKE' => true, 'NOT RLIKE' => true, + 'SIMILAR TO' => true, 'NOT SIMILAR TO' => true, 'GLOB' => true, 'NOT GLOB' => true, + 'MATCH' => true, '~' => true, '~*' => true, '!~' => true, '!~*' => true, + '@>' => true, '<@' => true, '&&' => true, '@@' => true, '&' => true, '|' => true, '^' => true, + '<<' => true, '>>' => true, + ]; + + /** @return non-empty-string the operator, upper-cased with single spaces */ + private static function assertOperator(string $operator): string + { + $normalized = \strtoupper(\trim((string) \preg_replace('/\s+/', ' ', $operator))); + if (!isset(self::OPERATORS[$normalized])) { + throw new CompilerException(\sprintf( + 'Unknown where operator %s; use an Expression for operators outside the allowed list', + \json_encode($operator, \JSON_INVALID_UTF8_SUBSTITUTE), + )); + } + return $normalized; + } + /** * Combine expression with prefix/postfix (usually SQL keyword) but only if expression is not * empty. diff --git a/tests/Database/Unit/Driver/OperatorAllowlistTest.php b/tests/Database/Unit/Driver/OperatorAllowlistTest.php new file mode 100644 index 0000000..5b301cb --- /dev/null +++ b/tests/Database/Unit/Driver/OperatorAllowlistTest.php @@ -0,0 +1,56 @@ +', '>', '<', '>=', '<=', '<=>', 'like', 'NOT LIKE', 'ilike', 'regexp', 'is', 'IS NOT', '@>', '<@', '~*'] as $operator) { + yield [$operator]; + } + } + + /** @dataProvider injectedOperators */ + public function testRejectsOperatorsThatAreNotOperators(string $operator): void + { + $this->expectException(CompilerException::class); + $this->compile((new SelectQuery())->from('users')->where('id', $operator, 1)); + } + + /** @dataProvider knownOperators */ + public function testAcceptsKnownOperators(string $operator): void + { + $sql = $this->compile((new SelectQuery())->from('users')->where('id', $operator, 1)); + self::assertStringContainsString('?', $sql); + } + + public function testRawFragmentsStayExplicit(): void + { + $sql = $this->compile((new SelectQuery())->from('users')->where('id', new Fragment('= ANY'), new Fragment('(1)'))); + self::assertStringContainsString('= ANY', $sql); + } + + private function compile(SelectQuery $query): string + { + return (new MySQLCompiler('``'))->compile(new QueryParameters(), '', $query); + } +} From 4e99837ac0f328f7dd73c10f0c0fa73342ade5bd Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Fri, 20 Nov 2026 16:28:48 -0300 Subject: [PATCH 2/5] Refuse quotes and backslashes in JSON paths JsonExpression::wrapPath() escaped quotes with the regex "/(\\+)?'/", which PHP reads as (\+)?' and so matches a literal plus, not backslashes: in MySQL's default sql_mode orderBy("data->x\\' OR SLEEP(5) -- ") ended the string literal. Path segments with a quote, double quote or backslash now throw DriverException instead of being escaped. --- src/Database/Injection/JsonExpression.php | 6 ++++- .../Unit/Injection/JsonExpressionTest.php | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Database/Injection/JsonExpression.php b/src/Database/Injection/JsonExpression.php index 712d404..7c860c6 100644 --- a/src/Database/Injection/JsonExpression.php +++ b/src/Database/Injection/JsonExpression.php @@ -151,7 +151,11 @@ protected function getQuotes(): string */ private function wrapPath(string $value, string $delimiter = '->'): string { - $value = \preg_replace("/(\\+)?'/", "''", $value); + // Quotes and backslashes are refused instead of escaped: the escaping depended on the + // server's sql_mode (a backslash before a quote ended the string in MySQL). + if (\strpbrk($value, "'\"\\") !== false) { + throw new DriverException('JSON path segments cannot contain quotes or backslashes.'); + } $segments = \explode($delimiter, $value); $path = \implode('.', \array_map(fn(string $segment): string => $this->wrapPathSegment($segment), $segments)); diff --git a/tests/Database/Unit/Injection/JsonExpressionTest.php b/tests/Database/Unit/Injection/JsonExpressionTest.php index 45c427b..f2d4885 100644 --- a/tests/Database/Unit/Injection/JsonExpressionTest.php +++ b/tests/Database/Unit/Injection/JsonExpressionTest.php @@ -194,4 +194,26 @@ public function __construct() protected function compile(string $statement): string {} }; } + + public static function unsafePathDataProvider(): \Traversable + { + // A backslash before the quote used to survive the escaping in MySQL's default sql_mode. + yield ["x\\' OR SLEEP(5) -- "]; + yield ["x' OR SLEEP(5) -- "]; + yield ['x" OR 1']; + yield ['x\\']; + } + + /** + * @dataProvider unsafePathDataProvider + */ + public function testWrapPathRejectsQuotesAndBackslashes(string $value): void + { + $expression = $this->createExpression(); + $ref = new \ReflectionMethod($expression, 'wrapPath'); + $ref->setAccessible(true); + + $this->expectException(DriverException::class); + $ref->invoke($expression, $value); + } } From 6ce6ab07a16ec3b7fdbed91d924528fa15edca86 Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Fri, 20 Nov 2026 22:48:42 -0300 Subject: [PATCH 3/5] Skip operator normalization for operators already in canonical form --- src/Database/Driver/Compiler.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Database/Driver/Compiler.php b/src/Database/Driver/Compiler.php index 53806c3..18da819 100644 --- a/src/Database/Driver/Compiler.php +++ b/src/Database/Driver/Compiler.php @@ -764,6 +764,9 @@ protected function condition(QueryParameters $params, Quoter $q, array $context) /** @return non-empty-string the operator, upper-cased with single spaces */ private static function assertOperator(string $operator): string { + if (isset(self::OPERATORS[$operator])) { + return $operator; + } $normalized = \strtoupper(\trim((string) \preg_replace('/\s+/', ' ', $operator))); if (!isset(self::OPERATORS[$normalized])) { throw new CompilerException(\sprintf( From 0138aedf713d6667cbec528dd1e8f0124624e702 Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Fri, 20 Nov 2026 22:48:42 -0300 Subject: [PATCH 4/5] MySQL: server-side prepares and utf8mb4 by default --- .../Config/MySQL/ConnectionConfig.php | 4 ++- .../Config/MySQL/SocketConnectionConfig.php | 2 +- .../Config/MySQL/TcpConnectionConfig.php | 2 +- .../Unit/Config/MySQL/SafeDefaultsTest.php | 36 +++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/Database/Unit/Config/MySQL/SafeDefaultsTest.php diff --git a/src/Database/Config/MySQL/ConnectionConfig.php b/src/Database/Config/MySQL/ConnectionConfig.php index 379b929..129ffa2 100644 --- a/src/Database/Config/MySQL/ConnectionConfig.php +++ b/src/Database/Config/MySQL/ConnectionConfig.php @@ -28,8 +28,10 @@ abstract class ConnectionConfig extends BaseConnectionConfig * @todo Replace with {@see \Pdo\Mysql::ATTR_INIT_COMMAND} when minimum PHP version is 8.5 * @todo Should be moved into common driver settings? */ - 1002 => 'SET NAMES "UTF8"', + 1002 => 'SET NAMES utf8mb4', \PDO::ATTR_STRINGIFY_FETCHES => false, + // Server-side prepares: values are never escaped and interpolated by the client. + \PDO::ATTR_EMULATE_PREPARES => false, ]; /** diff --git a/src/Database/Config/MySQL/SocketConnectionConfig.php b/src/Database/Config/MySQL/SocketConnectionConfig.php index e6513c8..d5307cc 100644 --- a/src/Database/Config/MySQL/SocketConnectionConfig.php +++ b/src/Database/Config/MySQL/SocketConnectionConfig.php @@ -26,7 +26,7 @@ class SocketConnectionConfig extends ConnectionConfig implements ProvidesSourceS public function __construct( public string $database, public string $socket, - public ?string $charset = null, + public ?string $charset = 'utf8mb4', ?string $user = null, ?string $password = null, array $options = [], diff --git a/src/Database/Config/MySQL/TcpConnectionConfig.php b/src/Database/Config/MySQL/TcpConnectionConfig.php index db97d97..51e1381 100644 --- a/src/Database/Config/MySQL/TcpConnectionConfig.php +++ b/src/Database/Config/MySQL/TcpConnectionConfig.php @@ -33,7 +33,7 @@ public function __construct( public string $database, public string $host = 'localhost', int|string $port = 3307, - public ?string $charset = null, + public ?string $charset = 'utf8mb4', ?string $user = null, ?string $password = null, array $options = [], diff --git a/tests/Database/Unit/Config/MySQL/SafeDefaultsTest.php b/tests/Database/Unit/Config/MySQL/SafeDefaultsTest.php new file mode 100644 index 0000000..30c5080 --- /dev/null +++ b/tests/Database/Unit/Config/MySQL/SafeDefaultsTest.php @@ -0,0 +1,36 @@ +getOptions(); + // Real prepares: values never go through client-side escaping. + self::assertFalse($options[\PDO::ATTR_EMULATE_PREPARES]); + // utf8 is the 3-byte utf8mb3, which rejects emoji and other 4-byte characters. + self::assertSame('SET NAMES utf8mb4', $options[1002]); + } + self::assertStringEndsWith(';charset=utf8mb4', (new TcpConnectionConfig('app'))->getDsn()); + self::assertStringEndsWith(';charset=utf8mb4', (new SocketConnectionConfig('app', '/run/mysqld.sock'))->getDsn()); + } + + public function testOptionsStillOverrideTheDefaults(): void + { + $config = new TcpConnectionConfig('app', options: [\PDO::ATTR_EMULATE_PREPARES => true]); + self::assertTrue($config->getOptions()[\PDO::ATTR_EMULATE_PREPARES]); + } +} From 8727f74ab2a6557ba9e1fe45f34410db9aaf6ada Mon Sep 17 00:00:00 2001 From: davidbalbino Date: Fri, 20 Nov 2026 22:48:42 -0300 Subject: [PATCH 5/5] Bound the prepared statement and compiled query caches --- src/Database/Driver/CompilerCache.php | 27 ++++++++--- src/Database/Driver/Driver.php | 7 +++ .../Unit/Driver/BoundedCachesTest.php | 48 +++++++++++++++++++ tests/ORM/Unit/Command/StatementPlansTest.php | 43 +++++++++++++++++ 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 tests/Database/Unit/Driver/BoundedCachesTest.php create mode 100644 tests/ORM/Unit/Command/StatementPlansTest.php diff --git a/src/Database/Driver/CompilerCache.php b/src/Database/Driver/CompilerCache.php index 07328be..9dc4b2d 100644 --- a/src/Database/Driver/CompilerCache.php +++ b/src/Database/Driver/CompilerCache.php @@ -29,6 +29,9 @@ */ final class CompilerCache implements CompilerInterface { + /** Compiled statements kept per compiler; the oldest is dropped past this. */ + public const LIMIT = 2048; + private array $cache = []; public function __construct( @@ -55,11 +58,11 @@ public function compile(QueryParameters $params, string $prefix, FragmentInterfa return $this->cache[$queryHash]; } - return $this->cache[$queryHash] = $this->compiler->compile( + return $this->store($queryHash, $this->compiler->compile( new QueryParameters(), $prefix, $fragment, - ); + )); } if ($fragment->getType() === self::INSERT_QUERY) { @@ -71,11 +74,11 @@ public function compile(QueryParameters $params, string $prefix, FragmentInterfa return $this->cache[$queryHash]; } - return $this->cache[$queryHash] = $this->compiler->compile( + return $this->store($queryHash, $this->compiler->compile( new QueryParameters(), $prefix, $fragment, - ); + )); } } @@ -88,11 +91,11 @@ public function compile(QueryParameters $params, string $prefix, FragmentInterfa return $this->cache[$queryHash]; } - return $this->cache[$queryHash] = $this->compiler->compile( + return $this->store($queryHash, $this->compiler->compile( new QueryParameters(), $prefix, $fragment, - ); + )); } } @@ -442,4 +445,16 @@ private function hashOrderBy(QueryParameters $params, array $tokens): string return $hash; } + + /** + * @param non-empty-string $sql + * @return non-empty-string + */ + private function store(string $hash, string $sql): string + { + if (\count($this->cache) >= self::LIMIT) { + unset($this->cache[\array_key_first($this->cache)]); + } + return $this->cache[$hash] = $sql; + } } diff --git a/src/Database/Driver/Driver.php b/src/Database/Driver/Driver.php index 1b3e7d0..8444b41 100644 --- a/src/Database/Driver/Driver.php +++ b/src/Database/Driver/Driver.php @@ -51,6 +51,9 @@ abstract class Driver implements DriverInterface, NamedInterface, LoggerAwareInt /** @var \PDOStatement[]|PDOStatementInterface[] */ protected array $queryCache = []; + /** Prepared statements kept per connection; the oldest is dropped past this. */ + public const QUERY_CACHE_LIMIT = 512; + private ?string $name = null; private ?\DateTimeZone $timezone = null; private ?string $timezoneName = null; @@ -598,6 +601,10 @@ protected function prepare(string $query): \PDOStatement|PDOStatementInterface $statement = $this->getPDO()->prepare($query); if ($this->useCache) { + if (\count($this->queryCache) >= static::QUERY_CACHE_LIMIT) { + // Bounded for long-lived workers: drop the oldest prepared statement. + unset($this->queryCache[\array_key_first($this->queryCache)]); + } $this->queryCache[$query] = $statement; } diff --git a/tests/Database/Unit/Driver/BoundedCachesTest.php b/tests/Database/Unit/Driver/BoundedCachesTest.php new file mode 100644 index 0000000..7664a51 --- /dev/null +++ b/tests/Database/Unit/Driver/BoundedCachesTest.php @@ -0,0 +1,48 @@ +query("SELECT {$index}")->fetchAll(); + } + $cache = (new \ReflectionProperty(Driver::class, 'queryCache'))->getValue($driver); + self::assertLessThanOrEqual(Driver::QUERY_CACHE_LIMIT, \count($cache)); + // The most recent statements are still cached. + self::assertArrayHasKey('SELECT ' . (Driver::QUERY_CACHE_LIMIT + 199), $cache); + } + + public function testTheCompiledQueryCacheIsBounded(): void + { + $compiler = new CompilerCache(new PostgresCompiler()); + for ($size = 1; $size <= CompilerCache::LIMIT + 300; $size++) { + $compiler->compile( + new QueryParameters(), + '', + (new SelectQuery())->from('users')->where('id', 'IN', \range(1, $size)), + ); + } + $cache = (new \ReflectionProperty(CompilerCache::class, 'cache'))->getValue($compiler); + self::assertLessThanOrEqual(CompilerCache::LIMIT, \count($cache)); + } +} diff --git a/tests/ORM/Unit/Command/StatementPlansTest.php b/tests/ORM/Unit/Command/StatementPlansTest.php new file mode 100644 index 0000000..617fe91 --- /dev/null +++ b/tests/ORM/Unit/Command/StatementPlansTest.php @@ -0,0 +1,43 @@ +execute('CREATE TABLE items (id INTEGER PRIMARY KEY, value TEXT)'); + $limit = (new \ReflectionClassConstant(StatementPlans::class, 'MAX_PLANS'))->getValue(); + + for ($index = 0; $index < $limit + 100; $index++) { + // Every column set makes a new plan key; the plan is cached before it runs, so a + // column the table lacks still fills the cache. + try { + StatementPlans::update($db, 'items', ['value' => 'x'], ["c{$index}" => 1]); + } catch (\Throwable) { + } + } + + $plans = (new \ReflectionProperty(StatementPlans::class, 'plans'))->getValue(); + self::assertInstanceOf(\WeakMap::class, $plans); + self::assertLessThanOrEqual($limit, \count($plans[$db])); + + // Past the cap a new column set is not planned: the caller uses the query builder. + self::assertSame([false, null], StatementPlans::insert($db, 'items', ['id' => 1, 'value' => 'a'], [])); + self::assertNull(StatementPlans::update($db, 'items', ['value' => 'b'], ['id' => 1])); + } +}