From bb3cec0d139694b852a55624342ab1106f46b1da Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 16 May 2025 20:48:34 +0200 Subject: [PATCH] Fix Invalid URL error --- src/main/utils/ipcWrappers.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/utils/ipcWrappers.ts b/src/main/utils/ipcWrappers.ts index 7e8da42..27a35fd 100644 --- a/src/main/utils/ipcWrappers.ts +++ b/src/main/utils/ipcWrappers.ts @@ -8,25 +8,33 @@ import { ipcMain, IpcMainEvent, IpcMainInvokeEvent, WebFrameMain } from "electro import { DISCORD_HOSTNAMES } from "main/constants"; import { IpcEvents } from "shared/IpcEvents"; -export function validateSender(frame: WebFrameMain | null) { - if (!frame) throw new Error("ipc: No sender frame"); +export function validateSender(frame: WebFrameMain | null, event: string) { + if (!frame) throw new Error(`ipc[${event}]: No sender frame`); + if (!frame.url) return; + + try { + var { hostname, protocol } = new URL(frame.url); + } catch (e) { + throw new Error(`ipc[${event}]: Invalid URL ${frame.url}`); + } - const { hostname, protocol } = new URL(frame.url); if (protocol === "file:") return; - if (!DISCORD_HOSTNAMES.includes(hostname)) throw new Error("ipc: Disallowed host " + hostname); + if (!DISCORD_HOSTNAMES.includes(hostname)) { + throw new Error(`ipc[${event}]: Disallowed hostname ${hostname}`); + } } export function handleSync(event: IpcEvents, cb: (e: IpcMainEvent, ...args: any[]) => any) { ipcMain.on(event, (e, ...args) => { - validateSender(e.senderFrame); + validateSender(e.senderFrame, event); e.returnValue = cb(e, ...args); }); } export function handle(event: IpcEvents, cb: (e: IpcMainInvokeEvent, ...args: any[]) => any) { ipcMain.handle(event, (e, ...args) => { - validateSender(e.senderFrame); + validateSender(e.senderFrame, event); return cb(e, ...args); }); }