Skip to content

Fix LZ77 window loss across BFINAL-terminated messages (RFC 7692 7.2.2) - #965

Open
arturobernalg wants to merge 1 commit into
apache:masterfrom
arturobernalg:permessage-deflate-bfinal-lz77-window
Open

arturobernalg wants to merge 1 commit into
apache:masterfrom
arturobernalg:permessage-deflate-bfinal-lz77-window

Conversation

@arturobernalg

Copy link
Copy Markdown
Member

Follow-up to #942, which fixed the server read thread spinning forever on a BFINAL-terminated DEFLATE block in permessage-deflate. That fix left two residual gaps in the same code path, not a regression against it (before #942, a BFINAL block broke the message outright on both counts):

  1. reset() without setDictionary() drops the LZ77 window a later block, or a later message under context takeover, is entitled to back-reference. RFC 7692 7.2.2 (MUST): "decompress each new message using the LZ77 sliding window used to process the last compressed message". Reproduced with the RFC's own literal wire vectors (7.2.3.4's BFINAL "Hello" followed by 7.2.3.2's context-takeover "Hello") against a real WebSocketServer and a hand-rolled RFC 6455 client: DataFormatException: invalid distance too far back, connection closed 1002.
  2. A BFINAL message missing the trailing 0x00 octet section 7.2.3.4 requires (a non-conformant sender, but one worth tolerating - the typical raw Deflater.finish()) leaves only this method's own synthetic sync-flush tail unconsumed; feeding just that into the reset inflater leaves it mid-header, corrupting whatever is decoded next, independent of any LZ77 window sharing.
    Fix: a 32 KiB ring buffer (WebSocketSlidingWindow) re-primes the dictionary on each mid-stream reset; the reset inflater is only fed again when more than the synthetic tail remains. Applied to both decoders (server PerMessageDeflateExtension, client PerMessageDeflate).

Tested beyond the unit/E2E suite added here: both decoders went through the project's own runnable WebSocket examples (WebSocketEchoServer/Client, H1.1; WebSocketH2EchoServer/Client, H2 cleartext via RFC 8441 Extended CONNECT) with no regression, plus a probe exercising permessage-deflate with context takeover over H2 specifically (3 consecutive large messages on one connection) - confirmed the H2 path reuses the same extension classes fixed here, not a separate implementation.

@arturobernalg

Copy link
Copy Markdown
Member Author

Please @Hsnmohd check

PR apache#942's mid-stream reset for a BFINAL-terminated block has two gaps.
First, reset() without setDictionary() drops the LZ77 window a later
block, or a later message under context takeover, is entitled to
back-reference (RFC 7692 7.2.2, MUST). Second, a BFINAL message missing
the trailing 0x00 octet section 7.2.3.4 requires - a non-conformant
sender, but one worth tolerating - leaves only this method's own
synthetic sync-flush tail unconsumed; feeding just that into the reset
inflater leaves it mid-header, corrupting whatever is decoded next.

Residual gap left by apache#942, not a regression: before it, a BFINAL block
in either decoder broke the message outright.

Fix: a 32 KiB ring buffer (WebSocketSlidingWindow) re-primes the
dictionary on each reset; the reset inflater is only fed again when
more than the synthetic tail remains. Costs one extra byte-copy per
decoded byte; Inflater exposes no other way to track its window.

Applied to both decoders (server PerMessageDeflateExtension, client
PerMessageDeflate), with unit and E2E coverage for both.
@arturobernalg
arturobernalg force-pushed the permessage-deflate-bfinal-lz77-window branch from 8080bdc to e712a2e Compare November 25, 2026 10:52
@ok2c ok2c added the 5.7.x 5.7 release series label Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.7.x 5.7 release series

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants