fix: Do not let a malformed WebSocket frame escape the message listener (#33275)

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
This commit is contained in:
Arpit Jain authored and GitHub committed 2026-10-04 07:27:35 +02:00
1 parent 1d7d708ad5
commit 6cd4aeb032
2 files changed
+13 -2

No files matched your search

+7 -2
View File
@@ -163,8 +163,13 @@ export class Frontend extends Extension {
ws.on("message", (data: Buffer, isBinary: boolean) => {
if (!isBinary && data) {
const message = data.toString();
const {topic, payload} = JSON.parse(message);
this.mqtt.onMessage(`${this.mqttBaseTopic}/${topic}`, Buffer.from(stringify(payload)));
try {
const {topic, payload} = JSON.parse(message);
this.mqtt.onMessage(`${this.mqttBaseTopic}/${topic}`, Buffer.from(stringify(payload)));
} catch {
logger.error(`Received invalid message from WebSocket: ${message}`);
}
}
});
+6
View File
@@ -262,6 +262,12 @@ describe("Extension: Frontend", () => {
mockWSClientEvents.message(null, false);
await flushPromises();
// Malformed payload, must not escape the listener
mockLogger.error.mockClear();
expect(() => mockWSClientEvents.message("not json", false)).not.toThrow();
expect(mockLogger.error).toHaveBeenCalledWith("Received invalid message from WebSocket: not json");
await flushPromises();
// Error
mockWSClientEvents.error(new Error("This is an error"));
expect(mockLogger.error).toHaveBeenCalledWith("WebSocket error: This is an error");