electron: [Bug]: Unsanboxed preload running after page with content is loaded (ESM)
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
28.0.0
What operating system are you using?
Windows
Operating System Version
Windows 10 Family
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
Electron 28 added support for ESM, the new doc states that:
Unsandboxed ESM preload scripts will run after page load on pages with no content
If the response body for a renderer’s loaded page is completely empty (i.e.
Content-Length: 0), its preload script will not block the page load, which may result in race conditions.If this impacts you, change your response body to have something in it (e.g. an empty
htmltag (<html></html>)) or swap back to using a CommonJS preload script (.jsor.cjs), which will block the page load.
So for an HTML with content, I expect the preload to be run before the page loads:
Actual Behavior
For the following HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self';">
</head>
<body>
<main>
<p>
There is nothing interesting to see here...
</p>
</main>
</body>
</html>
Loaded as:
new BrowserWindow({
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: false,
preload: /*preload path */,
}
});
With in package.json:
"type": "module"
The JS file linked to my HTML shows the page is loaded before the preload.
For context, listen is a function exposed on the preload with contextBridge.
Testcase Gist URL
No response
Additional Information
I can’t see the Content-Length header in the response headers:
The file is loaded from http://localhost:3000/worker.html
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 3
- Comments: 22 (1 by maintainers)
Commits related to this issue
- Change inline browser preload script to CommonJS This gets around a race condition when loading ESM preload scripts: https://github.com/electron/electron/issues/40777 — committed to hillary-mutisya/TypeAgent by hillary-mutisya 2 years ago
- Fix race condition when running preload script for inline browser (#619) Our preload script for the inline browser is built as an ESM module. There is a known issue when loading ESM preload scripts: ... — committed to microsoft/TypeAgent by hillary-mutisya 2 years ago
- Fix race condition when running preload script for inline browser (#619) Our preload script for the inline browser is built as an ESM module. There is a known issue when loading ESM preload scripts: ... — committed to microsoft/TypeAgent by hillary-mutisya 2 years ago
I am able to reproduce every time with the following setup:
main.js
preload.mjs
resulting in the following output in devtools/console:
I had the time to search a bit more about this issue, and I managed to pinpoint some elements that trigger it:
rimrafin particular though… I have not tried with other libraries.rimraf.Test cases:
🟢 No bug - 🔴 Always bug - 🟡 Inconsistent
So yeah, very inconsistent bug… Sadly it’s a blocker for switching an app to ESM.
Bump, issue still present on
v32.1.0.Can confirm that this is also happening to our app, though intermittently, so clearly a race-condition.