Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket: fix bug where interleaved control frame breaks compression #3353

Open
wants to merge 1 commit into
base: branch6.4
Choose a base branch
from

Conversation

staticglobal
Copy link

There is a bug in at least versions 6.4 and 6.3 and I assume going back further than that, but have not verified:

If tornado receives a control frame while in the middle of processing a compressed multi-fragment data message, it corrupts the final message.

Consider tornado receiving the following sequence of websocket frames:

  • data frame 0 [opcode 0x2] [final: 0] [compressed: 1]
  • data frame 1 [opcode 0x0] [final: 0]
  • control frame [compressed: 0] [final: 1]
  • data frame 2 [opcode 0x0] [final: 1]

Background: the bit to indicate if a message is compressed only appears in the first frame. Tornado caches that flag in self._frame_compressed, and then when it receives the final frame it consults that instance variable to determine if the payload should be decompressed. The issue here is that if a control frame is received in the interim, the value of its compressed bit is used to overwrite self._frame_compressed

Per RFC7692:

An endpoint MUST NOT set the "Per-Message Compressed" bit of control
frames and non-first fragments of a data message. An endpoint
receiving such a frame MUST Fail the WebSocket Connection.

So self._frame_compressed was initially set to 1 when receiving the initial frame, then set to 0 when the control frame arrived, so then when the final data frame arrives and is sent for processing, Tornado believes it does not need to be decompressed when in fact it does.

This change does two things:

  • Do not update self._frame_compressed when receiving a control frame
  • Do not inspect self._frame_compressed when processing a control frame. It should never need to be decompressed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant