Le 11/11/2026 à 7:57 AM, Siddharth Agravat a écrit :
Hello Christopher,

- With post-80 using CLIENT_PLUGIN_AUTH, MySQL doesn't send err_packet (0xff) on plugin mismatch (reports 0xfe) and expect client to use correct plugin. Same is not the case with pre-41/post-41 (MySQL relies with error 1251) - this can happen with many reasons like version upgrades (using the prior auth account using old plugin) to continue for health checks. Idea here is to not silently ignore the mismatch (ultimately MySQL blocking haproxy host due to 1129 err) - This false positive (haproxy is reporting OK packet with 0xfe all the time) is updated in the doc but code isn't immediately reporting the error in case of mismatch, the prior fix the support of sha2 is added


Hi,

Sorry for the delay. I worked a bit on that because I was worried about returning an error in such case because it is more configuration error that a service error. It does not mean the server will reject regular traffic. But I can understand the problem.

Instead, here is a patchset to support parsing of AuthSwitchRequest packet with post-80 modes. I did some tests but it could be useful to validate the series on your side.

Regards,
--
Christopher Faulet
From 86700bf2c83331775de2b549c7c5978250add357 Mon Sep 17 00:00:00 2001
From: Christopher Faulet <[email protected]>
Date: Tue, 17 Nov 2026 15:27:35 +0100
Subject: [PATCH 1/5] MINOR: tcpcheck: add TCPCHK_EXPT_FL_ALLOW_EMPTY flag on
 expect rules

Custom expect functions are not evaluated when the response buffer is
empty. With the new TCPCHK_EXPT_FL_ALLOW_EMPTY flag, a custom expect
rule can be evaluated with an empty response, on the condition the
custom function properly handles this situation. It is useful when
the connection closure by the server is itself a valid result.
---
 include/haproxy/tcpcheck-t.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/haproxy/tcpcheck-t.h b/include/haproxy/tcpcheck-t.h
