Make sandbox APIs more compatible with normal renderers

- Expose remote shortcuts for the `fs`, `os` and `child_process` modules.
- Expose the `url` and `timers` modules(the browserify versions)
- Add `process.crash` and `process.platform`
This commit is contained in:
Thiago de Arruda 2017-03-21 10:41:23 -03:00
parent 46aed5ff6f
commit e1aebef57c
6 changed files with 26 additions and 14 deletions

View file

@ -9,6 +9,7 @@
#include "atom_natives.h" // NOLINT: This file is generated with js2c
#include "atom/common/api/api_messages.h"
#include "atom/common/api/atom_bindings.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/v8_value_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
@ -85,6 +86,7 @@ void InitializeBindings(v8::Local<v8::Object> binding,
auto isolate = context->GetIsolate();
mate::Dictionary b(isolate, binding);
b.SetMethod("get", GetBinding);
b.SetMethod("crash", AtomBindings::Crash);
}
class AtomSandboxedRenderFrameObserver : public content::RenderFrameObserver {

View file

@ -441,7 +441,13 @@
'sandbox_args': [
'./lib/sandboxed_renderer/init.js',
'-r',
'./lib/sandboxed_renderer/api/exports/electron.js:electron'
'./lib/sandboxed_renderer/api/exports/electron.js:electron',
'-r',
'./lib/sandboxed_renderer/api/exports/fs.js:fs',
'-r',
'./lib/sandboxed_renderer/api/exports/os.js:os',
'-r',
'./lib/sandboxed_renderer/api/exports/child_process.js:child_process'
],
'isolated_args': [
'lib/isolated_renderer/init.js',

View file

@ -0,0 +1 @@
module.exports = require('electron').remote.require('child_process')

View file

@ -0,0 +1 @@
module.exports = require('electron').remote.require('fs')

View file

@ -0,0 +1 @@
module.exports = require('electron').remote.require('os')

View file

@ -21,23 +21,23 @@ for (let prop of Object.keys(events.EventEmitter.prototype)) {
Object.setPrototypeOf(process, events.EventEmitter.prototype)
const electron = require('electron')
const fs = require('fs')
const preloadModules = new Map([
['electron', electron]
['child_process', require('child_process')],
['electron', electron],
['fs', fs],
['os', require('os')],
['url', require('url')],
['timers', require('timers')]
])
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()
const preloadSrc = 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()
preloadProcess.platform = electron.remote.process.platform
preloadProcess.crash = () => binding.crash()
process.on('exit', () => preloadProcess.emit('exit'))
// This is the `require` function that will be visible to the preload script
@ -67,12 +67,13 @@ function preloadRequire (module) {
// and any `require('electron')` calls in `preload.js` will work as expected
// 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) {
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate) {
${preloadSrc}
})`
// eval in window scope:
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
const geval = eval
let preloadFn = geval(preloadWrapperSrc)
preloadFn(preloadRequire, preloadProcess, Buffer, global)
const preloadFn = geval(preloadWrapperSrc)
const {setImmediate} = require('timers')
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate)