Skip to content

Commit

Permalink
feat: enable context isolation by default (#27949)
Browse files Browse the repository at this point in the history
* feat: enable context isolation by default

* chore: set default in ctx iso getter

* spec: make all specs work with the new contextIsolation default

* spec: fix affinity specs

* spec: update tests for new ctx iso default

* spec: update tests for new ctx iso default

* spec: update tests for new ctx iso default

* spec: update tests for new ctx iso default

* chore: move stray prod deps to dev deps

* spec: update tests for new ctx iso default

* turn off contextIsolation for visibility tests

* turn off contextIsolation for <webview> tag nodeintegration attribute loads native modules when navigation happens

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
Co-authored-by: Samuel Attard <sattard@slack-corp.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
  • Loading branch information
4 people committed Mar 2, 2021
1 parent 0e714d1 commit 8828382
Show file tree
Hide file tree
Showing 37 changed files with 291 additions and 194 deletions.
13 changes: 2 additions & 11 deletions shell/browser/web_contents_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,7 @@ WebContentsPreferences::WebContentsPreferences(
SetDefaultBoolIfUndefined(options::kWebviewTag, false);
SetDefaultBoolIfUndefined(options::kSandbox, false);
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
if (IsUndefined(options::kContextIsolation)) {
node::Environment* env = node::Environment::GetCurrent(isolate);
EmitWarning(env,
"The default of contextIsolation is deprecated and will be "
"changing from false to true in a future release of Electron. "
"See https://github.com/electron/electron/issues/23506 for "
"more information",
"electron");
}
SetDefaultBoolIfUndefined(options::kContextIsolation, false);
SetDefaultBoolIfUndefined(options::kContextIsolation, true);
SetDefaultBoolIfUndefined(options::kWorldSafeExecuteJavaScript, true);
SetDefaultBoolIfUndefined(options::kJavaScript, true);
SetDefaultBoolIfUndefined(options::kImages, true);
Expand Down Expand Up @@ -429,7 +420,7 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->opener_id = opener_id;

// Run Electron APIs and preload script in isolated world
prefs->context_isolation = IsEnabled(options::kContextIsolation);
prefs->context_isolation = IsEnabled(options::kContextIsolation, true);

#if BUILDFLAG(ENABLE_REMOTE_MODULE)
// Whether to enable the remote module
Expand Down
24 changes: 16 additions & 8 deletions spec-main/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ describe('app module', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -425,7 +426,8 @@ describe('app module', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -443,7 +445,8 @@ describe('app module', () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -462,7 +465,8 @@ describe('app module', () => {
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
enableRemoteModule: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -480,7 +484,8 @@ describe('app module', () => {
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
enableRemoteModule: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -498,7 +503,8 @@ describe('app module', () => {
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
enableRemoteModule: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -516,7 +522,8 @@ describe('app module', () => {
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
enableRemoteModule: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand All @@ -533,7 +540,8 @@ describe('app module', () => {
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
enableRemoteModule: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
Expand Down
41 changes: 22 additions & 19 deletions spec-main/api-browser-window-affinity-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';

import { ipcMain, BrowserWindow, WebPreferences, app } from 'electron/main';
import { closeWindow } from './window-helpers';
import { emittedOnce } from './events-helpers';

describe('BrowserWindow with affinity module', () => {
const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
Expand Down Expand Up @@ -88,19 +89,15 @@ describe('BrowserWindow with affinity module', () => {
const affinityWithNodeTrue = 'affinityWithNodeTrue';
const affinityWithNodeFalse = 'affinityWithNodeFalse';

function testNodeIntegration (present: boolean) {
return new Promise<void>((resolve) => {
ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => {
if (present) {
expect(typeofProcess).to.not.equal('undefined');
expect(typeofBuffer).to.not.equal('undefined');
} else {
expect(typeofProcess).to.equal('undefined');
expect(typeofBuffer).to.equal('undefined');
}
resolve();
});
});
async function testNodeIntegration (present: boolean) {
const [, typeofProcess, typeofBuffer] = await emittedOnce(ipcMain, 'answer');
if (present) {
expect(typeofProcess).to.not.equal('undefined');
expect(typeofBuffer).to.not.equal('undefined');
} else {
expect(typeofProcess).to.equal('undefined');
expect(typeofBuffer).to.equal('undefined');
}
}

it('disables node integration when specified to false', async () => {
Expand All @@ -109,7 +106,8 @@ describe('BrowserWindow with affinity module', () => {
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: false
nodeIntegration: false,
contextIsolation: false
})
]);
await closeWindow(w, { assertNotWindows: false });
Expand All @@ -120,15 +118,17 @@ describe('BrowserWindow with affinity module', () => {
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: false
nodeIntegration: false,
contextIsolation: false
})
]);
const [, w2] = await Promise.all([
testNodeIntegration(true),
createWindowWithWebPrefs({
affinity: affinityWithNodeTrue,
preload,
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
})
]);
await Promise.all([
Expand All @@ -143,7 +143,8 @@ describe('BrowserWindow with affinity module', () => {
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
})
]);
await closeWindow(w, { assertNotWindows: false });
Expand All @@ -155,15 +156,17 @@ describe('BrowserWindow with affinity module', () => {
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: true
nodeIntegration: true,
contextIsolation: false
})
]);
const [, w2] = await Promise.all([
testNodeIntegration(false),
createWindowWithWebPrefs({
affinity: affinityWithNodeFalse,
preload,
nodeIntegration: false
nodeIntegration: false,
contextIsolation: false
})
]);
await Promise.all([
Expand Down

0 comments on commit 8828382

Please sign in to comment.