Skip to content

Commit 16a3f41

Browse files
chore: add deprecation warning for the default of contextIsolation (electron#23507)
* chore: add deprecation warning for the default of contextIsolation * chore: add to breaking changes * Update docs/breaking-changes.md Co-authored-by: Jeremy Apthorp <jeremya@chromium.org> * chore: fix specs on windows Co-authored-by: Jeremy Apthorp <jeremya@chromium.org>
1 parent 605e502 commit 16a3f41

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

docs/breaking-changes.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ This document uses the following convention to categorize breaking changes:
1414

1515
## Planned Breaking API Changes (12.0)
1616

17+
### Default Changed: `contextIsolation` defaults to `true`
18+
19+
In Electron 12, `contextIsolation` will be enabled by default. To restore
20+
the previous behavior, `contextIsolation: false` must be specified in WebPreferences.
21+
22+
We [recommend having contextIsolation enabled](https://github.com/electron/electron/blob/master/docs/tutorial/security.md#3-enable-context-isolation-for-remote-content) for the security of your application.
23+
24+
For more details see: https://github.com/electron/electron/issues/23506
25+
1726
### Removed: `crashReporter` methods in the renderer process
1827

1928
The following `crashReporter` methods are no longer available in the renderer

shell/browser/web_contents_preferences.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "shell/common/gin_converters/value_converter.h"
2727
#include "shell/common/gin_helper/dictionary.h"
2828
#include "shell/common/options_switches.h"
29+
#include "shell/common/process_util.h"
2930
#include "third_party/blink/public/mojom/v8_cache_options.mojom.h"
3031

3132
#if defined(OS_WIN)
@@ -126,6 +127,15 @@ WebContentsPreferences::WebContentsPreferences(
126127
SetDefaultBoolIfUndefined(options::kWebviewTag, false);
127128
SetDefaultBoolIfUndefined(options::kSandbox, false);
128129
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
130+
if (IsUndefined(options::kContextIsolation)) {
131+
node::Environment* env = node::Environment::GetCurrent(isolate);
132+
EmitWarning(env,
133+
"The default of contextIsolation is deprecated and will be "
134+
"changing from false to true in a future release of Electron. "
135+
"See https://github.com/electron/electron/issues/23506 for "
136+
"more information",
137+
"electron");
138+
}
129139
SetDefaultBoolIfUndefined(options::kContextIsolation, false);
130140
SetDefaultBoolIfUndefined(options::kJavaScript, true);
131141
SetDefaultBoolIfUndefined(options::kImages, true);
@@ -183,6 +193,10 @@ void WebContentsPreferences::SetDefaults() {
183193
last_preference_ = preference_.Clone();
184194
}
185195

196+
bool WebContentsPreferences::IsUndefined(base::StringPiece key) {
197+
return !preference_.FindKeyOfType(key, base::Value::Type::BOOLEAN);
198+
}
199+
186200
bool WebContentsPreferences::SetDefaultBoolIfUndefined(base::StringPiece key,
187201
bool val) {
188202
auto* current_value =

shell/browser/web_contents_preferences.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class WebContentsPreferences
7272
// Get WebContents according to process ID.
7373
static content::WebContents* GetWebContentsFromProcessID(int process_id);
7474

75+
// Checks if the key is not defined
76+
bool IsUndefined(base::StringPiece key);
77+
7578
// Set preference value to given bool if user did not provide value
7679
bool SetDefaultBoolIfUndefined(base::StringPiece key, bool val);
7780

spec/fixtures/api/gpu-info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ app.commandLine.appendSwitch('--disable-software-rasterizer');
44

55
app.whenReady().then(() => {
66
const infoType = process.argv.pop();
7-
const w = new BrowserWindow({ show: false });
7+
const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } });
88
w.webContents.once('did-finish-load', () => {
99
app.getGPUInfo(infoType).then(
1010
(gpuInfo) => {

spec/fixtures/api/site-instance-overrides/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ app.whenReady().then(() => {
2828
win = new BrowserWindow({
2929
show: false,
3030
webPreferences: {
31-
preload: path.resolve(__dirname, 'preload.js')
31+
preload: path.resolve(__dirname, 'preload.js'),
32+
contextIsolation: true
3233
}
3334
});
3435
win.loadFile('index.html');

spec/fixtures/api/window-all-closed/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ app.on('quit', () => {
1515
});
1616

1717
app.whenReady().then(() => {
18-
const win = new BrowserWindow();
18+
const win = new BrowserWindow({
19+
webPreferences: {
20+
contextIsolation: true
21+
}
22+
});
1923
win.close();
2024
});

0 commit comments

Comments
 (0)