From 1f5f597561590fe1bcce15b9b8c58f603b4fe166 Mon Sep 17 00:00:00 2001 From: TC Date: Mon, 5 Sep 2022 00:31:29 +0200 Subject: [PATCH] Prepare migration for sandboxing (path.join in preload) --- index.js | 10 +++++----- package.json | 4 ++-- preload.js | 29 +++++++++++++++++++++++------ providers/app-controls.js | 3 +++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 73cf0808..81249b3a 100644 --- a/index.js +++ b/index.js @@ -136,12 +136,12 @@ function createMainWindow() { preload: path.join(__dirname, "preload.js"), nodeIntegrationInSubFrames: true, affinity: "main-window", // main window, and addition windows should work in one process - ...(isTesting() + ...(!isTesting() ? { - // Only necessary when testing with Spectron - contextIsolation: false, - nodeIntegration: true, - } + // Sandbox is only enabled in tests for now + // See https://www.electronjs.org/docs/latest/tutorial/sandbox#preload-scripts + sandbox: false, + } : undefined), }, frame: !is.macOS() && !useInlineMenu, diff --git a/package.json b/package.json index 6496a321..c13c3a0b 100644 --- a/package.json +++ b/package.json @@ -67,8 +67,8 @@ } }, "scripts": { - "test": "playwright test", - "test:debug": "DEBUG=pw:browser* playwright test", + "test": "NODE_ENV=test playwright test", + "test:debug": "DEBUG=pw:browser* NODE_ENV=test playwright test", "start": "electron .", "start:debug": "ELECTRON_ENABLE_LOGGING=1 electron .", "icon": "rimraf assets/generated && electron-icon-maker --input=assets/youtube-music.png --output=assets/generated", diff --git a/preload.js b/preload.js index f508a6ae..2b9122f1 100644 --- a/preload.js +++ b/preload.js @@ -1,4 +1,3 @@ -const path = require("path"); require("./providers/front-logger")(); const config = require("./config"); const { fileExists } = require("./plugins/utils"); @@ -10,14 +9,26 @@ const plugins = config.plugins.getEnabled(); let api; -plugins.forEach(([plugin, options]) => { - const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js"); +plugins.forEach(async ([plugin, options]) => { + const preloadPath = await ipcRenderer.invoke( + "getPath", + __dirname, + "plugins", + plugin, + "preload.js" + ); fileExists(preloadPath, () => { const run = require(preloadPath); run(options); }); - const actionPath = path.join(__dirname, "plugins", plugin, "actions.js"); + const actionPath = await ipcRenderer.invoke( + "getPath", + __dirname, + "plugins", + plugin, + "actions.js" + ); fileExists(actionPath, () => { const actions = require(actionPath).actions || {}; @@ -30,8 +41,14 @@ plugins.forEach(([plugin, options]) => { }); document.addEventListener("DOMContentLoaded", () => { - plugins.forEach(([plugin, options]) => { - const pluginPath = path.join(__dirname, "plugins", plugin, "front.js"); + plugins.forEach(async ([plugin, options]) => { + const pluginPath = await ipcRenderer.invoke( + "getPath", + __dirname, + "plugins", + plugin, + "front.js" + ); fileExists(pluginPath, () => { const run = require(pluginPath); run(options); diff --git a/providers/app-controls.js b/providers/app-controls.js index d51b6901..3567e22a 100644 --- a/providers/app-controls.js +++ b/providers/app-controls.js @@ -1,3 +1,5 @@ +const path = require("path"); + const is = require("electron-is"); const { app, BrowserWindow, ipcMain, ipcRenderer } = require("electron"); @@ -11,6 +13,7 @@ module.exports.setupAppControls = () => { ipcMain.on('restart', restart); ipcMain.handle('getDownloadsFolder', () => app.getPath("downloads")); ipcMain.on('reload', () => BrowserWindow.getFocusedWindow().webContents.loadURL(config.get("url"))); + ipcMain.handle('getPath', (_, ...args) => path.join(...args)); } function restart() {