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
8 changes: 5 additions & 3 deletions src/AdapterPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Override;
use PDO;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Exception;
use PhpDb\Adapter\Platform\AbstractPlatform;
use PhpDb\Sql\Platform\StatementFactoryInterface;

Expand Down Expand Up @@ -81,9 +82,10 @@ public function quoteTrustedValue(int|float|string|bool $value): ?string
#[Override]
public function quoteValue(string $value): string
{
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue ?? parent::quoteValue($value);
return (
$this->quoteViaDriver($value)
?? throw Exception\RuntimeException::forVulnerablePlatformQuote(static::class, __METHOD__)
);
}

protected function quoteViaDriver(string $value): ?string
Expand Down
54 changes: 15 additions & 39 deletions test/unit/AdapterPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection;
use PhpDb\Adapter\Driver\Pdo\Result;
use PhpDb\Adapter\Driver\Pdo\Statement;
use PhpDb\Adapter\Exception\RuntimeException;
use PhpDb\Mysql\AdapterPlatform;
use PhpDb\Mysql\Pdo\Driver;
use PhpDb\Mysql\Sql\Platform;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

use function sprintf;

#[CoversMethod(AdapterPlatform::class, '__construct')]
#[CoversMethod(AdapterPlatform::class, 'getName')]
#[CoversMethod(AdapterPlatform::class, 'getStatementFactory')]
Expand Down Expand Up @@ -187,51 +190,24 @@ public function quoteTrustedValue(): void
}

#[Test]
public function quoteValue(): void
public function quoteValueListThrowsWhenTheDriverCannotQuote(): void
{
static::assertSame("'value'", $this->platform->quoteValue('value'));
static::assertSame("'Foo O\\'Bar'", $this->platform->quoteValue("Foo O'Bar"));
static::assertSame(
'\'\\\'; DELETE FROM some_table; -- \'',
$this->platform->quoteValue('\'; DELETE FROM some_table; -- '),
);
static::assertSame(
"'\\\\\\'; DELETE FROM some_table; -- '",
$this->platform->quoteValue('\\\'; DELETE FROM some_table; -- '),
);
}
$this->expectException(RuntimeException::class);

#[Test]
public function quoteValueList(): void
{
/**
* @todo Determine if vulnerability warning is required during unit testing
*/
//$this->expectError();
//$this->expectExceptionMessage(
// 'Attempting to quote a value in PhpDb\Adapter\Platform\Mysql without extension/driver support can '
// . 'introduce security vulnerabilities in a production environment'
//);
static::assertSame("'Foo O\\'Bar'", $this->platform->quoteValueList("Foo O'Bar"));
$this->platform->quoteValueList("Foo O'Bar");
}

#[Test]
public function quoteValueRaisesNoticeWithoutPlatformSupport(): void
public function quoteValueThrowsWhenTheDriverCannotQuote(): void
{
/**
* todo(@tyrsson): Determine if vulnerability warning is required during unit testing
*
* todo(@tyrsson): This testing needs expanded to cover all possible driver types
* since using \PDO currently causes a TypeError to be raised due to the
* underlying quoteViaDriver method returning false instead of ?string
*/
//$this->expectNotice();
//$this->expectExceptionMessage(
// 'Attempting to quote a value in PhpDb\Adapter\Platform\Mysql without extension/driver support can '
// . 'introduce security vulnerabilities in a production environment'
//);
$this->expectNotToPerformAssertions();
$this->platform->quoteValue('value');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf(
RuntimeException::VULNERABLE_PLATFORM_QUOTE,
AdapterPlatform::class,
AdapterPlatform::class . '::quoteValue',
));

$this->platform->quoteValue("Foo O'Bar");
}

#[Test]
Expand Down
12 changes: 2 additions & 10 deletions test/unit/Sql/Ddl/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace PhpDbTest\Mysql\Sql\Ddl;

use BackedEnum;
use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection;
use PhpDb\Adapter\Driver\Pdo\Result;
use PhpDb\Adapter\Driver\Pdo\Statement;
use PDO;
use PhpDb\Mysql\AdapterPlatform;
use PhpDb\Mysql\Pdo\Driver;
use PhpDb\Mysql\Sql\ColumnFormatEnum;
use PhpDb\Mysql\Sql\Ddl\AlterTable;
use PhpDb\Mysql\Sql\Ddl\ColumnOptionTrait;
Expand Down Expand Up @@ -507,12 +504,7 @@ public function generatesExpectedSqlForChangedColumnOptions(array $options, stri

protected function setUp(): void
{
$driver = new Driver(
$this->createStub(AbstractPdoConnection::class),
$this->createStub(Statement::class),
$this->createStub(Result::class),
);
$this->platform = new AdapterPlatform($driver);
$this->platform = new AdapterPlatform(new PDO('sqlite::memory:'));
}

private function buildSql(AlterTable $table): string
Expand Down
12 changes: 2 additions & 10 deletions test/unit/Sql/Ddl/CreateTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace PhpDbTest\Mysql\Sql\Ddl;

use BackedEnum;
use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection;
use PhpDb\Adapter\Driver\Pdo\Result;
use PhpDb\Adapter\Driver\Pdo\Statement;
use PDO;
use PhpDb\Mysql\AdapterPlatform;
use PhpDb\Mysql\Pdo\Driver;
use PhpDb\Mysql\Sql\ColumnFormatEnum;
use PhpDb\Mysql\Sql\Ddl\ColumnOptionTrait;
use PhpDb\Mysql\Sql\Ddl\CreateTable;
Expand Down Expand Up @@ -383,12 +380,7 @@ public function zerofillOption(): void

protected function setUp(): void
{
$driver = new Driver(
$this->createStub(AbstractPdoConnection::class),
$this->createStub(Statement::class),
$this->createStub(Result::class),
);
$this->platform = new AdapterPlatform($driver);
$this->platform = new AdapterPlatform(new PDO('sqlite::memory:'));
}

private function buildSql(CreateTable $table): string
Expand Down