Merge pull request #8939 from electron/expose-remote-to-sandbox
Expose remote module to sandbox
This commit is contained in:
commit
f71353a4c5
4 changed files with 29 additions and 16 deletions
|
@ -7,8 +7,6 @@ const {WebContents} = process.atomBinding('web_contents')
|
|||
|
||||
const {ipcMain, isPromise, webContents} = electron
|
||||
|
||||
const fs = require('fs')
|
||||
|
||||
const objectsRegistry = require('./objects-registry')
|
||||
|
||||
const hasProp = {}.hasOwnProperty
|
||||
|
@ -268,13 +266,6 @@ ipcMain.on('ELECTRON_BROWSER_REQUIRE', function (event, module) {
|
|||
}
|
||||
})
|
||||
|
||||
ipcMain.on('ELECTRON_BROWSER_READ_FILE', function (event, file) {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) event.returnValue = {err: err.message}
|
||||
else event.returnValue = {data: data.toString()}
|
||||
})
|
||||
})
|
||||
|
||||
ipcMain.on('ELECTRON_BROWSER_GET_BUILTIN', function (event, module) {
|
||||
try {
|
||||
event.returnValue = valueToMeta(event.sender, electron[module])
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
'use strict'
|
||||
|
||||
const {Buffer} = require('buffer')
|
||||
// Note: Don't use destructuring assignment for `Buffer`, or we'll hit a
|
||||
// browserify bug that makes the statement invalid, throwing an error in
|
||||
// sandboxed renderer.
|
||||
const Buffer = require('buffer').Buffer
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron')
|
||||
const resolvePromise = Promise.resolve.bind(Promise)
|
||||
|
|
|
@ -4,5 +4,21 @@ Object.defineProperties(exports, {
|
|||
get: function () {
|
||||
return require('../ipc-renderer')
|
||||
}
|
||||
},
|
||||
remote: {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require('../../../renderer/api/remote')
|
||||
}
|
||||
},
|
||||
CallbacksRegistry: {
|
||||
get: function () {
|
||||
return require('../../../common/api/callbacks-registry')
|
||||
}
|
||||
},
|
||||
isPromise: {
|
||||
get: function () {
|
||||
return require('../../../common/api/is-promise')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -25,13 +25,16 @@ const preloadModules = new Map([
|
|||
['electron', electron]
|
||||
])
|
||||
|
||||
// Fetch the preload script. This needs to be done through the browser process
|
||||
// since we may not have filesystem access in a sandboxed renderer.
|
||||
let preloadSrc = electron.ipcRenderer.sendSync('ELECTRON_BROWSER_READ_FILE', preloadPath)
|
||||
if (preloadSrc.err) {
|
||||
throw new Error(preloadSrc.err)
|
||||
const extraModules = [
|
||||
'fs'
|
||||
]
|
||||
for (let extraModule of extraModules) {
|
||||
preloadModules.set(extraModule, electron.remote.require(extraModule))
|
||||
}
|
||||
|
||||
// Fetch the preload script using the "fs" module proxy.
|
||||
let preloadSrc = preloadModules.get('fs').readFileSync(preloadPath).toString()
|
||||
|
||||
// Pass different process object to the preload script(which should not have
|
||||
// access to things like `process.atomBinding`).
|
||||
const preloadProcess = new events.EventEmitter()
|
||||
|
@ -65,7 +68,7 @@ function preloadRequire (module) {
|
|||
// since browserify won't try to include `electron` in the bundle, falling back
|
||||
// to the `preloadRequire` function above.
|
||||
let preloadWrapperSrc = `(function(require, process, Buffer, global) {
|
||||
${preloadSrc.data}
|
||||
${preloadSrc}
|
||||
})`
|
||||
|
||||
// eval in window scope:
|
||||
|
|
Loading…
Reference in a new issue