From: Christopher Faulet Date: Tue, 1 Dec 2026 12:57:54 +0000 (+0100) Subject: BUG/MINOR: mux-h1: support the H2 preface received in several segments X-Git-Url: https://git.kaiwu.me/web/http/static/%7B@url%7D?a=commitdiff_plain;h=27327db883ef2c7752ad2dd617ab321c77e7e342;p=haproxy.git BUG/MINOR: mux-h1: support the H2 preface received in several segments When a cleartext HTTP/2 prior-knowledge client connects to an HTTP/1 listener, the connection is upgraded to H2 if the first request matches the H2 connection preface. But the check with b_isteq() only succeeds when the whole 24-byte preface is already buffered. When the preface is received in two or more segments and the first one holds 18 to 23 bytes ("PRI * HTTP/2.0\r\n\r\n" possibly with the beginning of "SM"), b_isteq() reports a partial match (0), the data are parsed as a complete but invalid H1 request and the client receives a 400 instead of the upgrade. The fix detects the partial match by truncating the preface to the number of buffered bytes, and then waits for more data instead of parsing it as H1. The full preface still upgrades, anything that is not a strict prefix of the preface is parsed as H1 exactly as before, and the http-request timeout still applies while waiting. This patch could be backported to all supported versions. --- diff --git a/src/mux_h1.c b/src/mux_h1.c index b77f70080..6abb10dd2 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -4369,6 +4369,28 @@ static int h1_send(struct h1c *h1c) return sent || (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) || (h1c->state == H1_CS_CLOSED); } +/* Checks if the data in match the beginning of the HTTP/2 connection + * preface. It returns 1 if the full preface is present, 0 if it does not + * match at all and -1 if it can still be a partial preface, waiting for + * more data. The H2 preface may be received in several segments, so the + * connection may not be upgraded to H2 until all the preface was received. + */ +static int h1_match_h2_preface(const struct buffer *buf) +{ + const struct ist preface = ist(H2_CONN_PREFACE); + size_t len = b_data(buf); + ssize_t ret; + + /* Truncate the preface to the number of available bytes to detect + * a partial match on a segmented preface. + */ + if (len > preface.len) + len = preface.len; + ret = b_isteq(buf, 0, b_data(buf), ist2(preface.ptr, len)); + + return (ret == len) ? ((len < preface.len) ? -1 : 1) : 0; +} + /* callback called on any event by the connection handler. * It applies changes and returns zero, -1 if everything was destroyed, * and -2 if the mux was destroyed, but the connection is still alive because @@ -4390,6 +4412,7 @@ static int h1_process(struct h1c * h1c) struct h1s *h1s = h1c->h1s; struct buffer *buf; size_t count; + int is_upgrade; /* When it happens for a backend connection, we may release it (it is probably a 408) */ if (h1c->flags & H1C_F_IS_BACK) @@ -4398,9 +4421,16 @@ static int h1_process(struct h1c * h1c) /* First of all handle H2 preface (except for DRAINING mode). * If H1 to H2 upgrade is allowed, no need to create the H1 * stream. If not allowed, a 405 must be returned. + * + * The preface can be received in several segments. If so, it will + * be detected as a partial preface and we must wait for more data. + * Otherwise, with the full preface, is > 0 and the + * H1 stream must not be created. */ - if (h1c->state != H1_CS_DRAINING && - b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) { + is_upgrade = (h1c->state != H1_CS_DRAINING) ? + h1_match_h2_preface(&h1c->ibuf) : 0; + + if (is_upgrade > 0) { if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request (the flag is */ !(h1s && (h1s->flags & H1S_F_NOT_FIRST)) && /* moved to the h1s once created) */ !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */ @@ -4428,6 +4458,18 @@ static int h1_process(struct h1c * h1c) } } + if (is_upgrade < 0) { + /* Partial H2 preface: it may be received in several + * segments, so wait for more data to complete the + * preface instead of parsing the buffered data as an + * H1 request. + */ + TRACE_STATE("partial H2 preface, waiting for more data", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn); + h1_set_idle_expiration(h1c); + h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); + goto no_parsing; + } + /* Create the H1 stream if not already there */ if (!h1s) { h1s = h1c_frt_stream_new(h1c, NULL, sess);