index 785b88ec6..c97688eae 100644
--- a/include/haproxy/tcpcheck-t.h
+++ b/include/haproxy/tcpcheck-t.h
@@ -91,6 +91,7 @@ enum tcpcheck_expect_type {
 #define TCPCHK_EXPT_FL_HTTP_HVAL_REG   0x1000 /* Regex match on the HTTP header value*/
 #define TCPCHK_EXPT_FL_HTTP_HVAL_FMT   0x2000 /* The HTTP header value is a log-format string */
 #define TCPCHK_EXPT_FL_HTTP_HVAL_FULL  0x4000 /* Match the full header value ( no stop on commas ) */
+#define TCPCHK_EXPT_FL_ALLOW_EMPTY     0x8000 /* Custom expect function also handles an empty response */
 
 #define TCPCHK_EXPT_FL_HTTP_HNAME_TYPE 0x003E /* Mask to get matching method on header name */
 #define TCPCHK_EXPT_FL_HTTP_HVAL_TYPE  0x1F00 /* Mask to get matching method on header value */
-- 
2.56.1

From 2926bb0baa5e8eca705472e4833301ddb36eb9fc Mon Sep 17 00:00:00 2001
From: Christopher Faulet <[email protected]>
Date: Tue, 17 Nov 2026 15:28:55 +0100
Subject: [PATCH 2/5] MINOR: tcpcheck: evaluate custom expect rules on empty
 response when allowed

The custom expect functions are not evaluated when the response
buffer is empty. When the TCPCHK_EXPT_FL_ALLOW_EMPTY flag is set
on the rule, the function is now called with an empty buffer, on
the condition it properly handles this situation. It is useful
when the connection closure by the server is itself a valid
result.
---
 src/tcpcheck.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/tcpcheck.c b/src/tcpcheck.c
index 3131407df..1b5189c8a 100644
--- a/src/tcpcheck.c
+++ b/src/tcpcheck.c
@@ -2417,12 +2417,11 @@ enum tcpcheck_eval_ret tcpcheck_eval_expect(struct check *check, struct tcpcheck
 		break;
 
 	case TCPCHK_EXPECT_CUSTOM:
-		/* Don't eval custom function if the buffer is empty. It means
-		 * custom functions can't expect an empty response. If this
-		 * change, don't forget to change this test and update all
-		 * custom functions.
+		/* Don't eval custom function if the buffer is empty, unless
+		 * explicitly allowed. Custom functions not flagged as such
+		 * can't expect an empty response.
 		 */
-		if (!b_data(&check->bi))
+		if (!b_data(&check->bi) && !(expect->flags & TCPCHK_EXPT_FL_ALLOW_EMPTY))
 			break;
 		if (expect->custom)
 			ret = expect->custom(check, rule, last_read);
-- 
2.56.1

From 40c0b42eafcb99b9a38ea1f7ffc4c59fd35101cc Mon Sep 17 00:00:00 2001
From: Christopher Faulet <[email protected]>
Date: Tue, 17 Nov 2026 15:29:00 +0100
Subject: [PATCH 3/5] MEDIUM: tcpcheck: handle MySQL AuthSwitchRequest in
 mysql-check post-80 modes

With the post-80 and post-80-sha2 modes, when the authentication
plugin of the check user differs from the announced one, the server
replies to the handshake response with an AuthSwitchRequest. The
check now answers with an empty password for the requested plugin,
then concludes on the authentication result: an OK packet marks the
server as UP, an ERR packet as DOWN, and a full authentication
request fails cleanly as it requires the password.

The AuthSwitchRequest packet is validated before being handled: a
packet with an empty payload or with a plugin name not terminated by
a NUL byte is reported as an invalid response. Otherwise the plugin
name would be searched outside the packet bounds.

To do so, the COM_QUIT command is no longer pipelined right after
the handshake response. It is now sent once the authentication
result is known, packed with the auth switch reply when there is
one, so the session is cleanly closed in every case and no
connection error is recorded on the server. The legacy pre-41 and
post-41 modes keep the pipelined COM_QUIT, they cannot receive an
auth switch request.
---
 include/haproxy/tcpcheck.h |   2 +
 src/tcpcheck.c             | 390 +++++++++++++++++++++++++++++++++++--
 2 files changed, 372 insertions(+), 20 deletions(-)

diff --git a/include/haproxy/tcpcheck.h b/include/haproxy/tcpcheck.h
index de40b1e31..d4878f9f4 100644
--- a/include/haproxy/tcpcheck.h
+++ b/include/haproxy/tcpcheck.h
@@ -58,6 +58,8 @@ void free_tcpcheck_http_hdr(struct tcpcheck_http_hdr *hdr);
 
 enum tcpcheck_eval_ret tcpcheck_mysql_expect_iniths(struct check *check, struct tcpcheck_rule *rule, int last_read);
 enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpcheck_rule *rule, int last_read);
+enum tcpcheck_eval_ret tcpcheck_mysql_expect_auth(struct check *check, struct tcpcheck_rule *rule, int last_read);
+enum tcpcheck_eval_ret tcpcheck_mysql_expect_auth_done(struct check *check, struct tcpcheck_rule *rule, int last_read);
 enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct tcpcheck_rule *rule, int last_read);
 enum tcpcheck_eval_ret tcpcheck_spop_expect_agenthello(struct check *check, struct tcpcheck_rule *rule, int last_read);
 enum tcpcheck_eval_ret tcpcheck_agent_expect_reply(struct check *check, struct tcpcheck_rule *rule, int last_read);
diff --git a/src/tcpcheck.c b/src/tcpcheck.c
index 1b5189c8a..feba2809c 100644
--- a/src/tcpcheck.c
+++ b/src/tcpcheck.c
@@ -663,6 +663,334 @@ enum tcpcheck_eval_ret tcpcheck_mysql_expect_ok(struct check *check, struct tcpc
 	return tcpcheck_mysql_expect_packet(check, rule, hslen, last_read);
 }
 
