Use remote/fs to read preload script during sandboxed setup

Also expose the "fs" module to preload scripts, as a shortcut to
`require('electron').remote.require('fs')`
This commit is contained in:
Thiago de Arruda 2017-03-16 14:23:00 -03:00
parent f6befbe764
commit e9b955b9ec
2 changed files with 9 additions and 15 deletions

View file

@ -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])

View file

@ -22,13 +22,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()
@ -62,7 +65,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: