Restrict desktop external URL schemes (#845)
This commit is contained in:
@@ -1,7 +1,25 @@
|
||||
import { shell, ipcMain } from "electron";
|
||||
|
||||
const ALLOWED_EXTERNAL_URL_PROTOCOLS = new Set(["http:", "https:"]);
|
||||
|
||||
export function isAllowedExternalUrl(value: unknown): value is string {
|
||||
if (typeof value !== "string") {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(value);
|
||||
return ALLOWED_EXTERNAL_URL_PROTOCOLS.has(url.protocol);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerOpenerHandlers(): void {
|
||||
ipcMain.handle("paseo:opener:openUrl", async (_event, url: string) => {
|
||||
ipcMain.handle("paseo:opener:openUrl", async (_event, url: unknown) => {
|
||||
if (!isAllowedExternalUrl(url)) {
|
||||
throw new Error("Unsupported external URL");
|
||||
}
|
||||
await shell.openExternal(url);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user