Skip to content

Commit b636824

Browse files
committed
Improve DNS parsing by correctly handling "\0" bytes
1 parent f7248a3 commit b636824

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public function test_dsn_parsing(): void {
4040
// DSN with semicolon in the database name.
4141
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;dbname=w;;p;' );
4242
$this->assertSame( 'w;p', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
43+
44+
// DSN with semicolon in the database name and a terminating semicolon.
45+
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=w;;;p' );
46+
$this->assertSame( 'w;', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
47+
48+
// DSN with two semicolons in the database name.
49+
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=w;;;;p' );
50+
$this->assertSame( 'w;;p', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
51+
52+
// DSN with a "\0" byte (always terminates the DSN string).
53+
$driver = new WP_PDO_MySQL_On_SQLite( "mysql-on-sqlite:path=:memory:;dbname=w\0p;" );
54+
$this->assertSame( 'w', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
4355
}
4456

4557
public function test_query(): void {

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ public function __construct(
598598
?string $password = null,
599599
array $options = array()
600600
) {
601+
// PDO DSN can't include "\0" bytes; parsing stops at the first one.
602+
$first_null_byte_index = strpos( $dsn, "\0" );
603+
if ( false !== $first_null_byte_index ) {
604+
$dsn = substr( $dsn, 0, $first_null_byte_index );
605+
}
606+
601607
// Parse the DSN.
602608
$dsn_parts = explode( ':', $dsn, 2 );
603609
if ( count( $dsn_parts ) < 2 ) {

0 commit comments

Comments
 (0)