fix: postMessage crash with invalid transferrable (#46667)

* fix: postMessage crash with invalid transferrable

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: address review feedback

Co-authored-by: Charles Kerr <charles@charleskerr.com>

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2025-04-17 18:23:23 +02:00 committed by GitHub
parent 7b66361ca8
commit 301f7b4e64
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();