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:
parent
f6befbe764
commit
e9b955b9ec
2 changed files with 9 additions and 15 deletions
|
@ -7,8 +7,6 @@ const {WebContents} = process.atomBinding('web_contents')
|
||||||
|
|
||||||
const {ipcMain, isPromise, webContents} = electron
|
const {ipcMain, isPromise, webContents} = electron
|
||||||
|
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const objectsRegistry = require('./objects-registry')
|
const objectsRegistry = require('./objects-registry')
|
||||||
|
|
||||||
const hasProp = {}.hasOwnProperty
|
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) {
|
ipcMain.on('ELECTRON_BROWSER_GET_BUILTIN', function (event, module) {
|
||||||
try {
|
try {
|
||||||
event.returnValue = valueToMeta(event.sender, electron[module])
|
event.returnValue = valueToMeta(event.sender, electron[module])
|
||||||
|
|
|
@ -22,13 +22,16 @@ const preloadModules = new Map([
|
||||||
['electron', electron]
|
['electron', electron]
|
||||||
])
|
])
|
||||||
|
|
||||||
// Fetch the preload script. This needs to be done through the browser process
|
const extraModules = [
|
||||||
// since we may not have filesystem access in a sandboxed renderer.
|
'fs'
|
||||||
let preloadSrc = electron.ipcRenderer.sendSync('ELECTRON_BROWSER_READ_FILE', preloadPath)
|
]
|
||||||
if (preloadSrc.err) {
|
for (let extraModule of extraModules) {
|
||||||
throw new Error(preloadSrc.err)
|
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
|
// Pass different process object to the preload script(which should not have
|
||||||
// access to things like `process.atomBinding`).
|
// access to things like `process.atomBinding`).
|
||||||
const preloadProcess = new events.EventEmitter()
|
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
|
// since browserify won't try to include `electron` in the bundle, falling back
|
||||||
// to the `preloadRequire` function above.
|
// to the `preloadRequire` function above.
|
||||||
let preloadWrapperSrc = `(function(require, process, Buffer, global) {
|
let preloadWrapperSrc = `(function(require, process, Buffer, global) {
|
||||||
${preloadSrc.data}
|
${preloadSrc}
|
||||||
})`
|
})`
|
||||||
|
|
||||||
// eval in window scope:
|
// eval in window scope:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue