fix: add native function to create preload script (#13032)
* add native function to create preload script * add tests * fix formatting * fix tests * rerun CI
This commit is contained in:
parent
e922b1733b
commit
ffc15e02a6
4 changed files with 36 additions and 4 deletions
|
@ -81,11 +81,19 @@ base::CommandLine::StringVector GetArgv() {
|
||||||
return base::CommandLine::ForCurrentProcess()->argv();
|
return base::CommandLine::ForCurrentProcess()->argv();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> CreatePreloadScript(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::String> preloadSrc) {
|
||||||
|
auto script = v8::Script::Compile(preloadSrc);
|
||||||
|
auto func = script->Run();
|
||||||
|
return func;
|
||||||
|
}
|
||||||
|
|
||||||
void InitializeBindings(v8::Local<v8::Object> binding,
|
void InitializeBindings(v8::Local<v8::Object> binding,
|
||||||
v8::Local<v8::Context> context) {
|
v8::Local<v8::Context> context) {
|
||||||
auto* isolate = context->GetIsolate();
|
auto* isolate = context->GetIsolate();
|
||||||
mate::Dictionary b(isolate, binding);
|
mate::Dictionary b(isolate, binding);
|
||||||
b.SetMethod("get", GetBinding);
|
b.SetMethod("get", GetBinding);
|
||||||
|
b.SetMethod("createPreloadScript", CreatePreloadScript);
|
||||||
b.SetMethod("crash", AtomBindings::Crash);
|
b.SetMethod("crash", AtomBindings::Crash);
|
||||||
b.SetMethod("hang", AtomBindings::Hang);
|
b.SetMethod("hang", AtomBindings::Hang);
|
||||||
b.SetMethod("getArgv", GetArgv);
|
b.SetMethod("getArgv", GetArgv);
|
||||||
|
|
|
@ -112,10 +112,8 @@ if (preloadSrc) {
|
||||||
${preloadSrc}
|
${preloadSrc}
|
||||||
})`
|
})`
|
||||||
|
|
||||||
// eval in window scope:
|
// eval in window scope
|
||||||
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
|
const preloadFn = binding.createPreloadScript(preloadWrapperSrc)
|
||||||
const geval = eval
|
|
||||||
const preloadFn = geval(preloadWrapperSrc)
|
|
||||||
const {setImmediate, clearImmediate} = require('timers')
|
const {setImmediate, clearImmediate} = require('timers')
|
||||||
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate)
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate)
|
||||||
} else if (preloadError) {
|
} else if (preloadError) {
|
||||||
|
|
10
spec/fixtures/module/preload-context.js
vendored
Normal file
10
spec/fixtures/module/preload-context.js
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
var test = 'test' // eslint-disable-line
|
||||||
|
|
||||||
|
const types = {
|
||||||
|
require: typeof require,
|
||||||
|
electron: typeof electron,
|
||||||
|
window: typeof window,
|
||||||
|
localVar: typeof window.test
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(JSON.stringify(types))
|
|
@ -249,6 +249,22 @@ describe('<webview> tag', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('runs in the correct scope when sandboxed', async () => {
|
||||||
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
||||||
|
preload: `${fixtures}/module/preload-context.js`,
|
||||||
|
src: `file://${fixtures}/api/blank.html`,
|
||||||
|
webpreferences: 'sandbox=yes'
|
||||||
|
})
|
||||||
|
|
||||||
|
const types = JSON.parse(message)
|
||||||
|
expect(types).to.include({
|
||||||
|
require: 'function', // arguments passed to it should be availale
|
||||||
|
electron: 'undefined', // objects from the scope it is called from should not be available
|
||||||
|
window: 'object', // the window object should be available
|
||||||
|
localVar: 'undefined' // but local variables should not be exposed to the window
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('preload script can require modules that still use "process" and "Buffer" when nodeintegration is off', async () => {
|
it('preload script can require modules that still use "process" and "Buffer" when nodeintegration is off', async () => {
|
||||||
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
||||||
preload: `${fixtures}/module/preload-node-off-wrapper.js`,
|
preload: `${fixtures}/module/preload-node-off-wrapper.js`,
|
||||||
|
|
Loading…
Reference in a new issue