Skip to content

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

Open
@robertknight

Description

@robertknight

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: hypothesis/client#1941

Steps to reproduce:

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

<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <body>
    <span id="wsStatus">Not connected</span>
    <button id="closeButton">Close connection</button>
    <p>
      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.
    </p>

    <script>
      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();
    </script>
  </body>
</html>

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions