Skip to content

Commit 4b4ffa0

Browse files
committed
Report an unknown database from COM_INIT_DB as MySQL does
COM_INIT_DB passed the adapter's error to the client, so selecting a missing database returned 1105 with the adapter's message, while the login returned MySQL's 1049 for the same name. Select the database the same way in both places, so both answer 1049 and the adapter's details stay in the log. Unlike the login, a failed COM_INIT_DB keeps the connection open.
1 parent 63fc7ee commit 4b4ffa0

2 files changed

Lines changed: 89 additions & 28 deletions

File tree

‎packages/mysql-proxy/src/class-mysql-session.php‎

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ public function receive_bytes( string $data ): ?string {
290290
$this->close( self::CLOSE_CLIENT_QUIT );
291291
return '';
292292
case MySQL_Protocol::COM_INIT_DB:
293-
return $this->process_query( $this->build_use_query( substr( $payload, 1 ) ) );
293+
return $this->use_database( substr( $payload, 1 ) )
294+
?? MySQL_Protocol::build_ok_packet( $this->packet_id++, $this->status_flags );
294295
case MySQL_Protocol::COM_QUERY:
295296
return $this->process_query( substr( $payload, 1 ) );
296297
case MySQL_Protocol::COM_PING:
@@ -499,20 +500,10 @@ private function complete_authentication( string $username, string $plugin, stri
499500

500501
// No adapter calls are allowed until the credentials have been verified.
501502
if ( '' !== $database ) {
502-
try {
503-
$result = $this->adapter->handle_query( $this->build_use_query( $database ) );
504-
$failed = (bool) $result->error_info;
505-
} catch ( Throwable $e ) {
506-
$failed = true;
507-
}
508-
if ( $failed ) {
503+
$error = $this->use_database( $database );
504+
if ( null !== $error ) {
509505
$this->close( self::CLOSE_LOGIN_FAILED );
510-
return MySQL_Protocol::build_err_packet(
511-
$this->packet_id++,
512-
1049,
513-
'42000',
514-
sprintf( "Unknown database: '%s'", $database )
515-
);
506+
return $error;
516507
}
517508
}
518509

@@ -550,13 +541,36 @@ private function close( string $reason ): void {
550541
}
551542

552543
/**
553-
* Build a USE statement for a client-supplied database name.
544+
* Select a client-supplied database, for COM_INIT_DB and the login.
545+
*
546+
* The name is quoted as an identifier. Like MySQL, any failure is reported
547+
* as an unknown database. The adapter's reason goes to the log.
554548
*
555549
* @param string $database The database name.
556-
* @return string The USE statement with a quoted identifier.
550+
* @return string|null The error packet, or null when the database is selected.
557551
*/
558-
private function build_use_query( string $database ): string {
559-
return 'USE `' . str_replace( '`', '``', $database ) . '`';
552+
private function use_database( string $database ): ?string {
553+
try {
554+
$result = $this->adapter->handle_query( 'USE `' . str_replace( '`', '``', $database ) . '`' );
555+
if ( ! $result->error_info ) {
556+
return null;
557+
}
558+
$this->logger->warning(
559+
'Client [{client_id}]: cannot select a database: {error}',
560+
array(
561+
'client_id' => $this->connection_id,
562+
'error' => $result->error_info[2],
563+
)
564+
);
565+
} catch ( Throwable $e ) {
566+
$this->log_internal_error( $e );
567+
}
568+
return MySQL_Protocol::build_err_packet(
569+
$this->packet_id++,
570+
1049,
571+
'42000',
572+
sprintf( "Unknown database: '%s'", $database )
573+
);
560574
}
561575

562576
/**
@@ -595,18 +609,28 @@ private function process_query( string $query ): string {
595609
$result->last_insert_id
596610
);
597611
} catch ( Throwable $e ) {
598-
// Keep internal details, such as file paths, out of the client's error.
599-
$this->logger->error(
600-
'Client [{client_id}]: internal error: {error}',
601-
array(
602-
'client_id' => $this->connection_id,
603-
'error' => $e->getMessage(),
604-
)
605-
);
612+
$this->log_internal_error( $e );
606613
return MySQL_Protocol::build_err_packet( $this->packet_id++, 1105, 'HY000', 'Unknown error' );
607614
}
608615
}
609616

617+
/**
618+
* Log an internal error, which must not reach the client.
619+
*
620+
* Its message can contain internal details, such as file paths.
621+
*
622+
* @param Throwable $error The internal error.
623+
*/
624+
private function log_internal_error( Throwable $error ): void {
625+
$this->logger->error(
626+
'Client [{client_id}]: internal error: {error}',
627+
array(
628+
'client_id' => $this->connection_id,
629+
'error' => $error->getMessage(),
630+
)
631+
);
632+
}
633+
610634
/**
611635
* Build the result set packets for a MySQL query.
612636
*

‎packages/mysql-proxy/tests/WP_MySQL_Session_Command_Test.php‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use PHPUnit\Framework\TestCase;
44
use WP_MySQL_Proxy\Adapter\Adapter;
55
use WP_MySQL_Proxy\Incomplete_Input_Exception;
6+
use WP_MySQL_Proxy\Logger;
67
use WP_MySQL_Proxy\MySQL_Authentication;
78
use WP_MySQL_Proxy\MySQL_Protocol;
89
use WP_MySQL_Proxy\MySQL_Result;
@@ -26,6 +27,42 @@ public function database_names(): array {
2627
);
2728
}
2829

30+
/** @dataProvider init_db_failures */
31+
public function test_init_db_reports_an_unknown_database( bool $throws ): void {
32+
$adapter = $this->createMock( Adapter::class );
33+
$expectation = $adapter->expects( $this->once() )->method( 'handle_query' )->with( 'USE `missing`' );
34+
if ( $throws ) {
35+
$expectation->willThrowException( new RuntimeException( 'Private adapter error' ) );
36+
} else {
37+
$expectation->willReturn( MySQL_Result::from_error( 'HY000', 1105, 'Private adapter error' ) );
38+
}
39+
40+
// The adapter's reason goes to the log, not to the client.
41+
$logger = $this->createMock( Logger::class );
42+
$logger->expects( $this->once() )->method( $throws ? 'error' : 'warning' )->with(
43+
$this->anything(),
44+
$this->callback(
45+
function ( array $context ): bool {
46+
return 'Private adapter error' === $context['error'];
47+
}
48+
)
49+
);
50+
51+
$session = $this->authenticated_session( $adapter, $logger );
52+
$response = $session->receive_bytes( MySQL_Protocol::build_packet( 0, chr( MySQL_Protocol::COM_INIT_DB ) . 'missing' ) );
53+
$this->assertSame( 0xff, ord( $response[4] ) );
54+
$this->assertSame( 1049, unpack( 'v', substr( $response, 5, 2 ) )[1] );
55+
$this->assertSame( "#42000Unknown database: 'missing'", substr( $response, 7 ) );
56+
$this->assertFalse( $session->is_closed(), 'Unlike the login, a failed COM_INIT_DB keeps the connection.' );
57+
}
58+
59+
public function init_db_failures(): array {
60+
return array(
61+
'adapter error' => array( false ),
62+
'adapter throws' => array( true ),
63+
);
64+
}
65+
2966
public function test_split_message_is_assembled(): void {
3067
$this->iniSet( 'memory_limit', '512M' );
3168
$queries = array();
@@ -140,8 +177,8 @@ public function test_failed_login_reports_its_close_reason(): void {
140177
$this->assertSame( MySQL_Session::CLOSE_LOGIN_FAILED, $session->get_close_reason() );
141178
}
142179

143-
private function authenticated_session( Adapter $adapter ): MySQL_Session {
144-
$session = new MySQL_Session( $adapter, 1, new MySQL_Authentication( 'user', 'password' ) );
180+
private function authenticated_session( Adapter $adapter, ?Logger $logger = null ): MySQL_Session {
181+
$session = new MySQL_Session( $adapter, 1, new MySQL_Authentication( 'user', 'password' ), $logger );
145182
$challenge = MySQL_Test_Client::challenge( $session->get_initial_handshake() );
146183
$session->receive_bytes( MySQL_Test_Client::handshake_response( $challenge ) );
147184
$this->assertTrue( $session->is_authenticated() );

0 commit comments

Comments
 (0)