Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/Database/Config/MySQL/ConnectionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Config/MySQL/SocketConnectionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Config/MySQL/TcpConnectionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down
35 changes: 35 additions & 0 deletions src/Database/Driver/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -742,6 +744,39 @@ 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
{
if (isset(self::OPERATORS[$operator])) {
return $operator;
}
$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.
Expand Down
27 changes: 21 additions & 6 deletions src/Database/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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,
);
));
}
}

Expand All @@ -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,
);
));
}
}

Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/Database/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Database/Injection/JsonExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
36 changes: 36 additions & 0 deletions tests/Database/Unit/Config/MySQL/SafeDefaultsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Arcano\Database\Tests\Unit\Config\MySQL;

use Arcano\Database\Config\MySQL\DsnConnectionConfig;
use Arcano\Database\Config\MySQL\SocketConnectionConfig;
use Arcano\Database\Config\MySQL\TcpConnectionConfig;
use PHPUnit\Framework\TestCase;

final class SafeDefaultsTest extends TestCase
{
public function testUsesServerPreparedStatementsAndUtf8mb4(): void
{
foreach ([
new TcpConnectionConfig('app'),
new SocketConnectionConfig('app', '/run/mysqld.sock'),
new DsnConnectionConfig('mysql:host=db;dbname=app'),
] as $config) {
$options = $config->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]);
}
}
48 changes: 48 additions & 0 deletions tests/Database/Unit/Driver/BoundedCachesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Arcano\Database\Tests\Unit\Driver;

use Arcano\Database\Config\SQLite\MemoryConnectionConfig;
use Arcano\Database\Config\SQLiteDriverConfig;
use Arcano\Database\Driver\CompilerCache;
use Arcano\Database\Driver\Driver;
use Arcano\Database\Driver\Postgres\PostgresCompiler;
use Arcano\Database\Driver\SQLite\SQLiteDriver;
use Arcano\Database\Query\QueryParameters;
use Arcano\Database\Query\SelectQuery;
use PHPUnit\Framework\TestCase;

/**
* A long-lived worker sees an open-ended number of statements (IN lists of every length,
* raw fragments with values): the prepared statement and compiled SQL caches must stay bounded.
*/
final class BoundedCachesTest extends TestCase
{
public function testThePreparedStatementCacheIsBounded(): void
{
$driver = SQLiteDriver::create(new SQLiteDriverConfig(new MemoryConnectionConfig(), queryCache: true));
for ($index = 0; $index < Driver::QUERY_CACHE_LIMIT + 200; $index++) {
$driver->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));
}
}
56 changes: 56 additions & 0 deletions tests/Database/Unit/Driver/OperatorAllowlistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Arcano\Database\Tests\Unit\Driver;

use Arcano\Database\Driver\MySQL\MySQLCompiler;
use Arcano\Database\Exception\CompilerException;
use Arcano\Database\Injection\Fragment;
use Arcano\Database\Query\QueryParameters;
use Arcano\Database\Query\SelectQuery;
use PHPUnit\Framework\TestCase;

/** A where operator is SQL text: only known operators may reach the statement. */
final class OperatorAllowlistTest extends TestCase
{
public static function injectedOperators(): \Traversable
{
yield ['= 1 OR 1=1 OR id ='];
yield ['=1;DROP TABLE users;--'];
yield ['LIKE/**/'];
yield [''];
}

public static function knownOperators(): \Traversable
{
foreach (['=', '!=', '<>', '>', '<', '>=', '<=', '<=>', '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);
}
}
22 changes: 22 additions & 0 deletions tests/Database/Unit/Injection/JsonExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
43 changes: 43 additions & 0 deletions tests/ORM/Unit/Command/StatementPlansTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Arcano\ORM\Tests\Unit\Command;

use Arcano\Database\Config\SQLite\MemoryConnectionConfig;
use Arcano\Database\Config\SQLiteDriverConfig;
use Arcano\Database\Database;
use Arcano\Database\Driver\SQLite\SQLiteDriver;
use Arcano\ORM\Command\Database\StatementPlans;
use PHPUnit\Framework\TestCase;

/**
* Insert and update plans are cached per database for the life of the worker: an open-ended
* number of column sets must not grow the cache without bound.
*/
final class StatementPlansTest extends TestCase
{
public function testThePlanCacheIsBoundedPerDatabase(): void
{
$db = new Database('default', '', SQLiteDriver::create(new SQLiteDriverConfig(new MemoryConnectionConfig())));
$db->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]));
}
}
Loading