]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: htx: never merge the next DATA block into the referenced one on defrag
authorOlivier Houchard <ohouchard@haproxy.com>
Tue, 17 Nov 2026 14:06:51 +0000 (15:06 +0100)
committerOlivier Houchard <cognet@ci0.org>
Tue, 17 Nov 2026 14:49:43 +0000 (15:49 +0100)
When htx_defrag() is called with a referenced DATA block, this block is
copied as a new block to preserve it. But the DATA block following it, if
any, is then copied using __htx_add_data_type_atonce(), which appends it to
the tail block, i.e. to the referenced block itself. The returned block is
thus larger than requested by <blkinfo> and contains the next block's data.
The compression filter, which continues with the block following the one
it replaced with htx_replace_blk_value(), then skips these data and
forwards them uncompressed in the middle of the compressed stream.

This can be triggered with a Lua filter appending data at the end of a
response (HTTPMessage:append()) before the compression filter: the appended
data are added in a new DATA block, and when the compressed output of the
previous block is larger than the input and requires a defragmentation, the
appended data are emitted as-is and the response is corrupted.

Let's never merge a DATA block into the referenced block.

This was introduced by commit 0c6f2207f ("MEDIUM: htx: Refactor htx
defragmentation to merge data blocks"). It should be backported to 3.4.

src/htx.c

index 5583cd5a2ad2e1a644e03243f954a5a2fa505a06..68cd003ae3596e5606b933b6183654766518eea2 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -44,10 +44,10 @@ static inline __attribute__((always_inline)) int htx_hdrs_too_big(const struct h
  * part. A temporary buffer is used to do so. This function never fails. Most of
  * time, we need keep a ref on a specific HTX block. Thus is <blk> is set, the
  * pointer on its new position, after defrag, is returned. If <blk> is a DATA
- * block, no merge with any previous DATA block is performed. In addition, if
- * the size of the block must be altered, <blkinfo> info must be provided (!=
- * 0). But in this case, it remains the caller responsibility to update the
- * block content.
+ * block, no merge with any previous nor next DATA block is performed. In
+ * addition, if the size of the block must be altered, <blkinfo> info must be
+ * provided (!= 0). But in this case, it remains the caller responsibility to
+ * update the block content.
  */
 struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk, uint32_t blkinfo)
 {
@@ -77,7 +77,8 @@ struct htx_blk *htx_defrag(struct htx *htx, struct htx_blk *blk, uint32_t blkinf
                switch (type) {
                        case HTX_BLK_DATA:
                        case HTX_BLK_RAW_DATA:
-                               if (blk != oldblk) {
+                               if (blk != oldblk &&
+                                   (blkpos == (uint32_t)-1 || htx_get_tail(tmp) != (int32_t)blkpos)) {
                                        newblk = __htx_add_data_type_atonce(tmp, htx_get_blk_value(htx, oldblk), type);
                                        break;
                                }