+/* Walk the MySQL packets received after the packet at <offset>. The
+ * AuthMoreData packets announcing a successful caching_sha2_password fast
+ * authentication are skipped, an OK_Packet or an ERR_Packet always follows.
+ * It returns 1 and sets <*poff>, <*plen> and <*ptype> with the next packet to
+ * handle, 0 if more data are expected, -1 if the response is invalid or
+ * truncated, and -2 if a full authentication is requested by the server, which
+ * cannot be handled by a check.
+ */
+static int tcpcheck_mysql_walk_packets(struct check *check, unsigned int offset, int last_read,
+					unsigned int *poff, unsigned int *plen, unsigned char *ptype)
+{
+	for (;;) {
+		/* 3 bytes for the packet length, 1 byte for the sequence id and 1 byte for the type */
+		if (b_data(&check->bi) < offset + 5)
+			goto incomplete;
+
+		*plen = ((unsigned char)*b_peek(&check->bi, offset)) +
+			(((unsigned char)*b_peek(&check->bi, offset+1)) << 8) +
+			(((unsigned char)*b_peek(&check->bi, offset+2)) << 16);
+		if (b_data(&check->bi) < offset + *plen + 4)
+			goto incomplete;
+
+		*ptype = (unsigned char)*b_peek(&check->bi, offset+4);
+		if (*ptype != 0x01 || *plen < 2)
+			break;
+
+		/* AuthMoreData packet: 0x03 announces a successful fast
+		 * authentication, 0x02 and 0x04 mean the full authentication
+		 * is required, which needs the password.
+		 */
+		switch ((unsigned char)*b_peek(&check->bi, offset+5)) {
+		case 0x03:
+			break;
+		case 0x02:
+		case 0x04:
+			return -2;
+		default:
+			return -1;
+		}
+		offset += *plen + 4;
+		continue;
+
+	  incomplete:
+		return (last_read ? -1 : 0);
+	}
+
+	*poff = offset;
+	return 1;
+}
+
+/* Store a check-scoped variable used by the following send/expect rules to
+ * continue the MySQL authentication. Returns 0 on failure.
+ */
+static int tcpcheck_mysql_store_var(struct check *check, struct ist name, int type, const void *value, size_t len)
+{
+	struct sample smp;
+
+	memset(&smp, 0, sizeof(smp));
+	smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL);
+	smp.data.type = type;
+	if (type == SMP_T_BOOL)
+		smp.data.u.sint = 1;
+	else {
+		smp.data.u.str.area = (char *)value;
+		smp.data.u.str.data = len;
+	}
+	return vars_set_by_name_ifexist(istptr(name), istlen(name), &smp);
+}
+
+/* Custom tcp-check expect function to parse the server reply to the client
+ * HandshakeResponse packet. On an OK_Packet, the server version is stored for
+ * the check result and the authentication is reported as done. On an
+ * AuthSwitchRequest, the AuthSwitchResponse packet to send, carrying an empty
+ * password, is stored in the <check.authreply> variable. Returns
+ * TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE to evaluate
+ * the next rule or TCPCHK_EVAL_STOP if an error occurred.
+ */
+enum tcpcheck_eval_ret tcpcheck_mysql_expect_auth(struct check *check, struct tcpcheck_rule *rule, int last_read)
+{
+	enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
+	enum healthcheck_status status = HCHK_STATUS_L7RSP;
+	struct buffer *msg = NULL;
+	struct ist desc = IST_NULL, plugin, version;
+	unsigned int hslen, poff = 0, plen = 0, reply_len;
+	unsigned char ptype = 0, seq;
+	char *reply = NULL;
+	int walk;
+
+	TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
+
+	/* Skip the initial handshake packet at the beginning of the buffer to
+	 * reach the server reply to the authentication request.
+	 */
+	if (b_data(&check->bi) < 5)
+		goto wait_more_data;
+	hslen = 4 + ((unsigned char)*b_peek(&check->bi, 0)) +
+		(((unsigned char)*b_peek(&check->bi, 1)) << 8) +
+		(((unsigned char)*b_peek(&check->bi, 2)) << 16);
+
+	walk = tcpcheck_mysql_walk_packets(check, hslen, last_read, &poff, &plen, &ptype);
+	switch (walk) {
+	case 0:
+		goto wait_more_data;
+	case -1:
+		desc = ist("Invalid or truncated response to the authentication request");
+		goto error;
+	case -2:
+		desc = ist("Full authentication requested by the server, a password is required");
+		goto error;
+	}
+
+	/* The initial handshake packet, with the server version, is still at
+	 * the beginning of the buffer. The version is a NUL-terminated
+	 * string at offset 5, right after the protocol version byte.
+	 */
+	version = iststop(ist2(b_peek(&check->bi, 5), b_data(&check->bi) - 5), '\0');
+	if (!tcpcheck_mysql_store_var(check, ist("check.version"), SMP_T_STR,
+				      istptr(version), istlen(version))) {
+		desc = ist("Failed to store the MySQL server version");
+		goto error;
+	}
+
+	switch (ptype) {
+	case 0x00: /* OK_Packet: the authentication is done */
+		/* Store the COM_QUIT command, sequence id 0, to cleanly close
+		 * the session once the authentication is completed.
+		 */
+		if (!tcpcheck_mysql_store_var(check, ist("check.authok"), SMP_T_BOOL, NULL, 0) ||
+		    !tcpcheck_mysql_store_var(check, ist("check.authreply"), SMP_T_BIN, "\x01\x00\x00\x00\x01", 5)) {
+			desc = ist("Failed to store the MySQL authentication completion");
+			goto error;
+		}
+		break;
+	case 0xff: /* ERR_Packet */
+		/* The shortest error packet carries at least 4 payload bytes:
+		 * the packet type (0xff), a 2-byte error code and the
+		 * description (at least one byte).
+		 */
+		if (plen < 4) {
+			desc = ist("Invalid MySQL error packet");
+			goto error;
+		}
+		check->code = ((unsigned char)*b_peek(&check->bi, poff+5)) +
+			(((unsigned char)*b_peek(&check->bi, poff+6)) << 8);
+		desc = ist2(b_peek(&check->bi, poff+7), plen - 3);
+		status = HCHK_STATUS_L7STS;
+		goto error;
+	case 0xfe: /* AuthSwitchRequest: reply with an empty password */
+		/* The plugin name is a NUL-terminated string at the beginning
+		 * of the packet payload. It is the only payload field, a
+		 * packet with no payload byte or without the terminating NUL
+		 * is invalid. The auth response for an empty password is
+		 * empty: mysql_native_password does not use a length prefix,
+		 * the sha2 plugins use a length-encoded empty string. The
+		 * COM_QUIT command is pipelined behind the reply, it starts a
+		 * new sequence and cleanly closes the session.
+		 */
+		if (plen < 2) {
+			desc = ist("Invalid MySQL authentication method switch request");
+			goto error;
+		}
+		plugin = iststop(ist2(b_peek(&check->bi, poff+5), plen-1), '\0');
+		if (istlen(plugin) == plen - 1) {
+			/* no terminating NUL found inside the packet */
+			desc = ist("Invalid MySQL authentication method switch request");
+			goto error;
+		}
+		if (isteq(plugin, ist("mysql_native_password")))
+			reply_len = 0;
+		else if (isteq(plugin, ist("caching_sha2_password")) ||
+			 isteq(plugin, ist("sha256_password")))
+			reply_len = 1;
+		else {
+			desc = ist("Unsupported authentication plugin requested by the server");
+			goto error;
+		}
+
+		reply = malloc(9 + reply_len);
+		if (!reply) {
+			desc = ist("Out of memory");
+			goto error;
+		}
+		seq = (unsigned char)*b_peek(&check->bi, poff+3);
+		reply[0] = (char)reply_len;
+		reply[1] = reply[2] = 0;
+		reply[3] = (char)(seq + 1);
+		if (reply_len)
+			reply[4] = 0;
+		/* COM_QUIT command, sequence id 0 */
+		reply[4 + reply_len] = 0x01;
+		reply[5 + reply_len] = reply[6 + reply_len] = 0;
+		reply[7 + reply_len] = 0;
+		reply[8 + reply_len] = 0x01;
+
+		if (!tcpcheck_mysql_store_var(check, ist("check.authreply"), SMP_T_BIN, reply, 9 + reply_len)) {
+			desc = ist("Failed to store the MySQL authentication reply");
+			goto error;
+		}
+		break;
+	default:
+		desc = ist("Unexpected MySQL packet in response to the authentication request");
+		goto error;
+	}
+
+  out:
+	free(reply);
+	free_trash_chunk(msg);
+	TRACE_LEAVE(CHK_EV_TCPCHK_EXP, check, 0, 0, (size_t[]){ret});
+	return ret;
+
+  error:
+	ret = TCPCHK_EVAL_STOP;
+	msg = alloc_trash_chunk();
+	if (msg)
+		tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
+	set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
+	goto out;
+
+  wait_more_data:
+	TRACE_DEVEL("waiting for more data", CHK_EV_TCPCHK_EXP, check);
+	ret = TCPCHK_EVAL_WAIT;
+	goto out;
+}
+
+/* Custom tcp-check expect function to conclude the MySQL check. It parses the
+ * reply to the authentication or to the AuthSwitchResponse packet. With no
+ * data at all and a connection cleanly closed by the server, the check
+ * succeeds if the authentication was already completed. Returns
+ * TCPCHK_EVAL_WAIT to wait for more data, TCPCHK_EVAL_CONTINUE to evaluate
+ * the next rule or TCPCHK_EVAL_STOP if an error occurred or if the check
+ * succeeded.
+ */
+enum tcpcheck_eval_ret tcpcheck_mysql_expect_auth_done(struct check *check, struct tcpcheck_rule *rule, int last_read)
+{
+	enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
+	enum healthcheck_status status = HCHK_STATUS_L7RSP;
+	struct buffer *msg = NULL;
+	struct ist desc = IST_NULL;
+	struct sample smp;
+	unsigned int poff = 0, plen = 0;
+	unsigned char ptype = 0;
+	char *info = NULL;
+	int walk;
+
+	TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
+
+	if (!b_data(&check->bi)) {
+		if (!last_read)
+			goto wait_more_data;
+
+		/* The server closed the connection without sending the result
+		 * of the authentication. It is only valid if it was already
+		 * completed, the connection was then closed because of the
+		 * COM_QUIT command.
+		 */
+		memset(&smp, 0, sizeof(smp));
+		smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL);
+		if (!vars_get_by_name(istptr(ist("check.authok")), istlen(ist("check.authok")), &smp, NULL) ||
+		    smp.data.type != SMP_T_BOOL || !smp.data.u.sint) {
+			desc = ist("Connection closed before the end of the MySQL authentication");
+			goto error;
+		}
+		goto success;
+	}
+
+	walk = tcpcheck_mysql_walk_packets(check, 0, last_read, &poff, &plen, &ptype);
+	switch (walk) {
+	case 0:
+		goto wait_more_data;
+	case -1:
+		desc = ist("Invalid or truncated response to the authentication request");
+		goto error;
+	case -2:
+		desc = ist("Full authentication requested by the server, a password is required");
+		goto error;
+	}
+
+	switch (ptype) {
+	case 0x00: /* OK_Packet */
+		goto success;
+	case 0xff: /* ERR_Packet */
+		if (plen < 4) {
+			desc = ist("Invalid MySQL error packet");
+			goto error;
+		}
+		check->code = ((unsigned char)*b_peek(&check->bi, poff+5)) +
+			(((unsigned char)*b_peek(&check->bi, poff+6)) << 8);
+		desc = ist2(b_peek(&check->bi, poff+7), plen - 3);
+		status = HCHK_STATUS_L7STS;
+		goto error;
+	case 0xfe:
+		desc = ist("Unexpected MySQL authentication method switch request");
+		goto error;
+	default:
+		desc = ist("Unexpected MySQL packet in response to the authentication request");
+		goto error;
+	}
+
+  success:
+	status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
+	memset(&smp, 0, sizeof(smp));
+	smp_set_owner(&smp, check->proxy, check->sess, NULL, SMP_OPT_FINAL);
+	if (vars_get_by_name(istptr(ist("check.version")), istlen(ist("check.version")), &smp, NULL) &&
+	    smp.data.type == SMP_T_STR)
+		info = b_head(&smp.data.u.str);
+	set_server_check_status(check, status, (info ? info : "MySQL server is ok"));
+	ret = TCPCHK_EVAL_STOP;
+	goto out;
+
+  error:
+	ret = TCPCHK_EVAL_STOP;
+	msg = alloc_trash_chunk();
+	if (msg)
+		tcpcheck_expect_onerror_message(msg, check, rule, 0, desc);
+	set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
+	goto out;
+
+  wait_more_data:
+	TRACE_DEVEL("waiting for more data", CHK_EV_TCPCHK_EXP, check);
+	ret = TCPCHK_EVAL_WAIT;
+	goto out;
+
+  out:
+	free_trash_chunk(msg);
+	TRACE_LEAVE(CHK_EV_TCPCHK_EXP, check, 0, 0, (size_t[]){ret});
+	return ret;
+}
+
 /* Custom tcp-check expect function to parse and validate the LDAP bind response
  * package packet. Returns TCPCHK_EVAL_WAIT to wait for more data,
  * TCPCHK_EVAL_CONTINUE to evaluate the next rule or TCPCHK_EVAL_STOP if an
@@ -4948,9 +5276,6 @@ static int do_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curp
 		"00"                           /* auth response length (0 = no password) */
 		"6d7973716c5f6e61746976655f"   /* auth plugin name: "mysql_native_password\0" */
 		"70617373776f726400"
