home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catch JSON.parse exceptions caused by faulty JSON string message from HA

RonnyWinkler opened this issue · comments

Hello HA websocket team,

I get JS exceptions while using the module caused by double quotation marks inside the message or other faulty JSON content. This is out of control of the app using this module.
A JSON error is causing an exception inside this module on JSON.parse().
Could this module catch errors on JSON.parse? That would at least prevent unhandled exceptions and possible app crashes.
The message could be ignored in this case. Or perhaps it would be possible to react on such errors with a special event returned from subscribeEvents() returning the JSON. This way the app can handle the error or at least the user can be informed about this error.

Here is the error stack:
SyntaxError: Unexpected token : in JSON at position 3272
at JSON.parse ()
at WebSocket.Connection._handleMessage (/node_modules/home-assistant-js-websocket/dist/haws.cjs:227:37)
at callListener (/node_modules/ws/lib/event-target.js:290:14)
at WebSocket.onMessage (/node_modules/ws/lib/event-target.js:209:9)
at WebSocket.emit (node:events:526:28)
at Receiver.receiverOnMessage (/node_modules/ws/lib/websocket.js:1180:20)
at Receiver.emit (node:events:526:28)
at Receiver.dataMessage (/node_modules/ws/lib/receiver.js:541:14)
at /node_modules/ws/lib/receiver.js:492:23
at /node_modules/ws/lib/permessage-deflate.js:309:9

This is an example of an faulty JSON string:
"context":{"id":"01GWY3Z35YSG4EDP1VG2ntext":{"id":"01GWY408A0S01VQ7J3J9TS0P89","parent_id":null,"user_id":null}}}
                                           /\ 
                                           ||

Which version of Home Assistant are you using?

Does it have orjson older than https://github.com/ijl/orjson/releases/tag/3.8.7?

Personally I'm using 2023.2.5
But I get this crash reports from other users using my Homey SmartHome app using this module to connect to HA. I can't say which version the users are using.
So it would need a HA version >2023.3.x (based on the commit date)? And this will ensure the JSON string is correct?

So it would need a HA version >2023.3.x (based on the commit date)? And this will ensure the JSON string is correct?

Assuming the orjson musl compat issue is the cause, thats correct.

Thanks. I'll inform users reporting issues caused by wrong JSONs.
If the error is still present on new HA versions I'll update this issue.