hypothesis / hypothesis/h

WebSocket server responds incorrectly if connection is closed without a status code

Open
#5,969 3 comments 0 reactions 0 assignees View on GitHub
code health WebSocket
Dominant language
Python
Stars
3.2k
Forks
459
Avg merge
27d 1h
Merged PRs (30d)
1

Description

If a browser-based client connects to h's WebSocket server using the `WebSocket` API and later closes the connection by calling `close()` without any arguments, h's WebSocket server will respond with an invalid close frame with a status code of 1005 which will result in Safari and Chrome displaying an error in the console.

Client issue: https://github.com/hypothesis/client/pull/1941

**Steps to reproduce:**

Save the following HTML file locally, open it and click the "Close connection" button.

```html





Not connected
Close connection


Clicking the "Close connection" button will display an error in the
browser console in Safari and Chrome, and the WS will receive a
"close" event with a status of 1006 indicating an abnormal disconnection.

In Firefox a status of 1005 is reported.


const btn = document.querySelector('#closeButton');
const status = document.querySelector('#wsStatus');

const ws = new WebSocket('ws://localhost:5001/ws');
ws.onopen = () => status.textContent = 'Connected';
ws.onclose = e => status.textContent = `Closed (status: ${e.code})`;

btn.onclick = () => ws.close();

```

**Expected:** No error message in the browser console and a close status of 1005.
**Actual:** Safari and Chrome show an error message in the browser console and a close status of 1006.

**Notes**

The 1005 status code should never be sent over the wire. Instead this code is reserved to be synthesized by the receiving end of the connection to indicate that a close frame was received with no status code.

I believe Safari and Chrome's behavior here are correct. Firefox's behavior is an error.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.