-		"010000"                       /* packet length */
-		"00"                           /* sequence ID */
-		"01"                           /* COM_QUIT command */
 	};
 
 	/* MySQL >=8.0 client Authentication packet with CLIENT_PLUGIN_AUTH capability
@@ -4958,19 +5283,10 @@ static int do_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curp
 	 * authentication plugin since MySQL 8.0, and the only one enabled by default
 	 * since MySQL 8.4 (mysql_native_password is disabled and removed in 9.0).
 	 *
-	 * When the client does not announce the plugin used by the account, the
-	 * server answers the handshake response with an AuthSwitchRequest. Since
-	 * the COM_QUIT packet is pipelined right after the handshake response, the
-	 * server then reads it as the auth switch reply, fails the handshake with a
-	 * "packets out of order" error and counts it as a connection error for the
-	 * client host in the host cache. After max_connect_errors such checks, the
-	 * server blocks the HAProxy host (ER_HOST_IS_BLOCKED) and every connection
-	 * from it, including proxied client traffic, is refused.
-	 *
-	 * Announcing caching_sha2_password with an empty auth response lets the
-	 * server authenticate a password-less caching_sha2_password account in a
-	 * single round trip: it answers with an OK packet and the pipelined COM_QUIT
-	 * closes the session cleanly, so no connection error is recorded.
+	 * If the plugin does not match the one of the check user, the server
+	 * replies with an AuthSwitchRequest, which is handled by the check: it
+	 * answers with an empty password for the requested plugin and cleanly
+	 * closes the session once the authentication is done.
 	 *
 	 * Client capabilities: 0x00088200 (little-endian: 00820800)
 	 *   - CLIENT_PROTOCOL_41       (0x00000200)
@@ -4988,9 +5304,6 @@ static int do_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curp
 		"00"                           /* auth response length (0 = no password) */
 		"63616368696e675f736861325f"   /* auth plugin name: "caching_sha2_password\0" */
 		"70617373776f726400"
