electron/lib/sandboxed_renderer/init.js

203 lines
6.8 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
'use strict';
/* eslint no-eval: "off" */
/* global binding, Buffer */
2020-03-20 20:28:31 +00:00
const events = require('events');
const { EventEmitter } = events;
2020-03-20 20:28:31 +00:00
process.electronBinding = require('@electron/internal/common/electron-binding-setup').electronBindingSetup(binding.get, 'renderer');
2020-03-20 20:28:31 +00:00
const v8Util = process.electronBinding('v8_util');
2019-06-02 20:03:03 +00:00
// Expose Buffer shim as a hidden value. This is used by C++ code to
// deserialize Buffer instances sent from browser process.
2020-03-20 20:28:31 +00:00
v8Util.setHiddenValue(global, 'Buffer', Buffer);
// The `lib/renderer/api/ipc-renderer.ts` module looks for the ipc object in the
// "ipc" hidden value
2020-03-20 20:28:31 +00:00
v8Util.setHiddenValue(global, 'ipc', new EventEmitter());
// The `lib/renderer/ipc-renderer-internal.ts` module looks for the ipc object in the
// "ipc-internal" hidden value
2020-03-20 20:28:31 +00:00
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter());
2019-06-02 20:03:03 +00:00
// The process object created by webpack is not an event emitter, fix it so
// the API is more compatible with non-sandboxed renderers.
for (const prop of Object.keys(EventEmitter.prototype)) {
if (Object.prototype.hasOwnProperty.call(process, prop)) {
2020-03-20 20:28:31 +00:00
delete process[prop];
}
}
2020-03-20 20:28:31 +00:00
Object.setPrototypeOf(process, EventEmitter.prototype);
2020-03-20 20:28:31 +00:00
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils');
const {
contentScripts,
preloadScripts,
isRemoteModuleEnabled,
isWebViewTagEnabled,
guestInstanceId,
openerId,
process: processProps
2020-03-20 20:28:31 +00:00
} = ipcRendererUtils.invokeSync('ELECTRON_BROWSER_SANDBOX_LOAD');
2020-03-20 20:28:31 +00:00
process.isRemoteModuleEnabled = isRemoteModuleEnabled;
// The electron module depends on process.electronBinding
2020-03-20 20:28:31 +00:00
const electron = require('electron');
const loadedModules = new Map([
['electron', electron],
['events', events],
['timers', require('timers')],
['url', require('url')]
2020-03-20 20:28:31 +00:00
]);
refactor: use mojo for electron internal IPC (#17406) * refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
2019-04-02 22:38:16 +00:00
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
// invoking the 'onMessage' callback.
v8Util.setHiddenValue(global, 'ipcNative', {
onMessage (internal, channel, ports, args, senderId) {
2020-03-20 20:28:31 +00:00
const sender = internal ? ipcRendererInternal : electron.ipcRenderer;
sender.emit(channel, { sender, senderId, ports }, ...args);
}
2020-03-20 20:28:31 +00:00
});
// ElectronSandboxedRendererClient will look for the "lifecycle" hidden object when
refactor: use mojo for electron internal IPC (#17406) * refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
2019-04-02 22:38:16 +00:00
v8Util.setHiddenValue(global, 'lifecycle', {
onLoaded () {
2020-03-20 20:28:31 +00:00
process.emit('loaded');
},
refactor: use mojo for electron internal IPC (#17406) * refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
2019-04-02 22:38:16 +00:00
onExit () {
2020-03-20 20:28:31 +00:00
process.emit('exit');
refactor: use mojo for electron internal IPC (#17406) * refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
2019-04-02 22:38:16 +00:00
},
onDocumentStart () {
2020-03-20 20:28:31 +00:00
process.emit('document-start');
refactor: use mojo for electron internal IPC (#17406) * refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
2019-04-02 22:38:16 +00:00
},
onDocumentEnd () {
2020-03-20 20:28:31 +00:00
process.emit('document-end');
refactor: use mojo for electron internal IPC (#17406) * refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
2019-04-02 22:38:16 +00:00
}
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init');
webFrameInit();
// Pass different process object to the preload script(which should not have
// access to things like `process.electronBinding`).
2020-03-20 20:28:31 +00:00
const preloadProcess = new EventEmitter();
2020-03-20 20:28:31 +00:00
Object.assign(preloadProcess, binding.process);
Object.assign(preloadProcess, processProps);
2020-03-20 20:28:31 +00:00
Object.assign(process, binding.process);
Object.assign(process, processProps);
Object.defineProperty(preloadProcess, 'noDeprecation', {
get () {
2020-03-20 20:28:31 +00:00
return process.noDeprecation;
},
set (value) {
2020-03-20 20:28:31 +00:00
process.noDeprecation = value;
}
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
process.on('loaded', () => preloadProcess.emit('loaded'));
process.on('exit', () => preloadProcess.emit('exit'));
process.on('document-start', () => preloadProcess.emit('document-start'));
process.on('document-end', () => preloadProcess.emit('document-end'));
// This is the `require` function that will be visible to the preload script
function preloadRequire (module) {
if (loadedModules.has(module)) {
2020-03-20 20:28:31 +00:00
return loadedModules.get(module);
}
2020-03-20 20:28:31 +00:00
throw new Error(`module not found: ${module}`);
}
// Process command line arguments.
2020-03-20 20:28:31 +00:00
const { hasSwitch } = process.electronBinding('command_line');
2020-03-20 20:28:31 +00:00
const contextIsolation = hasSwitch('context-isolation');
const isHiddenPage = hasSwitch('hidden-page');
const rendererProcessReuseEnabled = hasSwitch('disable-electron-site-instance-overrides');
const usesNativeWindowOpen = true;
// The arguments to be passed to isolated world.
2020-03-20 20:28:31 +00:00
const isolatedWorldArgs = { ipcRendererInternal, guestInstanceId, isHiddenPage, openerId, usesNativeWindowOpen, rendererProcessReuseEnabled };
switch (window.location.protocol) {
case 'devtools:': {
// Override some inspector APIs.
2020-03-20 20:28:31 +00:00
require('@electron/internal/renderer/inspector');
break;
}
case 'chrome-extension:': {
// Inject the chrome.* APIs that chrome extensions require
if (!process.electronBinding('features').isExtensionsEnabled()) {
2020-03-20 20:28:31 +00:00
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window);
}
2020-03-20 20:28:31 +00:00
break;
}
case 'chrome': {
2020-03-20 20:28:31 +00:00
break;
}
default: {
// Override default web functions.
2020-03-20 20:28:31 +00:00
const { windowSetup } = require('@electron/internal/renderer/window-setup');
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen, rendererProcessReuseEnabled);
// Inject content scripts.
if (!process.electronBinding('features').isExtensionsEnabled()) {
2020-03-20 20:28:31 +00:00
require('@electron/internal/renderer/content-scripts-injector')(contentScripts);
}
}
}
// Load webview tag implementation.
if (process.isMainFrame) {
2020-03-20 20:28:31 +00:00
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init');
webViewInit(contextIsolation, isWebViewTagEnabled, guestInstanceId);
}
// Pass the arguments to isolatedWorld.
if (contextIsolation) {
2020-03-20 20:28:31 +00:00
v8Util.setHiddenValue(global, 'isolated-world-args', isolatedWorldArgs);
}
// Wrap the script into a function executed in global scope. It won't have
// access to the current scope, so we'll expose a few objects as arguments:
//
// - `require`: The `preloadRequire` function
// - `process`: The `preloadProcess` object
2019-06-02 20:03:03 +00:00
// - `Buffer`: Shim of `Buffer` implementation
// - `global`: The window object, which is aliased to `global` by webpack.
function runPreloadScript (preloadSrc) {
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports) {
${preloadSrc}
2020-03-20 20:28:31 +00:00
})`;
// eval in window scope
2020-03-20 20:28:31 +00:00
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
const { setImmediate, clearImmediate } = require('timers');
2020-03-20 20:28:31 +00:00
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, {});
}
for (const { preloadPath, preloadSrc, preloadError } of preloadScripts) {
try {
if (preloadSrc) {
2020-03-20 20:28:31 +00:00
runPreloadScript(preloadSrc);
} else if (preloadError) {
2020-03-20 20:28:31 +00:00
throw preloadError;
}
} catch (error) {
2020-03-20 20:28:31 +00:00
console.error(`Unable to load preload script: ${preloadPath}`);
console.error(error);
2020-03-20 20:28:31 +00:00
ipcRendererInternal.send('ELECTRON_BROWSER_PRELOAD_ERROR', preloadPath, error);
}
}
// Warn about security issues
if (process.isMainFrame) {
2020-03-20 20:28:31 +00:00
const { securityWarnings } = require('@electron/internal/renderer/security-warnings');
securityWarnings();
}