Skip to content

Commit 4928497

Browse files
authored
mysql client clear text authentication password should end with a 0x00 byte (#1216)
Signed-off-by: Billy Yuan <billy112487983@gmail.com> Signed-off-by: Billy Yuan <billy112487983@gmail.com>
1 parent 2a04691 commit 4928497

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/ChangeUserCommandCodec.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void decodePayload(ByteBuf payload, int payloadLength) {
4242
int header = payload.getUnsignedByte(payload.readerIndex());
4343
switch (header) {
4444
case AUTH_SWITCH_REQUEST_STATUS_FLAG:
45-
handleAuthSwitchRequest(cmd.password().getBytes(StandardCharsets.UTF_8), payload);
45+
handleAuthSwitchRequest(cmd.password(), payload);
4646
break;
4747
case AUTH_MORE_DATA_STATUS_FLAG:
4848
handleAuthMoreData(cmd.password().getBytes(StandardCharsets.UTF_8), payload);
@@ -56,28 +56,28 @@ void decodePayload(ByteBuf payload, int payloadLength) {
5656
}
5757
}
5858

59-
private void handleAuthSwitchRequest(byte[] password, ByteBuf payload) {
59+
private void handleAuthSwitchRequest(String password, ByteBuf payload) {
6060
// Protocol::AuthSwitchRequest
6161
payload.skipBytes(1); // status flag, always 0xFE
6262
String pluginName = BufferUtils.readNullTerminatedString(payload, StandardCharsets.UTF_8);
6363
authPluginData = new byte[NONCE_LENGTH];
6464
payload.readBytes(authPluginData);
65-
byte[] authResponse;
6665
switch (pluginName) {
6766
case "mysql_native_password":
68-
authResponse = Native41Authenticator.encode(password, authPluginData);
67+
sendBytesAsPacket(Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), authPluginData));
6968
break;
7069
case "caching_sha2_password":
71-
authResponse = CachingSha2Authenticator.encode(password, authPluginData);
70+
sendBytesAsPacket(CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), authPluginData));
7271
break;
7372
case "mysql_clear_password":
74-
authResponse = password;
73+
ByteBuf buffer = encoder.chctx.alloc().buffer();
74+
BufferUtils.writeNullTerminatedString(buffer, password, StandardCharsets.UTF_8);
75+
sendNonSplitPacket(buffer);
7576
break;
7677
default:
7778
encoder.handleCommandResponse(CommandResponse.failure(new UnsupportedOperationException("Unsupported authentication method: " + pluginName)));
7879
return;
7980
}
80-
sendBytesAsPacket(authResponse);
8181
}
8282

8383
private void sendChangeUserCommand() {

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/InitialHandshakeCommandCodec.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void handleAuthentication(ByteBuf payload) {
173173
handleErrorPacketPayload(payload);
174174
break;
175175
case AUTH_SWITCH_REQUEST_STATUS_FLAG:
176-
handleAuthSwitchRequest(cmd.password().getBytes(StandardCharsets.UTF_8), payload);
176+
handleAuthSwitchRequest(cmd.password(), payload);
177177
break;
178178
case AUTH_MORE_DATA_STATUS_FLAG:
179179
handleAuthMoreData(cmd.password().getBytes(StandardCharsets.UTF_8), payload);
@@ -183,28 +183,28 @@ private void handleAuthentication(ByteBuf payload) {
183183
}
184184
}
185185

186-
private void handleAuthSwitchRequest(byte[] password, ByteBuf payload) {
186+
private void handleAuthSwitchRequest(String password, ByteBuf payload) {
187187
// Protocol::AuthSwitchRequest
188188
payload.skipBytes(1); // status flag, always 0xFE
189189
String pluginName = BufferUtils.readNullTerminatedString(payload, StandardCharsets.UTF_8);
190190
byte[] nonce = new byte[NONCE_LENGTH];
191191
payload.readBytes(nonce);
192-
byte[] authResponse;
193192
switch (pluginName) {
194193
case "mysql_native_password":
195-
authResponse = Native41Authenticator.encode(password, nonce);
194+
sendBytesAsPacket(Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce));
196195
break;
197196
case "caching_sha2_password":
198-
authResponse = CachingSha2Authenticator.encode(password, nonce);
197+
sendBytesAsPacket(CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce));
199198
break;
200199
case "mysql_clear_password":
201-
authResponse = password;
200+
ByteBuf buffer = encoder.chctx.alloc().buffer();
201+
BufferUtils.writeNullTerminatedString(buffer, password, StandardCharsets.UTF_8);
202+
sendNonSplitPacket(buffer);
202203
break;
203204
default:
204205
encoder.handleCommandResponse(CommandResponse.failure(new UnsupportedOperationException("Unsupported authentication method: " + pluginName)));
205206
return;
206207
}
207-
sendBytesAsPacket(authResponse);
208208
}
209209

210210
private void sendSslRequest() {
@@ -249,7 +249,10 @@ private void sendHandshakeResponseMessage(String username, String password, Stri
249249
authResponse = CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
250250
break;
251251
case "mysql_clear_password":
252-
authResponse = password.getBytes(StandardCharsets.UTF_8);
252+
ByteBuf buffer = encoder.chctx.alloc().heapBuffer();
253+
BufferUtils.writeNullTerminatedString(buffer, password, StandardCharsets.UTF_8);
254+
authResponse = new byte[buffer.readableBytes()];
255+
buffer.readBytes(authResponse);
253256
break;
254257
default:
255258
LOGGER.warn("Unknown authentication method: " + authMethod + ", the client will try to use mysql_native_password instead.");

0 commit comments

Comments
 (0)