Skip to content

Commit 9ee22d0

Browse files
committed
Keep internal error details out of client errors
PHP type errors carry the absolute path of the calling file, so the server's layout could reach any authenticated client. Answer a plain 1105 and log the detail instead.
1 parent fe161ea commit 9ee22d0

5 files changed

Lines changed: 135 additions & 14 deletions

File tree

‎packages/mysql-proxy/src/Adapter/class-sqlite-adapter.php‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace WP_MySQL_Proxy\Adapter;
44

5+
use Error;
56
use PDO;
67
use PDOException;
78
use PDOStatement;
8-
use Throwable;
99
use WP_MySQL_Proxy\MySQL_Result;
1010
use WP_MySQL_On_SQLite;
1111
use WP_MySQL_Proxy\MySQL_Protocol;
@@ -39,12 +39,13 @@ public function handle_query( string $query ): MySQL_Result {
3939
$columns = $this->computeColumnInfo( $statement );
4040
}
4141
return MySQL_Result::from_data( $affected_rows, $last_insert_id, $columns, $rows ?? array() );
42-
} catch ( Throwable $e ) {
43-
$error_info = $e->errorInfo ?? null; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
44-
if ( $e instanceof PDOException && $error_info ) {
45-
return MySQL_Result::from_error( $error_info[0], $error_info[1], $error_info[2] );
42+
} catch ( PDOException $e ) {
43+
// Only query errors reach the client. The session logs internal errors instead.
44+
$error_info = $e->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
45+
if ( ! $error_info || $this->is_internal_error( $e ) ) {
46+
throw $e;
4647
}
47-
return MySQL_Result::from_error( 'HY000', 1105, $e->getMessage() ?? 'Unknown error' );
48+
return MySQL_Result::from_error( $error_info[0], $error_info[1], $error_info[2] );
4849
}
4950
}
5051

