From d92233d58e4ef59bf59ed3098d3c6f8629b69fe9 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 12 May 2022 08:27:33 -0400 Subject: [PATCH] Add defensive code around Cluster.broadcastConnectUpdate (#5297) --- src/common/cluster/cluster.ts | 8 ++++++-- src/common/utils/type-narrowing.ts | 8 ++++++++ .../components/cluster-manager/cluster-status.tsx | 14 +++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/common/cluster/cluster.ts b/src/common/cluster/cluster.ts index fbe7880f3c..231f82eed2 100644 --- a/src/common/cluster/cluster.ts +++ b/src/common/cluster/cluster.ts @@ -504,7 +504,9 @@ export class Cluster implements ClusterModel, ClusterState { return ClusterStatus.AccessDenied; } - this.broadcastConnectUpdate(error.error || error.message, true); + const message = String(error.error || error.message) || String(error); + + this.broadcastConnectUpdate(message, true); return ClusterStatus.Offline; } @@ -521,7 +523,9 @@ export class Cluster implements ClusterModel, ClusterState { return ClusterStatus.AccessDenied; } - this.broadcastConnectUpdate(error.message, true); + const message = String(error.error || error.message) || String(error); + + this.broadcastConnectUpdate(message, true); return ClusterStatus.Offline; } diff --git a/src/common/utils/type-narrowing.ts b/src/common/utils/type-narrowing.ts index d03dfa6b9a..59303ebfc0 100644 --- a/src/common/utils/type-narrowing.ts +++ b/src/common/utils/type-narrowing.ts @@ -73,6 +73,14 @@ export function isString(val: unknown): val is string { return typeof val === "string"; } +/** + * checks if val is of type boolean + * @param val the value to be checked + */ +export function isBoolean(val: unknown): val is boolean { + return typeof val === "boolean"; +} + /** * checks if val is of type object and isn't null * @param val the value to be checked diff --git a/src/renderer/components/cluster-manager/cluster-status.tsx b/src/renderer/components/cluster-manager/cluster-status.tsx index 25a9b7408b..c424f8fa31 100644 --- a/src/renderer/components/cluster-manager/cluster-status.tsx +++ b/src/renderer/components/cluster-manager/cluster-status.tsx @@ -11,7 +11,7 @@ import React from "react"; import { ipcRendererOn } from "../../../common/ipc"; import type { Cluster } from "../../../common/cluster/cluster"; import type { IClassName } from "../../utils"; -import { cssNames } from "../../utils"; +import { isBoolean, hasTypedProperty, isObject, isString, cssNames } from "../../utils"; import { Button } from "../button"; import { Icon } from "../icon"; import { Spinner } from "../spinner"; @@ -55,8 +55,16 @@ class NonInjectedClusterStatus extends React.Component { - this.authOutput.push(res); + ipcRendererOn(`cluster:${this.cluster.id}:connection-update`, (evt, res: unknown) => { + if ( + isObject(res) + && hasTypedProperty(res, "message", isString) + && hasTypedProperty(res, "isError", isBoolean) + ) { + this.authOutput.push(res); + } else { + console.warn(`Got invalid connection update for ${this.cluster.id}`, { update: res }); + } }), ]); }