-		"010000"                       /* packet length */
-		"00"                           /* sequence ID */
-		"01"                           /* COM_QUIT command */
 	};
 
 	struct tcpcheck *tc = &curpx->tcpcheck;
@@ -5118,7 +5431,10 @@ static int do_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curp
 	chk->index = index++;
 	LIST_APPEND(&rs->rules, &chk->list);
 
-	if (mysql_req) {
+	if (mysql_req == mysql41_req || mysql_req == mysql40_req) {
+		/* Legacy modes: the COM_QUIT command is pipelined after the
+		 * handshake response and the check stops on the server reply.
+		 */
 		chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
 					    1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg);
 		if (!chk) {
@@ -5129,6 +5445,40 @@ static int do_parse_mysql_check_opt(char **args, int cur_arg, struct proxy *curp
 		chk->index = index++;
 		LIST_APPEND(&rs->rules, &chk->list);
 	}
+	else if (mysql_req) {
+		/* post-80 modes: handle a possible AuthSwitchRequest before
+		 * closing the session with COM_QUIT.
+		 */
+		chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
+					    1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg);
+		if (!chk) {
+			ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
+			goto error;
+		}
+		chk->expect.custom = tcpcheck_mysql_expect_auth;
+		chk->index = index++;
+		LIST_APPEND(&rs->rules, &chk->list);
+
+		chk = parse_tcpcheck_send((char *[]){"tcp-check", "send-binary-lf", "%[var(check.authreply),hex]", ""},
+					  1, curpx, &rs->rules, file, line, &errmsg);
+		if (!chk) {
+			ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
+			goto error;
+		}
+		chk->index = index++;
+		LIST_APPEND(&rs->rules, &chk->list);
+
+		chk = parse_tcpcheck_expect((char *[]){"tcp-check", "expect", "custom", ""},
+					    1, curpx, &rs->rules, TCPCHK_RULES_MYSQL_CHK, file, line, &errmsg);
+		if (!chk) {
+			ha_alert("parsing [%s:%d] : %s\n", file, line, errmsg);
+			goto error;
+		}
+		chk->expect.custom = tcpcheck_mysql_expect_auth_done;
+		chk->expect.flags |= TCPCHK_EXPT_FL_ALLOW_EMPTY;
+		chk->index = index++;
+		LIST_APPEND(&rs->rules, &chk->list);
+	}
 
   out:
 	free(errmsg);
-- 
2.56.1

From f8fad1f3b7df1cfa21972677dfbf98d79e274659 Mon Sep 17 00:00:00 2001
From: Christopher Faulet <[email protected]>
Date: Tue, 17 Nov 2026 15:29:05 +0100
Subject: [PATCH 4/5] DOC: config: update mysql-check doc for the auth switch
 support

Document the behavior of the post-80 and post-80-sha2 modes when
the authentication plugin of the check user differs from the
announced one.
---
 doc/configuration.txt | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index 0a3246c31..ab0a2d819 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -12079,22 +12079,28 @@ option mysql-check [ user <username> [ { post-41 | pre-41 | post-80 | post-80-sh
                the only one enabled by default since MySQL 8.4. Example:
                  CREATE USER 'haproxy'@'%' IDENTIFIED WITH caching_sha2_password BY '';
 
-  The authentication plugin announced by the check must match the one of the
-  check user. Otherwise the server either rejects the handshake (error 1251
-  with pre-41 and post-41) or replies with an authentication method switch
-  request that the check cannot answer (post-80, in which case the check is
-  still reported as passed). In both cases the server aborts the handshake and
-  counts it as a connection error for the HAProxy host. Once the host reaches
-  MySQL "max_connect_errors", the server refuses all connections from it,
-  including proxied client connections, until "FLUSH HOSTS" is issued.
+  With pre-41 and post-41, the authentication plugin announced by the check
+  must match the one of the check user. Otherwise the server rejects the
+  handshake with error 1251, aborts it and counts it as a connection error
+  for the HAProxy host. Once the host reaches MySQL "max_connect_errors", the
+  server refuses all connections from it, including proxied client
+  connections, until "FLUSH HOSTS" is issued.
 
-  If you specify a username, the check consists of sending two MySQL packet,
-  one Client Authentication packet, and one QUIT packet, to correctly close
-  MySQL session. We then parse the MySQL Handshake Initialization packet and/or
-  Error packet. It is a basic but useful test which does not produce error nor
-  aborted connect on the server. However, it requires an unlocked authorised
-  user without a password. To create a basic limited user in MySQL with
-  optional resource limits:
+  With post-80 and post-80-sha2, if the plugin does not match the one of the
+  check user, the server replies with an authentication method switch
+  request. The check answers with an empty password for the requested plugin,
+  reports the server as UP if the authentication succeeds and the session is
+  cleanly closed with a QUIT command, so no connection error is recorded on
+  the server. If the check user has a password, the full authentication
+  required by the server cannot be performed and the check fails.
+
+  If you specify a username, the check consists of sending an authentication
+  packet followed by a QUIT packet to correctly close the MySQL session. We
+  then parse the MySQL Handshake Initialization packet, the authentication
+  result and any error packet. It is a basic but useful test which does not
+  produce error nor aborted connect on the server. However, it requires an
+  unlocked authorised user without a password. To create a basic limited user
+  in MySQL with optional resource limits:
 
       CREATE USER '<username>'@'<ip_of_haproxy|network_of_haproxy/netmask>'
       /*!50701 WITH MAX_QUERIES_PER_HOUR 1 MAX_UPDATES_PER_HOUR 0 */
-- 
2.56.1

From 0d279ea61c5d65752651fd023b27fbb0d9cf365f Mon Sep 17 00:00:00 2001
From: Christopher Faulet <[email protected]>
Date: Fri, 20 Nov 2026 10:52:14 +0100
Subject: [PATCH 5/5] REGTESTS: mysql-check: refresh the regtest for the auth
 switch support

The mock servers no longer expect a COM_QUIT command pipelined with
the handshake response for the post-80 and post-80-sha2 modes, and
new servers exercise an auth switch request, with a success and
failures, including a full authentication request and an access
denied error. Two servers also send an invalid auth switch request,
with an empty payload or a plugin name not terminated by a NUL byte,
to make sure the packet is validated before being handled.
---
 reg-tests/checks/mysql-check.vtc | 105 ++++++++++++++++++++++++++++++-
 1 file changed, 103 insertions(+), 2 deletions(-)

diff --git a/reg-tests/checks/mysql-check.vtc b/reg-tests/checks/mysql-check.vtc
index 42ebbb989..9f79f8775 100644
--- a/reg-tests/checks/mysql-check.vtc
+++ b/reg-tests/checks/mysql-check.vtc
@@ -38,8 +38,49 @@ server s5 {
 
 server s6 {
   sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
-  recv 69
+  recv 64
   sendhex "0700000200000002000000"
+  recv 5
+} -start
+
+server s7 {
+  sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
+  recv 64
+  sendhex "2B000002FE63616368696E675F736861325F70617373776F7264000000000000000000000000000000000000000000"
+  recv 10
+  sendhex "020000040103"
+  sendhex "0700000500000002000000"
+  expect_close
+} -start
+
+server s8 {
+  sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
+  recv 64
+  sendhex "2B000002FE6D7973716C5F6E61746976655F70617373776F7264000000000000000000000000000000000000000000"
+  recv 9
+  sendhex "16000004FF15042332383030304163636573732064656E696564"
+} -start
+
+server s10 {
+  sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
+  recv 64
+  sendhex "00000002FE"
+  expect_close
+} -start
+
+server s11 {
+  sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
+  recv 64
+  sendhex "16000002FE6D7973716C5F6E61746976655F70617373776F7264"
+  expect_close
+} -start
+
+server s9 {
+  sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
+  recv 64
+  sendhex "2B000002FE63616368696E675F736861325F70617373776F7264000000000000000000000000000000000000000000"
+  recv 10
+  sendhex "020000040102"
 } -start
 
 syslog S1 -level notice {
@@ -72,6 +113,31 @@ syslog S6 -level notice {
     expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be6/srv succeeded, reason: Layer7 check passed.+info: \"8.0.19\".+check duration: [[:digit:]]+ms, status: 1/1 UP."
 } -start
 
+syslog S7 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be7/srv succeeded, reason: Layer7 check passed.+info: \"8.0.19\".+check duration: [[:digit:]]+ms, status: 1/1 UP."
+} -start
+
+syslog S8 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be8/srv failed, reason: Layer7 wrong status.+code: 1045.+info: \"#28000Access denied\".+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
+
+syslog S9 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be9/srv failed, reason: Layer7 invalid response.+info: \"Full authentication requested by the server, a password is required\".+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
+
+
+syslog S10 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be10/srv failed, reason: Layer7 invalid response.+info: \"Invalid MySQL authentication method switch request\".+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
+
+syslog S11 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be11/srv failed, reason: Layer7 invalid response.+info: \"Invalid MySQL authentication method switch request\".+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
 
 haproxy h1 -conf {
     global
@@ -138,10 +204,40 @@ haproxy h1 -conf {
     listen mysql3
         bind "fd@${mysql3}"
         tcp-request inspect-delay 100ms
-        tcp-request content accept if { req.len eq 69 } { req.payload(0,69) -m bin "3C000001008208000080000121000000000000000000000000000000000000000000000075736572000063616368696E675F736861325F70617373776F7264000100000001" }
+        tcp-request content accept if { req.len eq 64 } { req.payload(0,64) -m bin "3C000001008208000080000121000000000000000000000000000000000000000000000075736572000063616368696E675F736861325F70617373776F726400" }
         tcp-request content reject
         server srv ${s6_addr}:${s6_port}
 
+    backend be7
+        log ${S7_addr}:${S7_port} daemon
+        option log-health-checks
+        option mysql-check user user post-80
+        server srv ${s7_addr}:${s7_port} check inter 1s rise 1 fall 1
+
+    backend be8
+        log ${S8_addr}:${S8_port} daemon
+        option log-health-checks
+        option mysql-check user user post-80
+        server srv ${s8_addr}:${s8_port} check inter 1s rise 1 fall 1
+
+    backend be9
+        log ${S9_addr}:${S9_port} daemon
+        option log-health-checks
+        option mysql-check user user post-80-sha2
+        server srv ${s9_addr}:${s9_port} check inter 1s rise 1 fall 1
+    backend be10
+        log ${S10_addr}:${S10_port} daemon
+        option log-health-checks
+        option mysql-check user user post-80
+        server srv ${s10_addr}:${s10_port} check inter 1s rise 1 fall 1
+
+    backend be11
+        log ${S11_addr}:${S11_port} daemon
+        option log-health-checks
+        option mysql-check user user post-80
+        server srv ${s11_addr}:${s11_port} check inter 1s rise 1 fall 1
+
+
 } -start
 
 syslog S1 -wait
@@ -150,3 +246,8 @@ syslog S3 -wait
 syslog S4 -wait
 syslog S5 -wait
 syslog S6 -wait
+syslog S7 -wait
+syslog S8 -wait
+syslog S9 -wait
+syslog S10 -wait
+syslog S11 -wait
-- 
2.56.1

Reply via email to