From: Christopher Faulet Date: Thu, 3 Dec 2026 07:37:01 +0000 (+0100) Subject: BUG/MEDIUM: mux-h2: always end the stream with the trailers X-Git-Url: https://git.kaiwu.me/web/http/static/%7B@url%7D?a=commitdiff_plain;h=32c23db971f1f726ed4554c1e04ca26fa270ed6a;p=haproxy.git BUG/MEDIUM: mux-h2: always end the stream with the trailers Since the commit 221c40bbf ("MINOR: htx: Add a noop block to carry the EOM flag on an empty message"), an EOT block without the EOM flag is a valid state. In this case, h2s_make_trailers() emitted a trailers HEADERS frame without the ES flag, and the END_STREAM was then sent in an empty DATA frame when the NOOP block carrying the EOM flag was processed. But in H2, the trailers frame must end the stream (RFC 9113 #8.1). The peer must treat such message as malformed. This happens for instance when H3 trailers are received without the FIN, the FIN arriving in a separate frame, and forwarded to an H2 server. Waiting for the EOM before emitting the trailers is not an option: the H2 mux has no way to be woken up when the NOOP block carrying the EOM is forwarded, so the stream could be blocked until a timeout. But nothing else than the EOM may follow the trailers. So, like for the H1 mux, the EOT block now ends the message on the wire, with or without the EOM flag. The trailers frame always carries the ES flag and the stream is always half-closed (local). The NOOP block carrying the EOM, if any, is then silently drained. This patch must be backported to 3.5. --- diff --git a/src/mux_h2.c b/src/mux_h2.c index 7ef197307..7337a57a4 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -8021,7 +8021,8 @@ static size_t h2s_skip_data(struct h2s *h2s, struct buffer *buf, size_t count) * are automatically removed from the message. The htx message is assumed to be * valid since produced from the internal code. Processing stops when meeting * the EOT, which *is* removed. All trailers are processed at once and sent as a - * single frame. The ES flag is always set. + * single frame. The ES flag is always set: in H2, the trailers always end the + * stream, even if the EOT block does not carry the EOM flag. */ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) { @@ -8034,7 +8035,6 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) int ret = 0; int hdr; int idx; - int eom = 0; int drop; TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s); @@ -8043,8 +8043,8 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) hdr = 0; /* Skip the trailers if the corresponding conf option was set. But the - * blocks must still be parsed to find the EOT and to know if the - * message is finished, to emit the ES flag. + * blocks must still be parsed to find the EOT. In all cases, the ES + * flag is emitted because the trailers always end the stream. */ drop = ((!(h2c->flags & H2_CF_IS_BACK) && (h2c->proxy->options & PR_O_HTTP_DROP_RES_TRLS)) || ((h2c->flags & H2_CF_IS_BACK) && (h2c->proxy->options & PR_O_HTTP_DROP_REQ_TRLS))); @@ -8052,10 +8052,8 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) for (blk = htx_get_head_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { type = htx_get_blk_type(blk); - if (type == HTX_BLK_EOT) { - eom = !!(blk->flags & HTX_BLK_FL_EOM); + if (type == HTX_BLK_EOT) break; - } if (type == HTX_BLK_TLR) { if (drop) continue; @@ -8077,13 +8075,6 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) /* marker for end of trailers */ list[hdr].n = ist(""); - /* An EOT block without the EOM flag means the message is not - * finished. The trailers frame must not carry the END_STREAM flag - * and, if there is nothing to emit, it is simply skipped. - */ - if (!eom && !hdr) - goto done; - mbuf = br_tail(h2c->mbuf); retry: if (!h2_get_buf(h2c, mbuf)) { @@ -8106,14 +8097,11 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) if (outbuf.size < 9) goto full; - /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4[+ES=1 if eom] */ - memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); - if (eom) - outbuf.area[4] |= 0x01; + /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ + memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); write_n32(outbuf.area + 5, h2s->id); // 4 bytes outbuf.data = 9; - /* encode all headers */ for (idx = 0; idx < hdr; idx++) { /* these ones do not exist in H2 or must not appear in @@ -8164,11 +8152,8 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) * received an empty trailers block followed or we removed its * contents above). Because of this we can't send a HEADERS * frame, so we have to cheat and instead send an empty DATA - * frame conveying the ES flag. It can only be done when the - * message is finished, an empty DATA frame without the ES - * flag is pointless. + * frame conveying the ES flag. */ - BUG_ON(!eom); outbuf.area[3] = H2_FT_DATA; outbuf.area[4] = H2_F_DATA_END_STREAM; } @@ -8197,15 +8182,12 @@ static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx) h2c->flags &= ~H2_CF_SHTS_UPDATED; } - if (eom) { - if (h2s->st == H2_SS_OPEN) - h2s->st = H2_SS_HLOC; - else - h2s_close(h2s); - } + if (h2s->st == H2_SS_OPEN) + h2s->st = H2_SS_HLOC; + else + h2s_close(h2s); /* OK we could properly deliver the response */ - done: /* remove all header blocks till the end and compute the corresponding size. */ ret = 0; blk = htx_get_head_blk(htx);