fix: postMessage crash with invalid transferrable (#46639)

This commit is contained in:
Shelley Vohr 2025-04-17 15:09:48 +02:00 committed by GitHub
parent e8117f8e40
commit 352a403efd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 82 additions and 31 deletions

View file

@ -236,6 +236,23 @@ describe('ipc module', () => {
expect(ev.senderFrame.routingId).to.equal(w.webContents.mainFrame.routingId);
});
it('throws when the transferable is invalid', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
const p = once(ipcMain, 'port');
await w.webContents.executeJavaScript(`(${function () {
try {
const buffer = new ArrayBuffer(10);
// @ts-expect-error
require('electron').ipcRenderer.postMessage('port', '', [buffer]);
} catch (e) {
require('electron').ipcRenderer.postMessage('port', { error: (e as Error).message });
}
}})()`);
const [, msg] = await p;
expect(msg.error).to.eql('Invalid value for transfer');
});
it('can communicate between main and renderer', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
@ -411,6 +428,20 @@ describe('ipc module', () => {
expect(port2).not.to.be.null();
});
it('should not throw when supported values are passed as message', () => {
const { port1 } = new MessageChannelMain();
// @ts-expect-error - this shouldn't crash.
expect(() => { port1.postMessage(); }).to.not.throw();
expect(() => { port1.postMessage(undefined); }).to.not.throw();
expect(() => { port1.postMessage(42); }).to.not.throw();
expect(() => { port1.postMessage(false); }).to.not.throw();
expect(() => { port1.postMessage([]); }).to.not.throw();
expect(() => { port1.postMessage('hello'); }).to.not.throw();
expect(() => { port1.postMessage({ hello: 'goodbye' }); }).to.not.throw();
});
it('throws an error when an invalid parameter is sent to postMessage', () => {
const { port1 } = new MessageChannelMain();