diff --git a/src/main/ipc.ts b/src/main/ipc.ts index eb6c11a..03eec0f 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -35,6 +35,9 @@ import { isDeckGameMode, showGamePage } from "./utils/steamOS"; import { isValidVencordInstall } from "./utils/vencordLoader"; import { VENCORD_FILES_DIR } from "./vencordFilesDir"; +handleSync(IpcEvents.DEPRECATED_GET_VENCORD_PRELOAD_SCRIPT_PATH, () => + join(VENCORD_FILES_DIR, "vencordDesktopPreload.js") +); handleSync(IpcEvents.GET_VENCORD_PRELOAD_SCRIPT, () => readFileSync(join(VENCORD_FILES_DIR, "vencordDesktopPreload.js"), "utf-8") ); @@ -49,7 +52,7 @@ handle(IpcEvents.GET_VESKTOP_RENDERER_CSS, () => readFile(VESKTOP_RENDERER_CSS_P if (IS_DEV) { watch(VESKTOP_RENDERER_CSS_PATH, { persistent: false }, async () => { - mainWin.webContents.postMessage( + mainWin?.webContents.postMessage( IpcEvents.VESKTOP_RENDERER_CSS_UPDATE, await readFile(VESKTOP_RENDERER_CSS_PATH, "utf-8") ); diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index 6f9e05f..cb2c707 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -34,7 +34,7 @@ import { destroyTray, initTray } from "./tray"; import { clearData } from "./utils/clearData"; import { makeLinksOpenExternally } from "./utils/makeLinksOpenExternally"; import { applyDeckKeyboardFix, askToApplySteamLayout, isDeckGameMode } from "./utils/steamOS"; -import { downloadVencordFiles, ensureVencordFiles } from "./utils/vencordLoader"; +import { downloadVencordFiles, ensureVencordFiles, vencordSupportsSandboxing } from "./utils/vencordLoader"; import { VENCORD_FILES_DIR } from "./vencordFilesDir"; let isQuitting = false; @@ -323,7 +323,7 @@ function buildBrowserWindowOptions(): BrowserWindowConstructorOptions { backgroundColor, webPreferences: { nodeIntegration: false, - sandbox: true, + sandbox: vencordSupportsSandboxing(), contextIsolation: true, devTools: true, preload: join(__dirname, "preload.js"), diff --git a/src/main/utils/vencordLoader.ts b/src/main/utils/vencordLoader.ts index 25748b3..96ab1ff 100644 --- a/src/main/utils/vencordLoader.ts +++ b/src/main/utils/vencordLoader.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -import { mkdirSync } from "fs"; +import { mkdirSync, readFileSync } from "fs"; import { access, constants as FsConstants, writeFile } from "fs/promises"; import { VENCORD_FILES_DIR } from "main/vencordFilesDir"; import { join } from "path"; @@ -75,3 +75,16 @@ export async function ensureVencordFiles() { await Promise.all([downloadVencordFiles(), writeFile(join(VENCORD_FILES_DIR, "package.json"), "{}")]); } + +// TODO: remove this once enough time has passed +export function vencordSupportsSandboxing() { + const supports = readFileSync(join(VENCORD_FILES_DIR, "vencordDesktopMain.js"), "utf-8").includes( + "VencordGetRendererCss" + ); + if (!supports) { + console.warn( + "⚠️ [VencordLoader] Vencord version is outdated and does not support sandboxing. Please update Vencord to the latest version." + ); + } + return supports; +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 6ed74f9..e91ffbc 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -11,16 +11,22 @@ import { VesktopNative } from "./VesktopNative"; contextBridge.exposeInMainWorld("VesktopNative", VesktopNative); -// While sandboxed, Electron "polyfills" these APIs as local variables. -// We have to pass them as arguments as they are not global -Function( - "require", - "Buffer", - "process", - "clearImmediate", - "setImmediate", - ipcRenderer.sendSync(IpcEvents.GET_VENCORD_PRELOAD_SCRIPT) -)(require, Buffer, process, clearImmediate, setImmediate); +// TODO: remove this legacy workaround once some time has passed +const isSandboxed = typeof __dirname === "undefined"; +if (isSandboxed) { + // While sandboxed, Electron "polyfills" these APIs as local variables. + // We have to pass them as arguments as they are not global + Function( + "require", + "Buffer", + "process", + "clearImmediate", + "setImmediate", + ipcRenderer.sendSync(IpcEvents.GET_VENCORD_PRELOAD_SCRIPT) + )(require, Buffer, process, clearImmediate, setImmediate); +} else { + require(ipcRenderer.sendSync(IpcEvents.DEPRECATED_GET_VENCORD_PRELOAD_SCRIPT_PATH)); +} webFrame.executeJavaScript(ipcRenderer.sendSync(IpcEvents.GET_VENCORD_RENDERER_SCRIPT)); webFrame.executeJavaScript(ipcRenderer.sendSync(IpcEvents.GET_VESKTOP_RENDERER_SCRIPT)); diff --git a/src/shared/IpcEvents.ts b/src/shared/IpcEvents.ts index 40505c8..3bc6857 100644 --- a/src/shared/IpcEvents.ts +++ b/src/shared/IpcEvents.ts @@ -6,6 +6,7 @@ export const enum IpcEvents { GET_VENCORD_PRELOAD_SCRIPT = "VCD_GET_VC_PRELOAD_SCRIPT", + DEPRECATED_GET_VENCORD_PRELOAD_SCRIPT_PATH = "DEPRECATED_GET_VENCORD_PRELOAD_SCRIPT_PATH", GET_VENCORD_RENDERER_SCRIPT = "VCD_GET_VC_RENDERER_SCRIPT", GET_VESKTOP_RENDERER_SCRIPT = "VCD_GET_RENDERER_SCRIPT",