Skip to content

Commit

Permalink
fix: capture the promise global to avoid userland mutation (#20925)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Nov 4, 2019
1 parent 028e388 commit 0ee4d19
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions build/webpack/webpack.config.base.js
Expand Up @@ -74,7 +74,10 @@ module.exports = ({
global: ['@electron/internal/renderer/webpack-provider', '_global'],
Buffer: ['@electron/internal/renderer/webpack-provider', 'Buffer'],
})
] : [])
] : []),
new webpack.ProvidePlugin({
Promise: ['@electron/internal/common/webpack-globals-provider', 'Promise'],
}),
]
})
}
}
5 changes: 5 additions & 0 deletions filenames.auto.gni
Expand Up @@ -141,6 +141,7 @@ auto_filenames = {
"lib/common/electron-binding-setup.ts",
"lib/common/remote/type-utils.ts",
"lib/common/web-view-methods.ts",
"lib/common/webpack-globals-provider.ts",
"lib/renderer/api/context-bridge.ts",
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/desktop-capturer.ts",
Expand Down Expand Up @@ -188,6 +189,7 @@ auto_filenames = {

content_script_bundle_deps = [
"lib/common/electron-binding-setup.ts",
"lib/common/webpack-globals-provider.ts",
"lib/content_script/init.js",
"lib/renderer/chrome-api.ts",
"lib/renderer/extensions/event.ts",
Expand Down Expand Up @@ -275,6 +277,7 @@ auto_filenames = {
"lib/common/remote/type-utils.ts",
"lib/common/reset-search-paths.ts",
"lib/common/web-view-methods.ts",
"lib/common/webpack-globals-provider.ts",
"lib/renderer/ipc-renderer-internal-utils.ts",
"lib/renderer/ipc-renderer-internal.ts",
"package.json",
Expand All @@ -297,6 +300,7 @@ auto_filenames = {
"lib/common/remote/type-utils.ts",
"lib/common/reset-search-paths.ts",
"lib/common/web-view-methods.ts",
"lib/common/webpack-globals-provider.ts",
"lib/renderer/api/context-bridge.ts",
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/desktop-capturer.ts",
Expand Down Expand Up @@ -345,6 +349,7 @@ auto_filenames = {
"lib/common/init.ts",
"lib/common/remote/type-utils.ts",
"lib/common/reset-search-paths.ts",
"lib/common/webpack-globals-provider.ts",
"lib/renderer/api/context-bridge.ts",
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/desktop-capturer.ts",
Expand Down
2 changes: 2 additions & 0 deletions lib/common/asar.js
Expand Up @@ -8,6 +8,8 @@
const path = require('path')
const util = require('util')

const Promise = global.Promise

const envNoAsar = process.env.ELECTRON_NO_ASAR &&
process.type !== 'browser' &&
process.type !== 'renderer'
Expand Down
8 changes: 8 additions & 0 deletions lib/common/webpack-globals-provider.ts
@@ -0,0 +1,8 @@
// Captures original globals into a scope to ensure that userland modifications do
// not impact Electron. Note that users doing:
//
// global.Promise.resolve = myFn
//
// Will mutate this captured one as well and that is OK.

export const Promise = global.Promise
18 changes: 18 additions & 0 deletions spec/api-remote-spec.js
Expand Up @@ -576,4 +576,22 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
w.loadFile(path.join(fixtures, 'api', 'remote-event-handler.html'))
})
})

describe('with an overriden global Promise constrctor', () => {
let original

before(() => {
original = Promise
})

it('using a promise based method resolves correctly', async () => {
expect(await remote.getGlobal('returnAPromise')(123)).to.equal(123)
global.Promise = { resolve: () => ({}) }
expect(await remote.getGlobal('returnAPromise')(456)).to.equal(456)
})

after(() => {
global.Promise = original
})
})
})
1 change: 1 addition & 0 deletions spec/static/main.js
Expand Up @@ -75,6 +75,7 @@ ipcMain.on('echo', function (event, msg) {
})

global.setTimeoutPromisified = util.promisify(setTimeout)
global.returnAPromise = (value) => new Promise((resolve) => setTimeout(() => resolve(value), 100))

global.isCi = !!argv.ci
if (global.isCi) {
Expand Down

0 comments on commit 0ee4d19

Please sign in to comment.