Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear #290

Closed
tahayk opened this issue Nov 3, 2022 · 13 comments · Fixed by #293
Closed

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear #290

tahayk opened this issue Nov 3, 2022 · 13 comments · Fixed by #293

Comments

@tahayk
Copy link

tahayk commented Nov 3, 2022

I have been using PeerJS server for a couple of months, and sometimes I notice that the server crashes because of this error:

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear

Unfortunately I couldn't find a way to reproduce it, any idea how to handle it or fix it ?

Thank you in advance.

jonasgloning added a commit that referenced this issue Nov 4, 2022
@jonasgloning
Copy link
Member

jonasgloning commented Nov 4, 2022

Thanks for the report!

I found an issue from the ws library with more information.
TLDR: Some clients sometimes send invalid frames. The websocket server is missing an error handler and crashing because of it.

The fix should be just 3 lines of code. I'll publish it in the next few days, probably just after the weekend.

@jonasgloning jonasgloning linked a pull request Nov 4, 2022 that will close this issue
@jonasgloning jonasgloning removed a link to a pull request Nov 4, 2022
@tahayk
Copy link
Author

tahayk commented Nov 7, 2022

@jonasgloning thank you very much, looking forward for the next release.

@tahayk tahayk closed this as completed Nov 7, 2022
@intellix
Copy link

intellix commented Nov 7, 2022

I don't think is completed @tahayk - commits which updated some codestyling were created referencing this ticket and closed. There wasn't anything relating to this ticket yet... I'm also eagerly awaiting it :)

@jonasgloning
Copy link
Member

@intellix is right. I tried to create that commit on mobile - but the change didn't get saved, and the commit was mostly empty. I'll try again once I have access to a computer again.
Reopening this as a reminder :)

@jonasgloning jonasgloning reopened this Nov 7, 2022
github-actions bot pushed a commit that referenced this issue Nov 10, 2022
# [1.0.0-rc.3](v1.0.0-rc.2...v1.0.0-rc.3) (2022-11-10)

### Bug Fixes

* the server could crash if a client sends invalid frames ([33e6d75](33e6d75)), closes [#290](#290)
@github-actions

This comment was marked as outdated.

@jonasgloning
Copy link
Member

Hey @tahayk, I found a way to to reproduce your error message:

const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:9000/peerjs?key=peerjs&id=peerjs&key=peerjs');

ws.on('open', () => ws._socket.write(Buffer.from([0x10])));

But the server doesn’t crash. Even very old versions are fine.

Can you tell me a bit more about your environment? I’m afraid without reproducing the crash; we can’t fix it.

@RoundDiceChamp
Copy link

Hi @jonasgloning ,

maybe this helps. I'm able to reproduce the error with the following.

SERVER:

const express = require("express");
const { ExpressPeerServer } = require("peer");

const app = express();
app.on("error", (error) => console.error("Express error", error));

const server = app.listen(9000);
server.on("error", (error) => console.error("Server error", error));
server.on("clientError", (error) =>
  console.error("Server client error", error)
);

const peerServer = ExpressPeerServer(server);
peerServer.on("error", (error) =>
  console.error("ExpressPeerServer error", error)
);

app.use("/", peerServer);

CLIENT:

const WebSocket = require("ws");

const ws = new WebSocket("ws://localhost:9000/peerjs?key=peerjs&id=peerjs");

ws.on("error", (error) => console.error("Peer error", error));
ws.on("open", () => ws._socket.write(Buffer.from([0x10])));

The server then crushes with the following:
node:events:505
throw er; // Unhandled 'error' event
^

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear
at Receiver.getInfo (/Users/nope/repository/PeerTest/node_modules/ws/lib/receiver.js:171:14)
at Receiver.startLoop (/Users/nope/repository/PeerTest/node_modules/ws/lib/receiver.js:131:22)
at Receiver._write (/Users/nope/repository/PeerTest/node_modules/ws/lib/receiver.js:78:10)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at Receiver.Writable.write (node:internal/streams/writable:334:10)
at Socket.socketOnData (/Users/nope/repository/PeerTest/node_modules/ws/lib/websocket.js:1162:35)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
Emitted 'error' event on WebSocket instance at:
at Receiver.receiverOnError (/Users/nope/repository/PeerTest/node_modules/ws/lib/websocket.js:1049:13)
at Receiver.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'WS_ERR_UNEXPECTED_RSV_2_3',
[Symbol(status-code)]: 1002
}

