peerjs
peerjs copied to clipboard
`Uncaught TypeError: Cannot read properties of null (reading '__peerData') in BinaryPack and Json DataConnection when receiving null payload`
Please, check for existing issues to avoid duplicates.
- [x] No similar issues found.
What happened?
In WebRTC DataConnections, null is a completely standard JSON and BinaryPack primitive value.
When a sender transmits null via conn.send(null):
pack(null)orJSON.stringify(null)encodes the value into valid wire bytes.- The receiver receives the
MessageEventon theRTCDataChannel. - In
BinaryPack._handleDataMessage(lib/dataconnection/BufferedConnection/BinaryPack.ts:34):const deserializedData = unpack(data); const peerData = deserializedData["__peerData"]; // <--- THROWS ON null! - In
Json._handleDataMessage(lib/dataconnection/BufferedConnection/Json.ts:18):const deserializedData = this.parse(this.decoder.decode(data)); const peerData = deserializedData["__peerData"]; // <--- THROWS ON null! - In JavaScript, indexing into
nullthrows:TypeError: Cannot read properties of null (reading '__peerData') - Because
_handleDataMessagehas notry/catchwrapper, this unhandled exception crashes the DataChannel message event listener.
How can we reproduce the issue?
- Establish a data connection between Alice and Bob:
const conn = alice.connect("bob"); - Once connected, Alice sends
null:conn.send(null); - Bob's console throws:
Uncaught TypeError: Cannot read properties of null (reading '__peerData') at BinaryPack._handleDataMessage (BinaryPack.ts:34)
What do you expected to happen?
Bob should receive the null payload cleanly via conn.on("data", (data) => console.log(data === null)) without throwing a runtime error.
Environment setup
Alice:
- OS: Ubuntu 22.04 / Windows 11
- Platform: Browser
- Browser: Chrome 122.0
Bob:
- OS: Windows 11 / macOS Sonoma
- Platform: Browser
- Browser: Firefox 123.0 / Chrome 122.0
PeerJS version: 1.5.5
Is this a regression?
No, exists in BinaryPack.ts and Json.ts. Note that StreamConnection/MsgPack.ts:15 already safely uses msg.__peerData?.type === "close".
Anything else?
Can be verified with a one-liner:
node -e "let x = null; console.log(x['__peerData'])"
// TypeError: Cannot read properties of null (reading '__peerData')
Proposed Fix:
--- a/lib/dataconnection/BufferedConnection/BinaryPack.ts
+++ b/lib/dataconnection/BufferedConnection/BinaryPack.ts
@@ -34,3 +34,3 @@ export class BinaryPack extends BufferedConnection {
// PeerJS specific message
- const peerData = deserializedData["__peerData"];
+ const peerData = deserializedData?.["__peerData"];
if (peerData) {
--- a/lib/dataconnection/BufferedConnection/Json.ts
+++ b/lib/dataconnection/BufferedConnection/Json.ts
@@ -18,3 +18,3 @@ export class Json extends BufferedConnection {
// PeerJS specific message
- const peerData = deserializedData["__peerData"];
+ const peerData = deserializedData?.["__peerData"];
if (peerData && peerData.type === "close") {