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.
* 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)
{
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;
}