websocket icon indicating copy to clipboard operation
websocket copied to clipboard

Compressed messages fail in Safari

Open corny opened this issue 4 years ago • 13 comments

When Safari 13.1 for MacOS receives a compressed text message, it prints WebSocket connection to 'ws://localhost:8080/ws' failed: Could not decode a text frame as UTF-8 on the Javascript console and closes the WebSocket connection.

This is probably a bug in Safari. But this issue might help others to disable compression as a workaround. They have to pass AcceptOptions to Accept():

websocket.AcceptOptions{
	CompressionMode: websocket.CompressionDisabled,
}

corny avatar Mar 26 '20 17:03 corny

Received an email report about this as well, will look into it.

For now you can disable compression.

nhooyr avatar Mar 27 '20 02:03 nhooyr

Might be https://bugs.webkit.org/show_bug.cgi?id=202401 as reported in the email I got.

nhooyr avatar Mar 27 '20 02:03 nhooyr

Please let me know if this occurs on messages smaller than 4092 bytes.

nhooyr avatar Mar 27 '20 02:03 nhooyr

Yes, this also occurs with much smaller messages. A message with 186 bytes uncompressed size already causes the error. Messages with 30-40 bytes work well - maybe because they won't be compressed?

corny avatar Mar 27 '20 13:03 corny

Hmm, weird. Could you try the technology preview and see if that fixes it as the patches landed in Safari several months ago.

nhooyr avatar Mar 30 '20 05:03 nhooyr

Hmm actually fixes should be in 13.1 as it released just a week ago.

nhooyr avatar Mar 30 '20 05:03 nhooyr

Best to just disable compression for now until I figure out what's going on here.

nhooyr avatar Apr 06 '20 13:04 nhooyr

To clarify I mean I'm going to turn off compression by default.

nhooyr avatar Apr 06 '20 13:04 nhooyr

Confirmed reproduction btw with the new chat example. It's proven handy already.

nhooyr avatar Apr 14 '20 02:04 nhooyr

@nhooyr we are facing a similar issue with Safari version 15.1 (17612.2.9.1.20) in Monterey OS if compression is enabled

wsc, err := websocket.Accept(c.Response(), c.Request(), &websocket.AcceptOptions{
		CompressionMode:    websocket.CompressionContextTakeover,	
		InsecureSkipVerify: true,
})

We get below error is mentioned Safari,

The operation couldn’t be completed. (kNWErrorDomainPOSIX error 100 - Protocol error)

If we disable compression, it works fine. Is there any suggestion for using it with compression enabled?

gharia avatar Nov 29 '21 12:11 gharia

Now safari are using x-webkit-deflate-frame instead of permessage-deflate, so just comment case: x-webkit-deflate-frame is not working now.

I'm suffering this issue only under k8s + istio + gin. While running the same code on my macbook, this problem disappeared.

The log shows the error comes from deeply in frame.go -> readFrameHeader at line 51, as EOF. Is there a way to mirror the c.Request.Body reader to a buffer, so that I can log more about what exactly data have been read

Plasmatium avatar Feb 16 '22 16:02 Plasmatium

/reopen

Plasmatium avatar Feb 16 '22 16:02 Plasmatium

Sorry I don't currently have the time to help debug, in the middle of closing a home, working a new job and moving.

But I've reopened this issue to investigate at some future date.

nhooyr avatar Feb 18 '22 13:02 nhooyr

You can disable this conditionally only for Safari:

acceptOptions := &websocket.AcceptOptions{
	// ...
}

userAgentLower := strings.ToLower(r.Header.Get("User-Agent"))
isSafari := strings.Contains(userAgentLower, "safari") && !strings.Contains(userAgentLower, "chrome") && !strings.Contains(userAgentLower, "android")

if isSafari {
	acceptOptions.CompressionMode = websocket.CompressionDisabled
}

conn, err := websocket.Accept(w, r, acceptOptions)

See: https://github.com/poki/netlib/pull/28

erikdubbelboer avatar Apr 24 '23 09:04 erikdubbelboer

I would suggest not using compression at all. It's proven too unreliable even without Safari. See #391

Will be disabled by default in #256

nhooyr avatar Sep 28 '23 08:09 nhooyr

Going to close as the commit in dev exists to disable it.

nhooyr avatar Oct 13 '23 07:10 nhooyr