From 625b00c247b7e36c0c4e210e86c37d2c8b2bfb9e Mon Sep 17 00:00:00 2001 From: Steven Johnstone Date: Thu, 17 Feb 2022 13:37:34 +0000 Subject: [PATCH] Introduce and use "openBrowser" (#4876) Co-authored-by: Steven Johnstone --- src/common/utils/index.ts | 2 +- src/common/utils/openBrowser.ts | 29 +++++++++++++++++++ src/common/utils/openExternal.ts | 11 ------- src/extensions/common-api/utils.ts | 2 +- src/main/menu/menu.ts | 11 +++++-- src/main/window-manager.ts | 8 +++-- .../port-forward/port-forward-utils.ts | 4 +-- 7 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 src/common/utils/openBrowser.ts delete mode 100644 src/common/utils/openExternal.ts diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index 8b213228f0..d9130cbb05 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -30,7 +30,7 @@ export * from "./getRandId"; export * from "./hash-set"; export * from "./n-fircate"; export * from "./objects"; -export * from "./openExternal"; +export * from "./openBrowser"; export * from "./paths"; export * from "./promise-exec"; export * from "./reject-promise"; diff --git a/src/common/utils/openBrowser.ts b/src/common/utils/openBrowser.ts new file mode 100644 index 0000000000..3ba29b699b --- /dev/null +++ b/src/common/utils/openBrowser.ts @@ -0,0 +1,29 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { shell } from "electron"; + +const allowedProtocols = new Set(["http:", "https:"]); + +/** + * Opens a link using the program configured as the default browser + * on the target platform. Will reject URLs with a scheme other than + * http or https to prevent programs other than the default browser + * running. + * + * @param url The URL to open + */ +export function openBrowser(url: string): Promise { + if (allowedProtocols.has(new URL(url).protocol)) { + return shell.openExternal(url); + } + + return Promise.reject(new TypeError("not an http(s) URL")); +} + +/** + * @deprecated use openBrowser + */ +export const openExternal = openBrowser; diff --git a/src/common/utils/openExternal.ts b/src/common/utils/openExternal.ts deleted file mode 100644 index 19966cd10d..0000000000 --- a/src/common/utils/openExternal.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -// Opens a link in external browser -import { shell } from "electron"; - -export function openExternal(url: string) { - return shell.openExternal(url); -} diff --git a/src/extensions/common-api/utils.ts b/src/extensions/common-api/utils.ts index 7c3234a107..3210e4a12a 100644 --- a/src/extensions/common-api/utils.ts +++ b/src/extensions/common-api/utils.ts @@ -3,6 +3,6 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -export { Singleton, openExternal } from "../../common/utils"; +export { Singleton, openExternal, openBrowser } from "../../common/utils"; export { prevDefault, stopPropagation } from "../../renderer/utils/prevDefault"; export { cssNames } from "../../renderer/utils/cssNames"; diff --git a/src/main/menu/menu.ts b/src/main/menu/menu.ts index 0d3875042e..41bac74668 100644 --- a/src/main/menu/menu.ts +++ b/src/main/menu/menu.ts @@ -2,13 +2,14 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, webContents, shell } from "electron"; +import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, webContents } from "electron"; import { autorun, IComputedValue } from "mobx"; import type { WindowManager } from "../window-manager"; import { appName, isMac, isWindows, docsUrl, supportUrl, productName } from "../../common/vars"; import logger from "../logger"; import { exitApp } from "../exit-app"; import { broadcastMessage } from "../../common/ipc"; +import { openBrowser } from "../../common/utils"; import * as packageJson from "../../../package.json"; import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../../common/routes"; import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater"; @@ -261,14 +262,18 @@ export function getAppMenu( label: "Documentation", id: "documentation", click: async () => { - shell.openExternal(docsUrl); + openBrowser(docsUrl).catch(error => { + logger.error("[MENU]: failed to open browser", { error }); + }); }, }, { label: "Support", id: "support", click: async () => { - shell.openExternal(supportUrl); + openBrowser(supportUrl).catch(error => { + logger.error("[MENU]: failed to open browser", { error }); + }); }, }, ...ignoreIf(isMac, [ diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 9985f39597..1c34e4115a 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -5,11 +5,11 @@ import type { ClusterId } from "../common/cluster-types"; import { makeObservable, observable } from "mobx"; -import { app, BrowserWindow, dialog, ipcMain, shell, webContents } from "electron"; +import { app, BrowserWindow, dialog, ipcMain, webContents } from "electron"; import windowStateKeeper from "electron-window-state"; import { appEventBus } from "../common/app-event-bus/event-bus"; import { ipcMainOn } from "../common/ipc"; -import { delay, iter, Singleton } from "../common/utils"; +import { delay, iter, Singleton, openBrowser } from "../common/utils"; import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames"; import { IpcRendererNavigationEvents } from "../renderer/navigation/events"; import logger from "./logger"; @@ -134,7 +134,9 @@ export class WindowManager extends Singleton { webPreferences.nodeIntegration = false; }) .setWindowOpenHandler((details) => { - shell.openExternal(details.url); + openBrowser(details.url).catch(error => { + logger.error("[WINDOW-MANAGER]: failed to open browser", { error }); + }); return { action: "deny" }; }); diff --git a/src/renderer/port-forward/port-forward-utils.ts b/src/renderer/port-forward/port-forward-utils.ts index 31fd1f165b..a88f75a305 100644 --- a/src/renderer/port-forward/port-forward-utils.ts +++ b/src/renderer/port-forward/port-forward-utils.ts @@ -4,7 +4,7 @@ */ -import { openExternal } from "../utils"; +import { openBrowser } from "../utils"; import { Notifications } from "../components/notifications"; import type { ForwardedPort } from "./port-forward-item"; import logger from "../../common/logger"; @@ -16,7 +16,7 @@ export function portForwardAddress(portForward: ForwardedPort) { export function openPortForward(portForward: ForwardedPort) { const browseTo = portForwardAddress(portForward); - openExternal(browseTo) + openBrowser(browseTo) .catch(error => { logger.error(`failed to open in browser: ${error}`, { port: portForward.port,