Skip to content

Commit

Permalink
🔒️ disable sandbox and nodeIntegration (preload.js)
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Sep 6, 2022
1 parent 21793e3 commit 20d9b7f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/chrome-tabs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/
const {BrowserView, Menu, MenuItem} = require('electron');
const path = require('path');
const {openHelpDialog} = require('../help');
const {openSettingsDialog} = require('../settings');

Expand All @@ -22,8 +23,9 @@ const TABS_CONTAINER_HEIGHT = 46;
const webPreferences = {
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: true,
preload: `${__dirname}/preload.js`,
nodeIntegration: false,
sandbox: false,
preload: path.resolve(__dirname, 'preload.js'),
partition: 'persist:electronim'
};

Expand Down
10 changes: 8 additions & 2 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
const fs = require('fs');
const path = require('path');

const ROOT_DIR = path.resolve(__dirname, '../../');
const findRootDir = () => {
let rootDir = __dirname;
while (!fs.existsSync(path.join(rootDir, 'package.json'))) {
rootDir = path.join(rootDir, '..');
}
return rootDir;
};

const APP_EVENTS = {
activateTab: 'activateTab',
Expand All @@ -39,7 +45,7 @@ const APP_EVENTS = {
zoomReset: 'zoomReset'
};

const ELECTRONIM_VERSION = JSON.parse(fs.readFileSync(path.resolve(ROOT_DIR, 'package.json'), 'utf8')).version;
const ELECTRONIM_VERSION = JSON.parse(fs.readFileSync(path.resolve(findRootDir(), 'package.json'), 'utf8')).version;

module.exports = {
APP_EVENTS, ELECTRONIM_VERSION
Expand Down
5 changes: 3 additions & 2 deletions src/help/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const DOCS_DIR = path.resolve(__dirname, '../../docs');
const webPreferences = {
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: true,
preload: `${__dirname}/preload.js`
nodeIntegration: false,
sandbox: false,
preload: path.resolve(__dirname, 'preload.js')
};

// Visible for testing
Expand Down
6 changes: 3 additions & 3 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/
const {BrowserWindow, Notification, app, desktopCapturer, ipcMain: ipc} = require('electron');
const path = require('path');
const {APP_EVENTS} = require('../constants');
const {TABS_CONTAINER_HEIGHT, initTabContainer} = require('../chrome-tabs');
const {loadSettings, updateSettings, openSettingsDialog} = require('../settings');
Expand All @@ -24,8 +25,8 @@ const {initBrowserVersions, userAgentForView} = require('../user-agent');
const webPreferences = {
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: true,
preload: `${__dirname}/preload.js`,
sandbox: false,
preload: path.resolve(__dirname, 'preload.js'),
partition: 'persist:electronim'
};

Expand Down Expand Up @@ -191,7 +192,6 @@ const init = () => {
}).show();
browserVersionsReady();
});

return mainWindow;
};

Expand Down
5 changes: 3 additions & 2 deletions src/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const DEFAULT_SETTINGS = {tabs: [], enabledDictionaries: ['en-US']};
const webPreferences = {
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: true,
preload: `${__dirname}/preload.js`,
nodeIntegration: false,
sandbox: false,
preload: path.resolve(__dirname, 'preload.js'),
partition: 'persist:electronim'
};

Expand Down
2 changes: 1 addition & 1 deletion src/tab-manager/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Tab Manager module test suite', () => {
mockBrowserView = {
setAutoResize: jest.fn(),
webContents: {
executeJavaScript: jest.fn(),
executeJavaScript: jest.fn(async () => {}),
on: jest.fn(),
loadURL: jest.fn(),
userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/WillBeReplacedByLatestChromium Electron/0.0.99 Safari/537.36',
Expand Down
8 changes: 5 additions & 3 deletions src/tab-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/
const {app, BrowserView, Menu, MenuItem, session} = require('electron');
const path = require('path');
const {APP_EVENTS} = require('../constants');
const settings = require('../settings');
const {contextMenuHandler} = require('../spell-check');
Expand All @@ -26,8 +27,9 @@ const tabs = {};
const webPreferences = {
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: true,
preload: `${__dirname}/preload.js`
nodeIntegration: false,
sandbox: false,
preload: path.resolve(__dirname, 'preload.js')
};

const handlePageTitleUpdated = (ipcSender, tabId) => (_e, title) => {
Expand Down Expand Up @@ -104,7 +106,7 @@ const addTabs = ipcSender => tabsMetadata => {

const registerIdInTab = () => tab.webContents.executeJavaScript(`window.tabId = '${id}';`);
tab.webContents.on('dom-ready', registerIdInTab);
registerIdInTab();
registerIdInTab().then();

tabs[id.toString()] = tab;
});
Expand Down
2 changes: 1 addition & 1 deletion src/tab-manager/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
require('../main/preload');
const {webFrame} = require('electron');
require('../main/preload');
require('./browser-notification-shim');
require('./browser-mediadevices-shim');
const {initKeyboardShortcuts} = require('./browser-keyboard-shortcuts');
Expand Down

0 comments on commit 20d9b7f

Please sign in to comment.