Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: capture the promise global to avoid userland mutation #20947

Merged
merged 2 commits into from Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'],
}),
]
})
}
}
6 changes: 6 additions & 0 deletions filenames.auto.gni
Expand Up @@ -141,6 +141,7 @@ auto_filenames = {
"lib/common/error-utils.ts",
"lib/common/is-promise.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 @@ -176,6 +177,7 @@ auto_filenames = {
isolated_bundle_deps = [
"lib/common/electron-binding-setup.ts",
"lib/common/error-utils.ts",
"lib/common/webpack-globals-provider.ts",
"lib/isolated_renderer/init.js",
"lib/renderer/ipc-renderer-internal-utils.ts",
"lib/renderer/ipc-renderer-internal.ts",
Expand All @@ -190,6 +192,7 @@ auto_filenames = {
content_script_bundle_deps = [
"lib/common/electron-binding-setup.ts",
"lib/common/error-utils.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 @@ -277,6 +280,7 @@ auto_filenames = {
"lib/common/parse-features-string.js",
"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 @@ -301,6 +305,7 @@ auto_filenames = {
"lib/common/is-promise.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 @@ -351,6 +356,7 @@ auto_filenames = {
"lib/common/init.ts",
"lib/common/is-promise.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 @@ -508,4 +508,22 @@ describe('remote module', () => {
w.loadURL('about:blank')
})
})

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 @@ -72,6 +72,7 @@ ipcMain.on('echo', function (event, msg) {
})

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

global.permissionChecks = {
allow: () => electron.session.defaultSession.setPermissionCheckHandler(null),
Expand Down