peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

`Uncaught TypeError: Cannot read properties of null (reading '__peerData') in BinaryPack and Json DataConnection when receiving null payload`

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments

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):

  1. pack(null) or JSON.stringify(null) encodes the value into valid wire bytes.
  2. The receiver receives the MessageEvent on the RTCDataChannel.
  3. In BinaryPack._handleDataMessage (lib/dataconnection/BufferedConnection/BinaryPack.ts:34):
    const deserializedData = unpack(data);
    const peerData = deserializedData["__peerData"]; // <--- THROWS ON null!
    
  4. In Json._handleDataMessage (lib/dataconnection/BufferedConnection/Json.ts:18):
    const deserializedData = this.parse(this.decoder.decode(data));
    const peerData = deserializedData["__peerData"]; // <--- THROWS ON null!
    
  5. In JavaScript, indexing into null throws: TypeError: Cannot read properties of null (reading '__peerData')
  6. Because _handleDataMessage has no try/catch wrapper, this unhandled exception crashes the DataChannel message event listener.

How can we reproduce the issue?

  1. Establish a data connection between Alice and Bob:
    const conn = alice.connect("bob");
    
  2. Once connected, Alice sends null:
    conn.send(null);
    
  3. 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") {

codeCraft-Ritik avatar Oct 08 '26 04:10 codeCraft-Ritik