fix(rds): clear CLIENT_SSL before forwarding the TLS-terminated handshake response to the backend (#2698)

Real MySQL clients keep CLIENT_SSL set in the HandshakeResponse41 they send
over the encrypted stream after the SSLRequest. The proxy terminates TLS
itself and forwards that response to the plaintext backend verbatim, and a
MySQL server treats any first packet carrying CLIENT_SSL as an SSLRequest:
it waits for a TLS ClientHello that never comes while the proxy waits in
relayConnectionPhase for an auth verdict that never comes. Every TLS
connection through the proxy (mysql's default ssl-mode PREFERRED included)
hung until the client timed out; only --ssl-mode=DISABLED worked.

Clear the bit before the response reaches the backend, mirroring
forceClientSslCapability on the server handshake. The TLS tests now send
real-client-shaped responses (CLIENT_SSL set) and assert the backend never
sees the bit; the assertion fails without this fix.
This commit is contained in:
Matan Baruch authored and GitHub committed 2026-08-28 22:23:41 -04:00
1 parent 14112b8ace
commit 0090739617
2 files changed
+34 -4

No files matched your search

@@ -125,6 +125,12 @@ public class MySqlProtocolHandler {
// permanently one behind the client's for the rest of the connection phase.
sequenceOffset = 1;
clientResponseRaw[3] = (byte) (clientResponseRaw[3] - sequenceOffset);
// Real clients keep CLIENT_SSL set in the HandshakeResponse they send over the
// now-encrypted stream. The backend connection stays plaintext, and a MySQL server
// treats any first packet with CLIENT_SSL as an SSLRequest — it would sit waiting for
// a TLS ClientHello that never comes while the proxy waits for its auth verdict,
// deadlocking the connection. Clear the bit (the mirror of forceClientSslCapability).
clearClientSslCapability(clientResponseRaw);
} else {
clientResponseRaw = clientFirstRaw;
if (clientResponseRaw.length < 40) {
@@ -259,6 +265,15 @@ public class MySqlProtocolHandler {
}
}
/**
* Clears the {@code CLIENT_SSL} bit in a raw HandshakeResponse41 packet's capability flags.
* The client capabilities are the first 4 payload bytes, little-endian; {@code CLIENT_SSL}
* (0x0800) lives in the second byte.
*/
private static void clearClientSslCapability(byte[] raw) {
raw[5] &= (byte) ~(CLIENT_SSL >> 8);
}
/**
* Sets the {@code CLIENT_SSL} bit in a raw Handshake V10 packet's capability flags, so the
* client is offered TLS even when the (plaintext) backend didn't advertise it. Walks the same
@@ -153,8 +153,11 @@ class MySqlProtocolHandlerTest {
byte[] scramble = scrambleNativePassword("secret", nonce);
// Real clients send this at sequence 2 after the TLS upgrade — the proxy must
// rewrite it to 1 before forwarding to the (plaintext-only) backend.
byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2);
// rewrite it to 1 before forwarding to the (plaintext-only) backend. They also
// keep CLIENT_SSL set in it; a real backend would treat a first packet carrying
// that bit as an SSLRequest and hang waiting for a TLS handshake.
byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2,
CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_SSL);
tlsOut.write(response);
tlsOut.flush();
@@ -178,6 +181,11 @@ class MySqlProtocolHandlerTest {
assertEquals((byte) 1, backendResponseSeq.get(),
"backend never saw the SSLRequest, so it must receive the real response at sequence 1");
assertEquals("admin", extractUsername(backendResponsePayload.get()));
int backendCaps = (backendResponsePayload.get()[0] & 0xFF)
| ((backendResponsePayload.get()[1] & 0xFF) << 8);
assertEquals(0, backendCaps & CLIENT_SSL,
"the plaintext backend must not see CLIENT_SSL, or it treats the response as an "
+ "SSLRequest and waits forever for a TLS handshake");
}
@Test
@@ -238,7 +246,8 @@ class MySqlProtocolHandlerTest {
OutputStream tlsOut = sslClient.getOutputStream();
byte[] scramble = scrambleNativePassword("secret", nonce);
byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2);
byte[] response = buildHandshakeResponse41("admin", scramble, (byte) 2,
CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_SSL);
tlsOut.write(response);
tlsOut.flush();
@@ -352,8 +361,14 @@ class MySqlProtocolHandlerTest {
private static byte[] buildHandshakeResponse41(String username, byte[] scramble, byte sequence)
throws IOException {
return buildHandshakeResponse41(username, scramble, sequence,
CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION);
}
private static byte[] buildHandshakeResponse41(String username, byte[] scramble, byte sequence,
int capabilities) throws IOException {
ByteArrayOutputStream payload = new ByteArrayOutputStream();
writeInt32LE(payload, CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION);
writeInt32LE(payload, capabilities);
writeInt32LE(payload, 0); // max packet size
payload.write(0x21); // charset
payload.write(new byte[23]); // reserved