fix: re-entrancy issues in webContents.loadURL() (#48043)

This commit is contained in:
trop[bot] 2025-08-12 13:41:47 +02:00 committed by GitHub
commit a130d4ebfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 53 additions and 2 deletions

View file

@ -484,7 +484,7 @@ describe('webContents module', () => {
}
});
it('fails if loadURL is called inside a non-reentrant critical section', (done) => {
it('fails if loadURL is called inside did-start-loading', (done) => {
w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => {
expect(validatedURL).to.contain('blank.html');
done();
@ -497,6 +497,49 @@ describe('webContents module', () => {
w.loadURL('data:text/html,<h1>HELLO</h1>');
});
it('fails if loadurl is called after the navigation is ready to commit', () => {
w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => {
expect(validatedURL).to.contain('blank.html');
});
// @ts-expect-error internal-only event.
w.webContents.once('-ready-to-commit-navigation', () => {
w.loadURL(`file://${fixturesPath}/pages/blank.html`);
});
w.loadURL('data:text/html,<h1>HELLO</h1>');
});
it('fails if loadURL is called inside did-redirect-navigation', (done) => {
const server = http.createServer((req, res) => {
if (req.url === '/302') {
res.statusCode = 302;
res.setHeader('Location', '/200');
res.end();
} else if (req.url === '/200') {
res.end('ok');
} else {
res.end();
}
});
w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => {
expect(validatedURL).to.contain('blank.html');
server.close();
done();
});
listen(server).then(({ url }) => {
w.webContents.once('did-redirect-navigation', () => {
w.loadURL(`file://${fixturesPath}/pages/blank.html`);
});
w.loadURL(`${url}/302`);
}).catch(e => {
server.close();
done(e);
});
});
it('sets appropriate error information on rejection', async () => {
let err: any;
try {