@tahayk
Copy link
Author

tahayk commented Nov 17, 2022

Hey @jonasgloning, I couldn't reproduce the error with your code,
I'm running a docker container, everything is here: https://github.com/openreplay/openreplay/tree/main/peers
I'm using peerjs-server with expressjs

@jonasgloning
Copy link
Member

I could reproduce the crash with the code from @RoundDiceChamp, thanks!

New beta is on npm: https://github.com/peers/peerjs-server/releases/tag/v1.0.0-rc.4

@tahayk
Copy link
Author

tahayk commented Nov 21, 2022

Thank you very much @jonasgloning
I have been using v1.0.0-rc.4 for the past 3 days, so far no issues,
except that I'm still getting this issue:

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear
     at Receiver.getInfo (/work/node_modules/ws/lib/receiver.js:171:14)
     at Receiver.startLoop (/work/node_modules/ws/lib/receiver.js:131:22)
     at Receiver._write (/work/node_modules/ws/lib/receiver.js:78:10)
     at writeOrBuffer (node:internal/streams/writable:390:12)
     at _write (node:internal/streams/writable:331:10)
     at Receiver.Writable.write (node:internal/streams/writable:335:10)
     at Socket.socketOnData (/work/node_modules/ws/lib/websocket.js:1162:35)
     at Socket.emit (node:events:527:28)
     at addChunk (node:internal/streams/readable:324:12)
     at readableAddChunk (node:internal/streams/readable:297:9) {
   code: 'WS_ERR_UNEXPECTED_RSV_2_3',
   [Symbol(status-code)]: 1002
 }

but this time I'm able to catch it using the following code: (it wasn't possible before):

const app = express();
const server = app.listen(PORT, HOST, () => {
    console.log(`App listening on http://${HOST}:${PORT}`);
});

const peerServer = ExpressPeerServer(server, {
    debug: true,
    path: '/',
    proxied: true,
    allow_discovery: false
});

const peerError = (error) => {
    console.error('Error type:');
    console.error(error.type);
    console.error('Error message:');
    console.error(error);
}
peerServer.on('error', peerError);

@github-actions
Copy link

github-actions bot commented Jan 9, 2023

🎉 This issue has been resolved in version 1.0.0-rc.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Mar 7, 2023

🎉 This issue has been resolved in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this issue Apr 1, 2024
# [1.1.0-rc.1](v1.0.0...v1.1.0-rc.1) (2024-04-01)

### Bug Fixes

* **deps:** update dependency @types/express to v4.17.18 ([f4bcc16](f4bcc16))
* **deps:** update dependency @types/express to v4.17.19 ([ebec5b0](ebec5b0))
* **deps:** update dependency @types/express to v4.17.20 ([a6c01fd](a6c01fd))
* **deps:** update dependency @types/express to v4.17.21 ([80df87f](80df87f))
* **deps:** update dependency express to v4.18.3 ([ff0b83a](ff0b83a))
* **deps:** update dependency express to v4.19.0 ([208f108](208f108))
* **deps:** update dependency express to v4.19.1 ([3a67b5d](3a67b5d))
* **deps:** update dependency express to v4.19.2 ([ec993f6](ec993f6))
* **deps:** update dependency node-fetch to v3.3.1 ([2275ce3](2275ce3))
* **deps:** update dependency node-fetch to v3.3.2 ([05a1833](05a1833))
* **deps:** update dependency yargs to v17.7.2 ([23b4e47](23b4e47))
* empty npm package ([f4c359a](f4c359a)), closes [#318](#318)
* force new version ([26877ca](26877ca))
* import from ESM only environments ([993dee9](993dee9))
* reduce unnecessary timeouts ([638af56](638af56)), closes [#431](#431)
* remove confusing version number ([f6314df](f6314df))
* the server could crash if a client sends invalid frames ([33e6d75](33e6d75)), closes [#290](#290)

### Features

* build Docker image for `arm64` ([a61f103](a61f103))

### Reverts

* Revert "build: deploy to fly.io" ([c3de627](c3de627))
* Revert "chore: Configure Mend Bolt for GitHub (#349)" ([fd00aef](fd00aef)), closes [#349](#349)
Copy link

github-actions bot commented Apr 1, 2024

🎉 This issue has been resolved in version 1.1.0-rc.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants