Skip to content

Commit

Permalink
feat: expose dummy module.exports to the sandboxed preload scripts (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored Aug 22, 2023
1 parent 90865fa commit 3102a25
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/sandboxed_renderer/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,16 @@ require('@electron/internal/renderer/common-init');
// - `Buffer`: Shim of `Buffer` implementation
// - `global`: The window object, which is aliased to `global` by webpack.
function runPreloadScript (preloadSrc: string) {
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports) {
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports, module) {
${preloadSrc}
})`;

// eval in window scope
const preloadFn = binding.createPreloadScript(preloadWrapperSrc);
const { setImmediate, clearImmediate } = require('timers');
const exports = {};

preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, {});
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, exports, { exports });
}

for (const { preloadPath, preloadSrc, preloadError } of preloadScripts) {
Expand Down
2 changes: 1 addition & 1 deletion spec/chromium-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ describe('chromium features', () => {
w.webContents.executeJavaScript('window.child = window.open(); child.opener = null');
const [, { webContents }] = await once(app, 'browser-window-created');
const [,, message] = await once(webContents, 'console-message');
expect(message).to.equal('{"require":"function","module":"undefined","process":"object","Buffer":"function"}');
expect(message).to.equal('{"require":"function","module":"object","exports":"object","process":"object","Buffer":"function"}');
});

it('disables the <webview> tag when it is disabled on the parent window', async () => {
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/module/preload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const types = {
require: typeof require,
module: typeof module,
exports: typeof exports,
process: typeof process,
Buffer: typeof Buffer
};
Expand Down

0 comments on commit 3102a25

Please sign in to comment.