From 5e72cd34f0bc226261912b2cb596fc558e3bff46 Mon Sep 17 00:00:00 2001 From: benzntech Date: Sat, 28 Feb 2026 11:23:27 +0530 Subject: [PATCH] fix: improve Electron security and functionality - Add window control IPC handlers (minimize, maximize, close) - Add URL validation for open-external to prevent security issues - Add single instance lock to prevent multiple app instances - Add deep link protocol support (omniroute://) Refs: #149 --- electron/main.js | 43 ++++++++++++++++++++++++++++++++++++++++++- electron/package.json | 6 ++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/electron/main.js b/electron/main.js index 4d5dddf12a..7edd51c0dc 100644 --- a/electron/main.js +++ b/electron/main.js @@ -10,6 +10,21 @@ const path = require('path'); const { spawn } = require('child_process'); const fs = require('fs'); +// Single instance lock - prevent multiple instances +const gotTheLock = app.requestSingleInstanceLock(); +if (!gotTheLock) { + app.quit(); + process.exit(0); +} + +app.on('second-instance', () => { + if (mainWindow) { + if (mainWindow.isMinimized()) mainWindow.restore(); + mainWindow.show(); + mainWindow.focus(); + } +}); + // Environment detection const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged; const isProduction = !isDev; @@ -224,7 +239,14 @@ function setupIpcHandlers() { // Open external URL ipcMain.handle('open-external', (event, url) => { - shell.openExternal(url); + try { + const parsedUrl = new URL(url); + if (['http:', 'https:'].includes(parsedUrl.protocol)) { + shell.openExternal(url); + } + } catch { + console.error('Invalid URL:', url); + } }); // Get data directory @@ -240,6 +262,25 @@ function setupIpcHandlers() { startNextServer(); return { success: true }; }); + + // Window controls + ipcMain.on('window-minimize', () => { + mainWindow?.minimize(); + }); + + ipcMain.on('window-maximize', () => { + if (mainWindow) { + if (mainWindow.isMaximized()) { + mainWindow.unmaximize(); + } else { + mainWindow.maximize(); + } + } + }); + + ipcMain.on('window-close', () => { + mainWindow?.close(); + }); } // App lifecycle events diff --git a/electron/package.json b/electron/package.json index 6d698b7ca4..eefc5dd577 100644 --- a/electron/package.json +++ b/electron/package.json @@ -24,6 +24,12 @@ "appId": "online.omniroute.desktop", "productName": "OmniRoute", "copyright": "Copyright © 2025 OmniRoute", + "protocols": [ + { + "name": "OmniRoute", + "schemes": ["omniroute"] + } + ], "directories": { "output": "dist-electron", "buildResources": "assets"