question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket frame decoding issues

See original GitHub issue

Overview

I’m trying to proxy a websocket connection between my Slack web client (on Chrome) and the Slack servers.

Using the chrome dev tools, I can see JSON serialized text frames being sent back and forth over the socket connection. Eg:

image

I’ve set up a simple script using the websocket api to log messages as they come through the proxy:

from mitmproxy import ctx, websocket

def websocket_message(flow: websocket.WebSocketFlow) -> None:
    ctx.log.info(flow.messages[-1])

However, as the messages pass through the proxy, only messages I am receiving show up correctly.

Frames that the client is about to send show up as raw binary data instead of the expected serialized json text data.

Here is an excerpt of the logs for the same websocket connection as the image above:

>> send: text message: b'\xaaV*\xa9,HU\xb2\x02Q\x99y\xe9J:J\xc9\x19\x89yy\xa99@!\x173G\x0f\xafPo/G\xa0hf\x8a\x92\x95\xa1\x99\x89\xb9a-\x00'
>> send: text message: b'\xaaV*\xa9,HU\xb2R\xcaM-.NLOU\xd2QJ\xceH\xcc\xcbK\xcd\x01\x8a\xb9\x989zx\x85z{9\x02EKR+J\x80BYi@vf\x8a\x92\x95\xa1\x99\x89\xb9Q-\x00'
<< recv: text message: b'{"ok":true,"reply_to":16472,"ts":"1500471034.628876","text":"jf"}'
>> send: text message: b'\xaaV*\xa9,HU\xb2R*\xc8\xccKW\xd2Q\xcaLQ\xb224317\xae\x05\x00'
<< recv: text message: b'{"type":"pong","reply_to":16473}'
>> send: text message: b'\xaaV*\xa9,HU\xb2R*\xc8\xccKW\xd2Q\xcaLQ\xb224317\xa9\x05\x00'
>> send: text message: b'\xaaV*\xa9,HU\xb2R*\xc9L\xce\xceIU\xd2Q\xcaLQ\xb224317\xad\x05\x00'
<< recv: text message: b'{"type":"pong","reply_to":16474}'
>> send: text message: b'\xaaV*\xa9,HU\xb2R*\xc8\xccKW\xd2Q\xcaLQ\xb224317\xab\x05\x00'
<< recv: text message: b'{"type":"pong","reply_to":16476}'

When I tried proxying the same application with Charles, the frames appeared correctly both ways.

Steps to reproduce the problem:
  1. Run mitmproxy with the script provided above
  2. Open the Slack web client on Chrome and then open the network pane in dev tools
  3. Monitor the network pane and mitmproxy event log for discrepancies in frame encoding
System information

Mitmproxy version: 2.0.2 (release version) Python version: 3.6.2 Platform: Darwin-16.6.0-x86_64-i386-64bit SSL version: OpenSSL 1.1.0f 25 May 2017 Mac version: 10.12.5 (‘’, ‘’, ‘’) x86_64

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
hgarrereyncommented, Jul 19, 2017

Ok, I think the issue is that slack is using permessage deflate compression which isn’t handled by mitmproxy. I used a script to remove Sec-WebSocket-Extensions from the websocket request header and the resulting connection was set up without compression and was decoded properly by mitmproxy.

def request(flow: http.HTTPFlow) -> None:
    flow.request.headers["Sec-WebSocket-Extensions"] = ""

Permessage deflate is outlined here: https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-28

0reactions
fazegacommented, Mar 30, 2018

Thank you, you saved my day !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decode a websocket frame - Stack Overflow
I am trying to decode a websocket frame, but I'm not successful when it comes to decoding the extended payload. Here what I...
Read more >
Issue seen with websocket connection - Mattermost Forum
Send a ping FRAME with arbitrary data payload. Expected behavior. Server should not try and decode the data payload in the ping. Observed ......
Read more >
112089 - WebSocket should reject masked frame from server
Issue 112089: WebSocket should reject masked frame from server ... Console error message "Could not decode a text frame as UTF-8."
Read more >
Examples — websocket-client 1.4.2 documentation
WebSocket frames use masking with a random value to add entropy. The masking value in websocket-client is normally set using os.urandom in the...
Read more >
Writing a WebSocket server in C# - Web APIs | MDN
If you would like to use the WebSocket API, it is useful if you ... Messages may be sent in frames, but for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found