From 3224a1c3130ceb3ec2bbd3810d791be3ad16c1d8 Mon Sep 17 00:00:00 2001
From: Patrick Griffis <pgriffis@igalia.com>
Date: Wed, 16 Sep 2026 15:24:48 +0800
Subject: [PATCH] http2: Never send more body bytes than nghttp2 requested

When a request body comes from a non-pollable GInputStream, the data is
read asynchronously into data_source_buffer and copied out on the next
data-source callback. The copy used the buffered length and only asserted
that it did not exceed nghttp2's requested length. A peer that shrinks the
stream window with a SETTINGS frame after the read was issued makes the
callback's length smaller than the buffered amount, so the memcpy wrote
past the output buffer (the assertion is compiled out in release builds).

Copy at most the requested length and keep the remainder in the buffer for
the following callback.

The EOF flag handling of the original change is not applicable to 3.0.x,
where on_data_read() clears the buffer when it sets data_source_eof, so a
non-empty buffer always implies data_source_eof is FALSE.

Backport of 4f38c143553c77dc58c9d25ba9cac3ff3598e03c.

Fixes #551 (CVE-2026-85534)
---
 libsoup/http2/soup-client-message-io-http2.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/libsoup/http2/soup-client-message-io-http2.c b/libsoup/http2/soup-client-message-io-http2.c
index 2dd1e356..2d40f71c 100644
--- a/libsoup/http2/soup-client-message-io-http2.c
+++ b/libsoup/http2/soup-client-message-io-http2.c
@@ -1036,13 +1036,21 @@ on_data_source_read_callback (nghttp2_session     *session,
 
                 guint buffer_len = data->data_source_buffer->len;
                 if (buffer_len) {
-                        h2_debug (data->io, data, "[SEND_BODY] Sending %zu", buffer_len);
-                        g_assert (buffer_len <= length); /* QUESTION: Maybe not reliable */
-                        memcpy (buf, data->data_source_buffer->data, buffer_len);
-                        log_request_data (data, buf, buffer_len);
-                        g_byte_array_set_size (data->data_source_buffer, 0);
+                        /* nghttp2 may accept fewer bytes than we buffered, for
+                         * example after the peer shrinks the stream window with
+                         * a SETTINGS frame.
+                         */
+                        gsize to_send = MIN (buffer_len, length);
+
+                        h2_debug (data->io, data, "[SEND_BODY] Sending %zu", to_send);
+                        memcpy (buf, data->data_source_buffer->data, to_send);
+                        log_request_data (data, buf, to_send);
+                        if (to_send < buffer_len)
+                                g_byte_array_remove_range (data->data_source_buffer, 0, to_send);
+                        else
+                                g_byte_array_set_size (data->data_source_buffer, 0);
                         data->io->in_callback--;
-                        return buffer_len;
+                        return to_send;
                 } else if (data->data_source_eof) {
                         h2_debug (data->io, data, "[SEND_BODY] EOF");
                         *data_flags |= NGHTTP2_DATA_FLAG_EOF;
-- 
2.54.0

