tornadoweb / tornado

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
http://www.tornadoweb.org/
Apache License 2.0
21.77k stars 5.51k forks source link

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

Open staticglobal opened 11 months ago

staticglobal commented 11 months ago

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:

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:

bdarnell commented 5 months ago

Thanks! The fix looks good. Is it feasible to add a test? I guess we don't have good test infrastructure for manipulating individual frames like this.

I've used Autobahn (https://github.com/crossbario/autobahn-testsuite) for this before; I should revive that and get it in CI.