]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: stconn: Only forward kip hint on streams committed to forward data
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 19 Nov 2026 08:00:00 +0000 (09:00 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 23 Nov 2026 13:49:44 +0000 (14:49 +0100)
The <kip> hint of a sedesc is the payload length the producer endpoint
estimates it will emit. It is forwarded to the <kop> hint of the opposite
sedesc by sc_ep_fwd_kip(), so the consumer endpoint can use it as an
optimization, for instance the H1 mux to announce a chunk size covering the
whole payload at once.

But <kip> could be set by the producer on the data it demultiplexes, before
the app layer has a chance to see it. In particular, when data filters are
registered on the channel, flt_http_payload() resets <kip> after each pass,
because the filters may alter or drop any part of the payload. But in the
meantime, an I/O callback may already have moved the hint to the opposite
side: the consumer then uses a stale <kop> value, and the data it emits may
not match what this value announces.

This is how the H1 mux could be led to announce a chunk smaller than the
data sent with it, and to emit the remaining bytes without any chunk
framing, making an HTTP/1 server process a part of the request payload as
another request (request smuggling).

The root of the issue is that <kip>/<kop> are hints on data which were
not evaluated by the app layer yet, but they may be consumed as if they
were. <kop> is a minimal guarantee on the output payload: when it is
set, at least this size will be forwarded. It is only safe to feed it
with <kip> once the app layer committed to forward the data as-is.

This patch introduced a safe version of sc_ep_fwd_kip(),
sc_ep_fwd_kip_when_committed(), which only forwards the hint when the
producer channel is configured to forward data indefinitely (to_forward ==
CHN_INFINITE_FORWARD). This is the only case where the app layer committed
to forward everything, without any alteration: http_wait_request_body() and
http_wait_for_response_body() set the infinite forwarding only when no data
filter is registered on the channel. Otherwise <kop> stays null and the
consumer endpoints fall back to their conservative behavior (the H1 mux
announces the size of the data it emits).

When the app layer is not committed yet, <kip> is reset instead of being
kept for later. Until the infinite forwarding is reached, data may be
forwarded and emitted by the consumer with a null <kop>, while <kip> still
accounts for them, because it is only decremented when forwarded. Moving
it later would then set <kop> higher than the remaining payload, and the
H1 mux would announce a chunk bigger than the data left to send. Resetting
it keeps <kop> conservative: lower than the remaining payload, never
higher.

Thanks to Hyunho Seo for the issue analysis.

This patch must be backported as far as 3.3.

include/haproxy/stconn.h
src/applet.c
src/stconn.c

index 50db0c0bd31f38ed3c884387e5ddee706496b388..2034d4679bc585113231941fbdb32f4e769c6c5d 100644 (file)
@@ -44,6 +44,7 @@ struct stconn *sc_new(struct sedesc *sedesc);
 void sedesc_free(struct sedesc *sedesc);
 
 void se_shutdown(struct sedesc *sedesc, enum se_shut_mode mode);
+void sc_ep_fwd_kip_when_committed(struct stconn *sc_in, struct stconn *sc_out);
 
 struct stconn *sc_new_from_endp(struct sedesc *sedesc, struct session *sess, struct buffer *input);
 struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags);
index 94494d17cb7d1e92603a8d3b1db5ecc6cec743c7..e8510370113231668c57ca53816d335e97e51b2f 100644 (file)
@@ -844,7 +844,7 @@ struct task *task_run_applet(struct task *t, void *context, unsigned int state)
 
        channel_check_idletimer(ic);
 
-       sc_ep_fwd_kip(sco, sc);
+       sc_ep_fwd_kip_when_committed(sco, sc);
 
        input  = applet_output_data(app);
        output = co_data(oc);
index 327218edd42843f89a498097a9445669eb2d02a2..67d15a0c969a463739875ce1389e835c6d6ea1a6 100644 (file)
@@ -39,6 +39,31 @@ struct task *hld_strm_task(struct task *t, void *context, unsigned int state)
 static int sc_conn_recv(struct stconn *sc);
 static int sc_conn_send(struct stconn *sc);
 
+/* Forwards the <kip> hint from the producer to the consumer side, but only
+ * when the app layer committed to forward the data as-is. <kip> and <kop>
+ * are hints on the data the producer will emit, possibly before the app layer
+ * has seen them, so they must not be trusted for anything else than an
+ * optimization: here, announcing a chunk size that covers the whole payload
+ * at once. When data filters are registered on the channel, they may alter or
+ * drop any part of the payload after <kip> was accounted for, so <kop> could
+ * become a stale or wrong value. It is only safe to forward <kip> once the
+ * app layer committed to forward everything (infinite forwarding).
+ *
+ * Before that, data may be forwarded and emitted by the consumer while <kip>
+ * still accounts for them. Forwarding <kip> later would then lead to a <kop>
+ * value higher than the remaining payload. So, when the app layer has not
+ * committed yet, <kip> is reset instead. It is conservative: <kop> may be
+ * lower than the remaining payload, but never higher.
+ */
+void sc_ep_fwd_kip_when_committed(struct stconn *sc_in, struct stconn *sc_out)
+{
+       if (sc_ic(sc_in)->to_forward != CHN_INFINITE_FORWARD) {
+               sc_in->sedesc->kip = 0;
+               return;
+       }
+       sc_ep_fwd_kip(sc_in, sc_out);
+}
+
 /* Initializes an endpoint */
 void sedesc_init(struct sedesc *sedesc)
 {
@@ -1157,7 +1182,7 @@ int sc_conn_recv(struct stconn *sc)
                        flags |= CO_RFL_BUF_FLUSH;
                        goto abort_fastfwd;
                }
-               sc_ep_fwd_kip(sc, sc_opposite(sc));
+               sc_ep_fwd_kip_when_committed(sc, sc_opposite(sc));
                ret = CALL_MUX_WITH_RET(conn->mux, fastfwd(sc, ic->to_forward, flags));
                if (ret < 0)
                        goto abort_fastfwd;
@@ -1457,7 +1482,7 @@ int sc_conn_send(struct stconn *sc)
        if (!conn->mux)
                return 0;
 
-       sc_ep_fwd_kip(sco, sc);
+       sc_ep_fwd_kip_when_committed(sco, sc);
 
        if (sc_ep_have_ff_data(sc)) {
                unsigned int send_flag = 0;
@@ -1851,7 +1876,7 @@ int sc_applet_recv(struct stconn *sc)
                        flags |= CO_RFL_BUF_FLUSH;
                        goto abort_fastfwd;
                }
-               sc_ep_fwd_kip(sc, sc_opposite(sc));
+               sc_ep_fwd_kip_when_committed(sc, sc_opposite(sc));
                ret = appctx_fastfwd(sc, ic->to_forward, flags);
                if (ret < 0)
                        goto abort_fastfwd;
@@ -2081,7 +2106,7 @@ int sc_applet_send(struct stconn *sc)
        if (se_fl_test(sc->sedesc, SE_FL_ORPHAN))
                return 0;
 
-       sc_ep_fwd_kip(sco, sc);
+       sc_ep_fwd_kip_when_committed(sco, sc);
 
        /* TODO: Splicing is not supported, so it is not possible to have FF data stuck into the I/O buf */
        BUG_ON(sc_ep_have_ff_data(sc));