fix: make preload calculation identical between sandbox & non-sandboxed (#34531)

This commit is contained in:
Jeremy Rose 2022-06-15 13:22:28 -07:00 committed by GitHub
parent 4ec95edf06
commit bad8d5e08a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 29 additions and 59 deletions

View file

@ -68,6 +68,10 @@ ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD, async function (event
}; };
}); });
ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD, function (event) {
return { preloadPaths: event.sender._getPreloadPaths() };
});
ipcMainInternal.on(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, function (event, preloadPath: string, error: Error) { ipcMainInternal.on(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, function (event, preloadPath: string, error: Error) {
event.sender.emit('preload-error', event, preloadPath, error); event.sender.emit('preload-error', event, preloadPath, error);
}); });

View file

@ -3,6 +3,7 @@ export const enum IPC_MESSAGES {
BROWSER_GET_LAST_WEB_PREFERENCES = 'BROWSER_GET_LAST_WEB_PREFERENCES', BROWSER_GET_LAST_WEB_PREFERENCES = 'BROWSER_GET_LAST_WEB_PREFERENCES',
BROWSER_PRELOAD_ERROR = 'BROWSER_PRELOAD_ERROR', BROWSER_PRELOAD_ERROR = 'BROWSER_PRELOAD_ERROR',
BROWSER_SANDBOX_LOAD = 'BROWSER_SANDBOX_LOAD', BROWSER_SANDBOX_LOAD = 'BROWSER_SANDBOX_LOAD',
BROWSER_NONSANDBOX_LOAD = 'BROWSER_NONSANDBOX_LOAD',
BROWSER_WINDOW_CLOSE = 'BROWSER_WINDOW_CLOSE', BROWSER_WINDOW_CLOSE = 'BROWSER_WINDOW_CLOSE',
BROWSER_GET_PROCESS_MEMORY_INFO = 'BROWSER_GET_PROCESS_MEMORY_INFO', BROWSER_GET_PROCESS_MEMORY_INFO = 'BROWSER_GET_PROCESS_MEMORY_INFO',

View file

@ -2,6 +2,7 @@ import * as path from 'path';
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages'; import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal'; import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';
const Module = require('module'); const Module = require('module');
@ -38,6 +39,7 @@ require('../common/reset-search-paths');
require('@electron/internal/common/init'); require('@electron/internal/common/init');
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule; const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;
process.getProcessMemoryInfo = () => { process.getProcessMemoryInfo = () => {
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO); return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
@ -48,15 +50,8 @@ const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_co
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame'); const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
const nodeIntegration = mainFrame.getWebPreference('nodeIntegration'); const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
const preloadScript = mainFrame.getWebPreference('preload');
const preloadScripts = mainFrame.getWebPreference('preloadScripts');
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null; const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
// The webContents preload script is loaded after the session preload scripts.
if (preloadScript) {
preloadScripts.push(preloadScript);
}
// Common renderer initialization // Common renderer initialization
require('@electron/internal/renderer/common-init'); require('@electron/internal/renderer/common-init');
@ -127,8 +122,9 @@ if (nodeIntegration) {
} }
} }
const { preloadPaths } = ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD);
// Load the preload scripts. // Load the preload scripts.
for (const preloadScript of preloadScripts) { for (const preloadScript of preloadPaths) {
try { try {
Module._load(preloadScript); Module._load(preloadScript);
} catch (error) { } catch (error) {

View file

@ -8,10 +8,10 @@ WebPreferences of in-process child windows, rather than relying on
process-level command line switches, as before. process-level command line switches, as before.
diff --git a/third_party/blink/common/web_preferences/web_preferences.cc b/third_party/blink/common/web_preferences/web_preferences.cc diff --git a/third_party/blink/common/web_preferences/web_preferences.cc b/third_party/blink/common/web_preferences/web_preferences.cc
index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..c6dc3cebfb32489a5e50b850ed02a066bf3f258e 100644 index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..26b820b3ce387e01900cec4227ff290113a833c3 100644
--- a/third_party/blink/common/web_preferences/web_preferences.cc --- a/third_party/blink/common/web_preferences/web_preferences.cc
+++ b/third_party/blink/common/web_preferences/web_preferences.cc +++ b/third_party/blink/common/web_preferences/web_preferences.cc
@@ -142,6 +142,20 @@ WebPreferences::WebPreferences() @@ -142,6 +142,19 @@ WebPreferences::WebPreferences()
fake_no_alloc_direct_call_for_testing_enabled(false), fake_no_alloc_direct_call_for_testing_enabled(false),
v8_cache_options(blink::mojom::V8CacheOptions::kDefault), v8_cache_options(blink::mojom::V8CacheOptions::kDefault),
record_whole_document(false), record_whole_document(false),
@ -20,7 +20,6 @@ index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..c6dc3cebfb32489a5e50b850ed02a066
+ is_webview(false), + is_webview(false),
+ hidden_page(false), + hidden_page(false),
+ offscreen(false), + offscreen(false),
+ preload(base::FilePath::StringType()),
+ node_integration(false), + node_integration(false),
+ node_integration_in_worker(false), + node_integration_in_worker(false),
+ node_integration_in_sub_frames(false), + node_integration_in_sub_frames(false),
@ -33,21 +32,10 @@ index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..c6dc3cebfb32489a5e50b850ed02a066
accelerated_video_decode_enabled(false), accelerated_video_decode_enabled(false),
animation_policy( animation_policy(
diff --git a/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc b/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc diff --git a/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc b/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc
index 187fd8d9818693262256d5cbddd5e02659ba903d..f166517f4e70ced310253539fb6197f6b3af559c 100644 index 187fd8d9818693262256d5cbddd5e02659ba903d..28e5178361e03d1ac851fa74214931b2332dd9c2 100644
--- a/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc --- a/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc
+++ b/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc +++ b/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc
@@ -23,6 +23,10 @@ bool StructTraits<blink::mojom::WebPreferencesDataView, @@ -148,6 +148,19 @@ bool StructTraits<blink::mojom::WebPreferencesDataView,
!data.ReadCursiveFontFamilyMap(&out->cursive_font_family_map) ||
!data.ReadFantasyFontFamilyMap(&out->fantasy_font_family_map) ||
!data.ReadMathFontFamilyMap(&out->math_font_family_map) ||
+ // Begin Electron-specific WebPreferences.
+ !data.ReadPreloads(&out->preloads) ||
+ !data.ReadPreload(&out->preload) ||
+ // End Electron-specific WebPreferences.
!data.ReadLazyFrameLoadingDistanceThresholdsPx(
&out->lazy_frame_loading_distance_thresholds_px) ||
!data.ReadLazyImageLoadingDistanceThresholdsPx(
@@ -148,6 +152,19 @@ bool StructTraits<blink::mojom::WebPreferencesDataView,
data.fake_no_alloc_direct_call_for_testing_enabled(); data.fake_no_alloc_direct_call_for_testing_enabled();
out->v8_cache_options = data.v8_cache_options(); out->v8_cache_options = data.v8_cache_options();
out->record_whole_document = data.record_whole_document(); out->record_whole_document = data.record_whole_document();
@ -68,7 +56,7 @@ index 187fd8d9818693262256d5cbddd5e02659ba903d..f166517f4e70ced310253539fb6197f6
out->accelerated_video_decode_enabled = out->accelerated_video_decode_enabled =
data.accelerated_video_decode_enabled(); data.accelerated_video_decode_enabled();
diff --git a/third_party/blink/public/common/web_preferences/web_preferences.h b/third_party/blink/public/common/web_preferences/web_preferences.h diff --git a/third_party/blink/public/common/web_preferences/web_preferences.h b/third_party/blink/public/common/web_preferences/web_preferences.h
index 59947bfd3c042e5f7d3993967fece9b519f93472..368ec7fb398409cd6386269934eaffcce356793f 100644 index 59947bfd3c042e5f7d3993967fece9b519f93472..cb2a53f6147767394585ed371744d8a140aace71 100644
--- a/third_party/blink/public/common/web_preferences/web_preferences.h --- a/third_party/blink/public/common/web_preferences/web_preferences.h
+++ b/third_party/blink/public/common/web_preferences/web_preferences.h +++ b/third_party/blink/public/common/web_preferences/web_preferences.h
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
@ -79,17 +67,15 @@ index 59947bfd3c042e5f7d3993967fece9b519f93472..368ec7fb398409cd6386269934eaffcc
#include "net/nqe/effective_connection_type.h" #include "net/nqe/effective_connection_type.h"
#include "third_party/blink/public/common/common_export.h" #include "third_party/blink/public/common/common_export.h"
#include "third_party/blink/public/mojom/css/preferred_color_scheme.mojom-shared.h" #include "third_party/blink/public/mojom/css/preferred_color_scheme.mojom-shared.h"
@@ -154,6 +155,22 @@ struct BLINK_COMMON_EXPORT WebPreferences { @@ -154,6 +155,20 @@ struct BLINK_COMMON_EXPORT WebPreferences {
blink::mojom::V8CacheOptions v8_cache_options; blink::mojom::V8CacheOptions v8_cache_options;
bool record_whole_document; bool record_whole_document;
+ // Begin Electron-specific WebPreferences. + // Begin Electron-specific WebPreferences.
+ std::vector<base::FilePath> preloads;
+ bool context_isolation; + bool context_isolation;
+ bool is_webview; + bool is_webview;
+ bool hidden_page; + bool hidden_page;
+ bool offscreen; + bool offscreen;
+ base::FilePath preload;
+ bool node_integration; + bool node_integration;
+ bool node_integration_in_worker; + bool node_integration_in_worker;
+ bool node_integration_in_sub_frames; + bool node_integration_in_sub_frames;
@ -103,7 +89,7 @@ index 59947bfd3c042e5f7d3993967fece9b519f93472..368ec7fb398409cd6386269934eaffcc
// only controls whether or not the "document.cookie" field is properly // only controls whether or not the "document.cookie" field is properly
// connected to the backing store, for instance if you wanted to be able to // connected to the backing store, for instance if you wanted to be able to
diff --git a/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h b/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h diff --git a/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h b/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h
index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb59583381 100644 index bd425a5869477de9095c6a41c683609d065b08c0..c7ba3ff52c9de01028c9e2be214e20cd91cbf309 100644
--- a/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h --- a/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h
+++ b/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h +++ b/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h
@@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
@ -114,15 +100,11 @@ index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb
#include "mojo/public/cpp/bindings/struct_traits.h" #include "mojo/public/cpp/bindings/struct_traits.h"
#include "net/nqe/effective_connection_type.h" #include "net/nqe/effective_connection_type.h"
#include "third_party/blink/public/common/common_export.h" #include "third_party/blink/public/common/common_export.h"
@@ -428,6 +429,60 @@ struct BLINK_COMMON_EXPORT StructTraits<blink::mojom::WebPreferencesDataView, @@ -428,6 +429,52 @@ struct BLINK_COMMON_EXPORT StructTraits<blink::mojom::WebPreferencesDataView,
return r.record_whole_document; return r.record_whole_document;
} }
+ // Begin Electron-specific WebPreferences. + // Begin Electron-specific WebPreferences.
+ static const std::vector<base::FilePath>& preloads(const blink::web_pref::WebPreferences& r) {
+ return r.preloads;
+ }
+
+ static bool context_isolation(const blink::web_pref::WebPreferences& r) { + static bool context_isolation(const blink::web_pref::WebPreferences& r) {
+ return r.context_isolation; + return r.context_isolation;
+ } + }
@ -139,10 +121,6 @@ index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb
+ return r.offscreen; + return r.offscreen;
+ } + }
+ +
+ static const base::FilePath& preload(const blink::web_pref::WebPreferences& r) {
+ return r.preload;
+ }
+
+ static bool node_integration(const blink::web_pref::WebPreferences& r) { + static bool node_integration(const blink::web_pref::WebPreferences& r) {
+ return r.node_integration; + return r.node_integration;
+ } + }
@ -176,7 +154,7 @@ index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb
return r.cookie_enabled; return r.cookie_enabled;
} }
diff --git a/third_party/blink/public/mojom/webpreferences/web_preferences.mojom b/third_party/blink/public/mojom/webpreferences/web_preferences.mojom diff --git a/third_party/blink/public/mojom/webpreferences/web_preferences.mojom b/third_party/blink/public/mojom/webpreferences/web_preferences.mojom
index 738ac646e075f7a37ba2a263c7799a755e63d3fb..80d6ae17d34ef5c7fd14e21c4e1ee3ed4ef9c5fe 100644 index 738ac646e075f7a37ba2a263c7799a755e63d3fb..40ab2a4b9a1f9de5f201502adebc166a48fccd35 100644
--- a/third_party/blink/public/mojom/webpreferences/web_preferences.mojom --- a/third_party/blink/public/mojom/webpreferences/web_preferences.mojom
+++ b/third_party/blink/public/mojom/webpreferences/web_preferences.mojom +++ b/third_party/blink/public/mojom/webpreferences/web_preferences.mojom
@@ -10,6 +10,7 @@ import "third_party/blink/public/mojom/v8_cache_options.mojom"; @@ -10,6 +10,7 @@ import "third_party/blink/public/mojom/v8_cache_options.mojom";
@ -187,17 +165,15 @@ index 738ac646e075f7a37ba2a263c7799a755e63d3fb..80d6ae17d34ef5c7fd14e21c4e1ee3ed
enum PointerType { enum PointerType {
kPointerNone = 1, // 1 << 0 kPointerNone = 1, // 1 << 0
@@ -206,6 +207,22 @@ struct WebPreferences { @@ -206,6 +207,20 @@ struct WebPreferences {
V8CacheOptions v8_cache_options; V8CacheOptions v8_cache_options;
bool record_whole_document; bool record_whole_document;
+ // Begin Electron-specific WebPreferences. + // Begin Electron-specific WebPreferences.
+ array<mojo_base.mojom.FilePath> preloads;
+ bool context_isolation; + bool context_isolation;
+ bool is_webview; + bool is_webview;
+ bool hidden_page; + bool hidden_page;
+ bool offscreen; + bool offscreen;
+ mojo_base.mojom.FilePath preload;
+ bool node_integration; + bool node_integration;
+ bool node_integration_in_worker; + bool node_integration_in_worker;
+ bool node_integration_in_sub_frames; + bool node_integration_in_sub_frames;

View file

@ -497,11 +497,6 @@ void ElectronBrowserClient::OverrideWebkitPrefs(
? blink::mojom::PreferredColorScheme::kDark ? blink::mojom::PreferredColorScheme::kDark
: blink::mojom::PreferredColorScheme::kLight; : blink::mojom::PreferredColorScheme::kLight;
auto preloads =
SessionPreferences::GetValidPreloads(web_contents->GetBrowserContext());
if (!preloads.empty())
prefs->preloads = preloads;
SetFontDefaults(prefs); SetFontDefaults(prefs);
// Custom preferences of guest page. // Custom preferences of guest page.

View file

@ -486,10 +486,6 @@ void WebContentsPreferences::OverrideWebkitPrefs(
prefs->offscreen = offscreen_; prefs->offscreen = offscreen_;
// The preload script.
if (preload_path_)
prefs->preload = *preload_path_;
prefs->node_integration = node_integration_; prefs->node_integration = node_integration_;
prefs->node_integration_in_worker = node_integration_in_worker_; prefs->node_integration_in_worker = node_integration_in_worker_;
prefs->node_integration_in_sub_frames = node_integration_in_sub_frames_; prefs->node_integration_in_sub_frames = node_integration_in_sub_frames_;

View file

@ -496,9 +496,7 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
const auto& prefs = render_frame->GetBlinkPreferences(); const auto& prefs = render_frame->GetBlinkPreferences();
if (pref_name == options::kPreloadScripts) { if (pref_name == "isWebView") {
return gin::ConvertToV8(isolate, prefs.preloads);
} else if (pref_name == "isWebView") {
// FIXME(zcbenz): For child windows opened with window.open('') from // FIXME(zcbenz): For child windows opened with window.open('') from
// webview, the WebPreferences is inherited from webview and the value // webview, the WebPreferences is inherited from webview and the value
// of |is_webview| is wrong. // of |is_webview| is wrong.
@ -513,8 +511,6 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
return gin::ConvertToV8(isolate, prefs.hidden_page); return gin::ConvertToV8(isolate, prefs.hidden_page);
} else if (pref_name == options::kOffscreen) { } else if (pref_name == options::kOffscreen) {
return gin::ConvertToV8(isolate, prefs.offscreen); return gin::ConvertToV8(isolate, prefs.offscreen);
} else if (pref_name == options::kPreloadScript) {
return gin::ConvertToV8(isolate, prefs.preload.value());
} else if (pref_name == options::kNodeIntegration) { } else if (pref_name == options::kNodeIntegration) {
return gin::ConvertToV8(isolate, prefs.node_integration); return gin::ConvertToV8(isolate, prefs.node_integration);
} else if (pref_name == options::kNodeIntegrationInWorker) { } else if (pref_name == options::kNodeIntegrationInWorker) {

View file

@ -3263,7 +3263,15 @@ describe('BrowserWindow module', () => {
}); });
w.webContents.setWindowOpenHandler(() => ({ w.webContents.setWindowOpenHandler(() => ({
action: 'allow', action: 'allow',
overrideBrowserWindowOptions: { show: false, webPreferences: { contextIsolation: false, webviewTag: true, nodeIntegrationInSubFrames: true } } overrideBrowserWindowOptions: {
show: false,
webPreferences: {
contextIsolation: false,
webviewTag: true,
nodeIntegrationInSubFrames: true,
preload
}
}
})); }));
const webviewLoaded = emittedOnce(ipcMain, 'webview-loaded'); const webviewLoaded = emittedOnce(ipcMain, 'webview-loaded');

View file

@ -107,8 +107,6 @@ declare namespace NodeJS {
isWebView: boolean; isWebView: boolean;
hiddenPage: boolean; hiddenPage: boolean;
nodeIntegration: boolean; nodeIntegration: boolean;
preload: string
preloadScripts: string[];
webviewTag: boolean; webviewTag: boolean;
} }