Merge pull request #9286 from electron/to-string-renderer-errors

Throw string conversion errors in render process
This commit is contained in:
Kevin Sawicki 2017-04-26 10:12:21 -07:00 committed by GitHub
commit b9b3abbeae
2 changed files with 29 additions and 4 deletions

View file

@ -32,6 +32,13 @@ const resolveURL = function (url) {
return a.href
}
// Use this method to ensure values expected as strings in the main process
// are convertible to strings in the renderer process. This ensures exceptions
// converting values to strings are thrown in this process.
const toString = (value) => {
return value != null ? `${value}` : value
}
const windowProxies = {}
const getOrCreateProxy = (ipcRenderer, guestId) => {
@ -82,7 +89,7 @@ function BrowserWindowProxy (ipcRenderer, guestId) {
}
this.postMessage = (message, targetOrigin) => {
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, targetOrigin, window.location.origin)
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, toString(targetOrigin), window.location.origin)
}
this.eval = (...args) => {
@ -112,7 +119,7 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
if (url != null && url !== '') {
url = resolveURL(url)
}
const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, features)
const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, toString(frameName), toString(features))
if (guestId != null) {
return getOrCreateProxy(ipcRenderer, guestId)
} else {
@ -121,11 +128,11 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
}
window.alert = function (message, title) {
ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', `${message}`, `${title}`)
ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', toString(message), toString(title))
}
window.confirm = function (message, title) {
return ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CONFIRM', `${message}`, `${title}`)
return ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CONFIRM', toString(message), toString(title))
}
// But we do not support prompt().

View file

@ -361,6 +361,16 @@ describe('chromium feature', function () {
})
b = window.open()
})
it('throws an exception when the arguments cannot be converted to strings', function () {
assert.throws(function () {
window.open('', {toString: null})
}, /Cannot convert object to primitive value/)
assert.throws(function () {
window.open('', '', {toString: 3})
}, /Cannot convert object to primitive value/)
})
})
describe('window.opener', function () {
@ -538,6 +548,14 @@ describe('chromium feature', function () {
})
b = window.open('file://' + fixtures + '/pages/window-open-postMessage.html', '', 'show=no')
})
it('throws an exception when the targetOrigin cannot be converted to a string', function () {
var b = window.open('')
assert.throws(function () {
b.postMessage('test', {toString: null})
}, /Cannot convert object to primitive value/)
b.close()
})
})
describe('window.opener.postMessage', function () {