Skip to content
21 changes: 10 additions & 11 deletions src/main/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the terms of the Modified BSD License.

import { BrowserWindow, Cookie } from 'electron';
import { clearSession } from './utils';
import { clearSession, hardenedWebPreferences } from './utils';

export let connectWindow: BrowserWindow;

Expand Down Expand Up @@ -39,21 +39,20 @@ export async function connectAndGetServerInfo(
return;
}

const browserOptions: Electron.BrowserWindowConstructorOptions = {
title: 'JupyterLab Server Connection',
show: options?.showDialog === true
};
let partition: string | undefined;
if (options?.incognito) {
browserOptions.webPreferences = {
partition: `partition-${Date.now()}`
};
partition = `partition-${Date.now()}`;
}
if (options?.partition) {
browserOptions.webPreferences = {
partition: options.partition
};
partition = options.partition;
}

const browserOptions: Electron.BrowserWindowConstructorOptions = {
title: 'JupyterLab Server Connection',
show: options?.showDialog === true,
webPreferences: hardenedWebPreferences({ partition })
};

const window = new BrowserWindow(browserOptions);

const timeout = options?.timeout || 30000;
Expand Down
10 changes: 7 additions & 3 deletions src/main/dialog/themedview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import { BrowserView } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
import { DarkThemeBGColor, LightThemeBGColor } from '../utils';
import {
DarkThemeBGColor,
hardenedWebPreferences,
LightThemeBGColor
} from '../utils';

export class ThemedView {
constructor(options: ThemedView.IOptions) {
this._isDarkTheme = options.isDarkTheme;
this._view = new BrowserView({
webPreferences: {
webPreferences: hardenedWebPreferences({
preload: options.preload || path.join(__dirname, './preload.js')
}
})
});
this._view.setBackgroundColor(
this._isDarkTheme ? DarkThemeBGColor : LightThemeBGColor
Expand Down
10 changes: 7 additions & 3 deletions src/main/dialog/themedwindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import { BrowserWindow } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
import { DarkThemeBGColor, LightThemeBGColor } from '../utils';
import {
DarkThemeBGColor,
hardenedWebPreferences,
LightThemeBGColor
} from '../utils';

export class ThemedWindow {
constructor(options: ThemedWindow.IOptions) {
Expand All @@ -22,9 +26,9 @@ export class ThemedWindow {
titleBarStyle: 'hidden',
frame: process.platform === 'darwin',
backgroundColor: this._isDarkTheme ? DarkThemeBGColor : LightThemeBGColor,
webPreferences: {
webPreferences: hardenedWebPreferences({
preload: options.preload || path.join(__dirname, './preload.js')
}
})
});

// hide the traffic lights
Expand Down
5 changes: 3 additions & 2 deletions src/main/labview/labview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as fs from 'fs';
import {
clearSession,
DarkThemeBGColor,
hardenedWebPreferences,
isDarkTheme,
LightThemeBGColor
} from '../utils';
Expand Down Expand Up @@ -57,10 +58,10 @@ export class LabView implements IDisposable {
}
}
this._view = new BrowserView({
webPreferences: {
webPreferences: hardenedWebPreferences({
preload: path.join(__dirname, './preload.js'),
partition
}
})
});

this._view.setBackgroundColor(
Expand Down
5 changes: 3 additions & 2 deletions src/main/sessionwindow/sessionwindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
envPathForPythonPath,
getBundledPythonPath,
getLogFilePath,
hardenedWebPreferences,
isDarkTheme,
LightThemeBGColor
} from '../utils';
Expand Down Expand Up @@ -129,9 +130,9 @@ export class SessionWindow implements IDisposable {
titleBarStyle: 'hidden',
frame: process.platform === 'darwin',
backgroundColor: this._isDarkTheme ? DarkThemeBGColor : LightThemeBGColor,
webPreferences: {
webPreferences: hardenedWebPreferences({
devTools: false
}
})
});

this._window.setMenuBarVisibility(false);
Expand Down
10 changes: 7 additions & 3 deletions src/main/titlebarview/titlebarview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import { BrowserView } from 'electron';
import * as path from 'path';
import * as fs from 'fs';
import * as ejs from 'ejs';
import { DarkThemeBGColor, LightThemeBGColor } from '../utils';
import {
DarkThemeBGColor,
hardenedWebPreferences,
LightThemeBGColor
} from '../utils';
import { EventTypeRenderer } from '../eventtypes';

export class TitleBarView {
constructor(options: TitleBarView.IOptions) {
this._isDarkTheme = options.isDarkTheme;
this._view = new BrowserView({
webPreferences: {
webPreferences: hardenedWebPreferences({
preload: path.join(__dirname, './preload.js'),
devTools: process.env.NODE_ENV === 'development'
}
})
});

this._view.setBackgroundColor(
Expand Down
11 changes: 11 additions & 0 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export function isDevMode(): boolean {
return require.main.filename.indexOf('app.asar') === -1;
}

export function hardenedWebPreferences(
extra?: Electron.WebPreferences
): Electron.WebPreferences {
return {
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
...extra
};
}

export function getAppDir(): string {
let appDir = app.getAppPath();
if (!isDevMode()) {
Expand Down
11 changes: 8 additions & 3 deletions src/main/welcomeview/welcomeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// Distributed under the terms of the Modified BSD License.

import { BrowserView, net } from 'electron';
import { DarkThemeBGColor, getUserHomeDir, LightThemeBGColor } from '../utils';
import {
DarkThemeBGColor,
getUserHomeDir,
hardenedWebPreferences,
LightThemeBGColor
} from '../utils';
import * as path from 'path';
import * as fs from 'fs';
import { XMLParser } from 'fast-xml-parser';
Expand All @@ -25,10 +30,10 @@ export class WelcomeView {
this._registry = options.registry;
this._isDarkTheme = options.isDarkTheme;
this._view = new BrowserView({
webPreferences: {
webPreferences: hardenedWebPreferences({
preload: path.join(__dirname, './preload.js'),
devTools: process.env.NODE_ENV === 'development'
}
})
});

this._view.setBackgroundColor(
Expand Down
Loading