@@ -101,4 +102,22 @@ public function computeColumnInfo( PDOStatement $statement ) {
101102
}
102103
return $columns;
103104
}
105+
106+
/**
107+
* Whether a query exception is a PHP error that the driver reported as a query error.
108+
*
109+
* A PHP error is a bug, not a query error, and its message can contain server
110+
* file paths.
111+
*
112+
* @param PDOException $e The query exception.
113+
* @return bool
114+
*/
115+
private function is_internal_error( PDOException $e ): bool {
116+
for ( $cause = $e->getPrevious(); null !== $cause; $cause = $cause->getPrevious() ) {
117+
if ( $cause instanceof Error ) {
118+
return true;
119+
}
120+
}
121+
return false;
122+
}
104123
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private function handle_new_client(): void {
225225

226226
// Create a new session for the client.
227227
$client_id = $this->get_client_id( $socket );
228-
$session = new MySQL_Session( $this->adapter, $client_id, $this->authentication );
228+
$session = new MySQL_Session( $this->adapter, $client_id, $this->authentication, $this->logger );
229229
$this->clients[ $client_id ] = array(
230230
'socket' => $socket,
231231
'session' => $session,

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ class MySQL_Session {
133133
*/
134134
private $close_reason;
135135

136+
/**
137+
* Logger instance.
138+
*
139+
* @var Logger
140+
*/
141+
private $logger;
142+
136143
/**
137144
* Packet sequence ID.
138145
*
@@ -152,12 +159,14 @@ class MySQL_Session {
152159
*
153160
* @param Adapter $adapter The MySQL query adapter instance.
154161
* @param int $connection_id The connection ID.
155-
* @param MySQL_Authentication $authentication The configured account verifier.
162+
* @param MySQL_Authentication $authentication The configured account verifier.
163+
* @param Logger|null $logger The logger for internal errors. Default: a new logger.
156164
*/
157-
public function __construct( Adapter $adapter, int $connection_id, MySQL_Authentication $authentication ) {
165+
public function __construct( Adapter $adapter, int $connection_id, MySQL_Authentication $authentication, ?Logger $logger = null ) {
158166
$this->authentication = $authentication;
159167
$this->adapter = $adapter;
160168
$this->connection_id = $connection_id;
169+
$this->logger = $logger ?? new Logger();
161170
$this->packet_id = 0;
162171

163172
// Like MySQL's generate_user_salt(), use 7-bit characters other than NUL and '$'.
@@ -586,12 +595,15 @@ private function process_query( string $query ): string {
586595
$result->last_insert_id
587596
);
588597
} catch ( Throwable $e ) {
589-
return MySQL_Protocol::build_err_packet(
590-
$this->packet_id++,
591-
0,
592-
'HY000',
593-
'Unknown error: ' . $e->getMessage()
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+
)
594605
);
606+
return MySQL_Protocol::build_err_packet( $this->packet_id++, 1105, 'HY000', 'Unknown error' );
595607
}
596608
}
597609

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use WP_MySQL_Proxy\Adapter\SQLite_Adapter;
5+
use WP_MySQL_Proxy\MySQL_Authentication;
6+
use WP_MySQL_Proxy\MySQL_Protocol;
7+
use WP_MySQL_Proxy\MySQL_Session;
8+
9+
class WP_MySQL_SQLite_Adapter_Test extends TestCase {
10+
public function test_query_error_is_reported(): void {
11+
$result = ( new SQLite_Adapter( ':memory:' ) )->handle_query( 'SELECT * FROM missing_table' );
12+
$this->assertSame( array( '42S02', 1146, "Table 'missing_table' doesn't exist" ), $result->error_info );
13+
}
14+
15+
public function test_driver_message_is_reported(): void {
16+
$cause = new RuntimeException( 'Unsupported ALTER TABLE ADD action: X' );
17+
$result = $this->adapter_failing_with( $cause )->handle_query( 'SELECT 1' );
18+
$this->assertSame( array( 'HY000', 1105, 'Unsupported ALTER TABLE ADD action: X' ), $result->error_info );
19+
}
20+
21+
/** @dataProvider internal_errors */
22+
public function test_internal_error_is_not_reported( Throwable $cause ): void {
23+
$this->expectException( WP_MySQL_On_SQLite_Exception::class );
24+
$this->adapter_failing_with( $cause )->handle_query( 'SELECT 1' );
25+
}
26+
27+
public function internal_errors(): array {
28+
return array(
29+
'PHP error' => array( new TypeError( 'Private detail from /srv/secret/path.php' ) ),
30+
'wrapped PHP error' => array( new RuntimeException( 'Wrapper', 0, new TypeError( 'Private detail' ) ) ),
31+
);
32+
}
33+
34+
public function test_internal_error_does_not_reach_the_client(): void {
35+
$cause = new TypeError( 'f(): Argument #1 must be of type string, called in /srv/secret/path.php on line 1' );
36+
$session = new MySQL_Session( $this->adapter_failing_with( $cause ), 1, new MySQL_Authentication( 'user', 'password' ) );
37+
$challenge = MySQL_Test_Client::challenge( $session->get_initial_handshake() );
38+
$session->receive_bytes( MySQL_Test_Client::handshake_response( $challenge ) );
39+
$this->assertTrue( $session->is_authenticated() );
40+
41+
// The detail goes to the log, which the test runner shows on stderr.
42+
$response = $session->receive_bytes( MySQL_Protocol::build_packet( 0, chr( MySQL_Protocol::COM_QUERY ) . 'SELECT 1' ) );
43+
$this->assertSame( 1105, unpack( 'v', substr( $response, 5, 2 ) )[1] );
44+
$this->assertSame( 'Unknown error', substr( $response, 13 ) );
45+
$this->assertStringNotContainsString( '/srv/secret/path.php', $response );
46+
}
47+
48+
/**
49+
* An adapter whose driver fails a query the way the driver reports any error.
50+
*/
51+
private function adapter_failing_with( Throwable $cause ): SQLite_Adapter {
52+
$adapter = new SQLite_Adapter( ':memory:' );
53+
$property = new ReflectionProperty( SQLite_Adapter::class, 'sqlite_driver' );
54+
if ( PHP_VERSION_ID < 80100 ) {
55+
$property->setAccessible( true );
56+
}
57+
$exception = new WP_MySQL_On_SQLite_Exception( $property->getValue( $adapter ), $cause->getMessage(), $cause->getCode(), $cause );
58+
$property->setValue(
59+
$adapter,
60+
new class( $exception ) {
61+
private $exception;
62+
63+
public function __construct( Throwable $exception ) {
64+
$this->exception = $exception;
65+
}
66+
67+
public function query(): void {
68+
throw $this->exception;
69+
}
70+
}
71+
);
72+
return $adapter;
73+
}
74+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ public function test_oversized_message_is_refused(): void {
8383
$this->assertFalse( $session->has_buffered_data() );
8484
}
8585

86+
public function test_internal_error_is_not_sent_to_the_client(): void {
87+
$adapter = $this->createMock( Adapter::class );
88+
$adapter->expects( $this->once() )->method( 'handle_query' )->willThrowException(
89+
new RuntimeException( 'Private detail from /srv/secret/path.php' )
90+
);
91+
$session = $this->authenticated_session( $adapter );
92+
93+
// The detail goes to the log, which the test runner shows on stderr.
94+
$response = $session->receive_bytes( MySQL_Protocol::build_packet( 0, chr( MySQL_Protocol::COM_QUERY ) . 'SELECT 1' ) );
95+
96+
$this->assertSame( 0xff, ord( $response[4] ) );
97+
$this->assertSame( 1105, unpack( 'v', substr( $response, 5, 2 ) )[1] );
98+
$this->assertSame( 'Unknown error', substr( $response, 13 ) );
99+
$this->assertStringNotContainsString( '/srv/secret/path.php', $response );
100+
}
101+
86102
public function test_result_without_an_insert_id_is_an_ok_packet(): void {
87103
$adapter = $this->createMock( Adapter::class );
88104
$adapter->expects( $this->once() )->method( 'handle_query' )->willReturn( new MySQL_Result() );

0 commit comments

Comments
 (0)