From: Christopher Faulet Date: Thu, 3 Dec 2026 08:57:45 +0000 (+0100) Subject: BUG/MEDIUM: mux-h1: report an error when the payload is smaller than the C-L X-Git-Url: https://git.kaiwu.me/web/http/static/%7B@url%7D?a=commitdiff_plain;h=e0a2af08f46a35c5138a0164db44f35160bc242f;p=haproxy.git BUG/MEDIUM: mux-h1: report an error when the payload is smaller than the C-L When the end of a message with a content-length was reached, the H1 mux never checked the whole announced payload was sent. On the last DATA block carrying the EOM flag, on a NOOP block carrying the EOM flag or on the trailers, the message was switched to the DONE state, regardless of the remaining length. With the zero-copy forwarding, the EOM flag was just ignored and the message remained in DATA state. The same was true when the payload was fast-forwarded, the end of input being ignored by h1_done_ff(). The connection was not reused, thanks to the check performed in h1s_destroy(). But, on the server side, the request was considered as finished while the server was still waiting for the remaining payload. So the client got a 504 after the server timeout. This happens for instance when the content-length is modified by an http-request rule. Now, an error is reported in this case, like for a payload larger than announced or a chunk smaller than announced. In h1_done_ff(), the stream is also woken up because it is called by the producer. Since the error cannot be reported on the HTX message in that case, it is reported on the server side and the client gets a 502 instead of a 500. A reg-test was added. This patch should be backported to all stable versions. Before 3.5, there is no NOOP block, so the corresponding part must be dropped. Before 2.9, there is no h1_done_ff(), so the corresponding part must be dropped too. --- diff --git a/reg-tests/http-messaging/h1_clen_short_payload.vtc b/reg-tests/http-messaging/h1_clen_short_payload.vtc new file mode 100644 index 000000000..2aa1d678c --- /dev/null +++ b/reg-tests/http-messaging/h1_clen_short_payload.vtc @@ -0,0 +1,110 @@ +# This test checks the H1 mux reports an error when the end of a message is +# reached while the payload is smaller than the announced content-length, +# instead of considering the message as finished. Otherwise, the server would +# wait for the remaining payload until a timeout. Here, the content-length is +# modified by an http-request rule. The end of the message is reported on the +# last DATA block for the H1 client (c1) and alone on a NOOP block for the H2 +# client (c2), the END_STREAM being received in a separate empty DATA frame. +# For the H1 client c3, the payload is sent once the request headers were +# received by the server, so it is fast-forwarded (zero-copy) with the end of +# the input. In that case, the error is reported on the server side, hence the +# 502. + +varnishtest "H1 request with a payload smaller than the announced content-length" +feature ignore_unknown_macro + +barrier b1 cond 2 +barrier b2 cond 2 + +server s1 { + rxreqhdrs + expect req.http.content-length == "10" + expect_close +} -start + +server s2 { + rxreqhdrs + expect req.http.content-length == "10" + # the payload is sent before the end of the message + recv 3 + barrier b1 sync + expect_close +} -start + +server s3 { + rxreqhdrs + expect req.http.content-length == "10" + barrier b2 sync + recv 3 + expect_close +} -start + +haproxy h1 -conf { + global + .if feature(THREAD) + thread-groups 1 + .endif + + defaults + mode http + timeout client "${HAPROXY_TEST_TIMEOUT-5s}" + timeout server "${HAPROXY_TEST_TIMEOUT-5s}" + timeout connect "${HAPROXY_TEST_TIMEOUT-5s}" + + frontend fe1 + bind "fd@${fe1}" + http-request set-header content-length 10 + default_backend be1 + + frontend fe2 + bind "fd@${fe2}" proto h2 + http-request set-header content-length 10 + default_backend be2 + + frontend fe3 + bind "fd@${fe3}" + http-request set-header content-length 10 + default_backend be3 + + backend be1 + server s1 ${s1_addr}:${s1_port} + + backend be2 + server s2 ${s2_addr}:${s2_port} + + backend be3 + server s3 ${s3_addr}:${s3_port} +} -start + +client c1 -connect ${h1_fe1_sock} { + txreq -req POST -url "/" -body "abc" + rxresp + expect resp.status == 500 +} -run + +client c2 -connect ${h1_fe2_sock} { + txpri + stream 0 { + txsettings + rxsettings + txsettings -ack + rxsettings + } -run + + stream 1 { + txreq -req "POST" -scheme "https" -url "/" -hdr "content-length" "3" -nostrend + txdata -data "abc" -nostrend + barrier b1 sync + txdata -data "" + rxresp + expect resp.status == 500 + } -run +} -run + +client c3 -connect ${h1_fe3_sock} { + send "POST / HTTP/1.1\r\nhost: localhost\r\ncontent-length: 3\r\n\r\n" + barrier b2 sync + send "abc" + rxresp + expect resp.status == 502 +} -run diff --git a/src/mux_h1.c b/src/mux_h1.c index b803c5eab..ca48fbfc4 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -3216,6 +3216,12 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, goto error; } h1m->curr_len -= count; + if (eom && h1m->curr_len) { + COUNT("less payload than announced (0-copy)"); + TRACE_ERROR("less payload than announced", + H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); + goto error; + } if (!h1m->curr_len) h1m->state = (eom ? H1_MSG_DONE : H1_MSG_TRAILERS); } @@ -3358,6 +3364,12 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, goto error; } h1m->curr_len -= vlen; + if (last_data && h1m->curr_len) { + COUNT("less payload than announced"); + TRACE_ERROR("less payload than announced", + H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); + goto error; + } } else if (h1m->flags & H1_MF_CHNK) { h1m->curr_len -= vlen; @@ -3399,6 +3411,13 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, if (!chunk_memcat(&outbuf, "0\r\n", 3)) goto full; } + else if ((h1m->flags & H1_MF_CLEN) && h1m->curr_len) { + /* The trailers are after the payload */ + COUNT("less payload than announced"); + TRACE_ERROR("less payload than announced", + H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); + goto error; + } h1m->state = H1_MSG_TRAILERS; break; } @@ -3424,6 +3443,12 @@ static size_t h1_make_data(struct h1s *h1s, struct h1m *h1m, struct buffer *buf, goto full; last_data = 1; } + else if ((h1m->flags & H1_MF_CLEN) && h1m->curr_len) { + COUNT("less payload than announced"); + TRACE_ERROR("less payload than announced", + H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); + goto error; + } else last_data = 1; } @@ -5526,7 +5551,22 @@ static size_t h1_done_ff(struct stconn *sc) if (h1m->curr_len) h1m->curr_len -= total; - if (!h1m->curr_len && (h1m->flags & H1_MF_CLEN)) { + if ((sd->iobuf.flags & IOBUF_FL_EOI) && (h1m->flags & H1_MF_CLEN) && + h1m->curr_len > (sd->iobuf.pipe ? sd->iobuf.pipe->data : 0)) { + /* The end of the message was reached but less payload than + * announced was forwarded (data still in the pipe will be sent + * later). Here we are called by the producer, so the stream + * must be woken up to be notified of the error. + */ + h1s->flags |= H1S_F_PROCESSING_ERROR; + se_fl_set(h1s->sd, SE_FL_ERROR); + se_report_term_evt(h1s->sd, se_tevt_type_proto_err); + COUNT("less payload than announced (fast-forward)"); + TRACE_ERROR("less payload than announced", + H1_EV_STRM_SEND|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); + h1_alert(h1s); + } + else if (!h1m->curr_len && (h1m->flags & H1_MF_CLEN)) { h1m->state = ((sd->iobuf.flags & IOBUF_FL_EOI) ? H1_MSG_DONE : H1_MSG_TRAILERS); /* Remove H1C_F_CO_MSG_MORE if a content-length was set and all the payload was sent. * At worst, it remains trailers from H2/QUIC messages that will be dropped.